Refactored HttpUtility.cs

This commit is contained in:
sta 2014-08-17 22:45:43 +09:00
parent 3854f218b4
commit ca7916a8d8

View File

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