mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-20 21:54:25 -04:00
imported signalr 1.1.3 into NzbDrone.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Owin.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods for creating and consuming CallParameters.Headers and ResultParameters.Headers.
|
||||
/// </summary>
|
||||
internal static class Headers
|
||||
{
|
||||
public static IDictionary<string, string[]> SetHeader(this IDictionary<string, string[]> headers,
|
||||
string name, string value)
|
||||
{
|
||||
headers[name] = new[] { value };
|
||||
return headers;
|
||||
}
|
||||
|
||||
public static string[] GetHeaders(this IDictionary<string, string[]> headers,
|
||||
string name)
|
||||
{
|
||||
string[] value;
|
||||
return headers != null && headers.TryGetValue(name, out value) ? value : null;
|
||||
}
|
||||
|
||||
public static string GetHeader(this IDictionary<string, string[]> headers,
|
||||
string name)
|
||||
{
|
||||
var values = GetHeaders(headers, name);
|
||||
if (values == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (values.Length)
|
||||
{
|
||||
case 0:
|
||||
return String.Empty;
|
||||
case 1:
|
||||
return values[0];
|
||||
default:
|
||||
return String.Join(",", values);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Owin
|
||||
{
|
||||
internal static class OwinConstants
|
||||
{
|
||||
public const string Version = "owin.Version";
|
||||
|
||||
public const string RequestBody = "owin.RequestBody";
|
||||
public const string RequestHeaders = "owin.RequestHeaders";
|
||||
public const string RequestScheme = "owin.RequestScheme";
|
||||
public const string RequestMethod = "owin.RequestMethod";
|
||||
public const string RequestPathBase = "owin.RequestPathBase";
|
||||
public const string RequestPath = "owin.RequestPath";
|
||||
public const string RequestQueryString = "owin.RequestQueryString";
|
||||
public const string RequestProtocol = "owin.RequestProtocol";
|
||||
|
||||
public const string CallCancelled = "owin.CallCancelled";
|
||||
|
||||
public const string ResponseStatusCode = "owin.ResponseStatusCode";
|
||||
public const string ResponseReasonPhrase = "owin.ResponseReasonPhrase";
|
||||
public const string ResponseHeaders = "owin.ResponseHeaders";
|
||||
public const string ResponseBody = "owin.ResponseBody";
|
||||
|
||||
public const string TraceOutput = "host.TraceOutput";
|
||||
|
||||
public const string User = "server.User";
|
||||
public const string RemoteIpAddress = "server.RemoteIpAddress";
|
||||
public const string RemotePort = "server.RemotePort";
|
||||
public const string LocalIpAddress = "server.LocalIpAddress";
|
||||
public const string LocalPort = "server.LocalPort";
|
||||
|
||||
public const string DisableRequestCompression = "systemweb.DisableResponseCompression";
|
||||
public const string DisableRequestBuffering = "server.DisableRequestBuffering";
|
||||
public const string DisableResponseBuffering = "server.DisableResponseBuffering";
|
||||
|
||||
public const string ServerCapabilities = "server.Capabilities";
|
||||
public const string WebSocketVersion = "websocket.Version";
|
||||
public const string WebSocketAccept = "websocket.Accept";
|
||||
|
||||
public const string HostOnAppDisposing = "host.OnAppDisposing";
|
||||
public const string HostAppNameKey = "host.AppName";
|
||||
public const string HostAppModeKey = "host.AppMode";
|
||||
public const string AppModeDevelopment = "development";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Owin
|
||||
{
|
||||
internal static class OwinEnvironmentExtensions
|
||||
{
|
||||
internal static T Get<T>(this IDictionary<string, object> environment, string key)
|
||||
{
|
||||
object value;
|
||||
return environment.TryGetValue(key, out value) ? (T)value : default(T);
|
||||
}
|
||||
|
||||
internal static CancellationToken GetShutdownToken(this IDictionary<string, object> env)
|
||||
{
|
||||
object value;
|
||||
return env.TryGetValue(OwinConstants.HostOnAppDisposing, out value)
|
||||
&& value is CancellationToken
|
||||
? (CancellationToken)value
|
||||
: default(CancellationToken);
|
||||
}
|
||||
|
||||
internal static string GetAppInstanceName(this IDictionary<string, object> environment)
|
||||
{
|
||||
object value;
|
||||
if (environment.TryGetValue(OwinConstants.HostAppNameKey, out value))
|
||||
{
|
||||
var stringVal = value as string;
|
||||
|
||||
if (!String.IsNullOrEmpty(stringVal))
|
||||
{
|
||||
return stringVal;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
internal static bool SupportsWebSockets(this IDictionary<string, object> environment)
|
||||
{
|
||||
object value;
|
||||
if (environment.TryGetValue(OwinConstants.ServerCapabilities, out value))
|
||||
{
|
||||
var capabilities = value as IDictionary<string, object>;
|
||||
if (capabilities != null)
|
||||
{
|
||||
return capabilities.ContainsKey(OwinConstants.WebSocketVersion);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
internal static bool GetIsDebugEnabled(this IDictionary<string, object> environment)
|
||||
{
|
||||
object value;
|
||||
if (environment.TryGetValue(OwinConstants.HostAppModeKey, out value))
|
||||
{
|
||||
var stringVal = value as string;
|
||||
return !String.IsNullOrWhiteSpace(stringVal) &&
|
||||
OwinConstants.AppModeDevelopment.Equals(stringVal, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.AspNet.SignalR.Infrastructure;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Owin.Infrastructure
|
||||
{
|
||||
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "It is instantiated in the static Parse method")]
|
||||
internal sealed class ParamDictionary
|
||||
{
|
||||
private static readonly char[] DefaultParamSeparators = new[] { '&', ';' };
|
||||
private static readonly char[] ParamKeyValueSeparator = new[] { '=' };
|
||||
private static readonly char[] LeadingWhitespaceChars = new[] { ' ' };
|
||||
|
||||
internal static IEnumerable<KeyValuePair<string, string>> ParseToEnumerable(string value, char[] delimiters = null)
|
||||
{
|
||||
value = value ?? String.Empty;
|
||||
delimiters = delimiters ?? DefaultParamSeparators;
|
||||
|
||||
var items = value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
string[] pair = item.Split(ParamKeyValueSeparator, 2, StringSplitOptions.None);
|
||||
|
||||
string pairKey = UrlDecoder.UrlDecode(pair[0]).TrimStart(LeadingWhitespaceChars);
|
||||
string pairValue = pair.Length < 2 ? String.Empty : UrlDecoder.UrlDecode(pair[1]);
|
||||
|
||||
yield return new KeyValuePair<string, string>(pairKey, pairValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Owin.Infrastructure
|
||||
{
|
||||
internal static class PrefixMatcher
|
||||
{
|
||||
public static bool IsMatch(string pathBase, string path)
|
||||
{
|
||||
pathBase = EnsureStartsWithSlash(pathBase);
|
||||
path = EnsureStartsWithSlash(path);
|
||||
|
||||
var pathLength = path.Length;
|
||||
var pathBaseLength = pathBase.Length;
|
||||
|
||||
if (pathLength < pathBaseLength)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pathLength > pathBaseLength && path[pathBaseLength] != '/')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!path.StartsWith(pathBase, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string EnsureStartsWithSlash(string path)
|
||||
{
|
||||
if (path.Length == 0)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
if (path[0] == '/')
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
return '/' + path;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Infrastructure
|
||||
{
|
||||
// Taken from System.Net.Http.Formatting.Internal.UrlDecoder.cs (http://aspnetwebstack.codeplex.com/)
|
||||
|
||||
/// <summary>
|
||||
/// Helpers for decoding URI query components.
|
||||
/// </summary>
|
||||
internal static class UrlDecoder
|
||||
{
|
||||
// The implementation below is ported from WebUtility for use in .Net 4
|
||||
|
||||
public static string UrlDecode(string str)
|
||||
{
|
||||
if (str == null)
|
||||
return null;
|
||||
|
||||
return UrlDecodeInternal(str, Encoding.UTF8);
|
||||
}
|
||||
|
||||
#region UrlDecode implementation
|
||||
|
||||
private static string UrlDecodeInternal(string value, Encoding encoding)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int count = value.Length;
|
||||
var helper = new DecoderHelper(count, encoding);
|
||||
|
||||
// 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++)
|
||||
{
|
||||
char ch = value[pos];
|
||||
|
||||
if (ch == '+')
|
||||
{
|
||||
ch = ' ';
|
||||
}
|
||||
else if (ch == '%' && pos < count - 2)
|
||||
{
|
||||
int h1 = HexToInt(value[pos + 1]);
|
||||
int h2 = HexToInt(value[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;
|
||||
}
|
||||
}
|
||||
|
||||
if ((ch & 0xFF80) == 0)
|
||||
helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
|
||||
else
|
||||
helper.AddChar(ch);
|
||||
}
|
||||
|
||||
return helper.GetString();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DecoderHelper nested class
|
||||
|
||||
// Internal class to facilitate URL decoding -- keeps char buffer and byte buffer, allows appending of either chars or bytes
|
||||
private class DecoderHelper
|
||||
{
|
||||
private int _bufferSize;
|
||||
|
||||
// Accumulate characters in a special array
|
||||
private int _numChars;
|
||||
private char[] _charBuffer;
|
||||
|
||||
// Accumulate bytes for decoding into characters in a special array
|
||||
private int _numBytes;
|
||||
private byte[] _byteBuffer;
|
||||
|
||||
// Encoding to convert chars to bytes
|
||||
private Encoding _encoding;
|
||||
|
||||
private void FlushBytes()
|
||||
{
|
||||
if (_numBytes > 0)
|
||||
{
|
||||
_numChars += _encoding.GetChars(_byteBuffer, 0, _numBytes, _charBuffer, _numChars);
|
||||
_numBytes = 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal DecoderHelper(int bufferSize, Encoding encoding)
|
||||
{
|
||||
_bufferSize = bufferSize;
|
||||
_encoding = encoding;
|
||||
|
||||
_charBuffer = new char[bufferSize];
|
||||
// byte buffer created on demand
|
||||
}
|
||||
|
||||
internal void AddChar(char ch)
|
||||
{
|
||||
if (_numBytes > 0)
|
||||
FlushBytes();
|
||||
|
||||
_charBuffer[_numChars++] = ch;
|
||||
}
|
||||
|
||||
internal void AddByte(byte b)
|
||||
{
|
||||
if (_byteBuffer == null)
|
||||
_byteBuffer = new byte[_bufferSize];
|
||||
|
||||
_byteBuffer[_numBytes++] = b;
|
||||
}
|
||||
|
||||
internal String GetString()
|
||||
{
|
||||
if (_numBytes > 0)
|
||||
FlushBytes();
|
||||
|
||||
if (_numChars > 0)
|
||||
return new String(_charBuffer, 0, _numChars);
|
||||
else
|
||||
return String.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user