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 ()
{
_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 {

View File

@ -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,

View File

@ -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]
};
}