Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HttpRuleParser.cs
Go to the documentation of this file.
1using System.Text;
2
3namespace System.Net.Http;
4
5internal static class HttpRuleParser
6{
7 private static readonly bool[] s_tokenChars = CreateTokenChars();
8
10
11 private static bool[] CreateTokenChars()
12 {
13 bool[] array = new bool[128];
14 for (int i = 33; i < 127; i++)
15 {
16 array[i] = true;
17 }
18 array[40] = false;
19 array[41] = false;
20 array[60] = false;
21 array[62] = false;
22 array[64] = false;
23 array[44] = false;
24 array[59] = false;
25 array[58] = false;
26 array[92] = false;
27 array[34] = false;
28 array[47] = false;
29 array[91] = false;
30 array[93] = false;
31 array[63] = false;
32 array[61] = false;
33 array[123] = false;
34 array[125] = false;
35 return array;
36 }
37
38 internal static bool IsTokenChar(char character)
39 {
40 if (character > '\u007f')
41 {
42 return false;
43 }
44 return s_tokenChars[(uint)character];
45 }
46
47 internal static int GetTokenLength(string input, int startIndex)
48 {
49 if (startIndex >= input.Length)
50 {
51 return 0;
52 }
53 for (int i = startIndex; i < input.Length; i++)
54 {
55 if (!IsTokenChar(input[i]))
56 {
57 return i - startIndex;
58 }
59 }
60 return input.Length - startIndex;
61 }
62
63 internal static bool IsToken(string input)
64 {
65 for (int i = 0; i < input.Length; i++)
66 {
67 if (!IsTokenChar(input[i]))
68 {
69 return false;
70 }
71 }
72 return true;
73 }
74
75 internal static bool IsToken(ReadOnlySpan<byte> input)
76 {
77 for (int i = 0; i < input.Length; i++)
78 {
79 if (!IsTokenChar((char)input[i]))
80 {
81 return false;
82 }
83 }
84 return true;
85 }
86
87 internal static string GetTokenString(ReadOnlySpan<byte> input)
88 {
89 return Encoding.ASCII.GetString(input);
90 }
91
92 internal static int GetWhitespaceLength(string input, int startIndex)
93 {
94 if (startIndex >= input.Length)
95 {
96 return 0;
97 }
98 for (int i = startIndex; i < input.Length; i++)
99 {
100 char c = input[i];
101 if (c != ' ' && c != '\t')
102 {
103 return i - startIndex;
104 }
105 }
106 return input.Length - startIndex;
107 }
108
109 internal static bool ContainsNewLine(string value, int startIndex = 0)
110 {
111 return value.AsSpan(startIndex).IndexOfAny('\r', '\n') != -1;
112 }
113
114 internal static int GetNumberLength(string input, int startIndex, bool allowDecimal)
115 {
116 int num = startIndex;
117 bool flag = !allowDecimal;
118 if (input[num] == '.')
119 {
120 return 0;
121 }
122 while (num < input.Length)
123 {
124 char c = input[num];
125 if (c >= '0' && c <= '9')
126 {
127 num++;
128 continue;
129 }
130 if (flag || c != '.')
131 {
132 break;
133 }
134 flag = true;
135 num++;
136 }
137 return num - startIndex;
138 }
139
140 internal static int GetHostLength(string input, int startIndex, bool allowToken, out string host)
141 {
142 host = null;
143 if (startIndex >= input.Length)
144 {
145 return 0;
146 }
147 int i = startIndex;
148 bool flag;
149 bool num;
150 for (flag = true; i < input.Length; flag = num, i++)
151 {
152 char c = input[i];
153 switch (c)
154 {
155 case '/':
156 return 0;
157 default:
158 num = flag && IsTokenChar(c);
159 continue;
160 case '\t':
161 case '\r':
162 case ' ':
163 case ',':
164 break;
165 }
166 break;
167 }
168 int num2 = i - startIndex;
169 if (num2 == 0)
170 {
171 return 0;
172 }
173 string text = input.Substring(startIndex, num2);
174 if ((!allowToken || !flag) && !IsValidHostName(text))
175 {
176 return 0;
177 }
178 host = text;
179 return num2;
180 }
181
182 internal static HttpParseResult GetCommentLength(string input, int startIndex, out int length)
183 {
184 return GetExpressionLength(input, startIndex, '(', ')', supportsNesting: true, 1, out length);
185 }
186
187 internal static HttpParseResult GetQuotedStringLength(string input, int startIndex, out int length)
188 {
189 return GetExpressionLength(input, startIndex, '"', '"', supportsNesting: false, 1, out length);
190 }
191
192 internal static HttpParseResult GetQuotedPairLength(string input, int startIndex, out int length)
193 {
194 length = 0;
195 if (input[startIndex] != '\\')
196 {
197 return HttpParseResult.NotParsed;
198 }
199 if (startIndex + 2 > input.Length || input[startIndex + 1] > '\u007f')
200 {
201 return HttpParseResult.InvalidFormat;
202 }
203 length = 2;
204 return HttpParseResult.Parsed;
205 }
206
207 private static HttpParseResult GetExpressionLength(string input, int startIndex, char openChar, char closeChar, bool supportsNesting, int nestedCount, out int length)
208 {
209 length = 0;
210 if (input[startIndex] != openChar)
211 {
212 return HttpParseResult.NotParsed;
213 }
214 int num = startIndex + 1;
215 while (num < input.Length)
216 {
217 int length2 = 0;
218 if (num + 2 < input.Length && GetQuotedPairLength(input, num, out length2) == HttpParseResult.Parsed)
219 {
220 num += length2;
221 continue;
222 }
223 char c = input[num];
224 if (c == '\r' || c == '\n')
225 {
226 return HttpParseResult.InvalidFormat;
227 }
228 if (supportsNesting && c == openChar)
229 {
230 if (nestedCount > 5)
231 {
232 return HttpParseResult.InvalidFormat;
233 }
234 int length3 = 0;
235 switch (GetExpressionLength(input, num, openChar, closeChar, supportsNesting, nestedCount + 1, out length3))
236 {
237 case HttpParseResult.Parsed:
238 num += length3;
239 break;
240 case HttpParseResult.InvalidFormat:
241 return HttpParseResult.InvalidFormat;
242 }
243 }
244 else
245 {
246 if (input[num] == closeChar)
247 {
248 length = num - startIndex + 1;
249 return HttpParseResult.Parsed;
250 }
251 num++;
252 }
253 }
254 return HttpParseResult.InvalidFormat;
255 }
256
257 private static bool IsValidHostName(string host)
258 {
259 Uri result;
260 return Uri.TryCreate("http://u@" + host + "/", UriKind.Absolute, out result);
261 }
262}
static string GetTokenString(ReadOnlySpan< byte > input)
static HttpParseResult GetCommentLength(string input, int startIndex, out int length)
static HttpParseResult GetExpressionLength(string input, int startIndex, char openChar, char closeChar, bool supportsNesting, int nestedCount, out int length)
static bool ContainsNewLine(string value, int startIndex=0)
static int GetHostLength(string input, int startIndex, bool allowToken, out string host)
static HttpParseResult GetQuotedPairLength(string input, int startIndex, out int length)
static HttpParseResult GetQuotedStringLength(string input, int startIndex, out int length)
static bool IsToken(ReadOnlySpan< byte > input)
static bool IsValidHostName(string host)
static int GetTokenLength(string input, int startIndex)
static bool IsToken(string input)
static Encoding DefaultHttpEncoding
static int GetWhitespaceLength(string input, int startIndex)
static int GetNumberLength(string input, int startIndex, bool allowDecimal)
static readonly bool[] s_tokenChars
static bool IsTokenChar(char character)
static Encoding Latin1
Definition Encoding.cs:513
static Encoding ASCII
Definition Encoding.cs:511
static bool TryCreate([NotNullWhen(true)] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result)
Definition Uri.cs:3793
UriKind
Definition UriKind.cs:4