Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (43 loc) · 1.56 KB
/
Copy pathProgram.cs
File metadata and controls
57 lines (43 loc) · 1.56 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
using GenHTTP.Api.Protocol;
using GenHTTP.Engine.Internal;
using GenHTTP.Modules.Functional;
using GenHTTP.Modules.Practices;
var port = (args.Length > 0 && ushort.TryParse(args[0], out var p)) ? p : (ushort)8080;
var rootMethods = new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Get),
FlexibleRequestMethod.Get(RequestMethod.Head),
FlexibleRequestMethod.Get(RequestMethod.Options)
};
var app = Inline.Create()
.Get("/cookie", (IRequest request) => ParseCookies(request))
.Post("/cookie", (IRequest request) => ParseCookies(request))
.Get("/echo", (IRequest request) => Echo(request))
.Post("/echo", (IRequest request) => Echo(request))
.Post((Stream body) => RequestContent(body))
.On(() => StringContent(), rootMethods);
return await Host.Create()
.Handler(app)
.Defaults(rangeSupport: true)
.Port(port)
.RunAsync();
static string Echo(IRequest request)
{
var headers = new System.Text.StringBuilder();
foreach (var h in request.Headers)
{
headers.AppendLine($"{h.Key}: {h.Value}");
}
return headers.ToString();
}
static string ParseCookies(IRequest request)
{
var sb = new System.Text.StringBuilder();
foreach (var cookie in request.Cookies.Values)
{
sb.AppendLine($"{cookie.Name}={cookie.Value}");
}
return sb.ToString();
}
static string StringContent() => "OK";
static Stream RequestContent(Stream body) => body;