From 74b8bb392ce7314f23fe7f9d88dce3c15bdf72f9 Mon Sep 17 00:00:00 2001 From: sta Date: Sun, 25 May 2014 16:29:43 +0900 Subject: [PATCH] Fix for request url in HttpListenerRequest.cs --- websocket-sharp/Net/HttpListenerRequest.cs | 53 ++++++++++------------ 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/websocket-sharp/Net/HttpListenerRequest.cs b/websocket-sharp/Net/HttpListenerRequest.cs index f9b6cd71..99cd1fca 100644 --- a/websocket-sharp/Net/HttpListenerRequest.cs +++ b/websocket-sharp/Net/HttpListenerRequest.cs @@ -579,43 +579,38 @@ namespace WebSocketSharp.Net host = UserHostAddress; string path = null; - Uri rawUri; - if (_rawUrl == "*") { - } - else if (_rawUrl.StartsWith ("/")) { + if (_rawUrl.StartsWith ("/")) { path = HttpUtility.UrlDecode (_rawUrl); } - else if (_rawUrl.MaybeUri () && Uri.TryCreate (_rawUrl, UriKind.Absolute, out rawUri)) { - host = rawUri.Host; - path = rawUri.PathAndQuery; + else if (_rawUrl.MaybeUri ()) { + if (!Uri.TryCreate (_rawUrl, UriKind.Absolute, out _url) || + !(_url.Scheme.StartsWith ("http") || _url.Scheme.StartsWith ("ws"))) { + _context.ErrorMessage = "Invalid request url: " + _rawUrl; + return; + } + } + else if (_rawUrl == "*") { } else { - var authority = HttpUtility.UrlDecode (_rawUrl); - var slash = authority.IndexOf ('/'); - if (slash > -1) { - host = authority.Substring (0, slash); - path = authority.Substring (slash); - } - else { - host = authority; - } + // As authority form + host = HttpUtility.UrlDecode (_rawUrl); } - var colon = host.IndexOf (':'); - if (colon > -1) - host = host.Substring (0, colon); + if (_url == null) { + var scheme = IsWebSocketRequest ? "ws" : "http"; + var secure = IsSecureConnection; + if (secure) + scheme += "s"; - var scheme = IsWebSocketRequest ? "ws" : "http"; - var url = String.Format ( - "{0}://{1}:{2}{3}", - IsSecureConnection ? scheme + "s" : scheme, - host, - LocalEndPoint.Port, - path); + var colon = host.IndexOf (':'); + if (colon == -1) + host = String.Format ("{0}:{1}", host, secure ? 443 : 80); - if (!Uri.TryCreate (url, UriKind.Absolute, out _url)) { - _context.ErrorMessage = "Invalid request url: " + url; - return; + var url = String.Format ("{0}://{1}{2}", scheme, host, path); + if (!Uri.TryCreate (url, UriKind.Absolute, out _url)) { + _context.ErrorMessage = "Invalid request url: " + url; + return; + } } _queryString = createQueryString (_url.Query);