-
Notifications
You must be signed in to change notification settings - Fork 10.1k
/
HttpCharacters.cs
224 lines (195 loc) · 5.91 KB
/
HttpCharacters.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.CompilerServices;
namespace Microsoft.AspNetCore.Http;
internal static class HttpCharacters
{
private const int _tableSize = 128;
private static readonly bool[] _alphaNumeric = InitializeAlphaNumeric();
private static readonly bool[] _authority = InitializeAuthority();
private static readonly bool[] _token = InitializeToken();
private static readonly bool[] _host = InitializeHost();
private static readonly bool[] _fieldValue = InitializeFieldValue();
internal static void Initialize()
{
// Access _alphaNumeric to initialize static fields
var _ = _alphaNumeric;
}
private static bool[] InitializeAlphaNumeric()
{
// ALPHA and DIGIT https://tools.ietf.org/html/rfc5234#appendix-B.1
var alphaNumeric = new bool[_tableSize];
for (var c = '0'; c <= '9'; c++)
{
alphaNumeric[c] = true;
}
for (var c = 'A'; c <= 'Z'; c++)
{
alphaNumeric[c] = true;
}
for (var c = 'a'; c <= 'z'; c++)
{
alphaNumeric[c] = true;
}
return alphaNumeric;
}
private static bool[] InitializeAuthority()
{
// Authority https://tools.ietf.org/html/rfc3986#section-3.2
// Examples:
// microsoft.com
// hostname:8080
// [::]:8080
// [fe80::]
// 127.0.0.1
// user@host.com
// user:password@host.com
var authority = new bool[_tableSize];
Array.Copy(_alphaNumeric, authority, _tableSize);
authority[':'] = true;
authority['.'] = true;
authority['-'] = true;
authority['['] = true;
authority[']'] = true;
authority['@'] = true;
return authority;
}
private static bool[] InitializeToken()
{
// tchar https://tools.ietf.org/html/rfc7230#appendix-B
var token = new bool[_tableSize];
Array.Copy(_alphaNumeric, token, _tableSize);
token['!'] = true;
token['#'] = true;
token['$'] = true;
token['%'] = true;
token['&'] = true;
token['\''] = true;
token['*'] = true;
token['+'] = true;
token['-'] = true;
token['.'] = true;
token['^'] = true;
token['_'] = true;
token['`'] = true;
token['|'] = true;
token['~'] = true;
return token;
}
private static bool[] InitializeHost()
{
// Matches Http.Sys
// Matches RFC 3986 except "*" / "+" / "," / ";" / "=" and "%" HEXDIG HEXDIG which are not allowed by Http.Sys
var host = new bool[_tableSize];
Array.Copy(_alphaNumeric, host, _tableSize);
host['!'] = true;
host['$'] = true;
host['&'] = true;
host['\''] = true;
host['('] = true;
host[')'] = true;
host['-'] = true;
host['.'] = true;
host['_'] = true;
host['~'] = true;
return host;
}
private static bool[] InitializeFieldValue()
{
// field-value https://tools.ietf.org/html/rfc7230#section-3.2
var fieldValue = new bool[_tableSize];
fieldValue[0x9] = true; // HTAB
for (var c = 0x20; c <= 0x7e; c++) // VCHAR and SP
{
fieldValue[c] = true;
}
return fieldValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsInvalidAuthorityChar(Span<byte> s)
{
var authority = _authority;
for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (c >= (uint)authority.Length || !authority[c])
{
return true;
}
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidHostChar(string s)
{
var host = _host;
for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (c >= (uint)host.Length || !host[c])
{
return i;
}
}
return -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidTokenChar(string s)
{
var token = _token;
for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (c >= (uint)token.Length || !token[c])
{
return i;
}
}
return -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidTokenChar(ReadOnlySpan<byte> span)
{
var token = _token;
for (var i = 0; i < span.Length; i++)
{
var c = span[i];
if (c >= (uint)token.Length || !token[c])
{
return i;
}
}
return -1;
}
// Follows field-value rules in https://tools.ietf.org/html/rfc7230#section-3.2
// Disallows characters > 0x7E.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidFieldValueChar(string s)
{
var fieldValue = _fieldValue;
for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (c >= (uint)fieldValue.Length || !fieldValue[c])
{
return i;
}
}
return -1;
}
// Follows field-value rules for chars <= 0x7F. Allows extended characters > 0x7F.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidFieldValueCharExtended(string s)
{
var fieldValue = _fieldValue;
for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (c < (uint)fieldValue.Length && !fieldValue[c])
{
return i;
}
}
return -1;
}
}