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

View File

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

View File

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