Refactored HttpUtility.cs
This commit is contained in:
parent
3854f218b4
commit
ca7916a8d8
@ -52,7 +52,7 @@ namespace WebSocketSharp.Net
|
||||
{
|
||||
internal sealed class HttpUtility
|
||||
{
|
||||
#region Private Static Fields
|
||||
#region Private Fields
|
||||
|
||||
private static Dictionary<string, char> _entities;
|
||||
private static char[] _hexChars = "0123456789abcdef".ToCharArray ();
|
||||
@ -60,43 +60,28 @@ namespace WebSocketSharp.Net
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Static Properties
|
||||
|
||||
private static Dictionary<string, char> Entities {
|
||||
get {
|
||||
lock (_sync) {
|
||||
if (_entities == null)
|
||||
initEntities ();
|
||||
|
||||
return _entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static int getChar (byte[] bytes, int offset, int length)
|
||||
{
|
||||
var value = 0;
|
||||
var val = 0;
|
||||
var end = length + offset;
|
||||
for (int i = offset; i < end; i++) {
|
||||
for (var i = offset; i < end; i++) {
|
||||
var current = getInt (bytes[i]);
|
||||
if (current == -1)
|
||||
return -1;
|
||||
|
||||
value = (value << 4) + current;
|
||||
val = (val << 4) + current;
|
||||
}
|
||||
|
||||
return value;
|
||||
return val;
|
||||
}
|
||||
|
||||
private static int getChar (string s, int offset, int length)
|
||||
{
|
||||
var value = 0;
|
||||
var val = 0;
|
||||
var end = length + offset;
|
||||
for (int i = offset; i < end; i++) {
|
||||
for (var i = offset; i < end; i++) {
|
||||
var c = s[i];
|
||||
if (c > 127)
|
||||
return -1;
|
||||
@ -105,10 +90,10 @@ namespace WebSocketSharp.Net
|
||||
if (current == -1)
|
||||
return -1;
|
||||
|
||||
value = (value << 4) + current;
|
||||
val = (val << 4) + current;
|
||||
}
|
||||
|
||||
return value;
|
||||
return val;
|
||||
}
|
||||
|
||||
private static char[] getChars (MemoryStream buffer, Encoding encoding)
|
||||
@ -116,6 +101,16 @@ namespace WebSocketSharp.Net
|
||||
return encoding.GetChars (buffer.GetBuffer (), 0, (int) buffer.Length);
|
||||
}
|
||||
|
||||
private static Dictionary<string, char> getEntities ()
|
||||
{
|
||||
lock (_sync) {
|
||||
if (_entities == null)
|
||||
initEntities ();
|
||||
|
||||
return _entities;
|
||||
}
|
||||
}
|
||||
|
||||
private static int getInt (byte b)
|
||||
{
|
||||
var c = (char) b;
|
||||
@ -454,11 +449,12 @@ namespace WebSocketSharp.Net
|
||||
|
||||
idx = ((int) c) & 0x0F;
|
||||
result.WriteByte ((byte) _hexChars[idx]);
|
||||
|
||||
return;
|
||||
}
|
||||
else {
|
||||
|
||||
result.WriteByte ((byte) c);
|
||||
}
|
||||
}
|
||||
|
||||
private static void urlPathEncodeChar (char c, Stream result)
|
||||
{
|
||||
@ -473,23 +469,30 @@ namespace WebSocketSharp.Net
|
||||
i = ((int) b) & 0x0F;
|
||||
result.WriteByte ((byte) _hexChars[i]);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
else if (c == ' ') {
|
||||
|
||||
if (c == ' ') {
|
||||
result.WriteByte ((byte) '%');
|
||||
result.WriteByte ((byte) '2');
|
||||
result.WriteByte ((byte) '0');
|
||||
|
||||
return;
|
||||
}
|
||||
else {
|
||||
|
||||
result.WriteByte ((byte) c);
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeCharBytes (IList buffer, char c, Encoding encoding)
|
||||
{
|
||||
if (c > 255)
|
||||
if (c > 255) {
|
||||
foreach (var b in encoding.GetBytes (new[] { c }))
|
||||
buffer.Add (b);
|
||||
else
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
buffer.Add ((byte) c);
|
||||
}
|
||||
|
||||
@ -503,7 +506,7 @@ namespace WebSocketSharp.Net
|
||||
if (requestUri == null || requestUri.Length == 0 || host == null || host.Length == 0)
|
||||
return null;
|
||||
|
||||
string scheme = null;
|
||||
string schm = null;
|
||||
string path = null;
|
||||
if (requestUri.StartsWith ("/")) {
|
||||
path = requestUri;
|
||||
@ -511,8 +514,8 @@ namespace WebSocketSharp.Net
|
||||
else if (requestUri.MaybeUri ()) {
|
||||
Uri uri;
|
||||
var valid = Uri.TryCreate (requestUri, UriKind.Absolute, out uri) &&
|
||||
(((scheme = uri.Scheme).StartsWith ("http") && !websocketRequest) ||
|
||||
(scheme.StartsWith ("ws") && websocketRequest));
|
||||
(((schm = uri.Scheme).StartsWith ("http") && !websocketRequest) ||
|
||||
(schm.StartsWith ("ws") && websocketRequest));
|
||||
|
||||
if (!valid)
|
||||
return null;
|
||||
@ -527,14 +530,14 @@ namespace WebSocketSharp.Net
|
||||
host = requestUri;
|
||||
}
|
||||
|
||||
if (scheme == null)
|
||||
scheme = (websocketRequest ? "ws" : "http") + (secure ? "s" : String.Empty);
|
||||
if (schm == null)
|
||||
schm = (websocketRequest ? "ws" : "http") + (secure ? "s" : String.Empty);
|
||||
|
||||
var colon = host.IndexOf (':');
|
||||
if (colon == -1)
|
||||
host = String.Format ("{0}:{1}", host, scheme == "http" || scheme == "ws" ? 80 : 443);
|
||||
host = String.Format ("{0}:{1}", host, schm == "http" || schm == "ws" ? 80 : 443);
|
||||
|
||||
var url = String.Format ("{0}://{1}{2}", scheme, host, path);
|
||||
var url = String.Format ("{0}://{1}{2}", schm, host, path);
|
||||
|
||||
Uri res;
|
||||
if (!Uri.TryCreate (url, UriKind.Absolute, out res))
|
||||
@ -590,10 +593,9 @@ namespace WebSocketSharp.Net
|
||||
var output = new StringBuilder ();
|
||||
using (var acc = new MemoryStream ()) {
|
||||
var end = count + offset;
|
||||
|
||||
int xchar;
|
||||
for (int i = offset; i < end; i++) {
|
||||
for (var i = offset; i < end; i++) {
|
||||
if (bytes[i] == '%' && i + 2 < count && bytes[i + 1] != '%') {
|
||||
int xchar;
|
||||
if (bytes[i + 1] == (byte) 'u' && i + 5 < end) {
|
||||
if (acc.Length > 0) {
|
||||
output.Append (getChars (acc, encoding));
|
||||
@ -621,9 +623,11 @@ namespace WebSocketSharp.Net
|
||||
acc.SetLength (0);
|
||||
}
|
||||
|
||||
if (bytes [i] == '+')
|
||||
if (bytes[i] == '+') {
|
||||
output.Append (' ');
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
output.Append ((char) bytes[i]);
|
||||
}
|
||||
|
||||
@ -638,7 +642,7 @@ namespace WebSocketSharp.Net
|
||||
{
|
||||
using (var res = new MemoryStream ()) {
|
||||
var end = offset + count;
|
||||
for (int i = offset; i < end; i++) {
|
||||
for (var i = offset; i < end; i++) {
|
||||
var c = (char) bytes[i];
|
||||
if (c == '+') {
|
||||
c = ' ';
|
||||
@ -663,7 +667,7 @@ namespace WebSocketSharp.Net
|
||||
{
|
||||
using (var res = new MemoryStream ()) {
|
||||
var end = offset + count;
|
||||
for (int i = offset; i < end; i++)
|
||||
for (var i = offset; i < end; i++)
|
||||
urlEncodeChar ((char) bytes[i], res, false);
|
||||
|
||||
res.Close ();
|
||||
@ -702,8 +706,7 @@ namespace WebSocketSharp.Net
|
||||
? "<"
|
||||
: c == '>'
|
||||
? ">"
|
||||
: c.ToString ()
|
||||
);
|
||||
: c.ToString ());
|
||||
|
||||
return output.ToString ();
|
||||
}
|
||||
@ -738,6 +741,7 @@ namespace WebSocketSharp.Net
|
||||
// 2 -> between '&' and ';' but no '#'
|
||||
// 3 -> '#' found after '&' and getting numbers
|
||||
var state = 0;
|
||||
|
||||
var number = 0;
|
||||
var haveTrailingDigits = false;
|
||||
foreach (var c in s) {
|
||||
@ -788,8 +792,9 @@ namespace WebSocketSharp.Net
|
||||
entity.Append (c);
|
||||
if (c == ';') {
|
||||
var key = entity.ToString ();
|
||||
if (key.Length > 1 && Entities.ContainsKey (key.Substring (1, key.Length - 2)))
|
||||
key = Entities [key.Substring (1, key.Length - 2)].ToString ();
|
||||
var entities = getEntities ();
|
||||
if (key.Length > 1 && entities.ContainsKey (key.Substring (1, key.Length - 2)))
|
||||
key = entities[key.Substring (1, key.Length - 2)].ToString ();
|
||||
|
||||
output.Append (key);
|
||||
state = 0;
|
||||
@ -941,10 +946,7 @@ namespace WebSocketSharp.Net
|
||||
if (query == null)
|
||||
throw new ArgumentNullException ("query");
|
||||
|
||||
if (encoding == null)
|
||||
throw new ArgumentNullException ("encoding");
|
||||
|
||||
return ParseQueryStringInternally (query, encoding);
|
||||
return ParseQueryStringInternally (query, encoding ?? Encoding.UTF8);
|
||||
}
|
||||
|
||||
public static string UrlDecode (string s)
|
||||
@ -960,13 +962,12 @@ namespace WebSocketSharp.Net
|
||||
if (encoding == null)
|
||||
encoding = Encoding.UTF8;
|
||||
|
||||
var len = s.Length;
|
||||
var buff = new List<byte> ();
|
||||
|
||||
int xchar;
|
||||
for (int i = 0; i < len; i++) {
|
||||
var len = s.Length;
|
||||
for (var i = 0; i < len; i++) {
|
||||
var c = s[i];
|
||||
if (c == '%' && i + 2 < len && s[i + 1] != '%') {
|
||||
int xchar;
|
||||
if (s[i + 1] == 'u' && i + 5 < len) {
|
||||
// Unicode hex sequence.
|
||||
xchar = getChar (s, i + 2, 4);
|
||||
@ -989,9 +990,11 @@ namespace WebSocketSharp.Net
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '+')
|
||||
if (c == '+') {
|
||||
writeCharBytes (buff, ' ', encoding);
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
writeCharBytes (buff, c, encoding);
|
||||
}
|
||||
|
||||
@ -1000,17 +1003,12 @@ namespace WebSocketSharp.Net
|
||||
|
||||
public static string UrlDecode (byte[] bytes, Encoding encoding)
|
||||
{
|
||||
if (bytes == null)
|
||||
return null;
|
||||
|
||||
var len = bytes.Length;
|
||||
if (len == 0)
|
||||
return String.Empty;
|
||||
|
||||
if (encoding == null)
|
||||
encoding = Encoding.UTF8;
|
||||
|
||||
return UrlDecodeInternally (bytes, 0, len, encoding);
|
||||
int len;
|
||||
return bytes == null
|
||||
? null
|
||||
: (len = bytes.Length) == 0
|
||||
? String.Empty
|
||||
: UrlDecodeInternally (bytes, 0, len, encoding ?? Encoding.UTF8);
|
||||
}
|
||||
|
||||
public static string UrlDecode (byte[] bytes, int offset, int count, Encoding encoding)
|
||||
@ -1028,10 +1026,7 @@ namespace WebSocketSharp.Net
|
||||
if (count < 0 || count > len - offset)
|
||||
throw new ArgumentOutOfRangeException ("count");
|
||||
|
||||
if (encoding == null)
|
||||
encoding = Encoding.UTF8;
|
||||
|
||||
return UrlDecodeInternally (bytes, offset, count, encoding);
|
||||
return UrlDecodeInternally (bytes, offset, count, encoding ?? Encoding.UTF8);
|
||||
}
|
||||
|
||||
public static byte[] UrlDecodeToBytes (byte[] bytes)
|
||||
@ -1055,10 +1050,7 @@ namespace WebSocketSharp.Net
|
||||
if (s.Length == 0)
|
||||
return new byte[0];
|
||||
|
||||
if (encoding == null)
|
||||
encoding = Encoding.UTF8;
|
||||
|
||||
var bytes = encoding.GetBytes (s);
|
||||
var bytes = (encoding ?? Encoding.UTF8).GetBytes (s);
|
||||
return UrlDecodeToBytesInternally (bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
@ -1156,10 +1148,7 @@ namespace WebSocketSharp.Net
|
||||
if (s.Length == 0)
|
||||
return new byte[0];
|
||||
|
||||
if (encoding == null)
|
||||
encoding = Encoding.UTF8;
|
||||
|
||||
var bytes = encoding.GetBytes (s);
|
||||
var bytes = (encoding ?? Encoding.UTF8).GetBytes (s);
|
||||
return UrlEncodeToBytesInternally (bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user