-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathByteSequenceHttpParser.cs
267 lines (235 loc) · 9.36 KB
/
ByteSequenceHttpParser.cs
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
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using MiniWebServer.Abstractions.Http;
using MiniWebServer.Server.Abstractions.Parsers.Http11;
using System.Buffers;
using System.Text;
using HttpMethod = MiniWebServer.Abstractions.Http.HttpMethod;
namespace MiniWebServer.HttpParser.Http11
{
// we will migrate code to this, working on RegEx is much slower and also hard to keep it safe (because we cannot control what clients send to us)
public class ByteSequenceHttpParser : IHttpComponentParser
{
private readonly ILogger<ByteSequenceHttpParser> logger;
private static readonly Dictionary<HttpMethod, byte[]> supportedMethodBytes = new() {
{
HttpMethod.Get,
Encoding.ASCII.GetBytes("GET")
},
{
HttpMethod.Post,
Encoding.ASCII.GetBytes("POST")
},
{
HttpMethod.Head,
Encoding.ASCII.GetBytes("HEAD")
},
{
HttpMethod.Put,
Encoding.ASCII.GetBytes("PUT")
},
{
HttpMethod.Delete,
Encoding.ASCII.GetBytes("DELETE")
},
{
HttpMethod.Connect,
Encoding.ASCII.GetBytes("CONNECT")
},
{
HttpMethod.Options,
Encoding.ASCII.GetBytes("OPTIONS")
},
{
HttpMethod.Trace,
Encoding.ASCII.GetBytes("TRACE")
}
};
private static readonly byte[] HTTP1_1_Bytes = Encoding.ASCII.GetBytes("HTTP/1.1");
private static readonly byte[] HTTP1_1_CR_Bytes = Encoding.ASCII.GetBytes("HTTP/1.1\r");
private readonly int maxUrlPartLength;
public ByteSequenceHttpParser(ILoggerFactory? loggerFactory = default, int maxUrlPartLength = -1)
{
// note that maxUrlPartLength includes all url parts (url, hash, query string)
if (loggerFactory != null)
{
logger = loggerFactory.CreateLogger<ByteSequenceHttpParser>();
}
else
{
logger = new NullLogger<ByteSequenceHttpParser>();
}
this.maxUrlPartLength = maxUrlPartLength <= 0 ? 8192 : maxUrlPartLength; // 8192 = 8KB
}
// in any case, if this function returns null, server should return a 400 Bad Request
public virtual HttpRequestLine? ParseRequestLine(ReadOnlySequence<byte> buffer)
{
SequencePosition? pos = buffer.PositionOf((byte)' '); // there are 2 SPs in a request line
if (pos.HasValue)
{
HttpMethod? httpMethod = null;
var methodBytes = buffer.Slice(0, pos.Value);
if (methodBytes.Length < 3)
{
logger.LogDebug("Method too short");
return null;
}
else if (methodBytes.Length > 7)
{
logger.LogDebug("Method too long");
return null;
}
var span = methodBytes.FirstSpan;
foreach (var supportedHeader in supportedMethodBytes)
{
if (span.SequenceCompareTo(supportedHeader.Value) == 0)
{
httpMethod = supportedHeader.Key;
break;
}
}
if (httpMethod == null)
{
logger.LogDebug("Not supported method");
return null;
}
buffer = buffer.Slice(buffer.GetPosition(1, pos.Value));
// start parsing Url
pos = buffer.PositionOf((byte)' '); // find the next SP
if (!pos.HasValue)
{
logger.LogDebug("2nd SP required");
return null;
}
if (buffer.Length > maxUrlPartLength + 11) // 11 = length of SP HTTP/1.1 CR LF
{
logger.LogDebug("Url part too long");
return null;
}
if (TryParseUrl(buffer.Slice(0, pos.Value), out string? url, out string? hash, out string? queryString, out string[]? segments, out HttpParameters? parameters) && !string.IsNullOrEmpty(url)) // url cannot be missing
{
buffer = buffer.Slice(buffer.GetPosition(1, pos.Value));
// remove last CR
var ba = buffer.ToArray();
if (HTTP1_1_CR_Bytes.SequenceEqual(ba) || HTTP1_1_Bytes.SequenceEqual(ba)) // todo: can we use memory pool instead of ToArray?
{
HttpRequestLine requestLine = new(
httpMethod,
url,
hash ?? string.Empty,
queryString ?? string.Empty,
new HttpProtocolVersion("1", "1"),
segments ?? [],
parameters ?? []
);
return requestLine;
}
else
{
logger.LogDebug("HTTP version not supported");
return null;
}
}
else
{
return null;
}
}
else
{
logger.LogDebug("1st SP not found");
return null;
}
}
public virtual HttpHeaderLine? ParseHeaderLine(ReadOnlySequence<byte> buffer)
{
SequencePosition? pos = buffer.PositionOf((byte)':'); // there are 2 SPs in a request line
if (pos.HasValue)
{
var headerNameBuffer = buffer.Slice(0, pos.Value);
var headerValue = string.Empty;
buffer = buffer.Slice(buffer.GetPosition(1, pos.Value));
if (!buffer.IsEmpty)
{
if (buffer.FirstSpan[0] == ' ') // since buffer is not empty, we always have at least 1 byte
{
buffer = buffer.Slice(buffer.GetPosition(1)); // take the part after ' '
}
}
if (!buffer.IsEmpty)
{
headerValue = Encoding.ASCII.GetString(buffer).Replace("\r", ""); // todo: find a way to replace before GetString()
}
return new HttpHeaderLine(Encoding.ASCII.GetString(headerNameBuffer), headerValue);
}
return null;
}
private static bool TryParseUrl(ReadOnlySequence<byte> readOnlySequence, out string? url, out string? hash, out string? queryString, out string[]? segments, out HttpParameters? parameters)
{
var s = "https://f" + Encoding.ASCII.GetString(readOnlySequence); // hack: add a faked scheme and host to make it an absolute uri
var uri = new Uri(s);
if (uri.AbsolutePath.Contains(".."))
{
url = null;
hash = null;
queryString = null;
segments = null;
parameters = null;
return false;
}
url = uri.AbsolutePath;
hash = uri.Fragment;
queryString = Uri.UnescapeDataString(uri.Query);
segments = uri.Segments;
if (TryParseParameters(queryString, out HttpParameters? ps) && ps != null)
{
parameters = ps;
}
else
{
parameters = null;
return false;
}
return true;
}
private static bool TryParseParameters(string queryString, out HttpParameters? parameters)
{
ArgumentNullException.ThrowIfNull(queryString);
var query = queryString.AsMemory();
if (!query.IsEmpty && query.Span[0] == '?') // remove first ?
query = query[1..];
parameters = [];
while (!query.IsEmpty)
{
var ampIdx = query.Span.IndexOf('&');
ReadOnlyMemory<char> segment;
if (ampIdx == -1)
{
// this is the only parameter left
segment = query;
query = default;
}
else
{
segment = query[..ampIdx];
query = query[(ampIdx + 1)..];
}
var equalIdx = segment.Span.IndexOf('=');
if (equalIdx == -1)
{
if (!segment.IsEmpty)
{
parameters.Add(new HttpParameter(new string(segment.Span), string.Empty));
}
}
else
{
var name = segment[..equalIdx];
var value = segment[(equalIdx + 1)..];
parameters.Add(new HttpParameter(new string(name.Span), new string(value.Span)));
}
}
return true;
}
}
}