-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathServer.cs
506 lines (438 loc) · 17.6 KB
/
Server.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using HttpServer.Authentication;
using HttpServer.BodyDecoders;
using HttpServer.Headers;
using HttpServer.Logging;
using HttpServer.Messages;
using HttpServer.Modules;
using HttpServer.Routing;
namespace HttpServer
{
/// <summary>
/// Http server.
/// </summary>
public class Server
{
[ThreadStatic] private static Server _server;
private readonly BodyDecoderCollection _bodyDecoders = new BodyDecoderCollection();
private readonly List<IHttpListener> _listeners = new List<IHttpListener>();
private readonly ILogger _logger = LogFactory.CreateLogger(typeof (Server));
private readonly List<IModule> _modules = new List<IModule>();
private readonly List<IRouter> _routers = new List<IRouter>();
private HttpFactory _factory;
private bool _isStarted;
/// <summary>
/// Initializes a new instance of the <see cref="Server"/> class.
/// </summary>
/// <param name="factory">Factory used to create objects used in this library.</param>
public Server(HttpFactory factory)
{
ServerName = "C# WebServer";
_server = this;
_factory = factory;
AuthenticationProvider = new AuthenticationProvider();
MaxContentSize = 1000000;
}
/// <summary>
/// Initializes a new instance of the <see cref="Server"/> class.
/// </summary>
public Server()
: this(new HttpFactory())
{
}
/// <summary>
/// Gets the authentication provider.
/// </summary>
/// <remarks>
/// A authentication provider is used to keep track of all authentication types
/// that can be used.
/// </remarks>
public AuthenticationProvider AuthenticationProvider { get; private set; }
/// <summary>
/// Gets or sets number of bytes that a body can be.
/// </summary>
/// <remarks>
/// <para>
/// Used to determine the answer to a 100-continue request.
/// </para>
/// <para>
/// 0 = turned off.
/// </para>
/// </remarks>
public int ContentLengthLimit { get; set; }
/// <summary>
/// Gets current server.
/// </summary>
/// <remarks>
/// Only valid when a request have been received and is being processed.
/// </remarks>
public static Server Current
{
get { return _server; }
}
/// <summary>
/// Gets or sets the maximum size of request body (in bytes)
/// </summary>
public long MaxContentSize { get; set; }
/// <summary>
/// Gets or sets server name.
/// </summary>
/// <remarks>
/// Used in the "Server" header when serving requests.
/// </remarks>
public string ServerName { get; set; }
/// <summary>
/// Add a decoder.
/// </summary>
/// <param name="decoder">decoder to add</param>
/// <remarks>
/// Adding zero decoders will make the server add the
/// default ones which is <see cref="MultiPartDecoder"/> and <see cref="UrlDecoder"/>.
/// </remarks>
public void Add(IBodyDecoder decoder)
{
_bodyDecoders.Add(decoder);
}
/// <summary>
/// Add a new router.
/// </summary>
/// <param name="router">Router to add</param>
/// <exception cref="InvalidOperationException">Server have been started.</exception>
public void Add(IRouter router)
{
if (_isStarted)
throw new InvalidOperationException("Server have been started.");
_routers.Add(router);
}
/// <summary>
/// Add a file module
/// </summary>
/// <param name="module">Module to add</param>
/// <exception cref="ArgumentNullException"><c>module</c> is <c>null</c>.</exception>
/// <exception cref="InvalidOperationException">Cannot add modules when server have been started.</exception>
public void Add(IModule module)
{
if (module == null)
throw new ArgumentNullException("module");
if (_isStarted)
throw new InvalidOperationException("Cannot add modules when server have been started.");
_modules.Add(module);
}
/// <summary>
/// Add a HTTP listener.
/// </summary>
/// <param name="listener"></param>
/// <exception cref="InvalidOperationException">Listener have been started.</exception>
public void Add(IHttpListener listener)
{
if (listener.IsStarted)
throw new InvalidOperationException("Listener have been started.");
_listeners.Add(listener);
}
protected virtual void DecodeBody(IRequest request)
{
Encoding encoding = null;
if (request.ContentType != null)
{
string encodingStr = request.ContentType.Parameters["Encoding"];
if (!string.IsNullOrEmpty(encodingStr))
encoding = Encoding.GetEncoding(encodingStr);
}
if (encoding == null)
encoding = Encoding.UTF8;
// process body.
DecodedData data = _bodyDecoders.Decode(request.Body, request.ContentType, encoding);
if (data == null)
return;
if (!(request is Request))
throw new InternalServerException("Request object has to derive from Request (sorry for breaking LSP).");
var r = (Request) request;
r.Files = data.Files;
r.Form = data.Parameters;
}
/// <summary>
/// An error have occurred and we need to send a result pack to the client
/// </summary>
/// <param name="context">The context.</param>
/// <param name="exception">The exception.</param>
/// <remarks>
/// Invoke base class (<see cref="Server"/>) to send the contents
/// of <see cref="IHttpContext.Response"/>.
/// </remarks>
protected virtual void DisplayErrorPage(IHttpContext context, Exception exception)
{
var httpException = exception as HttpException;
if (httpException != null)
{
context.Response.Reason = httpException.Code.ToString();
context.Response.Status = httpException.Code;
}
else
{
context.Response.Reason = "Internal Server Error";
context.Response.Status = HttpStatusCode.InternalServerError;
}
var args = new ErrorPageEventArgs(context) {Exception = exception};
ErrorPageRequested(this, args);
var writer = HttpFactory.Current.Get<ResponseWriter>();
if (args.IsHandled)
writer.Send(context, context.Response);
else
{
writer.SendErrorPage(context, context.Response, exception);
args.IsHandled = true;
}
}
private ProcessingResult HandleRequest(RequestEventArgs e)
{
var context = new RequestContext
{
HttpContext = e.Context,
Request = e.Request,
Response = e.Response
};
OnAuthentication(context);
OnBeforeRequest(context);
PrepareRequest(this, e);
if (e.Request.ContentLength.Value > 0)
DecodeBody(e.Request);
// Process routers.
ProcessingResult result = ProcessRouters(context);
if (ProcessResult(result, e))
_logger.Debug("Routers processed the request.");
// process modules.
result = ProcessModules(context);
if (ProcessResult(result, e))
return result;
RequestReceived(this, e);
return ProcessingResult.Continue;
}
private void Listener_OnErrorPage(object sender, ErrorPageEventArgs e)
{
_server = this;
DisplayErrorPage(e.Context, e.Exception);
e.IsHandled = true;
}
/// <summary>
/// Called before anything else.
/// </summary>
/// <param name="context">The context.</param>
/// <remarks>
/// Looks after a <see cref="AuthorizationHeader"/> in the request and will
/// use the <see cref="AuthenticationProvider"/> if found.
/// </remarks>
protected virtual void OnAuthentication(RequestContext context)
{
var authHeader = (AuthorizationHeader) context.Request.Headers[AuthorizationHeader.NAME];
if (authHeader != null)
AuthenticationProvider.Authenticate(context.Request);
}
/// <summary>
/// All server modules are about to be invoked.
/// </summary>
/// <param name="context">The context.</param>
/// <remarks>
/// Called when routers have been invoked but no modules yet.
/// </remarks>
protected virtual void OnBeforeModules(RequestContext context)
{
}
/// <summary>
/// A request have arrived but not yet been processed yet.
/// </summary>
/// <param name="context">The context.</param>
/// <remarks>
/// Default implementation adds a <c>Date</c> header and <c>Server</c> header.
/// </remarks>
protected virtual void OnBeforeRequest(RequestContext context)
{
if (context.Request.ContentLength.Value > MaxContentSize)
throw new HttpException(HttpStatusCode.RequestEntityTooLarge, "Too large body, limit is " + MaxContentSize);
context.Response.Add(new DateHeader("Date", DateTime.UtcNow));
context.Response.Add(new StringHeader("Server", ServerName));
}
private void OnRequest(object sender, RequestEventArgs e)
{
_server = this;
Exception exception;
try
{
ProcessingResult result = HandleRequest(e);
if (result != ProcessingResult.Continue)
return;
exception = null;
}
catch (HttpException err)
{
_logger.Error("Got an HTTP exception.", err);
e.Response.Status = err.Code;
e.Response.Reason = err.Message;
exception = err;
}
catch (Exception err)
{
_logger.Error("Got an unhandled exception.", err);
exception = err;
e.Response.Status = HttpStatusCode.InternalServerError;
e.Response.Reason = "Failed to process request.";
}
if (exception == null)
{
e.Response.Status = HttpStatusCode.NotFound;
e.Response.Reason = "Requested resource is not found. Sorry ;(";
exception = new HttpException(HttpStatusCode.NotFound, "Failed to find uri " + e.Request.Uri);
}
DisplayErrorPage(e.Context, exception);
e.IsHandled = true;
}
/// <summary>
/// Go through all modules and check if any of them can handle the current request.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private ProcessingResult ProcessModules(RequestContext context)
{
OnBeforeModules(context);
foreach (IModule module in _modules)
{
ProcessingResult result = module.Process(context);
if (result != ProcessingResult.Continue)
{
_logger.Debug(module.GetType().Name + ": " + result);
return result;
}
}
return ProcessingResult.Continue;
}
/// <summary>
/// Process result (check if it should be sent back or not)
/// </summary>
/// <param name="result"></param>
/// <param name="e"></param>
/// <returns><c>true</c> if request was processed properly.; otherwise <c>false</c>.</returns>
protected virtual bool ProcessResult(ProcessingResult result, RequestEventArgs e)
{
if (result == ProcessingResult.Abort)
{
e.IsHandled = true;
return true;
}
if (result == ProcessingResult.SendResponse)
{
SendResponse(e.Context, e.Request, e.Response);
e.IsHandled = true;
return true;
}
return false;
}
/// <summary>
/// Processes all routers.
/// </summary>
/// <param name="context">Request context.</param>
/// <returns>Processing result.</returns>
private ProcessingResult ProcessRouters(RequestContext context)
{
foreach (IRouter router in _routers)
{
if (router.Process(context) != ProcessingResult.SendResponse)
continue;
_logger.Debug(router.GetType().Name + " sends the response.");
return ProcessingResult.SendResponse;
}
return ProcessingResult.Continue;
}
/// <summary>
/// Requests authentication from the user.
/// </summary>
/// <param name="realm">Host/domain name that the server hosts.</param>
/// <remarks>
/// Used when calculating hashes in Digest authentication.
/// </remarks>
/// <seealso cref="DigestAuthentication"/>
/// <seealso cref="DigestAuthentication.GetHA1"/>
protected virtual void RequestAuthentication(string realm)
{
AuthenticationProvider.CreateChallenge(HttpContext.Current.Response, realm);
}
/// <summary>
/// Send a response.
/// </summary>
/// <param name="context"></param>
/// <param name="request"></param>
/// <param name="response"></param>
protected void SendResponse(IHttpContext context, IRequest request, IResponse response)
{
SendingResponse(this, new RequestEventArgs(context, request, response));
var generator = HttpFactory.Current.Get<ResponseWriter>();
generator.Send(context, response);
if (request.Connection != null && request.Connection.Type == ConnectionType.Close)
{
context.Stream.Close();
_logger.Debug("Closing connection.");
}
}
/// <summary>
/// Start http server.
/// </summary>
/// <param name="backLog">Number of pending connections.</param>
public void Start(int backLog)
{
if (_isStarted)
return;
if (_bodyDecoders.Count == 0)
{
_bodyDecoders.Add(new MultiPartDecoder());
_bodyDecoders.Add(new UrlDecoder());
}
foreach (IHttpListener listener in _listeners)
{
listener.ErrorPageRequested += Listener_OnErrorPage;
listener.RequestReceived += OnRequest;
listener.ContentLengthLimit = ContentLengthLimit;
listener.Start(backLog);
}
_isStarted = true;
}
/// <summary>
/// Stops the server
/// </summary>
/// <param name="removeModules">true if all modules should be removed.</param>
public void Stop(bool removeModules)
{
foreach (IHttpListener listener in _listeners)
{
listener.Stop();
}
if (removeModules)
_modules.Clear();
}
/// <summary>
/// Invoked just before a response is sent back to the client.
/// </summary>
public event EventHandler<RequestEventArgs> SendingResponse = delegate { };
/// <summary>
/// Invoked *after* the web server has tried to handled the request.
/// </summary>
/// <remarks>
/// The event can be used to handle the request after all routes and modules
/// have tried to process the request.
/// </remarks>
public event EventHandler<RequestEventArgs> RequestReceived = delegate { };
/// <summary>
/// Invoked *before* the web server has tried to handled the request.
/// </summary>
/// <remarks>
/// Event can be used to load a session from a cookie or to force
/// authentication or anything other you might need t do before a request
/// is handled.
/// </remarks>
public event EventHandler<RequestEventArgs> PrepareRequest = delegate { };
/// <summary>
/// An error page have been requested.
/// </summary>
public event EventHandler<ErrorPageEventArgs> ErrorPageRequested = delegate { };
}
}