Refactored Handshake*.cs

This commit is contained in:
sta 2014-01-15 17:36:23 +09:00
parent 99cf6b1b79
commit f48805a2d1
3 changed files with 21 additions and 29 deletions

View File

@ -53,8 +53,6 @@ namespace WebSocketSharp
protected HandshakeBase () protected HandshakeBase ()
{ {
_version = HttpVersion.Version11;
_headers = new NameValueCollection ();
} }
#endregion #endregion
@ -85,7 +83,7 @@ namespace WebSocketSharp
public NameValueCollection Headers { public NameValueCollection Headers {
get { get {
return _headers; return _headers ?? (_headers = new NameValueCollection ());
} }
protected set { protected set {
@ -95,7 +93,7 @@ namespace WebSocketSharp
public Version ProtocolVersion { public Version ProtocolVersion {
get { get {
return _version; return _version ?? (_version = HttpVersion.Version11);
} }
protected set { protected set {

View File

@ -57,11 +57,9 @@ namespace WebSocketSharp
public HandshakeRequest (string uriString) public HandshakeRequest (string uriString)
{ {
_method = "GET";
_uri = uriString.ToUri (); _uri = uriString.ToUri ();
_rawUrl = _uri.IsAbsoluteUri _rawUrl = _uri.IsAbsoluteUri ? _uri.PathAndQuery : uriString;
? _uri.PathAndQuery _method = "GET";
: uriString;
var headers = Headers; var headers = Headers;
headers ["User-Agent"] = "websocket-sharp/1.0"; headers ["User-Agent"] = "websocket-sharp/1.0";
@ -76,7 +74,7 @@ namespace WebSocketSharp
public AuthenticationResponse AuthResponse { public AuthenticationResponse AuthResponse {
get { get {
var response = Headers ["Authorization"]; var response = Headers ["Authorization"];
return !response.IsNullOrEmpty () return response != null && response.Length > 0
? AuthenticationResponse.Parse (response) ? AuthenticationResponse.Parse (response)
: null; : null;
} }
@ -112,6 +110,7 @@ namespace WebSocketSharp
get { get {
if (_queryString == null) { if (_queryString == null) {
_queryString = new NameValueCollection (); _queryString = new NameValueCollection ();
var i = RawUrl.IndexOf ('?'); var i = RawUrl.IndexOf ('?');
if (i > 0) { if (i > 0) {
var query = RawUrl.Substring (i + 1); var query = RawUrl.Substring (i + 1);
@ -155,16 +154,15 @@ namespace WebSocketSharp
#region Public Methods #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) if (requestLine.Length != 3)
throw new ArgumentException ( throw new ArgumentException ("Invalid request line: " + headerParts [0]);
"Invalid HTTP Request-Line: " + request [0], "request");
var headers = new WebHeaderCollection (); var headers = new WebHeaderCollection ();
for (int i = 1; i < request.Length; i++) for (int i = 1; i < headerParts.Length; i++)
headers.SetInternal (request [i], false); headers.SetInternal (headerParts [i], false);
return new HandshakeRequest { return new HandshakeRequest {
Headers = headers, Headers = headers,

View File

@ -72,7 +72,7 @@ namespace WebSocketSharp
public AuthenticationChallenge AuthChallenge { public AuthenticationChallenge AuthChallenge {
get { get {
var challenge = Headers ["WWW-Authenticate"]; var challenge = Headers ["WWW-Authenticate"];
return !challenge.IsNullOrEmpty () return challenge != null && challenge.Length > 0
? AuthenticationChallenge.Parse (challenge) ? AuthenticationChallenge.Parse (challenge)
: null; : null;
} }
@ -132,25 +132,21 @@ namespace WebSocketSharp
return res; return res;
} }
public static HandshakeResponse Parse (string [] response) public static HandshakeResponse Parse (string [] headerParts)
{ {
var statusLine = response [0].Split (' '); var statusLine = headerParts [0].Split (new char [] { ' ' }, 3);
if (statusLine.Length < 3) if (statusLine.Length != 3)
throw new ArgumentException ("Invalid status line."); throw new ArgumentException ("Invalid status line: " + headerParts [0]);
var reason = new StringBuilder (statusLine [2], 64);
for (int i = 3; i < statusLine.Length; i++)
reason.AppendFormat (" {0}", statusLine [i]);
var headers = new WebHeaderCollection (); var headers = new WebHeaderCollection ();
for (int i = 1; i < response.Length; i++) for (int i = 1; i < headerParts.Length; i++)
headers.SetInternal (response [i], true); headers.SetInternal (headerParts [i], true);
return new HandshakeResponse { return new HandshakeResponse {
Headers = headers, Headers = headers,
Reason = reason.ToString (), ProtocolVersion = new Version (statusLine [0].Substring (5)),
StatusCode = statusLine [1], Reason = statusLine [2],
ProtocolVersion = new Version (statusLine [0].Substring (5)) StatusCode = statusLine [1]
}; };
} }