From f48805a2d1097c7f880f051571a4e0349808074f Mon Sep 17 00:00:00 2001 From: sta Date: Wed, 15 Jan 2014 17:36:23 +0900 Subject: [PATCH] Refactored Handshake*.cs --- websocket-sharp/HandshakeBase.cs | 6 ++---- websocket-sharp/HandshakeRequest.cs | 20 +++++++++----------- websocket-sharp/HandshakeResponse.cs | 24 ++++++++++-------------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/websocket-sharp/HandshakeBase.cs b/websocket-sharp/HandshakeBase.cs index 542941b5..f60e235b 100644 --- a/websocket-sharp/HandshakeBase.cs +++ b/websocket-sharp/HandshakeBase.cs @@ -53,8 +53,6 @@ namespace WebSocketSharp protected HandshakeBase () { - _version = HttpVersion.Version11; - _headers = new NameValueCollection (); } #endregion @@ -85,7 +83,7 @@ namespace WebSocketSharp public NameValueCollection Headers { get { - return _headers; + return _headers ?? (_headers = new NameValueCollection ()); } protected set { @@ -95,7 +93,7 @@ namespace WebSocketSharp public Version ProtocolVersion { get { - return _version; + return _version ?? (_version = HttpVersion.Version11); } protected set { diff --git a/websocket-sharp/HandshakeRequest.cs b/websocket-sharp/HandshakeRequest.cs index 875057e2..79ecfee4 100644 --- a/websocket-sharp/HandshakeRequest.cs +++ b/websocket-sharp/HandshakeRequest.cs @@ -57,11 +57,9 @@ namespace WebSocketSharp public HandshakeRequest (string uriString) { - _method = "GET"; _uri = uriString.ToUri (); - _rawUrl = _uri.IsAbsoluteUri - ? _uri.PathAndQuery - : uriString; + _rawUrl = _uri.IsAbsoluteUri ? _uri.PathAndQuery : uriString; + _method = "GET"; var headers = Headers; headers ["User-Agent"] = "websocket-sharp/1.0"; @@ -76,7 +74,7 @@ namespace WebSocketSharp public AuthenticationResponse AuthResponse { get { var response = Headers ["Authorization"]; - return !response.IsNullOrEmpty () + return response != null && response.Length > 0 ? AuthenticationResponse.Parse (response) : null; } @@ -112,6 +110,7 @@ namespace WebSocketSharp get { if (_queryString == null) { _queryString = new NameValueCollection (); + var i = RawUrl.IndexOf ('?'); if (i > 0) { var query = RawUrl.Substring (i + 1); @@ -155,16 +154,15 @@ namespace WebSocketSharp #region Public Methods - public static HandshakeRequest Parse (string [] request) + public static HandshakeRequest Parse (string [] headerParts) { - var requestLine = request [0].Split (' '); + var requestLine = headerParts [0].Split (new char [] { ' ' }, 3); if (requestLine.Length != 3) - throw new ArgumentException ( - "Invalid HTTP Request-Line: " + request [0], "request"); + throw new ArgumentException ("Invalid request line: " + headerParts [0]); var headers = new WebHeaderCollection (); - for (int i = 1; i < request.Length; i++) - headers.SetInternal (request [i], false); + for (int i = 1; i < headerParts.Length; i++) + headers.SetInternal (headerParts [i], false); return new HandshakeRequest { Headers = headers, diff --git a/websocket-sharp/HandshakeResponse.cs b/websocket-sharp/HandshakeResponse.cs index 7bcc13cf..a52a64fc 100644 --- a/websocket-sharp/HandshakeResponse.cs +++ b/websocket-sharp/HandshakeResponse.cs @@ -72,7 +72,7 @@ namespace WebSocketSharp public AuthenticationChallenge AuthChallenge { get { var challenge = Headers ["WWW-Authenticate"]; - return !challenge.IsNullOrEmpty () + return challenge != null && challenge.Length > 0 ? AuthenticationChallenge.Parse (challenge) : null; } @@ -132,25 +132,21 @@ namespace WebSocketSharp return res; } - public static HandshakeResponse Parse (string [] response) + public static HandshakeResponse Parse (string [] headerParts) { - var statusLine = response [0].Split (' '); - if (statusLine.Length < 3) - throw new ArgumentException ("Invalid status line."); - - var reason = new StringBuilder (statusLine [2], 64); - for (int i = 3; i < statusLine.Length; i++) - reason.AppendFormat (" {0}", statusLine [i]); + var statusLine = headerParts [0].Split (new char [] { ' ' }, 3); + if (statusLine.Length != 3) + throw new ArgumentException ("Invalid status line: " + headerParts [0]); var headers = new WebHeaderCollection (); - for (int i = 1; i < response.Length; i++) - headers.SetInternal (response [i], true); + for (int i = 1; i < headerParts.Length; i++) + headers.SetInternal (headerParts [i], true); return new HandshakeResponse { Headers = headers, - Reason = reason.ToString (), - StatusCode = statusLine [1], - ProtocolVersion = new Version (statusLine [0].Substring (5)) + ProtocolVersion = new Version (statusLine [0].Substring (5)), + Reason = statusLine [2], + StatusCode = statusLine [1] }; }