-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.cs
More file actions
586 lines (503 loc) · 22.8 KB
/
HttpServer.cs
File metadata and controls
586 lines (503 loc) · 22.8 KB
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using WebExpress.Core.Config;
using WebExpress.Core.Internationalization;
using WebExpress.Core.WebApplication;
using WebExpress.Core.WebComponent;
using WebExpress.Core.WebHtml;
using WebExpress.Core.WebMessage;
using WebExpress.Core.WebModule;
using WebExpress.Core.WebPage;
using WebExpress.Core.WebResource;
using WebExpress.Core.WebSitemap;
using WebExpress.Core.WebUri;
namespace WebExpress.Core
{
/// <summary>
/// The web server for processing http requests (see RFC 2616). The web server uses Kestrel internally.
/// </summary>
public class HttpServer : IHost, II18N, IHttpApplication<HttpContext>
{
/// <summary>
/// Event is triggered after the web server is started.
/// </summary>
public event EventHandler Started;
/// <summary>
/// Provides the KestrelServer, which responds to the requests.
/// </summary>
private KestrelServer Kestrel { get; set; }
/// <summary>
/// Server thread termination.
/// </summary>
private CancellationToken ServerToken { get; } = new CancellationToken();
/// <summary>
/// Returns or sets the configuration
/// </summary>
public HttpServerConfig Config { get; set; }
/// <summary>
/// Returns or sets the context.
/// </summary>
public IHttpServerContext HttpServerContext { get; protected set; }
/// <summary>
/// Returns the culture.
/// </summary>
public CultureInfo Culture { get; set; }
/// <summary>
/// Returns the execution time of the web server.
/// </summary>
public static DateTime ExecutionTime { get; } = DateTime.Now;
/// <summary>
/// Returns the request number;
/// </summary>
public long RequestNumber { get; private set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="context">Der Serverkontext.</param>
public HttpServer(HttpServerContext context)
{
HttpServerContext = new HttpServerContext
(
context.Uri,
context.Endpoints,
context.PackagePath,
context.AssetPath,
context.DataPath,
context.ConfigPath,
context.ContextPath,
context.Culture,
context.Log,
this
);
Culture = HttpServerContext.Culture;
ComponentManager.Initialization(HttpServerContext);
}
/// <summary>
/// Starts the HTTP(S) server.
/// </summary>
public void Start()
{
if (HttpServerContext != null && HttpServerContext.Log != null)
{
HttpServerContext.Log.Info(message: this.I18N("webexpress:httpserver.run"));
}
if (!HttpListener.IsSupported)
{
HttpServerContext.Log.Error(message: this.I18N("webexpress:httpserver.notsupported"));
}
var logger = new LogFactory();
var transportOptions = new OptionsWrapper<SocketTransportOptions>(new SocketTransportOptions());
var transport = new SocketTransportFactory(transportOptions, logger);
var serviceCollection = new ServiceCollection();
serviceCollection.AddMemoryCache();
serviceCollection.AddLogging(x =>
{
x.SetMinimumLevel(LogLevel.Trace);
x.AddProvider(logger);
});
serviceCollection.AddHttpLogging(x =>
{
x.LoggingFields = Microsoft.AspNetCore.HttpLogging.HttpLoggingFields.All;
});
var serverOptions = new OptionsWrapper<KestrelServerOptions>(new KestrelServerOptions()
{
AllowSynchronousIO = true,
AllowResponseHeaderCompression = true,
AddServerHeader = true,
ApplicationServices = serviceCollection.BuildServiceProvider()
});
serverOptions.Value.Limits.MaxConcurrentConnections = Config?.Limit?.ConnectionLimit > 0 ? Config?.Limit?.ConnectionLimit : serverOptions.Value.Limits.MaxConcurrentConnections;
serverOptions.Value.Limits.MaxRequestBodySize = Config?.Limit?.UploadLimit > 0 ? Config?.Limit?.UploadLimit : serverOptions.Value.Limits.MaxRequestBodySize;
foreach (var endpoint in Config.Endpoints)
{
AddEndpoint(serverOptions, endpoint);
}
Kestrel = new KestrelServer(serverOptions, transport, logger);
Kestrel.StartAsync(this, ServerToken);
HttpServerContext.Log.Info(message: this.I18N("webexpress:httpserver.start"), args: new object[] { ExecutionTime.ToShortDateString(), ExecutionTime.ToLongTimeString() });
Started?.Invoke(this, new EventArgs());
}
/// <summary>
/// Adds an endpoint.
/// </summary>
/// <param name="serverOptions">The server options.</param>
/// <param name="endPoint">The endpoint.</param>
private void AddEndpoint(OptionsWrapper<KestrelServerOptions> serverOptions, EndpointConfig endPoint)
{
try
{
var uri = new UriBuilder(endPoint.Uri);
var asterisk = uri.Host.Equals("*");
var port = uri.Port;
var host = asterisk ? Dns.GetHostEntry(Dns.GetHostName()) : Dns.GetHostEntry(uri.Host);
var addressList = host.AddressList
.Union(asterisk ? Dns.GetHostEntry("localhost").AddressList : Array.Empty<IPAddress>())
.Where(x => x.AddressFamily == AddressFamily.InterNetwork || x.AddressFamily == AddressFamily.InterNetworkV6);
HttpServerContext.Log.Info(message: this.I18N("webexpress:httpserver.endpoint"), args: endPoint.Uri);
foreach (var ipAddress in addressList)
{
var ep = new IPEndPoint(ipAddress, port);
switch (uri.Scheme)
{
case "HTTPS": { AddEndpoint(serverOptions, ep, endPoint.PfxFile, endPoint.Password); break; }
default: { AddEndpoint(serverOptions, ep); break; }
}
}
}
catch (Exception ex)
{
HttpServerContext.Log.Error(message: this.I18N("webexpress:httpserver.listen.exeption"), args: endPoint);
HttpServerContext.Log.Exception(ex);
}
}
/// <summary>
/// Adds an endpoint.
/// </summary>
/// <param name="serverOptions">The server options.</param>
/// <param name="endPoint">The endpoint.</param>
private void AddEndpoint(OptionsWrapper<KestrelServerOptions> serverOptions, IPEndPoint endPoint)
{
serverOptions.Value.Listen(endPoint);
HttpServerContext.Log.Info(message: this.I18N("webexpress:httpserver.listen"), args: endPoint.ToString());
}
/// <summary>
/// Adds an endpoint.
/// </summary>
/// <param name="serverOptions">The server options.</param>
/// <param name="pfxFile">The certificate.</param>
/// <param name="password">The password to the certificate.</param>
/// <param name="endPoint">The endpoint.</param>
private void AddEndpoint(OptionsWrapper<KestrelServerOptions> serverOptions, IPEndPoint endPoint, string pfxFile, string password)
{
serverOptions.Value.Listen(endPoint, configure =>
{
var cert = new X509Certificate2(pfxFile, password);
configure.UseHttps(cert);
});
HttpServerContext.Log.Info(message: this.I18N("webexpress:httpserver.listen"), args: endPoint.ToString());
}
/// <summary>
/// Stops the HTTP(S) server
/// </summary>
public void Stop()
{
// End running threads
Kestrel.StopAsync(ServerToken);
// Stop running
ComponentManager.ShutDown();
}
/// <summary>
/// Handles an incoming request
/// Concurrent execution
/// </summary>
/// <param name="context">The context of the web request.</param>
/// <returns>The response to be sent back to the caller.</returns>
private Response HandleClient(HttpContext context)
{
var stopwatch = Stopwatch.StartNew();
var request = context.Request;
var response = default(Response);
var culture = request.Culture;
var uri = request?.Uri;
HttpServerContext.Log.Debug(message: this.I18N("webexpress:httpserver.connected"), args: context.RemoteEndPoint);
HttpServerContext.Log.Info(InternationalizationManager.I18N
(
"webexpress:httpserver.request",
context.RemoteEndPoint,
++RequestNumber,
$"{request?.Method} {request?.Uri} {request?.Protocoll}"
));
// search page in sitemap
var searchResult = ComponentManager.SitemapManager.SearchResource(context.Uri, new SearchContext()
{
Culture = culture,
HttpContext = context,
HttpServerContext = HttpServerContext
});
if (searchResult != null)
{
var resourceUri = new UriResource(request.Uri, searchResult.Uri.PathSegments);
resourceUri = new UriResource(resourceUri, resourceUri.PathSegments, request.Uri.Skip(resourceUri.PathSegments.Count())?.PathSegments);
resourceUri.ServerRoot = new UriResource(request.Uri, HttpServerContext.ContextPath.PathSegments);
resourceUri.ApplicationRoot = new UriResource(request.Uri, searchResult.ApplicationContext?.ContextPath.PathSegments);
resourceUri.ModuleRoot = new UriResource(request.Uri, searchResult.ModuleContext?.ContextPath.PathSegments);
resourceUri.ResourceRoot = new UriResource(request.Uri, searchResult.Uri.PathSegments);
request.Uri = resourceUri;
try
{
// execute resource
request.AddParameter(searchResult.Uri.Parameters.Select(x => new Parameter(x.Key, x.Value, ParameterScope.Url)));
if (searchResult.Instance != null)
{
searchResult.Instance?.PreProcess(request);
response = searchResult.Instance?.Process(request);
response = searchResult.Instance?.PostProcess(request, response);
if (searchResult.Instance is IPage)
{
response.Content += $"<!-- {stopwatch.ElapsedMilliseconds} ms -->";
}
if (response is ResponseNotFound)
{
response = CreateStatusPage<ResponseNotFound>
(
string.Empty,
request,
searchResult
);
}
if
(
!response.Header.Cookies.Where(x => x.Name.Equals("session")).Any() &&
!request.Header.Cookies.Where(x => x.Name.Equals("session")).Any() &&
request.Session != null
)
{
var cookie = new Cookie("session", request.Session.Id.ToString()) { Expires = DateTime.MaxValue };
response.Header.Cookies.Add(cookie);
}
}
else
{
// Resource not found
response = CreateStatusPage<ResponseNotFound>
(
string.Empty,
request,
searchResult
);
}
}
catch (RedirectException ex)
{
if (ex.Permanet)
{
response = new ResponseRedirectPermanentlyMoved(ex.Url);
}
else
{
response = new ResponseRedirectTemporarilyMoved(ex.Url);
}
}
catch (Exception ex)
{
HttpServerContext.Log.Exception(ex);
var message = $"<h4>Message</h4>{ex.Message}<br/><br/>" +
$"<h5>Source</h5>{ex.Source}<br/><br/>" +
$"<h5>StackTrace</h5>{ex.StackTrace.Replace("\n", "<br/>\n")}<br/><br/>" +
$"<h5>InnerException</h5>{ex.InnerException?.ToString().Replace("\n", "<br/>\n")}";
response = CreateStatusPage<ResponseInternalServerError>
(
message,
request,
searchResult
);
}
}
else
{
// Resource not found
response = CreateStatusPage<ResponseNotFound>(string.Empty, request);
}
stopwatch.Stop();
HttpServerContext.Log.Info(InternationalizationManager.I18N
(
"webexpress:httpserver.request.done",
context?.RemoteEndPoint,
RequestNumber,
stopwatch.ElapsedMilliseconds,
response.Status
));
return response;
}
/// <summary>
/// Sends the response message
/// </summary>
/// <param name="context">The context of the request</param>
/// <param name="response">The reply message</param>
/// <returns>Sending the message as a task, which is executed concurrently.</returns>
private async Task SendResponseAsync(HttpContext context, Response response)
{
try
{
var responseFeature = context.Features.Get<IHttpResponseFeature>();
var responseBodyFeature = context.Features.Get<IHttpResponseBodyFeature>();
responseFeature.StatusCode = response.Status;
responseFeature.ReasonPhrase = response.Reason;
responseFeature.Headers.KeepAlive = "true";
responseFeature.Headers.Add("PageID", "Test");
if (response.Header.Location != null)
{
responseFeature.Headers.Location = response.Header.Location;
}
if (!string.IsNullOrWhiteSpace(response.Header.CacheControl))
{
responseFeature.Headers.CacheControl = response.Header.CacheControl;
}
if (!string.IsNullOrWhiteSpace(response.Header.ContentType))
{
responseFeature.Headers.ContentType = response.Header.ContentType;
}
if (response.Header.WWWAuthenticate)
{
responseFeature.Headers.WWWAuthenticate = "Basic realm=\"Bereich\"";
}
if (response.Header.Cookies.Any())
{
responseFeature.Headers.SetCookie = string.Join(" ", response.Header.Cookies);
}
if (response?.Content is byte[] byteContent)
{
responseFeature.Headers.ContentLength = byteContent.Length;
await responseBodyFeature.Stream.WriteAsync(byteContent);
await responseBodyFeature.Stream.FlushAsync();
}
else if (response?.Content is string strContent)
{
var content = context.Encoding.GetBytes(strContent);
responseFeature.Headers.ContentLength = content.Length;
await responseBodyFeature.Stream.WriteAsync(content);
await responseBodyFeature.Stream.FlushAsync();
}
else if (response?.Content is IHtmlNode htmlContent)
{
var content = context.Encoding.GetBytes(htmlContent?.ToString());
responseFeature.Headers.ContentLength = content.Length;
await responseBodyFeature.Stream.WriteAsync(content);
await responseBodyFeature.Stream.FlushAsync();
}
responseBodyFeature.Stream.Close();
}
catch (Exception ex)
{
HttpServerContext.Log.Error(context.RemoteEndPoint + ": " + ex.Message);
}
}
/// <summary>
/// Creates a status page
/// </summary>
/// <param name="massage">The error message.</param>
/// <param name="request">The request.</param>
/// <param name="searchResult">The plugin by searching the status page or null.</param>
/// <returns>The response.</returns>
private Response CreateStatusPage<T>(string massage, Request request, SearchResult searchResult = null) where T : Response, new()
{
var response = new T() as Response;
var culture = Culture;
try
{
culture = new CultureInfo(request?.Header?.AcceptLanguage?.FirstOrDefault()?.ToLower());
}
catch
{
}
if (searchResult != null)
{
var statusPage = ComponentManager.ResponseManager.CreateStatusPage
(
massage,
response.Status,
searchResult?.ModuleContext?.PluginContext ??
searchResult?.ApplicationContext?.PluginContext
);
if (statusPage == null)
{
return response;
}
if (statusPage is II18N i18n)
{
i18n.Culture = culture;
}
if (statusPage is Resource resource)
{
resource.ApplicationContext = searchResult?.ApplicationContext ?? new ApplicationContext()
{
PluginContext = searchResult?.ModuleContext?.PluginContext ??
searchResult?.ApplicationContext?.PluginContext,
ApplicationId = "webex",
ApplicationName = "WebExpress",
ContextPath = new UriResource()
};
resource.ModuleContext = searchResult?.ModuleContext ?? new ModuleContext()
{
ApplicationContext = resource.ApplicationContext,
PluginContext = searchResult?.ModuleContext?.PluginContext ??
searchResult?.ApplicationContext?.PluginContext,
ModuleId = "webex",
ModuleName = "WebExpress",
ContextPath = new UriResource()
};
resource.Initialization(new ResourceContext(resource.ModuleContext));
}
return statusPage.Process(request);
}
var message = $"<html><head><title>{response.Status}</title></head><body>" +
$"<p>{massage}<br/><p>" +
$"</body></html>";
response.Content = message;
response.Header.ContentLength = message.Length;
response.Header.ContentType = "text/html; charset=utf-8";
return response;
}
/// <summary>
/// Create an HttpContext with a collection of HTTP features.
/// </summary>
/// <param name="contextFeatures">A collection of HTTP features to use to create the HttpContext.</param>
/// <returns>The HttpContext created.</returns>
public HttpContext CreateContext(IFeatureCollection contextFeatures)
{
try
{
return new HttpContext(contextFeatures, this.HttpServerContext);
}
catch (Exception ex)
{
return new HttpExceptionContext(ex, contextFeatures);
}
}
/// <summary>
/// Processes an http context asynchronously.
/// </summary>
/// <param name="context">The http context that the operation processes.</param>
/// <returns>Provides an asynchronous operation that handles the http context.</returns>
public async Task ProcessRequestAsync(HttpContext context)
{
if (context is HttpExceptionContext exceptionContext)
{
var message = "<html><head><title>404</title></head><body>" +
$"<h4>Message</h4>{exceptionContext.Exception.Message}<br/><br/>" +
$"<h5>Source</h5>{exceptionContext.Exception.Source}<br/><br/>" +
$"<h5>StackTrace</h5>{exceptionContext.Exception.StackTrace.Replace("\n", "<br/>\n")}<br/><br/>" +
$"<h5>InnerException</h5>{exceptionContext.Exception.InnerException?.ToString().Replace("\n", "<br/>\n")}" +
"</body></html>";
var response500 = CreateStatusPage<ResponseInternalServerError>(message, context?.Request);
await SendResponseAsync(exceptionContext, response500);
return;
}
var response = HandleClient(context);
await SendResponseAsync(context, response);
}
/// <summary>
/// Discard a specified http context.
/// </summary>
/// <param name="context">The http context to discard.</param>
/// <param name="exception">The exception that is thrown if processing did not complete successfully; otherwise null.</param>
public void DisposeContext(HttpContext context, Exception exception)
{
}
}
}