Refactored HandshakeBase.cs

This commit is contained in:
sta 2014-07-10 16:55:08 +09:00
parent 43040feb52
commit c5c39cf415
3 changed files with 48 additions and 67 deletions

View File

@ -37,12 +37,17 @@ namespace WebSocketSharp
{ {
#region Private Fields #region Private Fields
private byte[] _entity;
private NameValueCollection _headers; private NameValueCollection _headers;
private Version _version; private Version _version;
#endregion #endregion
#region Internal Fields
internal byte[] EntityBodyData;
#endregion
#region Protected Const Fields #region Protected Const Fields
protected const string CrLf = "\r\n"; protected const string CrLf = "\r\n";
@ -51,22 +56,10 @@ namespace WebSocketSharp
#region Protected Constructors #region Protected Constructors
protected HandshakeBase () protected HandshakeBase (Version version, NameValueCollection headers)
{ {
} _version = version;
_headers = headers;
#endregion
#region Internal Properties
internal byte[] EntityBodyData {
get {
return _entity;
}
set {
_entity = value;
}
} }
#endregion #endregion
@ -75,29 +68,21 @@ namespace WebSocketSharp
public string EntityBody { public string EntityBody {
get { get {
return _entity != null && _entity.LongLength > 0 return EntityBodyData != null && EntityBodyData.LongLength > 0
? getEncoding (_headers["Content-Type"]).GetString (_entity) ? getEncoding (_headers["Content-Type"]).GetString (EntityBodyData)
: String.Empty; : String.Empty;
} }
} }
public NameValueCollection Headers { public NameValueCollection Headers {
get { get {
return _headers ?? (_headers = new NameValueCollection ()); return _headers;
}
protected set {
_headers = value;
} }
} }
public Version ProtocolVersion { public Version ProtocolVersion {
get { get {
return _version ?? (_version = HttpVersion.Version11); return _version;
}
protected set {
_version = value;
} }
} }

View File

@ -27,6 +27,7 @@
#endregion #endregion
using System; using System;
using System.Collections.Specialized;
using System.Text; using System.Text;
using WebSocketSharp.Net; using WebSocketSharp.Net;
@ -45,15 +46,17 @@ namespace WebSocketSharp
#region Private Constructors #region Private Constructors
private HandshakeRequest () private HandshakeRequest (Version version, NameValueCollection headers)
: base (version, headers)
{ {
} }
#endregion #endregion
#region Public Constructors #region Internal Constructors
public HandshakeRequest (string pathAndQuery) internal HandshakeRequest (string pathAndQuery)
: base (HttpVersion.Version11, new NameValueCollection ())
{ {
_uri = pathAndQuery; _uri = pathAndQuery;
_method = "GET"; _method = "GET";
@ -87,10 +90,6 @@ namespace WebSocketSharp
get { get {
return _method; return _method;
} }
private set {
_method = value;
}
} }
public bool IsWebSocketRequest { public bool IsWebSocketRequest {
@ -113,17 +112,13 @@ namespace WebSocketSharp
get { get {
return _uri; return _uri;
} }
private set {
_uri = value;
}
} }
#endregion #endregion
#region Public Methods #region Internal Methods
public static HandshakeRequest Parse (string[] headerParts) internal static HandshakeRequest Parse (string[] headerParts)
{ {
var requestLine = headerParts[0].Split (new[] { ' ' }, 3); var requestLine = headerParts[0].Split (new[] { ' ' }, 3);
if (requestLine.Length != 3) if (requestLine.Length != 3)
@ -133,14 +128,17 @@ namespace WebSocketSharp
for (int i = 1; i < headerParts.Length; i++) for (int i = 1; i < headerParts.Length; i++)
headers.SetInternally (headerParts[i], false); headers.SetInternally (headerParts[i], false);
return new HandshakeRequest { var req = new HandshakeRequest (new Version (requestLine[2].Substring (5)), headers);
Headers = headers, req._method = requestLine[0];
HttpMethod = requestLine[0], req._uri = requestLine[1];
ProtocolVersion = new Version (requestLine[2].Substring (5)),
RequestUri = requestLine[1] return req;
};
} }
#endregion
#region Public Methods
public void SetCookies (CookieCollection cookies) public void SetCookies (CookieCollection cookies)
{ {
if (cookies == null || cookies.Count == 0) if (cookies == null || cookies.Count == 0)

View File

@ -27,6 +27,7 @@
#endregion #endregion
using System; using System;
using System.Collections.Specialized;
using System.Text; using System.Text;
using WebSocketSharp.Net; using WebSocketSharp.Net;
@ -43,15 +44,17 @@ namespace WebSocketSharp
#region Private Constructors #region Private Constructors
private HandshakeResponse () private HandshakeResponse (Version version, NameValueCollection headers)
: base (version, headers)
{ {
} }
#endregion #endregion
#region Public Constructors #region Internal Constructors
public HandshakeResponse (HttpStatusCode code) internal HandshakeResponse (HttpStatusCode code)
: base (HttpVersion.Version11, new NameValueCollection ())
{ {
_code = ((int) code).ToString (); _code = ((int) code).ToString ();
_reason = code.GetDescription (); _reason = code.GetDescription ();
@ -103,27 +106,19 @@ namespace WebSocketSharp
get { get {
return _reason; return _reason;
} }
private set {
_reason = value;
}
} }
public string StatusCode { public string StatusCode {
get { get {
return _code; return _code;
} }
private set {
_code = value;
}
} }
#endregion #endregion
#region Public Methods #region Internal Methods
public static HandshakeResponse CreateCloseResponse (HttpStatusCode code) internal static HandshakeResponse CreateCloseResponse (HttpStatusCode code)
{ {
var res = new HandshakeResponse (code); var res = new HandshakeResponse (code);
res.Headers["Connection"] = "close"; res.Headers["Connection"] = "close";
@ -131,7 +126,7 @@ namespace WebSocketSharp
return res; return res;
} }
public static HandshakeResponse Parse (string[] headerParts) internal static HandshakeResponse Parse (string[] headerParts)
{ {
var statusLine = headerParts[0].Split (new[] { ' ' }, 3); var statusLine = headerParts[0].Split (new[] { ' ' }, 3);
if (statusLine.Length != 3) if (statusLine.Length != 3)
@ -141,14 +136,17 @@ namespace WebSocketSharp
for (int i = 1; i < headerParts.Length; i++) for (int i = 1; i < headerParts.Length; i++)
headers.SetInternally (headerParts[i], true); headers.SetInternally (headerParts[i], true);
return new HandshakeResponse { var res = new HandshakeResponse (new Version (statusLine[0].Substring (5)), headers);
Headers = headers, res._code = statusLine[1];
ProtocolVersion = new Version (statusLine[0].Substring (5)), res._reason = statusLine[2];
Reason = statusLine[2],
StatusCode = statusLine[1] return res;
};
} }
#endregion
#region Public Methods
public void SetCookies (CookieCollection cookies) public void SetCookies (CookieCollection cookies)
{ {
if (cookies == null || cookies.Count == 0) if (cookies == null || cookies.Count == 0)