forked from stevenh/HttpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpListener.cs
404 lines (357 loc) · 14.8 KB
/
HttpListener.cs
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
using System;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using HttpServer.Headers;
using HttpServer.Logging;
using HttpServer.Messages;
namespace HttpServer
{
/// <summary>
/// Http listener.
/// </summary>
public class HttpListener : IHttpListener
{
private readonly HttpFactory _factory;
private readonly ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
private TcpListener _listener;
private ILogger _logger = LogFactory.CreateLogger(typeof (HttpListener));
private int _pendingAccepts;
private bool _shuttingDown;
/// <summary>
/// Initializes a new instance of the <see cref="HttpListener"/> class.
/// </summary>
/// <param name="address">The address.</param>
/// <param name="port">The port.</param>
protected HttpListener(IPAddress address, int port)
{
Address = address;
Port = port;
_factory = new HttpFactory();
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpListener"/> class.
/// </summary>
/// <param name="address">The address.</param>
/// <param name="port">The port.</param>
/// <param name="httpFactory">The HTTP factory.</param>
protected HttpListener(IPAddress address, int port, HttpFactory httpFactory)
{
Address = address;
Port = port;
_factory = httpFactory;
}
/// <summary>
/// Gets HTTP factory used to create types used by this HTTP library.
/// </summary>
protected IHttpFactory Factory
{
get { return _factory; }
}
/// <summary>
/// Gets or sets the maximum number of bytes that the request body can contain.
/// </summary>
/// <value>The content length limit.</value>
/// <remarks>
/// <para>
/// Used when responding to 100-continue.
/// </para>
/// <para>
/// 0 = turned off.
/// </para>
/// </remarks>
public int ContentLengthLimit { get; set; }
private void BeginAccept()
{
if (_shuttingDown)
return;
Interlocked.Increment(ref _pendingAccepts);
try
{
_listener.BeginAcceptSocket(OnSocketAccepted, null);
}
catch (Exception err)
{
_logger.Error("Unhandled exception in BeginAccept.", err);
}
}
private bool CanAcceptSocket(Socket socket)
{
try
{
var args = new SocketFilterEventArgs(socket);
SocketAccepted(this, args);
return args.IsSocketOk;
}
catch (Exception err)
{
_logger.Error("SocketAccepted trigger exception: " + err);
return true;
}
}
/// <summary>
/// Creates a new <see cref="HttpListener"/> instance with default factories.
/// </summary>
/// <param name="address">Address that the listener should accept connections on.</param>
/// <param name="port">Port that listener should accept connections on.</param>
/// <returns>Created HTTP listener.</returns>
public static HttpListener Create(IPAddress address, int port)
{
return new HttpListener(address, port);
}
/// <summary>
/// Creates a new <see cref="HttpListener"/> instance with default factories.
/// </summary>
/// <param name="address">Address that the listener should accept connections on.</param>
/// <param name="port">Port that listener should accept connections on.</param>
/// <param name="factory">Factory used to create different types in the framework.</param>
/// <returns>Created HTTP listener.</returns>
public static HttpListener Create(IPAddress address, int port, HttpFactory factory)
{
return new HttpListener(address, port);
}
/// <summary>
/// Creates a new <see cref="HttpListener"/> instance with default factories.
/// </summary>
/// <param name="address">Address that the listener should accept connections on.</param>
/// <param name="port">Port that listener should accept connections on.</param>
/// <param name="certificate">Certificate to use</param>
/// <returns>Created HTTP listener.</returns>
public static HttpListener Create(IPAddress address, int port, X509Certificate certificate)
{
//RequestParserFactory requestFactory = new RequestParserFactory();
//HttpContextFactory factory = new HttpContextFactory(NullLogWriter.Instance, 16384, requestFactory);
return new SecureHttpListener(address, port, certificate);
}
/// <summary>
/// Create a new context
/// </summary>
/// <param name="socket">Accepted socket</param>
/// <returns>A new context.</returns>
protected virtual HttpContext CreateContext(Socket socket)
{
return Factory.Get<HttpContext>(socket);
}
private void SendErrorPage(Exception exception)
{
var httpException = exception as HttpException;
var response = HttpContext.Current.Response;
response.Status = httpException != null ? httpException.Code : HttpStatusCode.InternalServerError;
response.Reason = exception.Message;
if (response.Body.CanWrite)
response.Body.SetLength(0);
var args = new ErrorPageEventArgs(HttpContext.Current) {Exception = exception};
ErrorPageRequested(this, args);
try
{
var generator = new ResponseWriter();
if (args.IsHandled)
generator.Send(HttpContext.Current, response);
else
generator.SendErrorPage(HttpContext.Current, response, exception);
}
catch (Exception err)
{
_logger.Error("Failed to display error page", err);
}
}
private void On100Continue(object sender, ContinueEventArgs e)
{
var response = new Response(e.Request.HttpVersion, HttpStatusCode.Continue, "Please continue mate.");
if (ContentLengthLimit != 0 && e.Request.ContentLength.Value > ContentLengthLimit)
{
_logger.Warning("Requested to send " + e.Request.ContentLength.Value + " bytes, but we only allow " + ContentLengthLimit);
Console.WriteLine("Requested to send " + e.Request.ContentLength.Value + " bytes, but we only allow " + ContentLengthLimit);
response.Status = HttpStatusCode.ExpectationFailed;
response.Reason = "Too large content length";
}
string responseString = string.Format("{0} {1} {2}\r\n\r\n",
e.Request.HttpVersion,
(int) response.Status,
response.Reason);
byte[] buffer = e.Request.Encoding.GetBytes(responseString);
HttpContext.Current.Stream.Write(buffer, 0, buffer.Length);
HttpContext.Current.Stream.Flush();
Console.WriteLine(responseString);
_logger.Info(responseString);
}
private void OnDisconnect(object sender, EventArgs e)
{
HttpFactory.Current = Factory;
var context = (HttpContext) sender;
context.Disconnected -= OnDisconnect;
context.RequestReceived -= OnRequest;
context.ContinueResponseRequested -= On100Continue;
}
/// <exception cref="Exception">Throwing exception if in debug mode and not exception handler have been specified.</exception>
private void OnRequest(object sender, RequestEventArgs e)
{
var context = (HttpContext) sender;
HttpFactory.Current = Factory;
HttpContext.Current = context;
try
{
var args = new RequestEventArgs(context, e.Request, e.Response);
RequestReceived(this, args);
if (!args.IsHandled)
{
// need to respond to the context.
var generator = new ResponseWriter();
generator.Send(context, args.Response);
}
// Disconnect when done.
if (e.Response.HttpVersion == "HTTP/1.0" || e.Response.Connection.Type == ConnectionType.Close)
context.Disconnect();
}
catch (Exception err)
{
if (err is HttpException)
{
var exception = (HttpException) err;
SendErrorPage(exception);
}
else
{
_logger.Debug("Request failed.", err);
SendErrorPage(err);
}
e.IsHandled = true;
}
}
private void OnSocketAccepted(IAsyncResult ar)
{
HttpFactory.Current = Factory;
Socket socket = null;
try
{
socket = _listener.EndAcceptSocket(ar);
Interlocked.Decrement(ref _pendingAccepts);
if (_shuttingDown && _pendingAccepts == 0)
_shutdownEvent.Set();
if (!CanAcceptSocket(socket))
{
_logger.Debug("Socket was rejected: " + socket.RemoteEndPoint);
socket.Disconnect(true);
BeginAccept();
return;
}
}
catch (Exception err)
{
_logger.Warning("Failed to end accept: " + err.Message);
BeginAccept();
if (socket != null)
socket.Disconnect(true);
return;
}
if (!_shuttingDown)
BeginAccept();
_logger.Trace("Accepted connection from: " + socket.RemoteEndPoint);
// Got a new context.
try
{
HttpContext context = CreateContext(socket);
HttpContext.Current = context;
context.HttpFactory = _factory;
context.RequestReceived += OnRequest;
context.Disconnected += OnDisconnect;
context.ContinueResponseRequested += On100Continue;
context.Start();
}
catch (Exception err)
{
_logger.Error("ContextReceived raised an exception: " + err.Message);
socket.Disconnect(true);
}
}
#region IHttpListener Members
/// <summary>
/// Gets listener address.
/// </summary>
public IPAddress Address { get; private set; }
/// <summary>
/// Gets if listener is secure.
/// </summary>
public virtual bool IsSecure
{
get { return true; }
}
/// <summary>
/// Gets if listener have been started.
/// </summary>
public bool IsStarted { get; private set; }
/// <summary>
/// Gets or sets logger.
/// </summary>
public ILogger Logger
{
get { return _logger; }
set
{
_logger = value ?? NullLogWriter.Instance;
_logger.Debug("Logger attached to " + (IsSecure ? "secure" : string.Empty) + " listener [" + Address +
":" + Port +
"].");
}
}
/// <summary>
/// Gets listening port.
/// </summary>
public int Port { get; private set; }
/// <summary>
/// Start listener.
/// </summary>
/// <param name="backLog">Number of pending accepts.</param>
/// <remarks>
/// Make sure that you are subscribing on <see cref="RequestReceived"/> first.
/// </remarks>
/// <exception cref="InvalidOperationException">Listener have already been started.</exception>
/// <exception cref="SocketException">Failed to start socket.</exception>
/// <exception cref="ArgumentOutOfRangeException">Invalid port number.</exception>
public void Start(int backLog)
{
if (_listener != null)
throw new InvalidOperationException("Listener have already been started.");
IsStarted = true;
_listener = new TcpListener(Address, Port);
_listener.Start(backLog);
if (Port == 0 && _listener.LocalEndpoint is IPEndPoint)
Port = ((IPEndPoint) _listener.LocalEndpoint).Port;
// do not use beginaccept. Let exceptions be thrown.
Interlocked.Increment(ref _pendingAccepts);
_listener.BeginAcceptSocket(OnSocketAccepted, null);
}
/// <summary>
/// Stop listener.
/// </summary>
public void Stop()
{
_shuttingDown = true;
_listener.Stop();
}
/// <summary>
/// A new request have been received.
/// </summary>
public event EventHandler<RequestEventArgs> RequestReceived = delegate { };
/// <summary>
/// Can be used to reject certain clients.
/// </summary>
public event EventHandler<SocketFilterEventArgs> SocketAccepted = delegate { };
/// <summary>
/// A HTTP exception have been thrown.
/// </summary>
/// <remarks>
/// Fill the body with a user friendly error page, or redirect to somewhere else.
/// </remarks>
public event EventHandler<ErrorPageEventArgs> ErrorPageRequested = delegate { };
#endregion
/// <summary>
/// Client asks if he may continue.
/// </summary>
/// <remarks>
/// If the body is too large or anything like that you should respond <see cref="HttpStatusCode.ExpectationFailed"/>.
/// </remarks>
public event EventHandler<RequestEventArgs> ContinueResponseRequested = delegate { };
}
}