up
This commit is contained in:
407
Assets/BestHTTP/Extensions/Extensions.cs
Normal file
407
Assets/BestHTTP/Extensions/Extensions.cs
Normal file
@@ -0,0 +1,407 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
#if NETFX_CORE
|
||||
using Windows.Security.Cryptography;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
using Windows.Storage.Streams;
|
||||
using BestHTTP.PlatformSupport.IO;
|
||||
|
||||
using FileStream = BestHTTP.PlatformSupport.IO.FileStream;
|
||||
#elif UNITY_WP8
|
||||
using Cryptography = BestHTTP.PlatformSupport.Cryptography;
|
||||
#else
|
||||
using Cryptography = System.Security.Cryptography;
|
||||
using FileStream = System.IO.FileStream;
|
||||
#endif
|
||||
|
||||
namespace BestHTTP.Extensions
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
#region ASCII Encoding (These are required because Windows Phone doesn't supports the Encoding.ASCII class.)
|
||||
|
||||
/// <summary>
|
||||
/// On WP8 platform there are no ASCII encoding.
|
||||
/// </summary>
|
||||
public static string AsciiToString(this byte[] bytes)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder(bytes.Length);
|
||||
foreach (byte b in bytes)
|
||||
sb.Append(b <= 0x7f ? (char)b : '?');
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// On WP8 platform there are no ASCII encoding.
|
||||
/// </summary>
|
||||
public static byte[] GetASCIIBytes(this string str)
|
||||
{
|
||||
byte[] result = new byte[str.Length];
|
||||
for (int i = 0; i < str.Length; ++i)
|
||||
{
|
||||
char ch = str[i];
|
||||
result[i] = (byte)((ch < (char)0x80) ? ch : '?');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void SendAsASCII(this BinaryWriter stream, string str)
|
||||
{
|
||||
for (int i = 0; i < str.Length; ++i)
|
||||
{
|
||||
char ch = str[i];
|
||||
|
||||
stream.Write((byte)((ch < (char)0x80) ? ch : '?'));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region FileSystem WriteLine function support
|
||||
|
||||
public static void WriteLine(this FileStream fs)
|
||||
{
|
||||
fs.Write(HTTPRequest.EOL, 0, 2);
|
||||
}
|
||||
|
||||
public static void WriteLine(this FileStream fs, string line)
|
||||
{
|
||||
var buff = line.GetASCIIBytes();
|
||||
fs.Write(buff, 0, buff.Length);
|
||||
fs.WriteLine();
|
||||
}
|
||||
|
||||
public static void WriteLine(this FileStream fs, string format, params object[] values)
|
||||
{
|
||||
var buff = string.Format(format, values).GetASCIIBytes();
|
||||
fs.Write(buff, 0, buff.Length);
|
||||
fs.WriteLine();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Other Extensions
|
||||
|
||||
public static string GetRequestPathAndQueryURL(this Uri uri)
|
||||
{
|
||||
string requestPathAndQuery = uri.GetComponents(UriComponents.PathAndQuery, UriFormat.UriEscaped);
|
||||
|
||||
// http://forum.unity3d.com/threads/best-http-released.200006/page-26#post-2723250
|
||||
if (string.IsNullOrEmpty(requestPathAndQuery))
|
||||
requestPathAndQuery = "/";
|
||||
|
||||
return requestPathAndQuery;
|
||||
}
|
||||
|
||||
public static string[] FindOption(this string str, string option)
|
||||
{
|
||||
//s-maxage=2678400, must-revalidate, max-age=0
|
||||
string[] options = str.ToLower().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
option = option.ToLower();
|
||||
|
||||
for (int i = 0; i < options.Length; ++i)
|
||||
if (options[i].Contains(option))
|
||||
return options[i].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void WriteArray(this Stream stream, byte[] array)
|
||||
{
|
||||
stream.Write(array, 0, array.Length);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region String Conversions
|
||||
|
||||
public static int ToInt32(this string str, int defaultValue = default(int))
|
||||
{
|
||||
if (str == null)
|
||||
return defaultValue;
|
||||
|
||||
try
|
||||
{
|
||||
return int.Parse(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static long ToInt64(this string str, long defaultValue = default(long))
|
||||
{
|
||||
if (str == null)
|
||||
return defaultValue;
|
||||
|
||||
try
|
||||
{
|
||||
return long.Parse(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static DateTime ToDateTime(this string str, DateTime defaultValue = default(DateTime))
|
||||
{
|
||||
if (str == null)
|
||||
return defaultValue;
|
||||
|
||||
try
|
||||
{
|
||||
DateTime.TryParse(str, out defaultValue);
|
||||
return defaultValue.ToUniversalTime();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static string ToStrOrEmpty(this string str)
|
||||
{
|
||||
if (str == null)
|
||||
return String.Empty;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region MD5 Hashing
|
||||
|
||||
public static string CalculateMD5Hash(this string input)
|
||||
{
|
||||
return input.GetASCIIBytes().CalculateMD5Hash();
|
||||
}
|
||||
|
||||
public static string CalculateMD5Hash(this byte[] input)
|
||||
{
|
||||
#if NETFX_CORE
|
||||
var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
|
||||
IBuffer buff = CryptographicBuffer.CreateFromByteArray(input);
|
||||
var hashed = alg.HashData(buff);
|
||||
var res = CryptographicBuffer.EncodeToHexString(hashed);
|
||||
return res;
|
||||
#else
|
||||
var hash = Cryptography.MD5.Create().ComputeHash(input);
|
||||
var sb = new StringBuilder();
|
||||
foreach (var b in hash)
|
||||
sb.Append(b.ToString("x2"));
|
||||
return sb.ToString();
|
||||
#endif
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Efficient String Parsing Helpers
|
||||
|
||||
internal static string Read(this string str, ref int pos, char block, bool needResult = true)
|
||||
{
|
||||
return str.Read(ref pos, (ch) => ch != block, needResult);
|
||||
}
|
||||
|
||||
internal static string Read(this string str, ref int pos, Func<char, bool> block, bool needResult = true)
|
||||
{
|
||||
if (pos >= str.Length)
|
||||
return string.Empty;
|
||||
|
||||
str.SkipWhiteSpace(ref pos);
|
||||
|
||||
int startPos = pos;
|
||||
|
||||
while (pos < str.Length && block(str[pos]))
|
||||
pos++;
|
||||
|
||||
string result = needResult ? str.Substring(startPos, pos - startPos) : null;
|
||||
|
||||
// set position to the next char
|
||||
pos++;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static string ReadPossibleQuotedText(this string str, ref int pos)
|
||||
{
|
||||
string result = string.Empty;
|
||||
if (str == null)
|
||||
return result;
|
||||
|
||||
// It's a quoted text?
|
||||
if (str[pos] == '\"')
|
||||
{
|
||||
// Skip the starting quote
|
||||
str.Read(ref pos, '\"', false);
|
||||
|
||||
// Read the text until the ending quote
|
||||
result = str.Read(ref pos, '\"');
|
||||
|
||||
// Next option
|
||||
str.Read(ref pos, ',', false);
|
||||
}
|
||||
else
|
||||
// It's not a quoted text, so we will read until the next option
|
||||
result = str.Read(ref pos, (ch) => ch != ',' && ch != ';');
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static void SkipWhiteSpace(this string str, ref int pos)
|
||||
{
|
||||
if (pos >= str.Length)
|
||||
return;
|
||||
|
||||
while (pos < str.Length && char.IsWhiteSpace(str[pos]))
|
||||
pos++;
|
||||
}
|
||||
|
||||
internal static string TrimAndLower(this string str)
|
||||
{
|
||||
if (str == null)
|
||||
return null;
|
||||
|
||||
char[] buffer = new char[str.Length];
|
||||
int length = 0;
|
||||
|
||||
for (int i = 0; i < str.Length; ++i)
|
||||
{
|
||||
char ch = str[i];
|
||||
if (!char.IsWhiteSpace(ch) && !char.IsControl(ch))
|
||||
buffer[length++] = char.ToLowerInvariant(ch);
|
||||
}
|
||||
|
||||
return new string(buffer, 0, length);
|
||||
}
|
||||
|
||||
internal static char? Peek(this string str, int pos)
|
||||
{
|
||||
if (pos < 0 || pos >= str.Length)
|
||||
return null;
|
||||
|
||||
return str[pos];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Specialized String Parsers
|
||||
|
||||
//public, max-age=2592000
|
||||
internal static List<HeaderValue> ParseOptionalHeader(this string str)
|
||||
{
|
||||
List<HeaderValue> result = new List<HeaderValue>();
|
||||
|
||||
if (str == null)
|
||||
return result;
|
||||
|
||||
int idx = 0;
|
||||
|
||||
// process the rest of the text
|
||||
while (idx < str.Length)
|
||||
{
|
||||
// Read key
|
||||
string key = str.Read(ref idx, (ch) => ch != '=' && ch != ',').TrimAndLower();
|
||||
HeaderValue qp = new HeaderValue(key);
|
||||
|
||||
if (str[idx - 1] == '=')
|
||||
qp.Value = str.ReadPossibleQuotedText(ref idx);
|
||||
|
||||
result.Add(qp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//deflate, gzip, x-gzip, identity, *;q=0
|
||||
internal static List<HeaderValue> ParseQualityParams(this string str)
|
||||
{
|
||||
List<HeaderValue> result = new List<HeaderValue>();
|
||||
|
||||
if (str == null)
|
||||
return result;
|
||||
|
||||
int idx = 0;
|
||||
while (idx < str.Length)
|
||||
{
|
||||
string key = str.Read(ref idx, (ch) => ch != ',' && ch != ';').TrimAndLower();
|
||||
|
||||
HeaderValue qp = new HeaderValue(key);
|
||||
|
||||
if (str[idx - 1] == ';')
|
||||
{
|
||||
str.Read(ref idx, '=', false);
|
||||
qp.Value = str.Read(ref idx, ',');
|
||||
}
|
||||
|
||||
result.Add(qp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Buffer Filling
|
||||
|
||||
/// <summary>
|
||||
/// Will fill the entire buffer from the stream. Will throw an exception when the underlying stream is closed.
|
||||
/// </summary>
|
||||
public static void ReadBuffer(this Stream stream, byte[] buffer)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
do
|
||||
{
|
||||
int read = stream.Read(buffer, count, buffer.Length - count);
|
||||
|
||||
if (read <= 0)
|
||||
throw ExceptionHelper.ServerClosedTCPStream();
|
||||
|
||||
count += read;
|
||||
} while (count < buffer.Length);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region MemoryStream
|
||||
|
||||
public static void WriteAll(this MemoryStream ms, byte[] buffer)
|
||||
{
|
||||
ms.Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public static void WriteString(this MemoryStream ms, string str)
|
||||
{
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(str);
|
||||
ms.WriteAll(buffer);
|
||||
}
|
||||
|
||||
public static void WriteLine(this MemoryStream ms)
|
||||
{
|
||||
ms.WriteAll(HTTPRequest.EOL);
|
||||
}
|
||||
|
||||
public static void WriteLine(this MemoryStream ms, string str)
|
||||
{
|
||||
ms.WriteString(str);
|
||||
ms.WriteLine();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public static class ExceptionHelper
|
||||
{
|
||||
public static Exception ServerClosedTCPStream()
|
||||
{
|
||||
return new Exception("TCP Stream closed unexpectedly by the remote server");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/BestHTTP/Extensions/Extensions.cs.meta
Normal file
11
Assets/BestHTTP/Extensions/Extensions.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 61f3ad8711ee4471d8e759861c7e3cb3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
40
Assets/BestHTTP/Extensions/HeaderParser.cs
Normal file
40
Assets/BestHTTP/Extensions/HeaderParser.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BestHTTP.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Will parse a comma-separeted header value
|
||||
/// </summary>
|
||||
public sealed class HeaderParser : KeyValuePairList
|
||||
{
|
||||
public HeaderParser(string headerStr)
|
||||
{
|
||||
base.Values = Parse(headerStr);
|
||||
}
|
||||
|
||||
private List<HeaderValue> Parse(string headerStr)
|
||||
{
|
||||
List<HeaderValue> result = new List<HeaderValue>();
|
||||
|
||||
int pos = 0;
|
||||
|
||||
try
|
||||
{
|
||||
while (pos < headerStr.Length)
|
||||
{
|
||||
HeaderValue current = new HeaderValue();
|
||||
|
||||
current.Parse(headerStr, ref pos);
|
||||
|
||||
result.Add(current);
|
||||
}
|
||||
}
|
||||
catch(System.Exception ex)
|
||||
{
|
||||
HTTPManager.Logger.Exception("HeaderParser - Parse", headerStr, ex);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/BestHTTP/Extensions/HeaderParser.cs.meta
Normal file
11
Assets/BestHTTP/Extensions/HeaderParser.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ec9c9be669874efca21e700638db160
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
114
Assets/BestHTTP/Extensions/HeaderValue.cs
Normal file
114
Assets/BestHTTP/Extensions/HeaderValue.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BestHTTP.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Used in string parsers. Its Value is optional.
|
||||
/// </summary>
|
||||
public sealed class HeaderValue
|
||||
{
|
||||
#region Public Properties
|
||||
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
public List<HeaderValue> Options { get; set; }
|
||||
|
||||
public bool HasValue { get { return !string.IsNullOrEmpty(this.Value); } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public HeaderValue()
|
||||
{ }
|
||||
|
||||
public HeaderValue(string key)
|
||||
{
|
||||
this.Key = key;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Helper Functions
|
||||
|
||||
public void Parse(string headerStr, ref int pos)
|
||||
{
|
||||
ParseImplementation(headerStr, ref pos, true);
|
||||
}
|
||||
|
||||
public bool TryGetOption(string key, out HeaderValue option)
|
||||
{
|
||||
option = null;
|
||||
|
||||
if (Options == null || Options.Count == 0)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < Options.Count; ++i)
|
||||
if (String.Equals(Options[i].Key, key, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
option = Options[i];
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Helper Functions
|
||||
|
||||
private void ParseImplementation(string headerStr, ref int pos, bool isOptionIsAnOption)
|
||||
{
|
||||
string key = headerStr.Read(ref pos, (ch) => ch != ';' && ch != '=' && ch != ',', true);
|
||||
this.Key = key;
|
||||
|
||||
char? skippedChar = headerStr.Peek(pos - 1);
|
||||
bool isValue = skippedChar == '=';
|
||||
bool isOption = isOptionIsAnOption && skippedChar == ';';
|
||||
|
||||
while (skippedChar != null && isValue || isOption)
|
||||
{
|
||||
|
||||
if (isValue)
|
||||
{
|
||||
string value = headerStr.ReadPossibleQuotedText(ref pos);
|
||||
this.Value = value;
|
||||
}
|
||||
else if (isOption)
|
||||
{
|
||||
HeaderValue option = new HeaderValue();
|
||||
option.ParseImplementation(headerStr, ref pos, false);
|
||||
|
||||
if (this.Options == null)
|
||||
this.Options = new List<HeaderValue>();
|
||||
|
||||
this.Options.Add(option);
|
||||
}
|
||||
|
||||
if (!isOptionIsAnOption)
|
||||
return;
|
||||
|
||||
skippedChar = headerStr.Peek(pos - 1);
|
||||
isValue = skippedChar == '=';
|
||||
isOption = isOptionIsAnOption && skippedChar == ';';
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(Value))
|
||||
return String.Concat(Key, '=', Value);
|
||||
else
|
||||
return Key;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
11
Assets/BestHTTP/Extensions/HeaderValue.cs.meta
Normal file
11
Assets/BestHTTP/Extensions/HeaderValue.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6e602b94f16de4df0bce5429a7b9f4c8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
70
Assets/BestHTTP/Extensions/HeartbeatManager.cs
Normal file
70
Assets/BestHTTP/Extensions/HeartbeatManager.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BestHTTP.Extensions
|
||||
{
|
||||
public interface IHeartbeat
|
||||
{
|
||||
void OnHeartbeatUpdate(TimeSpan dif);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A manager class that can handle subscribing and unsubscribeing in the same update.
|
||||
/// </summary>
|
||||
public sealed class HeartbeatManager
|
||||
{
|
||||
private List<IHeartbeat> Heartbeats = new List<IHeartbeat>();
|
||||
private IHeartbeat[] UpdateArray;
|
||||
private DateTime LastUpdate = DateTime.MinValue;
|
||||
|
||||
public void Subscribe(IHeartbeat heartbeat)
|
||||
{
|
||||
lock (Heartbeats)
|
||||
{
|
||||
if (!Heartbeats.Contains(heartbeat))
|
||||
Heartbeats.Add(heartbeat);
|
||||
}
|
||||
}
|
||||
|
||||
public void Unsubscribe(IHeartbeat heartbeat)
|
||||
{
|
||||
lock (Heartbeats)
|
||||
Heartbeats.Remove(heartbeat);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (LastUpdate == DateTime.MinValue)
|
||||
LastUpdate = DateTime.UtcNow;
|
||||
else
|
||||
{
|
||||
TimeSpan dif = DateTime.UtcNow - LastUpdate;
|
||||
LastUpdate = DateTime.UtcNow;
|
||||
|
||||
int count = 0;
|
||||
|
||||
lock (Heartbeats)
|
||||
{
|
||||
if (UpdateArray == null || UpdateArray.Length < Heartbeats.Count)
|
||||
Array.Resize(ref UpdateArray, Heartbeats.Count);
|
||||
|
||||
Heartbeats.CopyTo(0, UpdateArray, 0, Heartbeats.Count);
|
||||
|
||||
count = Heartbeats.Count;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
try
|
||||
{
|
||||
UpdateArray[i].OnHeartbeatUpdate(dif);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/BestHTTP/Extensions/HeartbeatManager.cs.meta
Normal file
11
Assets/BestHTTP/Extensions/HeartbeatManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a53e417bb6234382931cd702656827b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
27
Assets/BestHTTP/Extensions/KeyValuePairList.cs
Normal file
27
Assets/BestHTTP/Extensions/KeyValuePairList.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BestHTTP.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for specialized parsers
|
||||
/// </summary>
|
||||
public class KeyValuePairList
|
||||
{
|
||||
public List<HeaderValue> Values { get; protected set; }
|
||||
|
||||
public bool TryGet(string valueKeyName, out HeaderValue @param)
|
||||
{
|
||||
@param = null;
|
||||
for (int i = 0; i < Values.Count; ++i)
|
||||
if (string.CompareOrdinal(Values[i].Key, valueKeyName) == 0)
|
||||
{
|
||||
@param = Values[i];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/BestHTTP/Extensions/KeyValuePairList.cs.meta
Normal file
11
Assets/BestHTTP/Extensions/KeyValuePairList.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 385306b42b07044c885d807618235fe9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
50
Assets/BestHTTP/Extensions/WWWAuthenticateHeaderParser.cs
Normal file
50
Assets/BestHTTP/Extensions/WWWAuthenticateHeaderParser.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BestHTTP.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Used for parsing WWW-Authenticate headers:
|
||||
/// Digest realm="my realm", nonce="4664b327a2963503ba58bbe13ad672c0", qop=auth, opaque="f7e38bdc1c66fce214f9019ffe43117c"
|
||||
/// </summary>
|
||||
public sealed class WWWAuthenticateHeaderParser : KeyValuePairList
|
||||
{
|
||||
public WWWAuthenticateHeaderParser(string headerValue)
|
||||
{
|
||||
Values = ParseQuotedHeader(headerValue);
|
||||
}
|
||||
|
||||
private List<HeaderValue> ParseQuotedHeader(string str)
|
||||
{
|
||||
List<HeaderValue> result = new List<HeaderValue>();
|
||||
|
||||
if (str != null)
|
||||
{
|
||||
|
||||
int idx = 0;
|
||||
|
||||
// Read Type (Basic|Digest)
|
||||
string type = str.Read(ref idx, (ch) => !char.IsWhiteSpace(ch) && !char.IsControl(ch)).TrimAndLower();
|
||||
result.Add(new HeaderValue(type));
|
||||
|
||||
// process the rest of the text
|
||||
while (idx < str.Length)
|
||||
{
|
||||
// Read key
|
||||
string key = str.Read(ref idx, '=').TrimAndLower();
|
||||
HeaderValue qp = new HeaderValue(key);
|
||||
|
||||
// Skip any white space
|
||||
str.SkipWhiteSpace(ref idx);
|
||||
|
||||
qp.Value = str.ReadPossibleQuotedText(ref idx);
|
||||
|
||||
result.Add(qp);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 413bc04e0adfd469bbea744b57a8dd4c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user