-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathProgram.cs
More file actions
297 lines (252 loc) · 8.62 KB
/
Program.cs
File metadata and controls
297 lines (252 loc) · 8.62 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
using System.Security.Cryptography.X509Certificates;
using Waher.Content;
using Waher.Content.Markdown;
using Waher.Content.Markdown.Web;
using Waher.Events;
using Waher.Events.Console;
using Waher.Networking.HTTP;
using Waher.Networking.HTTP.Brotli;
using Waher.Networking.HTTP.ContentEncodings;
using Waher.Networking.Sniffers;
using Waher.Runtime.Console;
using Waher.Runtime.Inventory;
using Waher.Runtime.IO;
using Waher.Runtime.Profiling;
internal class Program
{
/// <summary>
/// Test server for use with the the h2spec and similar.
///
/// References:
/// https://github.com/summerwind/h2spec
/// https://github.com/httpwg/wiki/wiki/HTTP-Testing-Resources
/// </summary>
/// <example>
/// To run tests over HTTPS
///
/// Test-server command-line arguments:
/// -http PORT Port number for HTTP
/// -http2 PORT Port number for HTTPS
/// -cert FILENAME Certificate file name.
/// -pwd PASSWORD Certificate password, if any.
/// -no7540prio No RFC 7540 priorities, in accordance with RFC 9218
/// -deflate Permit deflate encoding
/// -gzip Permit gzip encoding
/// -br Permit br encoding
///
/// Example:
/// Waher.Networking.HTTP.TestServer.exe -http 8081 -https 8088 -cert CERTIFICATE_FILENAME -deflate
///
/// h2spec command-line arguments:
/// h2spec.exe http2 -h 127.0.0.1 -p 8088 -P /Hello -t -k --strict
/// </example>
/// <example>
/// To run tests over HTTP
///
/// Test-server command-line arguments:
/// Waher.Networking.HTTP.TestServer.exe -http 8081
///
/// h2spec command-line arguments:
///
/// h2spec.exe -h 127.0.0.1 -p 8081 -P /Hello
/// h2spec.exe -h 127.0.0.1 -p 8081 -P /Hello --strict
///
/// h2spec.exe http2 -h 127.0.0.1 -p 8081 -P /Hello
/// h2spec.exe http2 -h 127.0.0.1 -p 8081 -P /Hello --strict
///
/// h2spec.exe hpack -h 127.0.0.1 -p 8081 -P /Hello
/// h2spec.exe hpack -h 127.0.0.1 -p 8081 -P /Hello --strict
///
/// h2spec.exe generic -h 127.0.0.1 -p 8081 -P /Hello
/// h2spec.exe generic -h 127.0.0.1 -p 8081 -P /Hello --strict
///
/// h2spec.exe -h 127.0.0.1 -p 8081 -P /Hello.md
/// h2spec.exe -h 127.0.0.1 -p 8081 -P /Hello.md --strict
///
/// h2spec.exe http2 -h 127.0.0.1 -p 8081 -P /Hello.md
/// h2spec.exe http2 -h 127.0.0.1 -p 8081 -P /Hello.md --strict
///
/// h2spec.exe hpack -h 127.0.0.1 -p 8081 -P /Hello.md
/// h2spec.exe hpack -h 127.0.0.1 -p 8081 -P /Hello.md --strict
///
/// h2spec.exe generic -h 127.0.0.1 -p 8081 -P /Hello.md
/// h2spec.exe generic -h 127.0.0.1 -p 8081 -P /Hello.md --strict
/// </example>
/// <param name="args">Command-line arguments.</param>
private static void Main(string[] args)
{
X509Certificate2? Certificate = null;
HttpServer? WebServer = null;
ConsoleOutSniffer? Sniffer = null;
string s;
string? CertFileName = null;
string? CertPassword = null;
int HttpPort = 0;
int HttpsPort = 0;
int i = 0;
int c = args.Length;
bool Help = false;
bool Terminating = false;
bool No7540prio = false;
try
{
Log.Register(new ConsoleEventSink(true, true));
while (i < c)
{
s = args[i++].ToLower();
switch (s)
{
case "-http":
if (i >= c)
throw new Exception("Missing HTTP Port.");
if (HttpPort != 0)
throw new Exception("HTTP Port already defined.");
if (!int.TryParse(args[i++], out HttpPort) || HttpPort <= 0 || HttpPort > ushort.MaxValue)
throw new Exception("Invalid HTTP Port number.");
break;
case "-https":
if (i >= c)
throw new Exception("Missing HTTPS Port.");
if (HttpsPort != 0)
throw new Exception("HTTPS Port already defined.");
if (!int.TryParse(args[i++], out HttpsPort) || HttpsPort <= 0 || HttpsPort > ushort.MaxValue)
throw new Exception("Invalid HTTPS Port number.");
break;
case "-cert":
if (i >= c)
throw new Exception("Missing Certificate file name.");
if (!string.IsNullOrEmpty(CertFileName))
throw new Exception("Certificate file name already defined.");
CertFileName = args[i++];
break;
case "-pwd":
if (i >= c)
throw new Exception("Missing Certificate password.");
if (!string.IsNullOrEmpty(CertPassword))
throw new Exception("Certificate password already defined.");
CertPassword = args[i++];
break;
case "-no7540prio":
No7540prio = true;
break;
case "-deflate":
DeflateContentEncoding DeflateContentEncoding = new();
DeflateContentEncoding.ConfigureSupport(true, true);
break;
case "-gzip":
GZipContentEncoding GZipContentEncoding = new();
GZipContentEncoding.ConfigureSupport(true, true);
break;
case "-br":
BrotliContentEncoding BrotliContentEncoding = new();
BrotliContentEncoding.ConfigureSupport(true, true);
break;
case "-?":
Help = true;
break;
default:
throw new Exception("Unrecognized switch: " + s);
}
}
if (Help)
{
ConsoleOut.WriteLine("-http PORT_NUMBER The port number to use for unencrypted HTTP connections.");
ConsoleOut.WriteLine("-https PORT_NUMBER The port number to use for encrypted HTTPS connections.");
ConsoleOut.WriteLine("-cert FILENAME Certificate file name.");
ConsoleOut.WriteLine("-pwd PASSWORD Certificate password, if any.");
ConsoleOut.WriteLine("-no7540prio No RFC 7540 priorities, in accordance with RFC 9218");
ConsoleOut.WriteLine("-deflate Permit deflate encoding");
ConsoleOut.WriteLine("-gzip Permit gzip encoding");
ConsoleOut.WriteLine("-br Permit br encoding");
ConsoleOut.WriteLine("-? Help.");
return;
}
if (!string.IsNullOrEmpty(CertFileName))
{
if (string.IsNullOrEmpty(CertPassword))
Certificate = new X509Certificate2(CertFileName);
else
Certificate = new X509Certificate2(CertFileName, CertPassword);
}
if (HttpPort == 0)
throw new Exception("HTTP Port not defined.");
if (Certificate is not null && HttpsPort == 0)
throw new Exception("HTTPS Port not defined.");
Log.Informational("Starting application.");
Types.Initialize(
typeof(HttpServer).Assembly,
typeof(BrotliContentEncoding).Assembly,
typeof(Log).Assembly,
typeof(InternetContent).Assembly,
typeof(MarkdownDocument).Assembly,
typeof(MarkdownToHtmlConverter).Assembly);
Sniffer = new ConsoleOutSniffer(BinaryPresentationMethod.Hexadecimal, LineEnding.PadWithSpaces);
if (Certificate is null)
{
Log.Informational("Starting unencrypted web-server.");
WebServer = new HttpServer(HttpPort, Sniffer);
}
else
{
Log.Informational("Starting encrypted web-server.");
WebServer = new HttpServer([HttpPort], [HttpsPort], Certificate, Sniffer);
}
int ProfilerIndex = 0;
WebServer.SetHttp2ConnectionSettings(true, 2500000, 5000000, 16384, 100, 8192, false, No7540prio, true, true);
WebServer.ConnectionProfiled += (Sender, e) =>
{
if (!Directory.Exists("Profiler"))
Directory.CreateDirectory("Profiler");
string Uml = e.Profiler.ExportPlantUml(TimeUnit.Seconds);
string FileName = Path.Combine("Profiler", (++ProfilerIndex).ToString() + ".uml");
return Files.WriteAllTextAsync(FileName, Uml);
};
static Task Hello(HttpRequest Request, HttpResponse Response)
{
Response.ContentType = "text/plain";
return Response.Write("Hello World.");
};
static async Task HelloMarkdown(HttpRequest Request, HttpResponse Response)
{
string Markdown = "Title: Hello World\r\nCSS: Hello.css\r\n\r\nHello **World**.";
MarkdownSettings Settings = new()
{
Progress = Request.Http2Stream
};
MarkdownDocument Doc = await MarkdownDocument.CreateAsync(Markdown, Settings);
string Html = await Doc.GenerateHTML();
Response.ContentType = "text/html";
await Response.Write(Html);
}
;
static Task HelloStyles(HttpRequest Request, HttpResponse Response)
{
Response.ContentType = "text/css";
return Response.Write("body\r\n{\r\nbackground-color:yellow\r\n}");
}
;
WebServer.Register("/Hello", Hello, Hello);
WebServer.Register("/Hello.md", HelloMarkdown, HelloMarkdown);
WebServer.Register("/Hello.css", HelloStyles);
Log.Informational("Press CTRL+C to quit.");
Console.CancelKeyPress += (_, e) =>
{
Terminating = true;
e.Cancel = true;
};
while (!Terminating)
Thread.Sleep(100);
}
catch (Exception ex)
{
Log.Exception(ex);
}
finally
{
Log.Informational("Shutting down.");
WebServer?.DisposeAsync().Wait();
Sniffer?.DisposeAsync().Wait();
Log.TerminateAsync().Wait();
}
}
}