| 1 | // Licensed to the .NET Foundation under one or more agreements.
|
|---|
| 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
|---|
| 3 |
|
|---|
| 4 | // ReSharper disable RedundantUsingDirective
|
|---|
| 5 | // ReSharper disable RedundantUnsafeContext
|
|---|
| 6 | // ReSharper disable RedundantToStringCall
|
|---|
| 7 | // ReSharper disable ConditionIsAlwaysTrueOrFalse
|
|---|
| 8 |
|
|---|
| 9 | using System;
|
|---|
| 10 | using System.Collections.Generic;
|
|---|
| 11 | using System.Collections.Specialized;
|
|---|
| 12 | using System.Diagnostics;
|
|---|
| 13 | using System.Globalization;
|
|---|
| 14 | using System.Net;
|
|---|
| 15 | using System.Net.WebSockets;
|
|---|
| 16 | using System.Reflection;
|
|---|
| 17 | using System.Security.Cryptography.X509Certificates;
|
|---|
| 18 | using System.Text;
|
|---|
| 19 | using System.Threading.Tasks;
|
|---|
| 20 | using SpaceWizards.HttpListener.WebSockets;
|
|---|
| 21 |
|
|---|
| 22 | namespace SpaceWizards.HttpListener
|
|---|
| 23 | {
|
|---|
| 24 | public sealed unsafe partial class HttpListenerRequest
|
|---|
| 25 | {
|
|---|
| 26 | private CookieCollection? _cookies;
|
|---|
| 27 | private bool? _keepAlive;
|
|---|
| 28 | private string? _rawUrl;
|
|---|
| 29 | private Uri? _requestUri;
|
|---|
| 30 | private Version _version;
|
|---|
| 31 |
|
|---|
| 32 | public string[]? AcceptTypes => Helpers.ParseMultivalueHeader(Headers[HttpKnownHeaderNames.Accept]!);
|
|---|
| 33 |
|
|---|
| 34 | public string[]? UserLanguages => Helpers.ParseMultivalueHeader(Headers[HttpKnownHeaderNames.AcceptLanguage]!);
|
|---|
| 35 |
|
|---|
| 36 | private CookieCollection ParseCookies(Uri? uri, string setCookieHeader)
|
|---|
| 37 | {
|
|---|
| 38 | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, "uri:" + uri + " setCookieHeader:" + setCookieHeader);
|
|---|
| 39 | CookieCollection cookies = new CookieCollection();
|
|---|
| 40 | CookieParser parser = new CookieParser(setCookieHeader);
|
|---|
| 41 | while (true)
|
|---|
| 42 | {
|
|---|
| 43 | Cookie? cookie = parser.GetServer();
|
|---|
| 44 | if (cookie == null)
|
|---|
| 45 | {
|
|---|
| 46 | // EOF, done.
|
|---|
| 47 | break;
|
|---|
| 48 | }
|
|---|
| 49 | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, "CookieParser returned cookie: " + cookie.ToString());
|
|---|
| 50 | if (cookie.Name.Length == 0)
|
|---|
| 51 | {
|
|---|
| 52 | continue;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | cookies.InternalAdd(cookie, true);
|
|---|
| 56 | }
|
|---|
| 57 | return cookies;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public CookieCollection Cookies
|
|---|
| 61 | {
|
|---|
| 62 | get
|
|---|
| 63 | {
|
|---|
| 64 | if (_cookies == null)
|
|---|
| 65 | {
|
|---|
| 66 | string? cookieString = Headers[HttpKnownHeaderNames.Cookie];
|
|---|
| 67 | if (!string.IsNullOrEmpty(cookieString))
|
|---|
| 68 | {
|
|---|
| 69 | _cookies = ParseCookies(RequestUri, cookieString);
|
|---|
| 70 | }
|
|---|
| 71 | if (_cookies == null)
|
|---|
| 72 | {
|
|---|
| 73 | _cookies = new CookieCollection();
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | return _cookies;
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | // TFP: Added to manually set the encoding used for the query parameters
|
|---|
| 81 | private Encoding contentEncoding;
|
|---|
| 82 | public Encoding ContentEncoding
|
|---|
| 83 | {
|
|---|
| 84 | get
|
|---|
| 85 | {
|
|---|
| 86 | if (contentEncoding != null) {
|
|---|
| 87 | return contentEncoding;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | if (UserAgent != null && CultureInfo.InvariantCulture.CompareInfo.IsPrefix(UserAgent, "UP"))
|
|---|
| 91 | {
|
|---|
| 92 | string? postDataCharset = Headers["x-up-devcap-post-charset"];
|
|---|
| 93 | if (postDataCharset != null && postDataCharset.Length > 0)
|
|---|
| 94 | {
|
|---|
| 95 | try
|
|---|
| 96 | {
|
|---|
| 97 | contentEncoding = Encoding.GetEncoding(postDataCharset);
|
|---|
| 98 | return contentEncoding;
|
|---|
| 99 | }
|
|---|
| 100 | catch (ArgumentException)
|
|---|
| 101 | {
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | if (HasEntityBody)
|
|---|
| 106 | {
|
|---|
| 107 | if (ContentType != null)
|
|---|
| 108 | {
|
|---|
| 109 | string? charSet = Helpers.GetCharSetValueFromHeader(ContentType);
|
|---|
| 110 | if (charSet != null)
|
|---|
| 111 | {
|
|---|
| 112 | try
|
|---|
| 113 | {
|
|---|
| 114 | contentEncoding = Encoding.GetEncoding(charSet);
|
|---|
| 115 | return contentEncoding;
|
|---|
| 116 | }
|
|---|
| 117 | catch (ArgumentException)
|
|---|
| 118 | {
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 | contentEncoding = Encoding.Default;
|
|---|
| 124 | return contentEncoding;
|
|---|
| 125 | }
|
|---|
| 126 | set
|
|---|
| 127 | {
|
|---|
| 128 | contentEncoding = value;
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | public string? ContentType => Headers[HttpKnownHeaderNames.ContentType];
|
|---|
| 133 |
|
|---|
| 134 | public bool IsLocal => LocalEndPoint!.Address.Equals(RemoteEndPoint!.Address);
|
|---|
| 135 |
|
|---|
| 136 | public bool IsWebSocketRequest
|
|---|
| 137 | {
|
|---|
| 138 | get
|
|---|
| 139 | {
|
|---|
| 140 | if (!SupportsWebSockets)
|
|---|
| 141 | {
|
|---|
| 142 | return false;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | bool foundConnectionUpgradeHeader = false;
|
|---|
| 146 | if (string.IsNullOrEmpty(Headers[HttpKnownHeaderNames.Connection]) || string.IsNullOrEmpty(Headers[HttpKnownHeaderNames.Upgrade]))
|
|---|
| 147 | {
|
|---|
| 148 | return false;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | foreach (string connection in Headers.GetValues(HttpKnownHeaderNames.Connection)!)
|
|---|
| 152 | {
|
|---|
| 153 | if (string.Equals(connection, HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase))
|
|---|
| 154 | {
|
|---|
| 155 | foundConnectionUpgradeHeader = true;
|
|---|
| 156 | break;
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | if (!foundConnectionUpgradeHeader)
|
|---|
| 161 | {
|
|---|
| 162 | return false;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | foreach (string upgrade in Headers.GetValues(HttpKnownHeaderNames.Upgrade)!)
|
|---|
| 166 | {
|
|---|
| 167 | if (string.Equals(upgrade, HttpWebSocket.WebSocketUpgradeToken, StringComparison.OrdinalIgnoreCase))
|
|---|
| 168 | {
|
|---|
| 169 | return true;
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | return false;
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | public bool KeepAlive
|
|---|
| 178 | {
|
|---|
| 179 | get
|
|---|
| 180 | {
|
|---|
| 181 | if (!_keepAlive.HasValue)
|
|---|
| 182 | {
|
|---|
| 183 | string? header = Headers[HttpKnownHeaderNames.ProxyConnection];
|
|---|
| 184 | if (string.IsNullOrEmpty(header))
|
|---|
| 185 | {
|
|---|
| 186 | header = Headers[HttpKnownHeaderNames.Connection];
|
|---|
| 187 | }
|
|---|
| 188 | if (string.IsNullOrEmpty(header))
|
|---|
| 189 | {
|
|---|
| 190 | if (ProtocolVersion >= HttpVersion.Version11)
|
|---|
| 191 | {
|
|---|
| 192 | _keepAlive = true;
|
|---|
| 193 | }
|
|---|
| 194 | else
|
|---|
| 195 | {
|
|---|
| 196 | header = Headers[HttpKnownHeaderNames.KeepAlive];
|
|---|
| 197 | _keepAlive = !string.IsNullOrEmpty(header);
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | else
|
|---|
| 201 | {
|
|---|
| 202 | header = header.ToLowerInvariant();
|
|---|
| 203 | _keepAlive =
|
|---|
| 204 | header.IndexOf("close", StringComparison.OrdinalIgnoreCase) < 0 ||
|
|---|
| 205 | #if UNITY_NETFRAMEWORK
|
|---|
| 206 | header.IndexOf("keep-alive", StringComparison.OrdinalIgnoreCase) >= 0;
|
|---|
| 207 | #else
|
|---|
| 208 | header.Contains("keep-alive", StringComparison.OrdinalIgnoreCase);
|
|---|
| 209 | #endif
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, "_keepAlive=" + _keepAlive);
|
|---|
| 214 | return _keepAlive.Value;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | public NameValueCollection QueryString
|
|---|
| 219 | {
|
|---|
| 220 | get
|
|---|
| 221 | {
|
|---|
| 222 | NameValueCollection queryString = new NameValueCollection();
|
|---|
| 223 | Helpers.FillFromString(queryString, Url!.Query, true, ContentEncoding);
|
|---|
| 224 | return queryString;
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | public string? RawUrl => _rawUrl;
|
|---|
| 229 |
|
|---|
| 230 | private string RequestScheme => IsSecureConnection ? UriScheme.Https : UriScheme.Http;
|
|---|
| 231 |
|
|---|
| 232 | public string UserAgent => Headers[HttpKnownHeaderNames.UserAgent]!;
|
|---|
| 233 |
|
|---|
| 234 | public string UserHostAddress => LocalEndPoint!.ToString();
|
|---|
| 235 |
|
|---|
| 236 | public string UserHostName => Headers[HttpKnownHeaderNames.Host]!;
|
|---|
| 237 |
|
|---|
| 238 | public Uri? UrlReferrer
|
|---|
| 239 | {
|
|---|
| 240 | get
|
|---|
| 241 | {
|
|---|
| 242 | string? referrer = Headers[HttpKnownHeaderNames.Referer];
|
|---|
| 243 | if (referrer == null)
|
|---|
| 244 | {
|
|---|
| 245 | return null;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | bool success = Uri.TryCreate(referrer, UriKind.RelativeOrAbsolute, out Uri? urlReferrer);
|
|---|
| 249 | return success ? urlReferrer : null;
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | public Uri? Url => RequestUri;
|
|---|
| 254 |
|
|---|
| 255 | public Version ProtocolVersion => _version;
|
|---|
| 256 |
|
|---|
| 257 | public X509Certificate2? GetClientCertificate()
|
|---|
| 258 | {
|
|---|
| 259 | if (ClientCertState == ListenerClientCertState.InProgress)
|
|---|
| 260 | throw new InvalidOperationException(SR.Format(SR.net_listener_callinprogress, $"{nameof(GetClientCertificate)}()/{nameof(BeginGetClientCertificate)}()"));
|
|---|
| 261 | ClientCertState = ListenerClientCertState.InProgress;
|
|---|
| 262 |
|
|---|
| 263 | GetClientCertificateCore();
|
|---|
| 264 |
|
|---|
| 265 | ClientCertState = ListenerClientCertState.Completed;
|
|---|
| 266 | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"_clientCertificate:{ClientCertificate}");
|
|---|
| 267 |
|
|---|
| 268 | return ClientCertificate;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | public IAsyncResult BeginGetClientCertificate(AsyncCallback? requestCallback, object? state)
|
|---|
| 272 | {
|
|---|
| 273 | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this);
|
|---|
| 274 | if (ClientCertState == ListenerClientCertState.InProgress)
|
|---|
| 275 | throw new InvalidOperationException(SR.Format(SR.net_listener_callinprogress, $"{nameof(GetClientCertificate)}()/{nameof(BeginGetClientCertificate)}()"));
|
|---|
| 276 | ClientCertState = ListenerClientCertState.InProgress;
|
|---|
| 277 |
|
|---|
| 278 | return BeginGetClientCertificateCore(requestCallback, state);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | public Task<X509Certificate2?> GetClientCertificateAsync()
|
|---|
| 282 | {
|
|---|
| 283 | return Task.Factory.FromAsync(
|
|---|
| 284 | (callback, state) => ((HttpListenerRequest)state!).BeginGetClientCertificate(callback, state),
|
|---|
| 285 | iar => ((HttpListenerRequest)iar.AsyncState!).EndGetClientCertificate(iar),
|
|---|
| 286 | this);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | internal ListenerClientCertState ClientCertState { get; set; } = ListenerClientCertState.NotInitialized;
|
|---|
| 290 | internal X509Certificate2? ClientCertificate { get; set; }
|
|---|
| 291 |
|
|---|
| 292 | public int ClientCertificateError
|
|---|
| 293 | {
|
|---|
| 294 | get
|
|---|
| 295 | {
|
|---|
| 296 | if (ClientCertState == ListenerClientCertState.NotInitialized)
|
|---|
| 297 | throw new InvalidOperationException(SR.Format(SR.net_listener_mustcall, "GetClientCertificate()/BeginGetClientCertificate()"));
|
|---|
| 298 | else if (ClientCertState == ListenerClientCertState.InProgress)
|
|---|
| 299 | throw new InvalidOperationException(SR.Format(SR.net_listener_mustcompletecall, "GetClientCertificate()/BeginGetClientCertificate()"));
|
|---|
| 300 |
|
|---|
| 301 | return GetClientCertificateErrorCore();
|
|---|
| 302 | }
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | private static class Helpers
|
|---|
| 306 | {
|
|---|
| 307 | //
|
|---|
| 308 | // Get attribute off header value
|
|---|
| 309 | //
|
|---|
| 310 | internal static string? GetCharSetValueFromHeader(string headerValue)
|
|---|
| 311 | {
|
|---|
| 312 | const string AttrName = "charset";
|
|---|
| 313 |
|
|---|
| 314 | if (headerValue == null)
|
|---|
| 315 | return null;
|
|---|
| 316 |
|
|---|
| 317 | int l = headerValue.Length;
|
|---|
| 318 | int k = AttrName.Length;
|
|---|
| 319 |
|
|---|
| 320 | // find properly separated attribute name
|
|---|
| 321 | int i = 1; // start searching from 1
|
|---|
| 322 |
|
|---|
| 323 | while (i < l)
|
|---|
| 324 | {
|
|---|
| 325 | i = CultureInfo.InvariantCulture.CompareInfo.IndexOf(headerValue, AttrName, i, CompareOptions.IgnoreCase);
|
|---|
| 326 | if (i < 0)
|
|---|
| 327 | break;
|
|---|
| 328 | if (i + k >= l)
|
|---|
| 329 | break;
|
|---|
| 330 |
|
|---|
| 331 | char chPrev = headerValue[i - 1];
|
|---|
| 332 | char chNext = headerValue[i + k];
|
|---|
| 333 | if ((chPrev == ';' || chPrev == ',' || char.IsWhiteSpace(chPrev)) && (chNext == '=' || char.IsWhiteSpace(chNext)))
|
|---|
| 334 | break;
|
|---|
| 335 |
|
|---|
| 336 | i += k;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | if (i < 0 || i >= l)
|
|---|
| 340 | return null;
|
|---|
| 341 |
|
|---|
| 342 | // skip to '=' and the following whitespace
|
|---|
| 343 | i += k;
|
|---|
| 344 | while (i < l && char.IsWhiteSpace(headerValue[i]))
|
|---|
| 345 | i++;
|
|---|
| 346 | if (i >= l || headerValue[i] != '=')
|
|---|
| 347 | return null;
|
|---|
| 348 | i++;
|
|---|
| 349 | while (i < l && char.IsWhiteSpace(headerValue[i]))
|
|---|
| 350 | i++;
|
|---|
| 351 | if (i >= l)
|
|---|
| 352 | return null;
|
|---|
| 353 |
|
|---|
| 354 | // parse the value
|
|---|
| 355 | string? attrValue = null;
|
|---|
| 356 |
|
|---|
| 357 | int j;
|
|---|
| 358 |
|
|---|
| 359 | if (i < l && headerValue[i] == '"')
|
|---|
| 360 | {
|
|---|
| 361 | if (i == l - 1)
|
|---|
| 362 | return null;
|
|---|
| 363 | j = headerValue.IndexOf('"', i + 1);
|
|---|
| 364 | if (j < 0 || j == i + 1)
|
|---|
| 365 | return null;
|
|---|
| 366 |
|
|---|
| 367 | #if UNITY_NETFRAMEWORK
|
|---|
| 368 | attrValue = headerValue.Substring(i + 1, j - i - 1).Trim();
|
|---|
| 369 | #else
|
|---|
| 370 | attrValue = headerValue.AsSpan(i + 1, j - i - 1).Trim().ToString();
|
|---|
| 371 | #endif
|
|---|
| 372 | }
|
|---|
| 373 | else
|
|---|
| 374 | {
|
|---|
| 375 | for (j = i; j < l; j++)
|
|---|
| 376 | {
|
|---|
| 377 | if (headerValue[j] == ';')
|
|---|
| 378 | break;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | if (j == i)
|
|---|
| 382 | return null;
|
|---|
| 383 |
|
|---|
| 384 | #if UNITY_NETFRAMEWORK
|
|---|
| 385 | attrValue = headerValue.Substring(i, j - i).Trim();
|
|---|
| 386 | #else
|
|---|
| 387 | attrValue = headerValue.AsSpan(i, j - i).Trim().ToString();
|
|---|
| 388 | #endif
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | return attrValue;
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | internal static string[]? ParseMultivalueHeader(string s)
|
|---|
| 395 | {
|
|---|
| 396 | if (s == null)
|
|---|
| 397 | return null;
|
|---|
| 398 |
|
|---|
| 399 | int l = s.Length;
|
|---|
| 400 |
|
|---|
| 401 | // collect comma-separated values into list
|
|---|
| 402 |
|
|---|
| 403 | List<string> values = new List<string>();
|
|---|
| 404 | int i = 0;
|
|---|
| 405 |
|
|---|
| 406 | while (i < l)
|
|---|
| 407 | {
|
|---|
| 408 | // find next ,
|
|---|
| 409 | int ci = s.IndexOf(',', i);
|
|---|
| 410 | if (ci < 0)
|
|---|
| 411 | ci = l;
|
|---|
| 412 |
|
|---|
| 413 | // append corresponding server value
|
|---|
| 414 | values.Add(s.Substring(i, ci - i));
|
|---|
| 415 |
|
|---|
| 416 | // move to next
|
|---|
| 417 | i = ci + 1;
|
|---|
| 418 |
|
|---|
| 419 | // skip leading space
|
|---|
| 420 | if (i < l && s[i] == ' ')
|
|---|
| 421 | i++;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | // return list as array of strings
|
|---|
| 425 |
|
|---|
| 426 | int n = values.Count;
|
|---|
| 427 | string[] strings;
|
|---|
| 428 |
|
|---|
| 429 | // if n is 0 that means s was empty string
|
|---|
| 430 |
|
|---|
| 431 | if (n == 0)
|
|---|
| 432 | {
|
|---|
| 433 | strings = new string[1];
|
|---|
| 434 | strings[0] = string.Empty;
|
|---|
| 435 | }
|
|---|
| 436 | else
|
|---|
| 437 | {
|
|---|
| 438 | strings = new string[n];
|
|---|
| 439 | values.CopyTo(0, strings, 0, n);
|
|---|
| 440 | }
|
|---|
| 441 | return strings;
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 | private static string UrlDecodeStringFromStringInternal(string s, Encoding e)
|
|---|
| 446 | {
|
|---|
| 447 | int count = s.Length;
|
|---|
| 448 | UrlDecoder helper = new UrlDecoder(count, e);
|
|---|
| 449 |
|
|---|
| 450 | // go through the string's chars collapsing %XX and %uXXXX and
|
|---|
| 451 | // appending each char as char, with exception of %XX constructs
|
|---|
| 452 | // that are appended as bytes
|
|---|
| 453 |
|
|---|
| 454 | for (int pos = 0; pos < count; pos++)
|
|---|
| 455 | {
|
|---|
| 456 | char ch = s[pos];
|
|---|
| 457 |
|
|---|
| 458 | if (ch == '+')
|
|---|
| 459 | {
|
|---|
| 460 | ch = ' ';
|
|---|
| 461 | }
|
|---|
| 462 | else if (ch == '%' && pos < count - 2)
|
|---|
| 463 | {
|
|---|
| 464 | if (s[pos + 1] == 'u' && pos < count - 5)
|
|---|
| 465 | {
|
|---|
| 466 | int h1 = HexConverter.FromChar(s[pos + 2]);
|
|---|
| 467 | int h2 = HexConverter.FromChar(s[pos + 3]);
|
|---|
| 468 | int h3 = HexConverter.FromChar(s[pos + 4]);
|
|---|
| 469 | int h4 = HexConverter.FromChar(s[pos + 5]);
|
|---|
| 470 |
|
|---|
| 471 | if ((h1 | h2 | h3 | h4) != 0xFF)
|
|---|
| 472 | { // valid 4 hex chars
|
|---|
| 473 | ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
|
|---|
| 474 | pos += 5;
|
|---|
| 475 |
|
|---|
| 476 | // only add as char
|
|---|
| 477 | helper.AddChar(ch);
|
|---|
| 478 | continue;
|
|---|
| 479 | }
|
|---|
| 480 | }
|
|---|
| 481 | else
|
|---|
| 482 | {
|
|---|
| 483 | int h1 = HexConverter.FromChar(s[pos + 1]);
|
|---|
| 484 | int h2 = HexConverter.FromChar(s[pos + 2]);
|
|---|
| 485 |
|
|---|
| 486 | if ((h1 | h2) != 0xFF)
|
|---|
| 487 | { // valid 2 hex chars
|
|---|
| 488 | byte b = (byte)((h1 << 4) | h2);
|
|---|
| 489 | pos += 2;
|
|---|
| 490 |
|
|---|
| 491 | // don't add as char
|
|---|
| 492 | helper.AddByte(b);
|
|---|
| 493 | continue;
|
|---|
| 494 | }
|
|---|
| 495 | }
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | if ((ch & 0xFF80) == 0)
|
|---|
| 499 | helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
|
|---|
| 500 | else
|
|---|
| 501 | helper.AddChar(ch);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | return helper.GetString();
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | private sealed class UrlDecoder
|
|---|
| 508 | {
|
|---|
| 509 | private readonly int _bufferSize;
|
|---|
| 510 |
|
|---|
| 511 | // Accumulate characters in a special array
|
|---|
| 512 | private int _numChars;
|
|---|
| 513 | private readonly char[] _charBuffer;
|
|---|
| 514 |
|
|---|
| 515 | // Accumulate bytes for decoding into characters in a special array
|
|---|
| 516 | private int _numBytes;
|
|---|
| 517 | private byte[]? _byteBuffer;
|
|---|
| 518 |
|
|---|
| 519 | // Encoding to convert chars to bytes
|
|---|
| 520 | private readonly Encoding _encoding;
|
|---|
| 521 |
|
|---|
| 522 | private void FlushBytes()
|
|---|
| 523 | {
|
|---|
| 524 | if (_numBytes > 0)
|
|---|
| 525 | {
|
|---|
| 526 | _numChars += _encoding.GetChars(_byteBuffer!, 0, _numBytes, _charBuffer, _numChars);
|
|---|
| 527 | _numBytes = 0;
|
|---|
| 528 | }
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | internal UrlDecoder(int bufferSize, Encoding encoding)
|
|---|
| 532 | {
|
|---|
| 533 | _bufferSize = bufferSize;
|
|---|
| 534 | _encoding = encoding;
|
|---|
| 535 |
|
|---|
| 536 | _charBuffer = new char[bufferSize];
|
|---|
| 537 | // byte buffer created on demand
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | internal void AddChar(char ch)
|
|---|
| 541 | {
|
|---|
| 542 | if (_numBytes > 0)
|
|---|
| 543 | FlushBytes();
|
|---|
| 544 |
|
|---|
| 545 | _charBuffer[_numChars++] = ch;
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | internal void AddByte(byte b)
|
|---|
| 549 | {
|
|---|
| 550 | {
|
|---|
| 551 | if (_byteBuffer == null)
|
|---|
| 552 | _byteBuffer = new byte[_bufferSize];
|
|---|
| 553 |
|
|---|
| 554 | _byteBuffer[_numBytes++] = b;
|
|---|
| 555 | }
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | internal string GetString()
|
|---|
| 559 | {
|
|---|
| 560 | if (_numBytes > 0)
|
|---|
| 561 | FlushBytes();
|
|---|
| 562 |
|
|---|
| 563 | if (_numChars > 0)
|
|---|
| 564 | return new string(_charBuffer, 0, _numChars);
|
|---|
| 565 | else
|
|---|
| 566 | return string.Empty;
|
|---|
| 567 | }
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 | internal static void FillFromString(NameValueCollection nvc, string s, bool urlencoded, Encoding encoding)
|
|---|
| 572 | {
|
|---|
| 573 | int l = s.Length;
|
|---|
| 574 | int i = (l > 0 && s[0] == '?') ? 1 : 0;
|
|---|
| 575 |
|
|---|
| 576 | while (i < l)
|
|---|
| 577 | {
|
|---|
| 578 | // find next & while noting first = on the way (and if there are more)
|
|---|
| 579 |
|
|---|
| 580 | int si = i;
|
|---|
| 581 | int ti = -1;
|
|---|
| 582 |
|
|---|
| 583 | while (i < l)
|
|---|
| 584 | {
|
|---|
| 585 | char ch = s[i];
|
|---|
| 586 |
|
|---|
| 587 | if (ch == '=')
|
|---|
| 588 | {
|
|---|
| 589 | if (ti < 0)
|
|---|
| 590 | ti = i;
|
|---|
| 591 | }
|
|---|
| 592 | else if (ch == '&')
|
|---|
| 593 | {
|
|---|
| 594 | break;
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | i++;
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 | // extract the name / value pair
|
|---|
| 601 |
|
|---|
| 602 | string? name = null;
|
|---|
| 603 | string? value = null;
|
|---|
| 604 |
|
|---|
| 605 | if (ti >= 0)
|
|---|
| 606 | {
|
|---|
| 607 | name = s.Substring(si, ti - si);
|
|---|
| 608 | value = s.Substring(ti + 1, i - ti - 1);
|
|---|
| 609 | }
|
|---|
| 610 | else
|
|---|
| 611 | {
|
|---|
| 612 | value = s.Substring(si, i - si);
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | // add name / value pair to the collection
|
|---|
| 616 |
|
|---|
| 617 | if (urlencoded)
|
|---|
| 618 | nvc.Add(
|
|---|
| 619 | name == null ? null : UrlDecodeStringFromStringInternal(name, encoding),
|
|---|
| 620 | UrlDecodeStringFromStringInternal(value, encoding));
|
|---|
| 621 | else
|
|---|
| 622 | nvc.Add(name, value);
|
|---|
| 623 |
|
|---|
| 624 | // trailing '&'
|
|---|
| 625 |
|
|---|
| 626 | if (i == l - 1 && s[i] == '&')
|
|---|
| 627 | nvc.Add(null, "");
|
|---|
| 628 |
|
|---|
| 629 | i++;
|
|---|
| 630 | }
|
|---|
| 631 | }
|
|---|
| 632 | }
|
|---|
| 633 | }
|
|---|
| 634 | }
|
|---|