Refactored Ext.cs
This commit is contained in:
parent
aedefc1391
commit
decddde20a
@ -51,11 +51,11 @@ using WebSocketSharp.Server;
|
|||||||
namespace WebSocketSharp
|
namespace WebSocketSharp
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides a set of static methods for the websocket-sharp.
|
/// Provides a set of static methods for websocket-sharp.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Ext
|
public static class Ext
|
||||||
{
|
{
|
||||||
#region Private Const Fields
|
#region Private Fields
|
||||||
|
|
||||||
private const string _tspecials = "()<>@,;:\\\"/[]?={} \t";
|
private const string _tspecials = "()<>@,;:\\\"/[]?={} \t";
|
||||||
|
|
||||||
@ -63,16 +63,15 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
#region Private Methods
|
#region Private Methods
|
||||||
|
|
||||||
private static byte [] compress (this byte [] value)
|
private static byte[] compress (this byte[] data)
|
||||||
{
|
{
|
||||||
if (value.LongLength == 0)
|
if (data.LongLength == 0)
|
||||||
//return new Byte[] { 0x00, 0x00, 0x00, 0xff, 0xff };
|
//return new Byte[] { 0x00, 0x00, 0x00, 0xff, 0xff };
|
||||||
return value;
|
return data;
|
||||||
|
|
||||||
using (var input = new MemoryStream (value)) {
|
using (var input = new MemoryStream (data))
|
||||||
return input.compressToArray ();
|
return input.compressToArray ();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static MemoryStream compress (this Stream stream)
|
private static MemoryStream compress (this Stream stream)
|
||||||
{
|
{
|
||||||
@ -92,21 +91,20 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
private static byte[] compressToArray (this Stream stream)
|
private static byte[] compressToArray (this Stream stream)
|
||||||
{
|
{
|
||||||
using (var comp = stream.compress ()) {
|
using (var output = stream.compress ()) {
|
||||||
comp.Close ();
|
output.Close ();
|
||||||
return comp.ToArray ();
|
return output.ToArray ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte [] decompress (this byte [] value)
|
private static byte[] decompress (this byte[] data)
|
||||||
{
|
{
|
||||||
if (value.LongLength == 0)
|
if (data.LongLength == 0)
|
||||||
return value;
|
return data;
|
||||||
|
|
||||||
using (var input = new MemoryStream (value)) {
|
using (var input = new MemoryStream (data))
|
||||||
return input.decompressToArray ();
|
return input.decompressToArray ();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static MemoryStream decompress (this Stream stream)
|
private static MemoryStream decompress (this Stream stream)
|
||||||
{
|
{
|
||||||
@ -123,9 +121,9 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
private static byte[] decompressToArray (this Stream stream)
|
private static byte[] decompressToArray (this Stream stream)
|
||||||
{
|
{
|
||||||
using (var decomp = stream.decompress ()) {
|
using (var output = stream.decompress ()) {
|
||||||
decomp.Close ();
|
output.Close ();
|
||||||
return decomp.ToArray ();
|
return output.ToArray ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,23 +169,23 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
internal static byte[] Append (this ushort code, string reason)
|
internal static byte[] Append (this ushort code, string reason)
|
||||||
{
|
{
|
||||||
using (var buffer = new MemoryStream ()) {
|
using (var buff = new MemoryStream ()) {
|
||||||
var tmp = code.ToByteArrayInternally (ByteOrder.Big);
|
var tmp = code.ToByteArrayInternally (ByteOrder.Big);
|
||||||
buffer.Write (tmp, 0, 2);
|
buff.Write (tmp, 0, 2);
|
||||||
if (reason != null && reason.Length > 0) {
|
if (reason != null && reason.Length > 0) {
|
||||||
tmp = Encoding.UTF8.GetBytes (reason);
|
tmp = Encoding.UTF8.GetBytes (reason);
|
||||||
buffer.Write (tmp, 0, tmp.Length);
|
buff.Write (tmp, 0, tmp.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.Close ();
|
buff.Close ();
|
||||||
return buffer.ToArray ();
|
return buff.ToArray ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string CheckIfCanRead (this Stream stream)
|
internal static string CheckIfCanRead (this Stream stream)
|
||||||
{
|
{
|
||||||
return stream == null
|
return stream == null
|
||||||
? "'stream' must not be null."
|
? "'stream' is null."
|
||||||
: !stream.CanRead
|
: !stream.CanRead
|
||||||
? "'stream' cannot be read."
|
? "'stream' cannot be read."
|
||||||
: null;
|
: null;
|
||||||
@ -250,7 +248,7 @@ namespace WebSocketSharp
|
|||||||
internal static string CheckIfValidControlData (this byte[] data, string paramName)
|
internal static string CheckIfValidControlData (this byte[] data, string paramName)
|
||||||
{
|
{
|
||||||
return data.Length > 125
|
return data.Length > 125
|
||||||
? String.Format ("'{0}' length must be less.", paramName)
|
? String.Format ("'{0}' is greater than the allowable size.", paramName)
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,39 +265,39 @@ namespace WebSocketSharp
|
|||||||
internal static string CheckIfValidSendData (this byte[] data)
|
internal static string CheckIfValidSendData (this byte[] data)
|
||||||
{
|
{
|
||||||
return data == null
|
return data == null
|
||||||
? "'data' must not be null."
|
? "'data' is null."
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string CheckIfValidSendData (this FileInfo file)
|
internal static string CheckIfValidSendData (this FileInfo file)
|
||||||
{
|
{
|
||||||
return file == null
|
return file == null
|
||||||
? "'file' must not be null."
|
? "'file' is null."
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string CheckIfValidSendData (this string data)
|
internal static string CheckIfValidSendData (this string data)
|
||||||
{
|
{
|
||||||
return data == null
|
return data == null
|
||||||
? "'data' must not be null."
|
? "'data' is null."
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string CheckIfValidServicePath (this string servicePath)
|
internal static string CheckIfValidServicePath (this string servicePath)
|
||||||
{
|
{
|
||||||
return servicePath == null || servicePath.Length == 0
|
return servicePath == null || servicePath.Length == 0
|
||||||
? "'servicePath' must not be null or empty."
|
? "'servicePath' is null or empty."
|
||||||
: servicePath[0] != '/'
|
: servicePath[0] != '/'
|
||||||
? "'servicePath' not absolute path."
|
? "'servicePath' isn't absolute path."
|
||||||
: servicePath.IndexOfAny (new[] {'?', '#'}) != -1
|
: servicePath.IndexOfAny (new[] {'?', '#'}) != -1
|
||||||
? "'servicePath' must not contain either or both query and fragment components."
|
? "'servicePath' contains either or both query and fragment components."
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string CheckIfValidSessionID (this string id)
|
internal static string CheckIfValidSessionID (this string id)
|
||||||
{
|
{
|
||||||
return id == null || id.Length == 0
|
return id == null || id.Length == 0
|
||||||
? "'id' must not be null or empty."
|
? "'id' is null or empty."
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,11 +314,11 @@ namespace WebSocketSharp
|
|||||||
response.Close (HttpStatusCode.Unauthorized);
|
response.Close (HttpStatusCode.Unauthorized);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static byte [] Compress (this byte [] value, CompressionMethod method)
|
internal static byte[] Compress (this byte[] data, CompressionMethod method)
|
||||||
{
|
{
|
||||||
return method == CompressionMethod.Deflate
|
return method == CompressionMethod.Deflate
|
||||||
? value.compress ()
|
? data.compress ()
|
||||||
: value;
|
: data;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static Stream Compress (this Stream stream, CompressionMethod method)
|
internal static Stream Compress (this Stream stream, CompressionMethod method)
|
||||||
@ -351,13 +349,13 @@ namespace WebSocketSharp
|
|||||||
var len = values.Length;
|
var len = values.Length;
|
||||||
|
|
||||||
Func<int, bool> contains = null;
|
Func<int, bool> contains = null;
|
||||||
contains = index => {
|
contains = idx => {
|
||||||
if (index < len - 1) {
|
if (idx < len - 1) {
|
||||||
for (var i = index + 1; i < len; i++)
|
for (var i = idx + 1; i < len; i++)
|
||||||
if (values [i] == values [index])
|
if (values[i] == values[idx])
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return contains (++index);
|
return contains (++idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -382,21 +380,20 @@ namespace WebSocketSharp
|
|||||||
internal static void CopyTo (this Stream src, Stream dest, bool setDefaultPosition)
|
internal static void CopyTo (this Stream src, Stream dest, bool setDefaultPosition)
|
||||||
{
|
{
|
||||||
var readLen = 0;
|
var readLen = 0;
|
||||||
var bufferLen = 256;
|
var buffLen = 256;
|
||||||
var buffer = new byte [bufferLen];
|
var buff = new byte[buffLen];
|
||||||
while ((readLen = src.Read (buffer, 0, bufferLen)) > 0) {
|
while ((readLen = src.Read (buff, 0, buffLen)) > 0)
|
||||||
dest.Write (buffer, 0, readLen);
|
dest.Write (buff, 0, readLen);
|
||||||
}
|
|
||||||
|
|
||||||
if (setDefaultPosition)
|
if (setDefaultPosition)
|
||||||
dest.Position = 0;
|
dest.Position = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static byte [] Decompress (this byte [] value, CompressionMethod method)
|
internal static byte[] Decompress (this byte[] data, CompressionMethod method)
|
||||||
{
|
{
|
||||||
return method == CompressionMethod.Deflate
|
return method == CompressionMethod.Deflate
|
||||||
? value.decompress ()
|
? data.decompress ()
|
||||||
: value;
|
: data;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static Stream Decompress (this Stream stream, CompressionMethod method)
|
internal static Stream Decompress (this Stream stream, CompressionMethod method)
|
||||||
@ -452,7 +449,7 @@ namespace WebSocketSharp
|
|||||||
/// otherwise, <see langword="null"/>.
|
/// otherwise, <see langword="null"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="uri">
|
/// <param name="uri">
|
||||||
/// A <see cref="Uri"/> that represents a URI to get the absolute path from.
|
/// A <see cref="Uri"/> that represents the URI to get the absolute path from.
|
||||||
/// </param>
|
/// </param>
|
||||||
internal static string GetAbsolutePath (this Uri uri)
|
internal static string GetAbsolutePath (this Uri uri)
|
||||||
{
|
{
|
||||||
@ -484,7 +481,7 @@ namespace WebSocketSharp
|
|||||||
: code == CloseStatusCode.TooBig
|
: code == CloseStatusCode.TooBig
|
||||||
? "A too big data has been received."
|
? "A too big data has been received."
|
||||||
: code == CloseStatusCode.IgnoreExtension
|
: code == CloseStatusCode.IgnoreExtension
|
||||||
? "WebSocket client did not receive expected extension(s)."
|
? "WebSocket client didn't receive expected extension(s)."
|
||||||
: code == CloseStatusCode.ServerError
|
: code == CloseStatusCode.ServerError
|
||||||
? "WebSocket server got an internal error."
|
? "WebSocket server got an internal error."
|
||||||
: code == CloseStatusCode.TlsHandshakeFailure
|
: code == CloseStatusCode.TlsHandshakeFailure
|
||||||
@ -492,7 +489,21 @@ namespace WebSocketSharp
|
|||||||
: String.Empty;
|
: String.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string GetNameInternal (this string nameAndValue, string separator)
|
/// <summary>
|
||||||
|
/// Gets the name from the specified <see cref="string"/> that contains a pair of name and
|
||||||
|
/// value separated by a separator character.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A <see cref="string"/> that represents the name if any; otherwise, <c>null</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="nameAndValue">
|
||||||
|
/// A <see cref="string"/> that contains a pair of name and value separated by a separator
|
||||||
|
/// character.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="separator">
|
||||||
|
/// A <see cref="char"/> that represents the separator character.
|
||||||
|
/// </param>
|
||||||
|
internal static string GetName (this string nameAndValue, char separator)
|
||||||
{
|
{
|
||||||
var i = nameAndValue.IndexOf (separator);
|
var i = nameAndValue.IndexOf (separator);
|
||||||
return i > 0
|
return i > 0
|
||||||
@ -500,15 +511,29 @@ namespace WebSocketSharp
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string GetValueInternal (this string nameAndValue, string separator)
|
/// <summary>
|
||||||
|
/// Gets the value from the specified <see cref="string"/> that contains a pair of name and
|
||||||
|
/// value separated by a separator character.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A <see cref="string"/> that represents the value if any; otherwise, <c>null</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="nameAndValue">
|
||||||
|
/// A <see cref="string"/> that contains a pair of name and value separated by a separator
|
||||||
|
/// character.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="separator">
|
||||||
|
/// A <see cref="char"/> that represents the separator character.
|
||||||
|
/// </param>
|
||||||
|
internal static string GetValue (this string nameAndValue, char separator)
|
||||||
{
|
{
|
||||||
var i = nameAndValue.IndexOf (separator);
|
var i = nameAndValue.IndexOf (separator);
|
||||||
return i >= 0 && i < nameAndValue.Length - 1
|
return i > -1 && i < nameAndValue.Length - 1
|
||||||
? nameAndValue.Substring (i + 1).Trim ()
|
? nameAndValue.Substring (i + 1).Trim ()
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string GetValue (this string nameAndValue, string separator, bool unquote)
|
internal static string GetValue (this string nameAndValue, char separator, bool unquote)
|
||||||
{
|
{
|
||||||
var i = nameAndValue.IndexOf (separator);
|
var i = nameAndValue.IndexOf (separator);
|
||||||
if (i < 0 || i == nameAndValue.Length - 1)
|
if (i < 0 || i == nameAndValue.Length - 1)
|
||||||
@ -602,33 +627,33 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
internal static byte[] ReadBytes (this Stream stream, long length, int bufferLength)
|
internal static byte[] ReadBytes (this Stream stream, long length, int bufferLength)
|
||||||
{
|
{
|
||||||
using (var result = new MemoryStream ()) {
|
using (var res = new MemoryStream ()) {
|
||||||
var count = length / bufferLength;
|
var cnt = length / bufferLength;
|
||||||
var rem = (int) (length % bufferLength);
|
var rem = (int) (length % bufferLength);
|
||||||
|
|
||||||
var buffer = new byte [bufferLength];
|
var buff = new byte[bufferLength];
|
||||||
var end = false;
|
var end = false;
|
||||||
for (long i = 0; i < count; i++) {
|
for (long i = 0; i < cnt; i++) {
|
||||||
if (!stream.readBytes (buffer, 0, bufferLength, result)) {
|
if (!stream.readBytes (buff, 0, bufferLength, res)) {
|
||||||
end = true;
|
end = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!end && rem > 0)
|
if (!end && rem > 0)
|
||||||
stream.readBytes (new byte [rem], 0, rem, result);
|
stream.readBytes (new byte[rem], 0, rem, res);
|
||||||
|
|
||||||
result.Close ();
|
res.Close ();
|
||||||
return result.ToArray ();
|
return res.ToArray ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void ReadBytesAsync (
|
internal static void ReadBytesAsync (
|
||||||
this Stream stream, int length, Action<byte[]> completed, Action<Exception> error)
|
this Stream stream, int length, Action<byte[]> completed, Action<Exception> error)
|
||||||
{
|
{
|
||||||
var buffer = new byte [length];
|
var buff = new byte[length];
|
||||||
stream.BeginRead (
|
stream.BeginRead (
|
||||||
buffer,
|
buff,
|
||||||
0,
|
0,
|
||||||
length,
|
length,
|
||||||
ar => {
|
ar => {
|
||||||
@ -637,8 +662,8 @@ namespace WebSocketSharp
|
|||||||
var bytes = len < 1
|
var bytes = len < 1
|
||||||
? new byte[0]
|
? new byte[0]
|
||||||
: len < length
|
: len < length
|
||||||
? stream.readBytes (buffer, len, length - len)
|
? stream.readBytes (buff, len, length - len)
|
||||||
: buffer;
|
: buff;
|
||||||
|
|
||||||
if (completed != null)
|
if (completed != null)
|
||||||
completed (bytes);
|
completed (bytes);
|
||||||
@ -669,7 +694,7 @@ namespace WebSocketSharp
|
|||||||
internal static T[] Reverse<T> (this T[] array)
|
internal static T[] Reverse<T> (this T[] array)
|
||||||
{
|
{
|
||||||
var len = array.Length;
|
var len = array.Length;
|
||||||
T [] reverse = new T [len];
|
var reverse = new T[len];
|
||||||
|
|
||||||
var end = len - 1;
|
var end = len - 1;
|
||||||
for (var i = 0; i <= end; i++)
|
for (var i = 0; i <= end; i++)
|
||||||
@ -684,7 +709,7 @@ namespace WebSocketSharp
|
|||||||
var len = value.Length;
|
var len = value.Length;
|
||||||
var separators = new string (separator);
|
var separators = new string (separator);
|
||||||
|
|
||||||
var buffer = new StringBuilder (32);
|
var buff = new StringBuilder (32);
|
||||||
var quoted = false;
|
var quoted = false;
|
||||||
var escaped = false;
|
var escaped = false;
|
||||||
|
|
||||||
@ -703,8 +728,8 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
else if (separators.Contains (c)) {
|
else if (separators.Contains (c)) {
|
||||||
if (!quoted) {
|
if (!quoted) {
|
||||||
yield return buffer.ToString ();
|
yield return buff.ToString ();
|
||||||
buffer.Length = 0;
|
buff.Length = 0;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -712,11 +737,11 @@ namespace WebSocketSharp
|
|||||||
else {
|
else {
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.Append (c);
|
buff.Append (c);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer.Length > 0)
|
if (buff.Length > 0)
|
||||||
yield return buffer.ToString ();
|
yield return buff.ToString ();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static byte[] ToByteArray (this Stream stream)
|
internal static byte[] ToByteArray (this Stream stream)
|
||||||
@ -821,46 +846,43 @@ namespace WebSocketSharp
|
|||||||
{
|
{
|
||||||
result = null;
|
result = null;
|
||||||
if (uriString.Length == 0) {
|
if (uriString.Length == 0) {
|
||||||
message = "Must not be empty.";
|
message = "Empty string.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var uri = uriString.ToUri ();
|
var uri = uriString.ToUri ();
|
||||||
if (!uri.IsAbsoluteUri) {
|
if (!uri.IsAbsoluteUri) {
|
||||||
message = "Must be the absolute URI: " + uriString;
|
message = "Not absolute URI: " + uriString;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var scheme = uri.Scheme;
|
var schm = uri.Scheme;
|
||||||
if (scheme != "ws" && scheme != "wss") {
|
if (schm != "ws" && schm != "wss") {
|
||||||
message = "The scheme part must be 'ws' or 'wss': " + scheme;
|
message = "The scheme part isn't 'ws' or 'wss': " + uriString;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var fragment = uri.Fragment;
|
if (uri.Fragment.Length > 0) {
|
||||||
if (fragment.Length > 0) {
|
message = "Contains the fragment component: " + uriString;
|
||||||
message = "Must not contain the fragment component: " + uriString;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var port = uri.Port;
|
var port = uri.Port;
|
||||||
if (port > 0) {
|
if (port > 0) {
|
||||||
if (port > 65535) {
|
if (port > 65535) {
|
||||||
message = "The port part must be between 1 and 65535: " + port;
|
message = "The port part is greater than 65535: " + uriString;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((scheme == "ws" && port == 443) || (scheme == "wss" && port == 80)) {
|
if ((schm == "ws" && port == 443) || (schm == "wss" && port == 80)) {
|
||||||
message = String.Format (
|
message = "Invalid pair of scheme and port: " + uriString;
|
||||||
"Invalid pair of scheme and port: {0}, {1}", scheme, port);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
port = scheme == "ws" ? 80 : 443;
|
uri = String.Format (
|
||||||
var url = String.Format (
|
"{0}://{1}:{2}{3}", schm, uri.Host, schm == "ws" ? 80 : 443, uri.PathAndQuery)
|
||||||
"{0}://{1}:{2}{3}", scheme, uri.Host, port, uri.PathAndQuery);
|
.ToUri ();
|
||||||
uri = url.ToUri ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result = uri;
|
result = uri;
|
||||||
@ -871,19 +893,18 @@ namespace WebSocketSharp
|
|||||||
|
|
||||||
internal static string Unquote (this string value)
|
internal static string Unquote (this string value)
|
||||||
{
|
{
|
||||||
var start = value.IndexOf ('\"');
|
var start = value.IndexOf ('"');
|
||||||
var end = value.LastIndexOf ('\"');
|
var end = value.LastIndexOf ('"');
|
||||||
if (start < end)
|
if (start < end)
|
||||||
value = value.Substring (start + 1, end - start - 1).Replace ("\\\"", "\"");
|
value = value.Substring (start + 1, end - start - 1).Replace ("\\\"", "\"");
|
||||||
|
|
||||||
return value.Trim ();
|
return value.Trim ();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void WriteBytes (this Stream stream, byte [] value)
|
internal static void WriteBytes (this Stream stream, byte[] data)
|
||||||
{
|
{
|
||||||
using (var src = new MemoryStream (value)) {
|
using (var input = new MemoryStream (data))
|
||||||
src.CopyTo (stream);
|
input.CopyTo (stream);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -939,9 +960,8 @@ namespace WebSocketSharp
|
|||||||
/// with the specified both <paramref name="name"/> and <paramref name="value"/>.
|
/// with the specified both <paramref name="name"/> and <paramref name="value"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="collection"/> contains the entry
|
/// <c>true</c> if <paramref name="collection"/> contains the entry with both
|
||||||
/// with both <paramref name="name"/> and <paramref name="value"/>;
|
/// <paramref name="name"/> and <paramref name="value"/>; otherwise, <c>false</c>.
|
||||||
/// otherwise, <c>false</c>.
|
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="collection">
|
/// <param name="collection">
|
||||||
/// A <see cref="NameValueCollection"/> to test.
|
/// A <see cref="NameValueCollection"/> to test.
|
||||||
@ -957,12 +977,12 @@ namespace WebSocketSharp
|
|||||||
if (collection == null || collection.Count == 0)
|
if (collection == null || collection.Count == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var values = collection [name];
|
var vals = collection[name];
|
||||||
if (values == null)
|
if (vals == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
foreach (var v in values.Split (','))
|
foreach (var val in vals.Split (','))
|
||||||
if (v.Trim ().Equals (value, StringComparison.OrdinalIgnoreCase))
|
if (val.Trim ().Equals (value, StringComparison.OrdinalIgnoreCase))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -1038,59 +1058,13 @@ namespace WebSocketSharp
|
|||||||
/// A <see cref="string"/> that represents the description of the HTTP status code.
|
/// A <see cref="string"/> that represents the description of the HTTP status code.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="code">
|
/// <param name="code">
|
||||||
/// One of <see cref="HttpStatusCode"/> enum values, indicates the HTTP status codes.
|
/// One of <see cref="HttpStatusCode"/> enum values, indicates the HTTP status code.
|
||||||
/// </param>
|
/// </param>
|
||||||
public static string GetDescription (this HttpStatusCode code)
|
public static string GetDescription (this HttpStatusCode code)
|
||||||
{
|
{
|
||||||
return ((int) code).GetStatusDescription ();
|
return ((int) code).GetStatusDescription ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the name from the specified <see cref="string"/> that contains a pair of name and
|
|
||||||
/// value separated by a separator string.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// A <see cref="string"/> that represents the name if any; otherwise, <c>null</c>.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="nameAndValue">
|
|
||||||
/// A <see cref="string"/> that contains a pair of name and value separated by a separator
|
|
||||||
/// string.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="separator">
|
|
||||||
/// A <see cref="string"/> that represents a separator string.
|
|
||||||
/// </param>
|
|
||||||
public static string GetName (this string nameAndValue, string separator)
|
|
||||||
{
|
|
||||||
return (nameAndValue != null && nameAndValue.Length > 0) &&
|
|
||||||
(separator != null && separator.Length > 0)
|
|
||||||
? nameAndValue.GetNameInternal (separator)
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the name and value from the specified <see cref="string"/> that contains a pair of
|
|
||||||
/// name and value separated by a separator string.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// A <c>KeyValuePair<string, string></c> that represents the name and value if any.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="nameAndValue">
|
|
||||||
/// A <see cref="string"/> that contains a pair of name and value separated by a separator
|
|
||||||
/// string.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="separator">
|
|
||||||
/// A <see cref="string"/> that represents a separator string.
|
|
||||||
/// </param>
|
|
||||||
public static KeyValuePair<string, string> GetNameAndValue (
|
|
||||||
this string nameAndValue, string separator)
|
|
||||||
{
|
|
||||||
var name = nameAndValue.GetName (separator);
|
|
||||||
var value = nameAndValue.GetValue (separator);
|
|
||||||
return name != null
|
|
||||||
? new KeyValuePair<string, string> (name, value)
|
|
||||||
: new KeyValuePair<string, string> (null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the description of the specified HTTP status <paramref name="code"/>.
|
/// Gets the description of the specified HTTP status <paramref name="code"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -1154,34 +1128,12 @@ namespace WebSocketSharp
|
|||||||
return String.Empty;
|
return String.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the value from the specified <see cref="string"/> that contains a pair of name and
|
|
||||||
/// value separated by a separator string.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// A <see cref="string"/> that represents the value if any; otherwise, <c>null</c>.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="nameAndValue">
|
|
||||||
/// A <see cref="string"/> that contains a pair of name and value separated by a separator
|
|
||||||
/// string.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="separator">
|
|
||||||
/// A <see cref="string"/> that represents a separator string.
|
|
||||||
/// </param>
|
|
||||||
public static string GetValue (this string nameAndValue, string separator)
|
|
||||||
{
|
|
||||||
return (nameAndValue != null && nameAndValue.Length > 0) &&
|
|
||||||
(separator != null && separator.Length > 0)
|
|
||||||
? nameAndValue.GetValueInternal (separator)
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether the specified <see cref="ushort"/> is in the allowable range of
|
/// Determines whether the specified <see cref="ushort"/> is in the allowable range of
|
||||||
/// the WebSocket close status code.
|
/// the WebSocket close status code.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Not allowable ranges are the followings.
|
/// Not allowable ranges are the following:
|
||||||
/// <list type="bullet">
|
/// <list type="bullet">
|
||||||
/// <item>
|
/// <item>
|
||||||
/// <term>
|
/// <term>
|
||||||
@ -1230,12 +1182,11 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether the specified <see cref="ByteOrder"/> is host
|
/// Determines whether the specified <see cref="ByteOrder"/> is host (this computer
|
||||||
/// (this computer architecture) byte order.
|
/// architecture) byte order.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="order"/> is host byte order;
|
/// <c>true</c> if <paramref name="order"/> is host byte order; otherwise, <c>false</c>.
|
||||||
/// otherwise, <c>false</c>.
|
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="order">
|
/// <param name="order">
|
||||||
/// One of the <see cref="ByteOrder"/> enum values, to test.
|
/// One of the <see cref="ByteOrder"/> enum values, to test.
|
||||||
@ -1364,7 +1315,7 @@ namespace WebSocketSharp
|
|||||||
throw new ArgumentNullException ("protocol");
|
throw new ArgumentNullException ("protocol");
|
||||||
|
|
||||||
if (protocol.Length == 0)
|
if (protocol.Length == 0)
|
||||||
throw new ArgumentException ("Must not be empty.", "protocol");
|
throw new ArgumentException ("Empty string.", "protocol");
|
||||||
|
|
||||||
return request.Headers.Contains ("Upgrade", protocol) &&
|
return request.Headers.Contains ("Upgrade", protocol) &&
|
||||||
request.Headers.Contains ("Connection", "Upgrade");
|
request.Headers.Contains ("Connection", "Upgrade");
|
||||||
@ -1429,7 +1380,7 @@ namespace WebSocketSharp
|
|||||||
if (startIndex == 0 && array.Length == length)
|
if (startIndex == 0 && array.Length == length)
|
||||||
return array;
|
return array;
|
||||||
|
|
||||||
T [] subArray = new T [length];
|
var subArray = new T[length];
|
||||||
Array.Copy (array, startIndex, subArray, 0, length);
|
Array.Copy (array, startIndex, subArray, 0, length);
|
||||||
|
|
||||||
return subArray;
|
return subArray;
|
||||||
@ -1600,28 +1551,28 @@ namespace WebSocketSharp
|
|||||||
return default (T);
|
return default (T);
|
||||||
|
|
||||||
var type = typeof (T);
|
var type = typeof (T);
|
||||||
var buffer = src.ToHostOrder (srcOrder);
|
var buff = src.ToHostOrder (srcOrder);
|
||||||
|
|
||||||
return type == typeof (Boolean)
|
return type == typeof (Boolean)
|
||||||
? (T)(object) BitConverter.ToBoolean (buffer, 0)
|
? (T)(object) BitConverter.ToBoolean (buff, 0)
|
||||||
: type == typeof (Char)
|
: type == typeof (Char)
|
||||||
? (T)(object) BitConverter.ToChar (buffer, 0)
|
? (T)(object) BitConverter.ToChar (buff, 0)
|
||||||
: type == typeof (Double)
|
: type == typeof (Double)
|
||||||
? (T)(object) BitConverter.ToDouble (buffer, 0)
|
? (T)(object) BitConverter.ToDouble (buff, 0)
|
||||||
: type == typeof (Int16)
|
: type == typeof (Int16)
|
||||||
? (T)(object) BitConverter.ToInt16 (buffer, 0)
|
? (T)(object) BitConverter.ToInt16 (buff, 0)
|
||||||
: type == typeof (Int32)
|
: type == typeof (Int32)
|
||||||
? (T)(object) BitConverter.ToInt32 (buffer, 0)
|
? (T)(object) BitConverter.ToInt32 (buff, 0)
|
||||||
: type == typeof (Int64)
|
: type == typeof (Int64)
|
||||||
? (T)(object) BitConverter.ToInt64 (buffer, 0)
|
? (T)(object) BitConverter.ToInt64 (buff, 0)
|
||||||
: type == typeof (Single)
|
: type == typeof (Single)
|
||||||
? (T)(object) BitConverter.ToSingle (buffer, 0)
|
? (T)(object) BitConverter.ToSingle (buff, 0)
|
||||||
: type == typeof (UInt16)
|
: type == typeof (UInt16)
|
||||||
? (T)(object) BitConverter.ToUInt16 (buffer, 0)
|
? (T)(object) BitConverter.ToUInt16 (buff, 0)
|
||||||
: type == typeof (UInt32)
|
: type == typeof (UInt32)
|
||||||
? (T)(object) BitConverter.ToUInt32 (buffer, 0)
|
? (T)(object) BitConverter.ToUInt32 (buff, 0)
|
||||||
: type == typeof (UInt64)
|
: type == typeof (UInt64)
|
||||||
? (T)(object) BitConverter.ToUInt64 (buffer, 0)
|
? (T)(object) BitConverter.ToUInt64 (buff, 0)
|
||||||
: default (T);
|
: default (T);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1733,11 +1684,11 @@ namespace WebSocketSharp
|
|||||||
if (separator == null)
|
if (separator == null)
|
||||||
separator = String.Empty;
|
separator = String.Empty;
|
||||||
|
|
||||||
var buffer = new StringBuilder (64);
|
var buff = new StringBuilder (64);
|
||||||
(len - 1).Times (i => buffer.AppendFormat ("{0}{1}", array [i].ToString (), separator));
|
(len - 1).Times (i => buff.AppendFormat ("{0}{1}", array[i].ToString (), separator));
|
||||||
|
|
||||||
buffer.Append (array [len - 1].ToString ());
|
buff.Append (array[len - 1].ToString ());
|
||||||
return buffer.ToString ();
|
return buff.ToString ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -1812,12 +1763,13 @@ namespace WebSocketSharp
|
|||||||
if (response == null)
|
if (response == null)
|
||||||
throw new ArgumentNullException ("response");
|
throw new ArgumentNullException ("response");
|
||||||
|
|
||||||
if (content == null || content.Length == 0)
|
var len = 0;
|
||||||
|
if (content == null || (len = content.Length) == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var output = response.OutputStream;
|
var output = response.OutputStream;
|
||||||
response.ContentLength64 = content.Length;
|
response.ContentLength64 = len;
|
||||||
output.Write (content, 0, content.Length);
|
output.Write (content, 0, len);
|
||||||
output.Close ();
|
output.Close ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,20 +227,20 @@ namespace WebSocketSharp.Net
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (pair.StartsWith ("$version", StringComparison.InvariantCultureIgnoreCase)) {
|
if (pair.StartsWith ("$version", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
version = Int32.Parse (pair.GetValueInternal ("=").Trim ('"'));
|
version = Int32.Parse (pair.GetValue ('=', true));
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("$path", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("$path", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.Path = pair.GetValueInternal ("=");
|
cookie.Path = pair.GetValue ('=');
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("$domain", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("$domain", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.Domain = pair.GetValueInternal ("=");
|
cookie.Domain = pair.GetValue ('=');
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("$port", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("$port", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
var port = pair.Equals ("$port", StringComparison.InvariantCultureIgnoreCase)
|
var port = pair.Equals ("$port", StringComparison.InvariantCultureIgnoreCase)
|
||||||
? "\"\""
|
? "\"\""
|
||||||
: pair.GetValueInternal ("=");
|
: pair.GetValue ('=');
|
||||||
|
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.Port = port;
|
cookie.Port = port;
|
||||||
@ -289,10 +289,10 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
if (pair.StartsWith ("version", StringComparison.InvariantCultureIgnoreCase)) {
|
if (pair.StartsWith ("version", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.Version = Int32.Parse (pair.GetValueInternal ("=").Trim ('"'));
|
cookie.Version = Int32.Parse (pair.GetValue ('=', true));
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("expires", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("expires", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
var buffer = new StringBuilder (pair.GetValueInternal ("="), 32);
|
var buffer = new StringBuilder (pair.GetValue ('='), 32);
|
||||||
if (i < pairs.Length - 1)
|
if (i < pairs.Length - 1)
|
||||||
buffer.AppendFormat (", {0}", pairs [++i].Trim ());
|
buffer.AppendFormat (", {0}", pairs [++i].Trim ());
|
||||||
|
|
||||||
@ -309,34 +309,34 @@ namespace WebSocketSharp.Net
|
|||||||
cookie.Expires = expires.ToLocalTime ();
|
cookie.Expires = expires.ToLocalTime ();
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("max-age", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("max-age", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
var max = Int32.Parse (pair.GetValueInternal ("=").Trim ('"'));
|
var max = Int32.Parse (pair.GetValue ('=', true));
|
||||||
var expires = DateTime.Now.AddSeconds ((double) max);
|
var expires = DateTime.Now.AddSeconds ((double) max);
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.Expires = expires;
|
cookie.Expires = expires;
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("path", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("path", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.Path = pair.GetValueInternal ("=");
|
cookie.Path = pair.GetValue ('=');
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("domain", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("domain", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.Domain = pair.GetValueInternal ("=");
|
cookie.Domain = pair.GetValue ('=');
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("port", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("port", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
var port = pair.Equals ("port", StringComparison.InvariantCultureIgnoreCase)
|
var port = pair.Equals ("port", StringComparison.InvariantCultureIgnoreCase)
|
||||||
? "\"\""
|
? "\"\""
|
||||||
: pair.GetValueInternal ("=");
|
: pair.GetValue ('=');
|
||||||
|
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.Port = port;
|
cookie.Port = port;
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("comment", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("comment", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.Comment = pair.GetValueInternal ("=").UrlDecode ();
|
cookie.Comment = pair.GetValue ('=').UrlDecode ();
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("commenturl", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("commenturl", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
cookie.CommentUri = pair.GetValueInternal ("=").Trim ('"').ToUri ();
|
cookie.CommentUri = pair.GetValue ('=', true).ToUri ();
|
||||||
}
|
}
|
||||||
else if (pair.StartsWith ("discard", StringComparison.InvariantCultureIgnoreCase)) {
|
else if (pair.StartsWith ("discard", StringComparison.InvariantCultureIgnoreCase)) {
|
||||||
if (cookie != null)
|
if (cookie != null)
|
||||||
|
@ -522,7 +522,7 @@ namespace WebSocketSharp.Net
|
|||||||
foreach (var p in parts) {
|
foreach (var p in parts) {
|
||||||
var part = p.Trim ();
|
var part = p.Trim ();
|
||||||
if (part.StartsWith ("charset", StringComparison.OrdinalIgnoreCase)) {
|
if (part.StartsWith ("charset", StringComparison.OrdinalIgnoreCase)) {
|
||||||
var charset = part.GetValue ("=", true);
|
var charset = part.GetValue ('=', true);
|
||||||
if (charset != null && charset.Length > 0) {
|
if (charset != null && charset.Length > 0) {
|
||||||
try {
|
try {
|
||||||
_contentEncoding = Encoding.GetEncoding (charset);
|
_contentEncoding = Encoding.GetEncoding (charset);
|
||||||
|
Loading…
Reference in New Issue
Block a user