Repository navigation
Expand file tree
/
Copy pathMiniWebServer.cs
More file actions
237 lines (198 loc) · 8.37 KB
/
Copy pathMiniWebServer.cs
File metadata and controls
237 lines (198 loc) · 8.37 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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MiniWebServer.Abstractions;
using MiniWebServer.Server.Abstractions;
using System.Collections.Concurrent;
using System.IO.Pipelines;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
namespace MiniWebServer.Server;
public class MiniWebServer : BackgroundService, IServer
{
private readonly MiniWebServerConfiguration config;
private readonly ServiceProvider serviceProvider;
private readonly IProtocolHandlerFactory protocolHandlerFactory;
private readonly IDictionary<string, Host.Host> hostContainers;
private readonly ILogger<MiniWebServer> logger;
private bool running;
private ulong nextClientId = 0;
private readonly IRequestIdManager requestIdManager;
private readonly ConcurrentDictionary<ulong, Task> clientTasks = new();
private readonly CancellationTokenSource cancellationTokenSource; // we use this to cancel system-wise threads
private readonly CancellationToken cancellationToken;
public MiniWebServer(
MiniWebServerConfiguration config,
ServiceProvider serviceProvider,
IProtocolHandlerFactory? protocolHandlerFactory,
IDictionary<string, Host.Host>? hostContainers
)
{
this.config = config ?? throw new ArgumentNullException(nameof(config));
this.serviceProvider = serviceProvider;
this.protocolHandlerFactory = protocolHandlerFactory ?? throw new ArgumentNullException(nameof(protocolHandlerFactory));
this.hostContainers = hostContainers ?? throw new ArgumentNullException(nameof(hostContainers));
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
logger = loggerFactory.CreateLogger<MiniWebServer>();
running = false;
cancellationTokenSource = new();
cancellationToken = cancellationTokenSource.Token;
requestIdManager = serviceProvider.GetService<IRequestIdManager>() ?? new RequestIdManager();
}
private async Task HandleNewClientConnectionAsync(ulong connectionId, MiniWebServerBindingConfiguration binding, TcpClient tcpClient)
{
try
{
Stream stream = tcpClient.GetStream();
bool isHttps = false;
SslStream? sslStream = null;
if (binding.Certificate != null)
{
sslStream = new SslStream(stream);
SslServerAuthenticationOptions options = new()
{
ApplicationProtocols =
[
SslApplicationProtocol.Http11
],
ServerCertificate = binding.Certificate,
EnabledSslProtocols = SslProtocols.None, // use the system default version
ClientCertificateRequired = false,
CertificateRevocationCheckMode = X509RevocationMode.NoCheck,
RemoteCertificateValidationCallback = ValidateClientCertificate
};
try
{
await sslStream.AuthenticateAsServerAsync(options);
}
catch
{
await sslStream.DisposeAsync();
throw;
}
stream = sslStream;
isHttps = true;
}
using var serviceScope = serviceProvider.CreateScope();
//int httpVersion = ProtocolHandlerFactory.HTTP20; // TODO: use HTTP2 magic string or TLS protocol exts to determine HTTP version, we will need to move HTTP version to connection handler
var client = new MiniWebClientConnection(
new MiniWebConnectionConfiguration(
connectionId,
tcpClient,
stream,
isHttps,
hostContainers,
requestIdManager,
TimeSpan.FromMilliseconds(config.ReadRequestTimeout),
TimeSpan.FromMilliseconds(config.SendResponseTimeout),
TimeSpan.FromMilliseconds(config.ExecuteTimeout),
config.ReadBufferSize,
config.MaxRequestBodySize
),
serviceProvider.GetRequiredService<IProtocolHandlerFactory>(),
serviceScope.ServiceProvider,
cancellationToken
);
logger.LogInformation("[{cid}] - New connection added {tcpClient}", connectionId, tcpClient.Client.RemoteEndPoint);
await client.HandleRequestAsync();
}
catch (AuthenticationException ex)
{
logger.LogError(ex, "Error accepting client");
CloseConnection(tcpClient);
}
catch (Exception ex)
{
logger.LogError(ex, "Error accepting client");
CloseConnection(tcpClient);
}
clientTasks.TryRemove(connectionId, out _);
}
private bool ValidateClientCertificate(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
{
// ClientCertificateRequired = false, so no certificate from the client is acceptable
if (certificate == null)
{
return true;
}
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
logger.LogWarning("Client certificate validation failed: {errors}", sslPolicyErrors);
return false;
}
private async Task ClientConnectionListeningProc(MiniWebServerBindingConfiguration binding, CancellationToken cancellationToken)
{
var listener = new TcpListener(binding.HttpEndPoint);
listener.Start();
logger.LogInformation("Server started on {binding}({https})", binding.HttpEndPoint, binding.Certificate != null ? "HTTPS" : "HTTP");
while (running && !cancellationToken.IsCancellationRequested)
{
try
{
TcpClient client = await listener.AcceptTcpClientAsync(cancellationToken);
var connectionId = Interlocked.Increment(ref nextClientId); // this function can be called concurrently (or not?) so we cannot use ++
logger.LogDebug("New client connected! ClientID = {cid}", connectionId);
Task t = HandleNewClientConnectionAsync(connectionId, binding, client);
if (!clientTasks.TryAdd(connectionId, t))
{
// hope this will never happen
logger.LogError("Ooops! Cannot add task to client task list");
}
}
catch (OperationCanceledException)
{
logger.LogInformation("Server socket stopped listening on {binding}", binding.HttpEndPoint);
break;
}
catch (Exception ex)
{
logger.LogError(ex, "Error accepting client socket on {binding}", binding.HttpEndPoint);
break;
}
}
listener.Stop();
Task.WaitAll([.. clientTasks.Values], TimeSpan.FromSeconds(30));
}
private static void CloseConnection(TcpClient tcpClient)
{
tcpClient.Close();
tcpClient.Dispose();
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("Starting web server...");
running = true;
var clientConnectionListeningProcs = new List<Task>();
foreach (var binding in config.Bindings)
{
clientConnectionListeningProcs.Add(ClientConnectionListeningProc(binding, stoppingToken));
}
try
{
Task.WaitAll([.. clientConnectionListeningProcs], stoppingToken);
}
catch (OperationCanceledException)
{
// do nothing
}
return Task.CompletedTask;
}
public Task Start(CancellationToken? cancellationToken = null)
{
if (config.Bindings.Count == 0)
{
throw new InvalidOperationException("No binding settings found");
}
return ExecuteAsync(cancellationToken ?? CancellationToken.None);
}
public void Stop(CancellationToken? cancellationToken = null)
{
running = false;
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(3));
}
}