-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathStressServer.cs
395 lines (351 loc) · 16.8 KB
/
StressServer.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers;
using System.Collections.Specialized;
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Serilog;
namespace HttpStress
{
public class StressServer : IDisposable
{
// Header indicating expected response content length to be returned by the server
public const string ExpectedResponseContentLength = "Expected-Response-Content-Length";
private readonly IWebHost _webHost;
public string ServerUri { get; }
public StressServer(Configuration configuration)
{
ServerUri = configuration.ServerUri;
(string scheme, string hostname, int port) = ParseServerUri(configuration.ServerUri);
IWebHostBuilder host = WebHost.CreateDefaultBuilder();
if (configuration.UseHttpSys && OperatingSystem.IsWindows())
{
// Use http.sys. This requires additional manual configuration ahead of time;
// see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/httpsys?view=aspnetcore-2.2#configure-windows-server.
// In particular, you need to:
// 1. Create a self-signed cert and install it into your local personal store, e.g. New-SelfSignedCertificate -DnsName "localhost" -CertStoreLocation "cert:\LocalMachine\My"
// 2. Pre-register the URL prefix, e.g. netsh http add urlacl url=https://localhost:5001/ user=Users
// 3. Register the cert, e.g. netsh http add sslcert ipport=[::1]:5001 certhash=THUMBPRINTFROMABOVE appid="{some-guid}"
host = host.UseHttpSys(hso =>
{
hso.UrlPrefixes.Add(ServerUri);
hso.Authentication.Schemes = Microsoft.AspNetCore.Server.HttpSys.AuthenticationSchemes.None;
hso.Authentication.AllowAnonymous = true;
hso.MaxConnections = null;
hso.MaxRequestBodySize = null;
});
}
else
{
// Use Kestrel, and configure it for HTTPS with a self-signed test certificate.
host = host.UseKestrel(ko =>
{
// conservative estimation based on https://github.com/dotnet/aspnetcore/blob/caa910ceeba5f2b2c02c47a23ead0ca31caea6f0/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs#L204
ko.Limits.MaxRequestLineSize = Math.Max(ko.Limits.MaxRequestLineSize, configuration.MaxRequestUriSize + 100);
ko.Limits.MaxRequestHeaderCount = Math.Max(ko.Limits.MaxRequestHeaderCount, configuration.MaxRequestHeaderCount);
ko.Limits.MaxRequestHeadersTotalSize = Math.Max(ko.Limits.MaxRequestHeadersTotalSize, configuration.MaxRequestHeaderTotalSize);
ko.Limits.Http2.MaxStreamsPerConnection = configuration.ServerMaxConcurrentStreams ?? ko.Limits.Http2.MaxStreamsPerConnection;
ko.Limits.Http2.MaxFrameSize = configuration.ServerMaxFrameSize ?? ko.Limits.Http2.MaxFrameSize;
ko.Limits.Http2.InitialConnectionWindowSize = configuration.ServerInitialConnectionWindowSize ?? ko.Limits.Http2.InitialConnectionWindowSize;
ko.Limits.Http2.MaxRequestHeaderFieldSize = configuration.ServerMaxRequestHeaderFieldSize ?? ko.Limits.Http2.MaxRequestHeaderFieldSize;
switch (hostname)
{
case "+":
case "*":
ko.ListenAnyIP(port, ConfigureListenOptions);
break;
default:
IPAddress iPAddress = Dns.GetHostAddresses(hostname).First();
ko.Listen(iPAddress, port, ConfigureListenOptions);
break;
}
void ConfigureListenOptions(ListenOptions listenOptions)
{
if (scheme == "https")
{
// Create self-signed cert for server.
using (RSA rsa = RSA.Create())
{
var certReq = new CertificateRequest("CN=contoso.com", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
certReq.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false));
certReq.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection { new Oid("1.3.6.1.5.5.7.3.1") }, false));
certReq.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature, false));
X509Certificate2 cert = certReq.CreateSelfSigned(DateTimeOffset.UtcNow.AddMonths(-1), DateTimeOffset.UtcNow.AddMonths(1));
if (OperatingSystem.IsWindows())
{
cert = new X509Certificate2(cert.Export(X509ContentType.Pfx));
}
listenOptions.UseHttps(cert);
}
if (configuration.HttpVersion == HttpVersion.Version30)
{
listenOptions.Protocols = HttpProtocols.Http3;
}
}
else
{
listenOptions.Protocols =
configuration.HttpVersion == HttpVersion.Version20 ?
HttpProtocols.Http2 :
HttpProtocols.Http1 ;
}
}
});
if (configuration.HttpVersion == HttpVersion.Version30)
{
host = host.UseQuic(options =>
{
options.Alpn = "h3";
options.IdleTimeout = TimeSpan.FromMinutes(1);
});
}
};
LoggerConfiguration loggerConfiguration = new LoggerConfiguration();
if (configuration.Trace)
{
// Clear existing logs first.
foreach (var filename in Directory.GetFiles(".", "server*.log"))
{
try
{
File.Delete(filename);
} catch {}
}
loggerConfiguration = loggerConfiguration
// Output diagnostics to the file
.WriteTo.File("server.log", fileSizeLimitBytes: 50 << 20, rollOnFileSizeLimit: true)
.MinimumLevel.Debug();
}
if (configuration.LogAspNet)
{
loggerConfiguration = loggerConfiguration
// Output only warnings and errors
.WriteTo.Console(Serilog.Events.LogEventLevel.Warning);
}
Log.Logger = loggerConfiguration.CreateLogger();
host = host
.UseSerilog()
// Set up how each request should be handled by the server.
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(MapRoutes);
});
_webHost = host.Build();
_webHost.Start();
}
private static void MapRoutes(IEndpointRouteBuilder endpoints)
{
var loggerFactory = endpoints.ServiceProvider.GetService<ILoggerFactory>();
var logger = loggerFactory?.CreateLogger<StressServer>();
var head = new[] { "HEAD" };
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("ok");
});
endpoints.MapGet("/get", async context =>
{
// Get requests just send back the requested content.
string content = CreateResponseContent(context);
await context.Response.WriteAsync(content);
});
endpoints.MapGet("/slow", async context =>
{
// Sends back the content a character at a time.
string content = CreateResponseContent(context);
for (int i = 0; i < content.Length; i++)
{
await context.Response.WriteAsync(content[i].ToString());
await context.Response.Body.FlushAsync();
}
});
endpoints.MapGet("/headers", async context =>
{
(string name, StringValues values)[] headersToEcho =
context.Request.Headers
.Where(h => h.Key.StartsWith("header-"))
// kestrel does not seem to be splitting comma separated header values, handle here
.Select(h => (h.Key, new StringValues(h.Value.SelectMany(v => v.Split(',')).Select(x => x.Trim()).ToArray())))
.ToArray();
foreach ((string name, StringValues values) in headersToEcho)
{
context.Response.Headers.Add(name, values);
}
// send back a checksum of all the echoed headers
ulong checksum = CRC.CalculateHeaderCrc(headersToEcho);
AppendChecksumHeader(context.Response.Headers, checksum);
await context.Response.WriteAsync("ok");
if (context.Response.SupportsTrailers())
{
// just add variations of already echoed headers as trailers
foreach ((string name, StringValues values) in headersToEcho)
{
context.Response.AppendTrailer(name + "-trailer", values);
}
}
});
endpoints.MapGet("/variables", async context =>
{
NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(context.Request.QueryString.Value!);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nameValueCollection.Count; i++)
{
sb.Append(nameValueCollection[$"Var{i}"]);
}
await context.Response.WriteAsync(sb.ToString());
});
endpoints.MapGet("/abort", async context =>
{
// Server writes some content, then aborts the connection
string content = CreateResponseContent(context);
await context.Response.WriteAsync(content.Substring(0, content.Length / 2));
context.Abort();
});
endpoints.MapPost("/", async context =>
{
// Post echos back the requested content, first buffering it all server-side, then sending it all back.
var s = new MemoryStream();
await context.Request.Body.CopyToAsync(s);
ulong checksum = CRC.CalculateCRC(s.ToArray());
AppendChecksumHeader(context.Response.Headers, checksum);
s.Position = 0;
await s.CopyToAsync(context.Response.Body);
});
endpoints.MapPost("/duplex", async context =>
{
// Echos back the requested content in a full duplex manner.
ArrayPool<byte> bufferPool = ArrayPool<byte>.Shared;
byte[] buffer = bufferPool.Rent(512);
ulong hashAcc = CRC.InitialCrc;
int read;
try
{
while ((read = await context.Request.Body.ReadAsync(buffer)) != 0)
{
hashAcc = CRC.update_crc(hashAcc, buffer, read);
await context.Response.Body.WriteAsync(buffer, 0, read);
}
}
finally
{
bufferPool.Return(buffer);
}
hashAcc = CRC.InitialCrc ^ hashAcc;
if (context.Response.SupportsTrailers())
{
context.Response.AppendTrailer("crc32", hashAcc.ToString());
}
});
endpoints.MapPost("/duplexSlow", async context =>
{
// Echos back the requested content in a full duplex manner, but one byte at a time.
var buffer = new byte[1];
ulong hashAcc = CRC.InitialCrc;
while ((await context.Request.Body.ReadAsync(buffer)) != 0)
{
hashAcc = CRC.update_crc(hashAcc, buffer, buffer.Length);
await context.Response.Body.WriteAsync(buffer);
}
hashAcc = CRC.InitialCrc ^ hashAcc;
if (context.Response.SupportsTrailers())
{
context.Response.AppendTrailer("crc32", hashAcc.ToString());
}
});
endpoints.MapMethods("/", head, context =>
{
// Just set the max content length on the response.
string content = CreateResponseContent(context);
context.Response.Headers.ContentLength = content.Length;
return Task.CompletedTask;
});
endpoints.MapPut("/", async context =>
{
// Read the full request but don't send back a response body.
await context.Request.Body.CopyToAsync(Stream.Null);
});
}
private static void AppendChecksumHeader(IHeaderDictionary headers, ulong checksum)
{
headers.Add("crc32", checksum.ToString());
}
public void Dispose()
{
_webHost.Dispose();
}
private static (string scheme, string hostname, int port) ParseServerUri(string serverUri)
{
try
{
var uri = new Uri(serverUri);
return (uri.Scheme, uri.Host, uri.Port);
}
catch (UriFormatException)
{
// Simple uri parser: used to parse values valid in Kestrel
// but not representable by the System.Uri class, e.g. https://+:5050
Match m = Regex.Match(serverUri, "^(?<scheme>https?)://(?<host>[^:/]+)(:(?<port>[0-9]+))?");
if (!m.Success) throw;
string scheme = m.Groups["scheme"].Value;
string hostname = m.Groups["host"].Value;
int port = m.Groups["port"].Success ? int.Parse(m.Groups["port"].Value) : (scheme == "https" ? 443 : 80);
return (scheme, hostname, port);
}
}
private static string CreateResponseContent(HttpContext ctx)
{
return ServerContentUtils.CreateStringContent(GetExpectedContentLength());
int GetExpectedContentLength()
{
if (ctx.Request.Headers.TryGetValue(ExpectedResponseContentLength, out StringValues values) &&
values.Count == 1 &&
int.TryParse(values[0], out int result))
{
return result;
}
throw new Exception($"Could not parse {ExpectedResponseContentLength} header");
}
}
}
public static class ServerContentUtils
{
// deterministically generate ascii string of given length
public static string CreateStringContent(int contentSize) =>
new String(
Enumerable
.Range(0, contentSize)
.Select(i => (char)(i % 128))
.ToArray());
// used for validating content on client side
public static bool IsValidServerContent(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (input[i] != i % 128)
return false;
}
return true;
}
}
}