2016-11-11 19:55:12 +00:00
|
|
|
using System;
|
2019-01-13 19:27:29 +00:00
|
|
|
using System.Collections.Generic;
|
2016-11-11 19:55:12 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.Net;
|
|
|
|
using System.Text;
|
|
|
|
using MediaBrowser.Model.Services;
|
2018-09-12 17:26:21 +00:00
|
|
|
using SocketHttpListener.Net.WebSockets;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
namespace SocketHttpListener.Net
|
|
|
|
{
|
2019-01-25 22:38:25 +00:00
|
|
|
public sealed partial class HttpListenerRequest
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
private CookieCollection _cookies;
|
|
|
|
private bool? _keepAlive;
|
|
|
|
private string _rawUrl;
|
|
|
|
private Uri _requestUri;
|
|
|
|
private Version _version;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public string[] AcceptTypes => Helpers.ParseMultivalueHeader(Headers[HttpKnownHeaderNames.Accept]);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public string[] UserLanguages => Helpers.ParseMultivalueHeader(Headers[HttpKnownHeaderNames.AcceptLanguage]);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static CookieCollection ParseCookies(Uri uri, string setCookieHeader)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-01-13 20:37:13 +00:00
|
|
|
var cookies = new CookieCollection();
|
2018-09-12 17:26:21 +00:00
|
|
|
return cookies;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public CookieCollection Cookies
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
get
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (_cookies == null)
|
|
|
|
{
|
|
|
|
string cookieString = Headers[HttpKnownHeaderNames.Cookie];
|
|
|
|
if (!string.IsNullOrEmpty(cookieString))
|
|
|
|
{
|
|
|
|
_cookies = ParseCookies(RequestUri, cookieString);
|
|
|
|
}
|
|
|
|
if (_cookies == null)
|
|
|
|
{
|
|
|
|
_cookies = new CookieCollection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _cookies;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public Encoding ContentEncoding
|
|
|
|
{
|
|
|
|
get
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (UserAgent != null && CultureInfo.InvariantCulture.CompareInfo.IsPrefix(UserAgent, "UP"))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
string postDataCharset = Headers["x-up-devcap-post-charset"];
|
|
|
|
if (postDataCharset != null && postDataCharset.Length > 0)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return Encoding.GetEncoding(postDataCharset);
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
if (HasEntityBody)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (ContentType != null)
|
|
|
|
{
|
|
|
|
string charSet = Helpers.GetCharSetValueFromHeader(ContentType);
|
|
|
|
if (charSet != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return Encoding.GetEncoding(charSet);
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2019-01-16 19:50:40 +00:00
|
|
|
return Encoding.UTF8;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public string ContentType => Headers[HttpKnownHeaderNames.ContentType];
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool IsLocal => LocalEndPoint.Address.Equals(RemoteEndPoint.Address);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool IsWebSocketRequest
|
|
|
|
{
|
|
|
|
get
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!SupportsWebSockets)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return false;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
bool foundConnectionUpgradeHeader = false;
|
|
|
|
if (string.IsNullOrEmpty(Headers[HttpKnownHeaderNames.Connection]) || string.IsNullOrEmpty(Headers[HttpKnownHeaderNames.Upgrade]))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return false;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
foreach (string connection in Headers.GetValues(HttpKnownHeaderNames.Connection))
|
|
|
|
{
|
|
|
|
if (string.Equals(connection, HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
foundConnectionUpgradeHeader = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!foundConnectionUpgradeHeader)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
foreach (string upgrade in Headers.GetValues(HttpKnownHeaderNames.Upgrade))
|
|
|
|
{
|
|
|
|
if (string.Equals(upgrade, HttpWebSocket.WebSocketUpgradeToken, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool KeepAlive
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
get
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!_keepAlive.HasValue)
|
|
|
|
{
|
|
|
|
string header = Headers[HttpKnownHeaderNames.ProxyConnection];
|
|
|
|
if (string.IsNullOrEmpty(header))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
header = Headers[HttpKnownHeaderNames.Connection];
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
if (string.IsNullOrEmpty(header))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (ProtocolVersion >= HttpVersion.Version11)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
_keepAlive = true;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
header = Headers[HttpKnownHeaderNames.KeepAlive];
|
|
|
|
_keepAlive = !string.IsNullOrEmpty(header);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
else
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
header = header.ToLower(CultureInfo.InvariantCulture);
|
|
|
|
_keepAlive =
|
|
|
|
header.IndexOf("close", StringComparison.OrdinalIgnoreCase) < 0 ||
|
|
|
|
header.IndexOf("keep-alive", StringComparison.OrdinalIgnoreCase) >= 0;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return _keepAlive.Value;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public QueryParamCollection QueryString
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2019-01-13 20:37:13 +00:00
|
|
|
var queryString = new QueryParamCollection();
|
2018-09-12 17:26:21 +00:00
|
|
|
Helpers.FillFromString(queryString, Url.Query, true, ContentEncoding);
|
|
|
|
return queryString;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public string RawUrl => _rawUrl;
|
|
|
|
|
|
|
|
private string RequestScheme => IsSecureConnection ? UriScheme.Https : UriScheme.Http;
|
|
|
|
|
|
|
|
public string UserAgent => Headers[HttpKnownHeaderNames.UserAgent];
|
|
|
|
|
|
|
|
public string UserHostAddress => LocalEndPoint.ToString();
|
|
|
|
|
|
|
|
public string UserHostName => Headers[HttpKnownHeaderNames.Host];
|
|
|
|
|
|
|
|
public Uri UrlReferrer
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
string referrer = Headers[HttpKnownHeaderNames.Referer];
|
|
|
|
if (referrer == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:37:13 +00:00
|
|
|
bool success = Uri.TryCreate(referrer, UriKind.RelativeOrAbsolute, out var urlReferrer);
|
2018-09-12 17:26:21 +00:00
|
|
|
return success ? urlReferrer : null;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public Uri Url => RequestUri;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public Version ProtocolVersion => _version;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private static class Helpers
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
//
|
|
|
|
// Get attribute off header value
|
|
|
|
//
|
|
|
|
internal static string GetCharSetValueFromHeader(string headerValue)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
const string AttrName = "charset";
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (headerValue == null)
|
|
|
|
return null;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
int l = headerValue.Length;
|
|
|
|
int k = AttrName.Length;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// find properly separated attribute name
|
|
|
|
int i = 1; // start searching from 1
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
while (i < l)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
i = CultureInfo.InvariantCulture.CompareInfo.IndexOf(headerValue, AttrName, i, CompareOptions.IgnoreCase);
|
|
|
|
if (i < 0)
|
|
|
|
break;
|
|
|
|
if (i + k >= l)
|
|
|
|
break;
|
|
|
|
|
|
|
|
char chPrev = headerValue[i - 1];
|
|
|
|
char chNext = headerValue[i + k];
|
|
|
|
if ((chPrev == ';' || chPrev == ',' || char.IsWhiteSpace(chPrev)) && (chNext == '=' || char.IsWhiteSpace(chNext)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
i += k;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (i < 0 || i >= l)
|
|
|
|
return null;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// skip to '=' and the following whitespace
|
|
|
|
i += k;
|
|
|
|
while (i < l && char.IsWhiteSpace(headerValue[i]))
|
|
|
|
i++;
|
|
|
|
if (i >= l || headerValue[i] != '=')
|
|
|
|
return null;
|
|
|
|
i++;
|
|
|
|
while (i < l && char.IsWhiteSpace(headerValue[i]))
|
|
|
|
i++;
|
|
|
|
if (i >= l)
|
|
|
|
return null;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// parse the value
|
|
|
|
string attrValue = null;
|
|
|
|
|
|
|
|
int j;
|
|
|
|
|
|
|
|
if (i < l && headerValue[i] == '"')
|
|
|
|
{
|
|
|
|
if (i == l - 1)
|
|
|
|
return null;
|
|
|
|
j = headerValue.IndexOf('"', i + 1);
|
|
|
|
if (j < 0 || j == i + 1)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
attrValue = headerValue.Substring(i + 1, j - i - 1).Trim();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (j = i; j < l; j++)
|
|
|
|
{
|
|
|
|
if (headerValue[j] == ';')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j == i)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
attrValue = headerValue.Substring(i, j - i).Trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static string[] ParseMultivalueHeader(string s)
|
2017-09-03 02:42:13 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (s == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
int l = s.Length;
|
|
|
|
|
|
|
|
// collect comma-separated values into list
|
|
|
|
|
2019-01-13 20:37:13 +00:00
|
|
|
var values = new List<string>();
|
2018-09-12 17:26:21 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (i < l)
|
|
|
|
{
|
|
|
|
// find next ,
|
|
|
|
int ci = s.IndexOf(',', i);
|
|
|
|
if (ci < 0)
|
|
|
|
ci = l;
|
|
|
|
|
|
|
|
// append corresponding server value
|
|
|
|
values.Add(s.Substring(i, ci - i));
|
|
|
|
|
|
|
|
// move to next
|
|
|
|
i = ci + 1;
|
|
|
|
|
|
|
|
// skip leading space
|
|
|
|
if (i < l && s[i] == ' ')
|
|
|
|
i++;
|
|
|
|
}
|
2017-09-03 02:42:13 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// return list as array of strings
|
|
|
|
|
|
|
|
int n = values.Count;
|
|
|
|
string[] strings;
|
|
|
|
|
|
|
|
// if n is 0 that means s was empty string
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
strings = new string[1];
|
|
|
|
strings[0] = string.Empty;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strings = new string[n];
|
|
|
|
values.CopyTo(0, strings, 0, n);
|
|
|
|
}
|
|
|
|
return strings;
|
2017-09-03 02:42:13 +00:00
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private static string UrlDecodeStringFromStringInternal(string s, Encoding e)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
int count = s.Length;
|
2019-01-13 20:37:13 +00:00
|
|
|
var helper = new UrlDecoder(count, e);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
// go through the string's chars collapsing %XX and %uXXXX and
|
|
|
|
// appending each char as char, with exception of %XX constructs
|
|
|
|
// that are appended as bytes
|
|
|
|
|
|
|
|
for (int pos = 0; pos < count; pos++)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
char ch = s[pos];
|
|
|
|
|
|
|
|
if (ch == '+')
|
2017-06-21 14:50:54 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
ch = ' ';
|
2017-06-21 14:50:54 +00:00
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
else if (ch == '%' && pos < count - 2)
|
2017-06-21 14:50:54 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (s[pos + 1] == 'u' && pos < count - 5)
|
2017-06-21 14:50:54 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
int h1 = HexToInt(s[pos + 2]);
|
|
|
|
int h2 = HexToInt(s[pos + 3]);
|
|
|
|
int h3 = HexToInt(s[pos + 4]);
|
|
|
|
int h4 = HexToInt(s[pos + 5]);
|
|
|
|
|
|
|
|
if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0)
|
|
|
|
{ // valid 4 hex chars
|
|
|
|
ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
|
|
|
|
pos += 5;
|
|
|
|
|
|
|
|
// only add as char
|
|
|
|
helper.AddChar(ch);
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-21 14:50:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
int h1 = HexToInt(s[pos + 1]);
|
|
|
|
int h2 = HexToInt(s[pos + 2]);
|
|
|
|
|
|
|
|
if (h1 >= 0 && h2 >= 0)
|
|
|
|
{ // valid 2 hex chars
|
|
|
|
byte b = (byte)((h1 << 4) | h2);
|
|
|
|
pos += 2;
|
|
|
|
|
|
|
|
// don't add as char
|
|
|
|
helper.AddByte(b);
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-21 14:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
if ((ch & 0xFF80) == 0)
|
|
|
|
helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
|
2017-06-21 14:50:54 +00:00
|
|
|
else
|
2018-09-12 17:26:21 +00:00
|
|
|
helper.AddChar(ch);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2017-06-21 14:50:54 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return helper.GetString();
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private static int HexToInt(char h)
|
|
|
|
{
|
|
|
|
return (h >= '0' && h <= '9') ? h - '0' :
|
|
|
|
(h >= 'a' && h <= 'f') ? h - 'a' + 10 :
|
|
|
|
(h >= 'A' && h <= 'F') ? h - 'A' + 10 :
|
|
|
|
-1;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private class UrlDecoder
|
|
|
|
{
|
|
|
|
private int _bufferSize;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// Accumulate characters in a special array
|
|
|
|
private int _numChars;
|
|
|
|
private char[] _charBuffer;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// Accumulate bytes for decoding into characters in a special array
|
|
|
|
private int _numBytes;
|
|
|
|
private byte[] _byteBuffer;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// Encoding to convert chars to bytes
|
|
|
|
private Encoding _encoding;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private void FlushBytes()
|
|
|
|
{
|
|
|
|
if (_numBytes > 0)
|
|
|
|
{
|
|
|
|
_numChars += _encoding.GetChars(_byteBuffer, 0, _numBytes, _charBuffer, _numChars);
|
|
|
|
_numBytes = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
internal UrlDecoder(int bufferSize, Encoding encoding)
|
|
|
|
{
|
|
|
|
_bufferSize = bufferSize;
|
|
|
|
_encoding = encoding;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
_charBuffer = new char[bufferSize];
|
|
|
|
// byte buffer created on demand
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
internal void AddChar(char ch)
|
|
|
|
{
|
|
|
|
if (_numBytes > 0)
|
|
|
|
FlushBytes();
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
_charBuffer[_numChars++] = ch;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
internal void AddByte(byte b)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (_byteBuffer == null)
|
|
|
|
_byteBuffer = new byte[_bufferSize];
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
_byteBuffer[_numBytes++] = b;
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
internal string GetString()
|
|
|
|
{
|
|
|
|
if (_numBytes > 0)
|
|
|
|
FlushBytes();
|
|
|
|
|
|
|
|
if (_numChars > 0)
|
|
|
|
return new string(_charBuffer, 0, _numChars);
|
|
|
|
else
|
|
|
|
return string.Empty;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
internal static void FillFromString(QueryParamCollection nvc, string s, bool urlencoded, Encoding encoding)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
int l = (s != null) ? s.Length : 0;
|
|
|
|
int i = (s.Length > 0 && s[0] == '?') ? 1 : 0;
|
|
|
|
|
|
|
|
while (i < l)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
// find next & while noting first = on the way (and if there are more)
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
int si = i;
|
|
|
|
int ti = -1;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
while (i < l)
|
|
|
|
{
|
|
|
|
char ch = s[i];
|
|
|
|
|
|
|
|
if (ch == '=')
|
|
|
|
{
|
|
|
|
if (ti < 0)
|
|
|
|
ti = i;
|
|
|
|
}
|
|
|
|
else if (ch == '&')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract the name / value pair
|
|
|
|
|
|
|
|
string name = null;
|
|
|
|
string value = null;
|
|
|
|
|
|
|
|
if (ti >= 0)
|
|
|
|
{
|
|
|
|
name = s.Substring(si, ti - si);
|
|
|
|
value = s.Substring(ti + 1, i - ti - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = s.Substring(si, i - si);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add name / value pair to the collection
|
|
|
|
|
|
|
|
if (urlencoded)
|
|
|
|
nvc.Add(
|
|
|
|
name == null ? null : UrlDecodeStringFromStringInternal(name, encoding),
|
|
|
|
UrlDecodeStringFromStringInternal(value, encoding));
|
|
|
|
else
|
|
|
|
nvc.Add(name, value);
|
|
|
|
|
|
|
|
// trailing '&'
|
|
|
|
|
|
|
|
if (i == l - 1 && s[i] == '&')
|
|
|
|
nvc.Add(null, "");
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|