forked from kerryjiang/WebSocket4Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
199 lines (159 loc) · 5.92 KB
/
Copy pathExtensions.cs
File metadata and controls
199 lines (159 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
using System.Reflection;
using SuperSocket.ClientEngine;
#if !NETFX_CORE
using System.Security.Cryptography;
#else
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
#endif
namespace WebSocket4Net
{
public static partial class Extensions
{
private readonly static char[] m_CrCf = new char[] { '\r', '\n' };
public static void AppendFormatWithCrCf(this StringBuilder builder, string format, object arg)
{
builder.AppendFormat(format, arg);
builder.Append(m_CrCf);
}
public static void AppendFormatWithCrCf(this StringBuilder builder, string format, params object[] args)
{
builder.AppendFormat(format, args);
builder.Append(m_CrCf);
}
public static void AppendWithCrCf(this StringBuilder builder, string content)
{
builder.Append(content);
builder.Append(m_CrCf);
}
public static void AppendWithCrCf(this StringBuilder builder)
{
builder.Append(m_CrCf);
}
private const string m_Tab = "\t";
private const char m_Colon = ':';
private const string m_Space = " ";
private const string m_ValueSeparator = ", ";
public static bool ParseMimeHeader(this string source, IDictionary<string, object> valueContainer, out string verbLine)
{
verbLine = string.Empty;
var items = valueContainer;
string line;
string prevKey = string.Empty;
var reader = new StringReader(source);
while (!string.IsNullOrEmpty(line = reader.ReadLine()))
{
if (string.IsNullOrEmpty(verbLine))
{
verbLine = line;
continue;
}
object currentValue;
if (line.StartsWith(m_Tab) && !string.IsNullOrEmpty(prevKey))
{
if (!items.TryGetValue(prevKey, out currentValue))
return false;
items[prevKey] = currentValue + line.Trim();
continue;
}
int pos = line.IndexOf(m_Colon);
if (pos < 0)
continue;
string key = line.Substring(0, pos);
if (!string.IsNullOrEmpty(key))
key = key.Trim();
string value = line.Substring(pos + 1);
if (!string.IsNullOrEmpty(value) && value.StartsWith(m_Space) && value.Length > 1)
value = value.Substring(1);
if (string.IsNullOrEmpty(key))
continue;
if (!items.TryGetValue(key, out currentValue))
{
items.Add(key, value);
}
else
{
items[key] = currentValue + m_ValueSeparator + value;
}
prevKey = key;
}
return true;
}
public static TValue GetValue<TValue>(this IDictionary<string, object> valueContainer, string name)
{
var defaultValue = default(TValue);
return GetValue(valueContainer, name, defaultValue);
}
public static TValue GetValue<TValue>(this IDictionary<string, object> valueContainer, string name, TValue defaultValue)
{
object value;
if (!valueContainer.TryGetValue(name, out value))
return defaultValue;
return (TValue)value;
}
private static Type[] m_SimpleTypes = new Type[] {
typeof(String),
typeof(Decimal),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
typeof(Guid)
};
internal static bool IsSimpleType(this Type type)
{
#if NETFX_CORE || NETCORE
var typeInfo = type.GetTypeInfo();
return
typeInfo.IsValueType ||
typeInfo.IsPrimitive ||
m_SimpleTypes.Contains(type) ||
Convert.GetTypeCode(type) != TypeCode.Object;
#else
return
type.IsValueType ||
type.IsPrimitive ||
m_SimpleTypes.Contains(type) ||
Convert.GetTypeCode(type) != TypeCode.Object;
#endif
}
public static string GetOrigin(this Uri uri)
{
#if NETFX_CORE || NETCORE
return uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
#else
return uri.GetLeftPart(UriPartial.Authority);
#endif
}
public static byte[] ComputeMD5Hash(this byte[] source)
{
#if NETFX_CORE
var algProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
var hash = algProv.CreateHash();
hash.Append(CryptographicBuffer.CreateFromByteArray(source));
byte[] result;
CryptographicBuffer.CopyToByteArray(hash.GetValueAndReset(), out result);
return result;
#else
return MD5.Create().ComputeHash(source);
#endif
}
public static string CalculateChallenge(this string source)
{
#if NETFX_CORE
var algProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
var hash = algProv.CreateHash();
hash.Append(CryptographicBuffer.ConvertStringToBinary(source, BinaryStringEncoding.Utf8));
return CryptographicBuffer.EncodeToBase64String(hash.GetValueAndReset());
#elif !SILVERLIGHT
return Convert.ToBase64String(SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(source)));
#else
return Convert.ToBase64String(SHA1.Create().ComputeHash(ASCIIEncoding.Instance.GetBytes(source)));
#endif
}
}
}