-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpContextModels.cs
More file actions
151 lines (132 loc) · 4.48 KB
/
Copy pathHttpContextModels.cs
File metadata and controls
151 lines (132 loc) · 4.48 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
using System.Text;
public sealed class HttpRequestContext
{
public required string Method { get; init; }
public required string RawPath { get; init; }
public required string Path { get; init; }
public required string QueryString { get; init; }
public required string Body { get; init; }
public required IReadOnlyDictionary<string, string> Headers { get; init; }
public required IReadOnlyDictionary<string, string> Cookies { get; init; }
public required IReadOnlyDictionary<string, string> Query { get; init; }
public required IReadOnlyDictionary<string, string> Form { get; init; }
}
public sealed class HttpResponseContext
{
private readonly List<string> _setCookieHeaders = [];
public IReadOnlyList<string> SetCookieHeaders => _setCookieHeaders;
public void SetCookie(
string name,
string value,
string path = "/",
bool httpOnly = true,
int? maxAgeSeconds = null,
bool secure = false,
string sameSite = "Lax")
{
var encodedName = Uri.EscapeDataString(name);
var encodedValue = Uri.EscapeDataString(value);
var cookie = new StringBuilder($"{encodedName}={encodedValue}; Path={path}; SameSite={sameSite}");
if (httpOnly)
{
cookie.Append("; HttpOnly");
}
if (secure)
{
cookie.Append("; Secure");
}
if (maxAgeSeconds.HasValue)
{
cookie.Append($"; Max-Age={maxAgeSeconds.Value}");
}
_setCookieHeaders.Add(cookie.ToString());
}
}
public static class HttpParsing
{
public static HttpRequestContext BuildRequest(
string method,
string rawPath,
string body,
IReadOnlyDictionary<string, string> headers)
{
var pathWithoutQuery = rawPath.Split('?', 2)[0];
var queryString = rawPath.Contains('?') ? rawPath.Split('?', 2)[1] : string.Empty;
var query = ParseFormLike(queryString);
var form = ParseFormLike(body);
var cookies = ParseCookies(headers.TryGetValue("Cookie", out var cookieHeader) ? cookieHeader : string.Empty);
return new HttpRequestContext
{
Method = method,
RawPath = rawPath,
Path = pathWithoutQuery,
QueryString = queryString,
Body = body,
Headers = headers,
Cookies = cookies,
Query = query,
Form = form
};
}
public static Dictionary<string, string> ParseHeaders(IEnumerable<string> headerLines)
{
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var line in headerLines)
{
var separator = line.IndexOf(':');
if (separator <= 0)
{
continue;
}
var key = line[..separator].Trim();
var value = line[(separator + 1)..].Trim();
headers[key] = value;
}
return headers;
}
private static Dictionary<string, string> ParseCookies(string cookieHeader)
{
var cookies = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
if (string.IsNullOrWhiteSpace(cookieHeader))
{
return cookies;
}
var parts = cookieHeader.Split(';', StringSplitOptions.RemoveEmptyEntries);
foreach (var part in parts)
{
var kv = part.Split('=', 2);
var key = SafeDecode(kv[0].Trim().Replace('+', ' '));
var value = kv.Length > 1 ? SafeDecode(kv[1].Trim().Replace('+', ' ')) : string.Empty;
cookies[key] = value;
}
return cookies;
}
private static Dictionary<string, string> ParseFormLike(string input)
{
var values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
if (string.IsNullOrWhiteSpace(input))
{
return values;
}
var parts = input.Split('&', StringSplitOptions.RemoveEmptyEntries);
foreach (var part in parts)
{
var kv = part.Split('=', 2);
var key = SafeDecode(kv[0].Replace('+', ' '));
var value = kv.Length > 1 ? SafeDecode(kv[1].Replace('+', ' ')) : string.Empty;
values[key] = value;
}
return values;
}
private static string SafeDecode(string input)
{
try
{
return Uri.UnescapeDataString(input);
}
catch
{
return input;
}
}
}