Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
71 lines (49 loc) · 1.79 KB
/
Copy pathProgram.cs
File metadata and controls
71 lines (49 loc) · 1.79 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
using System.Text;
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<RequestMethod> { RequestMethod.Get, RequestMethod.Head, 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 StringBuilder();
var source = request.Header.Headers;
for (var i = 0; i < source.Count; i++)
{
var header = source[i];
headers.AppendLine($"{header.Key}: {header.Value}");
}
return headers.ToString();
}
static string ParseCookies(IRequest request)
{
var sb = new StringBuilder();
var cookies = request.Header.Headers.GetCookies();
for (var i = 0; i < cookies.Count; i++)
{
var cookie = cookies[i];
sb.AppendLine($"{cookie.Key}: {cookie.Value}");
}
return sb.ToString();
}
static string StringContent() => "OK";
static async Task<Stream> RequestContent(Stream body)
{
var memoryStream = new MemoryStream();
await body.CopyToAsync(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}