forked from MDA2AV/Http11Probe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
114 lines (91 loc) · 4.37 KB
/
Program.cs
File metadata and controls
114 lines (91 loc) · 4.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
using System.Text;
using EffinitiveFramework.Core;
using EffinitiveFramework.Core.Http;
using EffinitiveServer.Endpoints;
var port = args.Length > 0 && int.TryParse(args[0], out var p) ? p : 8080;
var app = EffinitiveApp
.Create()
.UsePort(port)
.MapEndpoints()
.Build();
Console.WriteLine($"Effinitive listening on http://localhost:{port}");
await app.RunAsync();
namespace EffinitiveServer.Endpoints
{
// ── GET / ──────────────────────────────────────────────────────
sealed class GetRoot : NoRequestEndpointBase<string>
{
protected override string Method => "GET";
protected override string Route => "/";
protected override string ContentType => Helpers.TextPlain;
public override ValueTask<string> HandleAsync(CancellationToken ct = default)
=> ValueTask.FromResult("OK");
}
// ── POST / ─────────────────────────────────────────────────────
sealed class PostRoot : NoRequestEndpointBase<string>
{
protected override string Method => "POST";
protected override string Route => "/";
protected override string ContentType => Helpers.TextPlain;
public override ValueTask<string> HandleAsync(CancellationToken ct = default)
{
var body = HttpContext?.Body;
return ValueTask.FromResult(body is { Length: > 0 } ? Encoding.UTF8.GetString(body) : "");
}
}
// ── GET/POST /echo ────────────────────────────────────────────
sealed class EchoGet : NoRequestEndpointBase<string>
{
protected override string Method => "GET";
protected override string Route => "/echo";
protected override string ContentType => Helpers.TextPlain;
public override ValueTask<string> HandleAsync(CancellationToken ct = default)
=> ValueTask.FromResult(Helpers.EchoHeaders(HttpContext));
}
sealed class EchoPost : NoRequestEndpointBase<string>
{
protected override string Method => "POST";
protected override string Route => "/echo";
protected override string ContentType => Helpers.TextPlain;
public override ValueTask<string> HandleAsync(CancellationToken ct = default)
=> ValueTask.FromResult(Helpers.EchoHeaders(HttpContext));
}
// ── GET/POST /cookie ──────────────────────────────────────────
sealed class CookieGet : NoRequestEndpointBase<string>
{
protected override string Method => "GET";
protected override string Route => "/cookie";
protected override string ContentType => Helpers.TextPlain;
public override ValueTask<string> HandleAsync(CancellationToken ct = default)
=> ValueTask.FromResult(Helpers.ParseCookies(HttpContext));
}
sealed class CookiePost : NoRequestEndpointBase<string>
{
protected override string Method => "POST";
protected override string Route => "/cookie";
protected override string ContentType => Helpers.TextPlain;
public override ValueTask<string> HandleAsync(CancellationToken ct = default)
=> ValueTask.FromResult(Helpers.ParseCookies(HttpContext));
}
// ── Shared helpers ────────────────────────────────────────────
static class Helpers
{
public const string TextPlain = "text/plain";
public static string EchoHeaders(HttpRequest? ctx)
{
if (ctx?.Headers is null) return "";
var sb = new StringBuilder();
foreach (var h in ctx.Headers)
sb.Append(h.Key).Append(": ").Append(h.Value).Append("\r\n");
return sb.ToString();
}
public static string ParseCookies(HttpRequest? ctx)
{
if (ctx is null) return "";
var sb = new StringBuilder();
foreach (var c in ctx.Cookies)
sb.Append(c.Key).Append('=').Append(c.Value).Append("\r\n");
return sb.ToString();
}
}
}