Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HttpListenerRequestUriBuilder.cs
Go to the documentation of this file.
3using System.Text;
4
5namespace System.Net;
6
7internal sealed class HttpListenerRequestUriBuilder
8{
9 private enum ParsingResult
10 {
11 Success,
14 }
15
16 private enum EncodingType
17 {
18 Primary,
20 }
21
23
25
26 private readonly string _rawUri;
27
28 private readonly string _cookedUriScheme;
29
30 private readonly string _cookedUriHost;
31
32 private readonly string _cookedUriPath;
33
34 private readonly string _cookedUriQuery;
35
37
39
40 private string _rawPath;
41
43
52
58
59 private Uri Build()
60 {
62 if (_requestUri == null)
63 {
65 }
66 return _requestUri;
67 }
68
76
78 {
79 bool flag = false;
82 if (parsingResult == ParsingResult.EncodingError)
83 {
84 Encoding encoding = GetEncoding(EncodingType.Secondary);
86 }
87 if (parsingResult != 0 && 0 == 0 && System.Net.NetEventSource.Log.IsEnabled())
88 {
90 }
91 }
92
94 {
95 if (type == EncodingType.Secondary)
96 {
97 return s_ansiEncoding;
98 }
99 return s_utf8Encoding;
100 }
101
124
126 {
127 int num = 0;
128 char c = '\0';
129 while (num < _rawPath.Length)
130 {
131 c = _rawPath[num];
132 if (c == '%')
133 {
134 num++;
135 c = _rawPath[num];
136 if (c == 'u' || c == 'U')
137 {
139 {
140 return ParsingResult.EncodingError;
141 }
142 if (!AppendUnicodeCodePointValuePercentEncoded(_rawPath.Substring(num + 1, 4)))
143 {
144 return ParsingResult.InvalidString;
145 }
146 num += 5;
147 }
148 else
149 {
150 if (!AddPercentEncodedOctetToRawOctetsList(encoding, _rawPath.Substring(num, 2)))
151 {
152 return ParsingResult.InvalidString;
153 }
154 num += 2;
155 }
156 }
157 else
158 {
160 {
161 return ParsingResult.EncodingError;
162 }
164 num++;
165 }
166 }
168 {
169 return ParsingResult.EncodingError;
170 }
171 return ParsingResult.Success;
172 }
173
175 {
176 if (!int.TryParse(codePoint, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result))
177 {
178 if (System.Net.NetEventSource.Log.IsEnabled())
179 {
180 System.Net.NetEventSource.Error(this, System.SR.Format(System.SR.net_log_listener_cant_convert_percent_value, codePoint), "AppendUnicodeCodePointValuePercentEncoded");
181 }
182 return false;
183 }
184 string text = null;
185 try
186 {
187 text = char.ConvertFromUtf32(result);
189 return true;
190 }
192 {
193 if (System.Net.NetEventSource.Log.IsEnabled())
194 {
195 System.Net.NetEventSource.Error(this, System.SR.Format(System.SR.net_log_listener_cant_convert_percent_value, codePoint), "AppendUnicodeCodePointValuePercentEncoded");
196 }
197 }
199 {
200 if (System.Net.NetEventSource.Log.IsEnabled())
201 {
202 System.Net.NetEventSource.Error(this, System.SR.Format(System.SR.net_log_listener_cant_convert_to_utf8, text, ex2.Message), "AppendUnicodeCodePointValuePercentEncoded");
203 }
204 }
205 return false;
206 }
207
209 {
210 if (!byte.TryParse(escapedCharacter, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result))
211 {
212 if (System.Net.NetEventSource.Log.IsEnabled())
213 {
215 }
216 return false;
217 }
218 _rawOctets.Add(result);
219 return true;
220 }
221
223 {
224 if (_rawOctets.Count == 0)
225 {
226 return true;
227 }
228 string text = null;
229 try
230 {
231 text = encoding.GetString(_rawOctets.ToArray());
232 if (encoding == s_utf8Encoding)
233 {
235 }
236 else
237 {
239 }
241 return true;
242 }
244 {
245 if (System.Net.NetEventSource.Log.IsEnabled())
246 {
248 }
249 }
251 {
252 if (System.Net.NetEventSource.Log.IsEnabled())
253 {
254 System.Net.NetEventSource.Error(this, System.SR.Format(System.SR.net_log_listener_cant_convert_to_utf8, text, ex2.Message), "EmptyDecodeAndAppendRawOctetsList");
255 }
256 }
257 return false;
258 }
259
261 {
262 foreach (byte octet in octets)
263 {
265 handler.AppendLiteral("%");
266 handler.AppendFormatted(octet, "X2");
267 target.Append(ref handler);
268 }
269 }
270
272 {
274 bool flag = true;
275 foreach (byte octet in octets)
276 {
277 if (flag)
278 {
279 flag = false;
280 }
281 else
282 {
283 stringBuilder.Append(' ');
284 }
287 handler.AppendFormatted(octet, "X2");
288 stringBuilder2.Append(ref handler);
289 }
290 return stringBuilder.ToString();
291 }
292
293 private static string GetPath(string uriString)
294 {
295 int num = 0;
296 if (uriString[0] != '/')
297 {
298 int num2 = 0;
299 if (uriString.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
300 {
301 num2 = 7;
302 }
303 else if (uriString.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
304 {
305 num2 = 8;
306 }
307 if (num2 > 0)
308 {
309 num = uriString.IndexOf('/', num2);
310 if (num == -1)
311 {
312 num = uriString.Length;
313 }
314 }
315 else
316 {
317 uriString = "/" + uriString;
318 }
319 }
320 int num3 = uriString.IndexOf('?');
321 if (num3 == -1)
322 {
323 num3 = uriString.Length;
324 }
325 return AddSlashToAsteriskOnlyPath(uriString.Substring(num, num3 - num));
326 }
327
328 private static string AddSlashToAsteriskOnlyPath(string path)
329 {
330 if (path.Length == 1 && path[0] == '*')
331 {
332 return "/*";
333 }
334 return path;
335 }
336}
static CultureInfo InvariantCulture
static void AppendOctetsPercentEncoded(StringBuilder target, IEnumerable< byte > octets)
static Uri GetRequestUri(string rawUri, string cookedUriScheme, string cookedUriHost, string cookedUriPath, string cookedUriQuery)
static string GetOctetsAsString(IEnumerable< byte > octets)
bool AddPercentEncodedOctetToRawOctetsList(Encoding encoding, string escapedCharacter)
ParsingResult BuildRequestUriUsingRawPath(Encoding encoding)
HttpListenerRequestUriBuilder(string rawUri, string cookedUriScheme, string cookedUriHost, string cookedUriPath, string cookedUriQuery)
static readonly System.Net.NetEventSource Log
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static string net_log_listener_cant_convert_to_utf8
Definition SR.cs:96
static string net_log_listener_cant_convert_percent_value
Definition SR.cs:94
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_log_listener_cant_create_uri
Definition SR.cs:90
static string net_log_listener_cant_convert_raw_path
Definition SR.cs:92
static string net_log_listener_cant_convert_bytes
Definition SR.cs:98
Definition SR.cs:7
static Encoding GetEncoding(int codepage)
Definition Encoding.cs:593
virtual string EncodingName
Definition Encoding.cs:362
unsafe string GetString(byte *bytes, int byteCount)
Definition Encoding.cs:973
override string ToString()
StringBuilder Append(char value, int repeatCount)
static bool TryCreate([NotNullWhen(true)] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result)
Definition Uri.cs:3793
static readonly string SchemeDelimiter
Definition Uri.cs:187
UriKind
Definition UriKind.cs:4