diff --git a/websocket-sharp/Net/HttpListenerRequest.cs b/websocket-sharp/Net/HttpListenerRequest.cs index cd08dc61..f461201f 100644 --- a/websocket-sharp/Net/HttpListenerRequest.cs +++ b/websocket-sharp/Net/HttpListenerRequest.cs @@ -337,12 +337,12 @@ namespace WebSocketSharp.Net /// Gets the query string included in the request. /// /// - /// A that contains the query string parameters - /// included in the request. + /// A that contains the query string parameters. /// public NameValueCollection QueryString { get { - return _queryString ?? (_queryString = HttpUtility.ParseQueryStringSimply (_url.Query)); + return _queryString ?? + (_queryString = HttpUtility.ParseQueryStringInternally (_url.Query, Encoding.UTF8)); } } diff --git a/websocket-sharp/Net/HttpUtility.cs b/websocket-sharp/Net/HttpUtility.cs index 527321a9..f6c76d03 100644 --- a/websocket-sharp/Net/HttpUtility.cs +++ b/websocket-sharp/Net/HttpUtility.cs @@ -675,57 +675,33 @@ namespace WebSocketSharp.Net return res; } - internal static void ParseQueryString ( - string query, Encoding encoding, NameValueCollection result) + internal static NameValueCollection ParseQueryStringInternally (string query, Encoding encoding) { - if (query.Length == 0) - return; + int len; + if (query == null || (len = query.Length) == 0 || (len == 1 && query [0] == '?')) + return new NameValueCollection (1); - var decoded = HtmlDecode (query); - var decodedLength = decoded.Length; - var namePos = 0; - var first = true; - while (namePos <= decodedLength) { - var valuePos = -1; - var valueEnd = -1; - for (int q = namePos; q < decodedLength; q++) { - if (valuePos == -1 && decoded [q] == '=') { - valuePos = q + 1; - } - else if (decoded [q] == '&') { - valueEnd = q; - break; - } - } + if (query [0] == '?') + query = query.Substring (1); - if (first) { - first = false; - if (decoded [namePos] == '?') - namePos++; - } + var res = new QueryStringCollection (); + var components = query.Split ('&'); + foreach (var component in components) { + var i = component.IndexOf ('='); + if (i > -1) { + var name = UrlDecode (component.Substring (0, i), encoding); + var val = component.Length > i + 1 + ? UrlDecode (component.Substring (i + 1), encoding) + : String.Empty; - string name; - if (valuePos == -1) { - name = null; - valuePos = namePos; + res.Add (name, val); } else { - name = UrlDecode (decoded.Substring (namePos, valuePos - namePos - 1), encoding); + res.Add (null, UrlDecode (component, encoding)); } - - if (valueEnd < 0) { - namePos = -1; - valueEnd = decoded.Length; - } - else { - namePos = valueEnd + 1; - } - - var value = UrlDecode (decoded.Substring (valuePos, valueEnd - valuePos), encoding); - result.Add (name, value); - if (namePos == -1) - break; } + + return res; } internal static string UrlDecodeInternally ( @@ -1074,7 +1050,10 @@ namespace WebSocketSharp.Net public static NameValueCollection ParseQueryString (string query) { - return ParseQueryString (query, Encoding.UTF8); + if (query == null) + throw new ArgumentNullException ("query"); + + return ParseQueryStringInternally (query, Encoding.UTF8); } public static NameValueCollection ParseQueryString (string query, Encoding encoding) @@ -1082,50 +1061,10 @@ namespace WebSocketSharp.Net if (query == null) throw new ArgumentNullException ("query"); - var len = query.Length; - if (len == 0 || (len == 1 && query [0] == '?')) - return new NameValueCollection (1); - - if (query [0] == '?') - query = query.Substring (1); - if (encoding == null) - encoding = Encoding.UTF8; + throw new ArgumentNullException ("encoding"); - var res = new QueryStringCollection (); - ParseQueryString (query, encoding, res); - - return res; - } - - // Used by HttpListenerRequest and TcpListenerWebSocketContext. - public static NameValueCollection ParseQueryStringSimply (string query) - { - int len; - if (query == null || (len = query.Length) == 0 || (len == 1 && query [0] == '?')) - return new NameValueCollection (1); - - if (query [0] == '?') - query = query.Substring (1); - - var res = new QueryStringCollection (); - var components = query.Split ('&'); - foreach (var component in components) { - var i = component.IndexOf ('='); - if (i > -1) { - var name = UrlDecode (component.Substring (0, i), Encoding.UTF8); - var val = component.Length > i + 1 - ? UrlDecode (component.Substring (i + 1), Encoding.UTF8) - : String.Empty; - - res.Add (name, val); - } - else { - res.Add (null, UrlDecode (component, Encoding.UTF8)); - } - } - - return res; + return ParseQueryStringInternally (query, encoding); } public static string UrlDecode (string s) diff --git a/websocket-sharp/Net/WebSockets/HttpListenerWebSocketContext.cs b/websocket-sharp/Net/WebSockets/HttpListenerWebSocketContext.cs index 76892ccc..83c32e2f 100644 --- a/websocket-sharp/Net/WebSockets/HttpListenerWebSocketContext.cs +++ b/websocket-sharp/Net/WebSockets/HttpListenerWebSocketContext.cs @@ -168,10 +168,10 @@ namespace WebSocketSharp.Net.WebSockets } /// - /// Gets the query string variables included in the request. + /// Gets the query string included in the request. /// /// - /// A that contains the query string variables. + /// A that contains the query string parameters. /// public override NameValueCollection QueryString { get { diff --git a/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs b/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs index 90787d57..ac7c0293 100644 --- a/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs +++ b/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs @@ -32,6 +32,7 @@ using System.Collections.Specialized; using System.Net.Sockets; using System.Security.Cryptography.X509Certificates; using System.Security.Principal; +using System.Text; namespace WebSocketSharp.Net.WebSockets { @@ -183,16 +184,16 @@ namespace WebSocketSharp.Net.WebSockets } /// - /// Gets the query string variables included in the request. + /// Gets the query string included in the request. /// /// - /// A that contains the query string variables. + /// A that contains the query string parameters. /// public override NameValueCollection QueryString { get { return _queryString ?? - (_queryString = - HttpUtility.ParseQueryStringSimply (_uri != null ? _uri.Query : null)); + (_queryString = HttpUtility.ParseQueryStringInternally ( + _uri != null ? _uri.Query : null, Encoding.UTF8)); } } diff --git a/websocket-sharp/Net/WebSockets/WebSocketContext.cs b/websocket-sharp/Net/WebSockets/WebSocketContext.cs index 35803982..c6424d8b 100644 --- a/websocket-sharp/Net/WebSockets/WebSocketContext.cs +++ b/websocket-sharp/Net/WebSockets/WebSocketContext.cs @@ -119,10 +119,10 @@ namespace WebSocketSharp.Net.WebSockets public abstract string Origin { get; } /// - /// Gets the query string variables included in the request. + /// Gets the query string included in the request. /// /// - /// A that contains the query string variables. + /// A that contains the query string parameters. /// public abstract NameValueCollection QueryString { get; }