diff --git a/websocket-sharp/Net/HttpListenerRequest.cs b/websocket-sharp/Net/HttpListenerRequest.cs index 41521947..fafa4729 100644 --- a/websocket-sharp/Net/HttpListenerRequest.cs +++ b/websocket-sharp/Net/HttpListenerRequest.cs @@ -544,40 +544,9 @@ namespace WebSocketSharp.Net if (noHost) host = UserHostAddress; - string scheme = null; - string path = null; - if (_uri.StartsWith ("/")) { - path = _uri; - } - else if (_uri.MaybeUri ()) { - Uri uri; - if (!Uri.TryCreate (_uri, UriKind.Absolute, out uri) || - !(uri.Scheme.StartsWith ("http") || uri.Scheme.StartsWith ("ws"))) { - _context.ErrorMessage = "Invalid request url: " + _uri; - return; - } - - scheme = uri.Scheme; - host = uri.Authority; - path = uri.PathAndQuery; - } - else if (_uri == "*") { - } - else { - // As authority form - host = _uri; - } - - if (scheme == null) - scheme = (IsWebSocketRequest ? "ws" : "http") + (IsSecureConnection ? "s" : String.Empty); - - var colon = host.IndexOf (':'); - if (colon == -1) - host = String.Format ("{0}:{1}", host, scheme == "http" || scheme == "ws" ? 80 : 443); - - var url = String.Format ("{0}://{1}{2}", scheme, host, path); - if (!Uri.TryCreate (url, UriKind.Absolute, out _url)) { - _context.ErrorMessage = "Invalid request url: " + url; + _url = HttpUtility.CreateRequestUrl (_uri, host, IsWebSocketRequest, IsSecureConnection); + if (_url == null) { + _context.ErrorMessage = "Invalid request url"; return; } diff --git a/websocket-sharp/Net/HttpUtility.cs b/websocket-sharp/Net/HttpUtility.cs index a4b52e4c..527321a9 100644 --- a/websocket-sharp/Net/HttpUtility.cs +++ b/websocket-sharp/Net/HttpUtility.cs @@ -629,6 +629,52 @@ namespace WebSocketSharp.Net return hash (String.Format ("{0}:{1}", secret, data)); } + internal static Uri CreateRequestUrl ( + string requestUri, string host, bool websocketRequest, bool secure) + { + if (requestUri == null || requestUri.Length == 0 || host == null || host.Length == 0) + return null; + + string scheme = null; + string path = null; + if (requestUri.StartsWith ("/")) { + path = requestUri; + } + 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)); + + if (!valid) + return null; + + host = uri.Authority; + path = uri.PathAndQuery; + } + else if (requestUri == "*") { + } + else { + // As authority form + host = requestUri; + } + + if (scheme == null) + scheme = (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); + + var url = String.Format ("{0}://{1}{2}", scheme, host, path); + + Uri res; + if (!Uri.TryCreate (url, UriKind.Absolute, out res)) + return null; + + return res; + } + internal static void ParseQueryString ( string query, Encoding encoding, NameValueCollection result) { diff --git a/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs b/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs index aa0d9364..90787d57 100644 --- a/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs +++ b/websocket-sharp/Net/WebSockets/TcpListenerWebSocketContext.cs @@ -66,7 +66,9 @@ namespace WebSocketSharp.Net.WebSockets _secure = secure; _stream = WebSocketStream.CreateServerStream (client, secure, cert); _request = _stream.ReadHandshake (HandshakeRequest.Parse, 90000); - _uri = createRequestUrl (_request, secure); + _uri = HttpUtility.CreateRequestUrl ( + _request.RequestUri, _request.Headers ["Host"], _request.IsWebSocketRequest, secure); + _websocket = new WebSocket (this, protocol, logger); } @@ -308,56 +310,6 @@ namespace WebSocketSharp.Net.WebSockets #endregion - #region Private Methods - - private static Uri createRequestUrl (HandshakeRequest request, bool secure) - { - var host = request.Headers ["Host"]; - if (host == null || host.Length == 0) - return null; - - string scheme = null; - string path = null; - - var reqUri = request.RequestUri; - if (reqUri.StartsWith ("/")) { - path = reqUri; - } - else if (reqUri.MaybeUri ()) { - Uri uri; - if (!Uri.TryCreate (reqUri, UriKind.Absolute, out uri) || - !uri.Scheme.StartsWith ("ws")) - return null; - - scheme = uri.Scheme; - host = uri.Authority; - path = uri.PathAndQuery; - } - else if (reqUri == "*") { - } - else { - // As authority form - host = reqUri; - } - - if (scheme == null) - scheme = secure ? "wss" : "ws"; - - var colon = host.IndexOf (':'); - if (colon == -1) - host = String.Format ("{0}:{1}", host, scheme == "ws" ? 80 : 443); - - var url = String.Format ("{0}://{1}{2}", scheme, host, path); - - Uri res; - if (!Uri.TryCreate (url, UriKind.Absolute, out res)) - return null; - - return res; - } - - #endregion - #region Internal Methods internal void Close ()