From df9c8b1e74156a5f7afaee43cd7ee43c59fee6cc Mon Sep 17 00:00:00 2001 From: sta Date: Sun, 5 Jul 2015 16:20:22 +0900 Subject: [PATCH] Refactored a few for HttpRequest.cs --- websocket-sharp/HttpRequest.cs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/websocket-sharp/HttpRequest.cs b/websocket-sharp/HttpRequest.cs index cfced5c8..d8c9abd7 100644 --- a/websocket-sharp/HttpRequest.cs +++ b/websocket-sharp/HttpRequest.cs @@ -26,6 +26,13 @@ */ #endregion +#region Contributors +/* + * Contributors: + * - David Burhans + */ +#endregion + using System; using System.Collections.Specialized; using System.IO; @@ -41,7 +48,7 @@ namespace WebSocketSharp private string _method; private string _uri; private bool _websocketRequest; - private bool _websocketRequestWasSet; + private bool _websocketRequestSet; #endregion @@ -91,14 +98,14 @@ namespace WebSocketSharp public bool IsWebSocketRequest { get { - if (!_websocketRequestWasSet) { + if (!_websocketRequestSet) { var headers = Headers; _websocketRequest = _method == "GET" && ProtocolVersion > HttpVersion.Version10 && headers.Contains ("Upgrade", "websocket") && headers.Contains ("Connection", "Upgrade"); - _websocketRequestWasSet = true; + _websocketRequestSet = true; } return _websocketRequest; @@ -129,16 +136,18 @@ namespace WebSocketSharp internal static HttpRequest CreateWebSocketRequest (Uri uri) { var req = new HttpRequest ("GET", uri.PathAndQuery); - var headers = req.Headers; + + // Only includes a port number in the Host header value if it's non-default. + // See: https://tools.ietf.org/html/rfc6455#page-17 + var port = uri.Port; + var schm = uri.Scheme; + headers["Host"] = (port == 80 && schm == "ws") || (port == 443 && schm == "wss") + ? uri.DnsSafeHost + : uri.Authority; + headers["Upgrade"] = "websocket"; headers["Connection"] = "Upgrade"; - var port = uri.Port; - var scheme = uri.Scheme; - bool isDefaultPort = (port == 80 && scheme == "ws") || (port == 443 && scheme == "wss"); - // only include port in host header if it is non-default - // https://tools.ietf.org/html/rfc6455#page-17 - headers["Host"] = isDefaultPort ? uri.DnsSafeHost : uri.Authority; return req; }