1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
//
// System.Runtime.Remoting.Channels.Http.HttpServerChannel
//
// Summary: Implements a client channel that transmits method calls over HTTP.
//
// Authors:
// Martin Willemoes Hansen (mwh@sysrq.dk)
// Ahmad Tantawy (popsito82@hotmail.com)
// Ahmad Kadry (kadrianoz@hotmail.com)
// Hussein Mehanna (hussein_mehanna@hotmail.com)
//
// (C) 2003 Martin Willemoes Hansen
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.MetadataServices;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
namespace System.Runtime.Remoting.Channels.Http
{
public class HttpServerChannel : BaseChannelWithProperties, IChannel,
IChannelReceiver, IChannelReceiverHook
{
private int _channelPriority = 1; // priority of channel (default=1)
private String _channelName = "http"; // channel name
private String _machineName = null; // machine name
private int _port = -1; // port to listen on
private ChannelDataStore _channelData = null; // channel data
private bool _bUseIpAddress = true; // by default, we'll use the ip address.
private IPAddress _bindToAddr = IPAddress.Any; // address to bind to.
private bool _bSuppressChannelData = false; // should we hand out null for our channel data
private IServerChannelSinkProvider _sinkProvider = null;
private HttpServerTransportSink _transportSink = null;
private bool _wantsToListen = true;
private TcpListener _tcpListener;
private Thread _listenerThread;
private bool _bListening = false; // are we listening at the moment?
RemotingThreadPool threadPool;
#if TARGET_JVM
private volatile bool stopped = false;
#endif
public HttpServerChannel() : base()
{
SetupChannel(null,null);
}
public HttpServerChannel(int port) : base()
{
_port = port;
SetupChannel(null,null);
}
public HttpServerChannel(String name, int port) : base()
{
_channelName = name;
_port = port;
_wantsToListen = false;
SetupChannel(null,null);
}
public HttpServerChannel(String name, int port, IServerChannelSinkProvider sinkProvider) : base()
{
//enter the name later ya gameeel
_port = port;
_wantsToListen = false;
SetupChannel(sinkProvider,null);
}
public HttpServerChannel (IDictionary properties, IServerChannelSinkProvider sinkProvider) : base()
{
if (properties != null)
foreach(DictionaryEntry Dict in properties)
{
switch((string)Dict.Key)
{
case "name":
_channelName = (string)Dict.Value;
break;
case "priority":
_channelPriority = Convert.ToInt32 (Dict.Value);
break;
case "bindTo":
_bindToAddr = IPAddress.Parse ((string)Dict.Value);
break;
case "listen":
_wantsToListen = Convert.ToBoolean (Dict.Value);;
break;
case "machineName":
_machineName = (string)Dict.Value;
break;
case "port":
_wantsToListen = false;
_port = Convert.ToInt32 (Dict.Value);
break;
case "suppressChannelData":
_bSuppressChannelData = Convert.ToBoolean (Dict.Value);
break;
case "useIpAddress":
_bUseIpAddress = Convert.ToBoolean (Dict.Value);
break;
}
}
SetupChannel (sinkProvider, properties);
}
void SetupChannel (IServerChannelSinkProvider sinkProvider, IDictionary properties)
{
if (properties == null) properties = new Hashtable ();
SetupMachineName();
_sinkProvider = sinkProvider;
String[] urls = { this.GetChannelUri() };
// needed for CAOs
_channelData = new ChannelDataStore(urls);
if(_sinkProvider == null)
{
_sinkProvider = new SdlChannelSinkProvider();
_sinkProvider.Next = new SoapServerFormatterSinkProvider();
}
// collect channel data from all providers
IServerChannelSinkProvider provider = _sinkProvider;
while (provider != null)
{
provider.GetChannelData(_channelData);
provider = provider.Next;
}
// create the sink chain
IServerChannelSink snk =
ChannelServices.CreateServerChannelSinkChain(_sinkProvider,this);
_transportSink = new HttpServerTransportSink (snk, properties);
SinksWithProperties = _transportSink;
}
internal void Listen()
{
try {
#if !TARGET_JVM
while(true)
#else
while(!stopped)
#endif
{
Socket socket = _tcpListener.AcceptSocket();
RequestArguments request = new RequestArguments (socket, _transportSink);
threadPool.RunThread (new ThreadStart (request.Process));
}
} catch {}
}
public void StartListening (Object data)
{
if (_bListening || _port < 0) return;
#if TARGET_JVM
stopped = false;
#endif
threadPool = RemotingThreadPool.GetSharedPool ();
_tcpListener = new TcpListener (_bindToAddr, _port);
_tcpListener.Start();
if (_port == 0) {
_port = ((IPEndPoint)_tcpListener.LocalEndpoint).Port;
String[] uris = { this.GetChannelUri() };
_channelData.ChannelUris = uris;
}
if(_listenerThread == null)
{
ThreadStart t = new ThreadStart(this.Listen);
_listenerThread = new Thread(t);
_listenerThread.IsBackground = true;
}
if(!_listenerThread.IsAlive)
_listenerThread.Start();
_bListening = true;
}
public void StopListening(Object data)
{
#if TARGET_JVM
stopped = true;
#endif
if( _bListening)
{
#if !TARGET_JVM
_listenerThread.Abort ();
#else
_listenerThread.Interrupt ();
#endif
_tcpListener.Stop();
threadPool.Free ();
_listenerThread.Join ();
_listenerThread = null;
}
_bListening = false;
}
void SetupMachineName()
{
if (_machineName == null)
{
if (_bUseIpAddress) {
IPHostEntry he = Dns.Resolve (Dns.GetHostName());
if (he.AddressList.Length == 0) throw new RemotingException ("IP address could not be determined for this host");
_machineName = he.AddressList [0].ToString ();
}
else
_machineName = Dns.GetHostByName(Dns.GetHostName()).HostName;
}
} // SetupMachineName
public int ChannelPriority
{
get { return _channelPriority; }
}
public String ChannelName
{
get { return _channelName; }
}
public String GetChannelUri()
{
return "http://" + _machineName + ":" + _port;
}
public virtual String[] GetUrlsForUri(String objectUri)
{
String[] retVal = new String[1];
if (!objectUri.StartsWith("/"))
objectUri = "/" + objectUri;
retVal[0] = GetChannelUri() + objectUri;
return retVal;
}
public String Parse(String url,out String objectURI)
{
return HttpHelper.Parse(url,out objectURI);
}
public Object ChannelData
{
get
{
if (_bSuppressChannelData) return null;
else return _channelData;
}
}
public String ChannelScheme
{
get { return "http"; }
}
public bool WantsToListen
{
get { return _wantsToListen; }
set { _wantsToListen = value; }
}
public IServerChannelSink ChannelSinkChain
{
get { return _transportSink.NextChannelSink; }
}
public void AddHookChannelUri (String channelUri)
{
string [] newUris = new string[1] { channelUri };
_channelData.ChannelUris = newUris;
_wantsToListen = false;
}
public override object this [object key]
{
get { return Properties[key]; }
set { Properties[key] = value; }
}
public override ICollection Keys
{
get { return Properties.Keys; }
}
} // HttpServerChannel
internal class HttpServerTransportSink : IServerChannelSink, IChannelSinkBase
{
private static String s_serverHeader =
"mono .NET Remoting, mono .NET CLR " + System.Environment.Version.ToString();
// sink state
private IServerChannelSink _nextSink;
private IDictionary _properties;
public HttpServerTransportSink (IServerChannelSink nextSink, IDictionary properties)
{
_nextSink = nextSink;
_properties = properties;
} // IServerChannelSink
internal void ServiceRequest (RequestArguments reqArg, Stream requestStream, ITransportHeaders headers)
{
ITransportHeaders responseHeaders;
Stream responseStream;
bool requestDispatched = false;
ServerProcessing processing;
try
{
processing = DispatchRequest (requestStream, headers, out responseStream, out responseHeaders);
requestDispatched = true;
switch (processing)
{
case ServerProcessing.Complete:
HttpServer.SendResponse (reqArg, 200, responseHeaders, responseStream);
break;
case ServerProcessing.OneWay:
HttpServer.SendResponse (reqArg, 200, null, null);
break;
case ServerProcessing.Async:
break;
}
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine("The exception was caught during HttpServerChannel.ServiceRequest: {0}, {1}", ex.GetType(), ex.Message);
#endif
if (!requestDispatched)
{
try
{
HttpServer.SendResponse (reqArg, 400, null, null);
}
catch (Exception e)
{
#if DEBUG
Console.WriteLine("Fail to send response in HttpServerChannels.ServiceRequest: {0}, {1}", e.GetType(), e.Message);
#endif
}
}
}
}
internal ServerProcessing DispatchRequest (Stream requestStream, ITransportHeaders headers, out Stream responseStream, out ITransportHeaders responseHeaders)
{
ServerChannelSinkStack sinkStack = new ServerChannelSinkStack();
IMessage responseMessage;
return _nextSink.ProcessMessage (sinkStack, null, headers, requestStream,
out responseMessage,
out responseHeaders, out responseStream);
}
//
// IServerChannelSink implementation
//
public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
IMessage requestMsg,
ITransportHeaders requestHeaders, Stream requestStream,
out IMessage responseMsg, out ITransportHeaders responseHeaders,
out Stream responseStream)
{
throw new NotSupportedException();
} // ProcessMessage
public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, Object state,
IMessage msg, ITransportHeaders headers, Stream stream)
{
// Never called
throw new NotSupportedException ();
}
public Stream GetResponseStream(IServerResponseChannelSinkStack sinkStack, Object state,
IMessage msg, ITransportHeaders headers)
{
return null;
} // GetResponseStream
public IServerChannelSink NextChannelSink
{
get { return _nextSink; }
}
public IDictionary Properties
{
get { return _properties; }
} // Properties
internal static String ServerHeader
{
get { return s_serverHeader; }
}
} // HttpServerTransportSink
} // namespace System.Runtime.Remoting.Channels.Http
|