Modified WebHeaderCollection.cs

This commit is contained in:
sta 2013-03-20 19:49:03 +09:00
parent 01d97d02b6
commit 85e8197697
20 changed files with 58 additions and 74 deletions

Binary file not shown.

View File

@ -425,29 +425,23 @@ namespace WebSocketSharp.Net {
void Add (string name, string value, bool ignoreRestricted) void Add (string name, string value, bool ignoreRestricted)
{ {
name = Trim (name);
CheckName (name);
Action <string, string> add; Action <string, string> add;
if (ignoreRestricted) if (ignoreRestricted)
add = AddWithoutCheckingNameAndRestricted; add = AddWithoutCheckingNameAndRestricted;
else else
add = AddWithoutCheckingName; add = AddWithoutCheckingName;
DoWithCheckingState (add, name, value); DoWithCheckingState (add, CheckName (name), value, true);
} }
void AddWithoutCheckingName (string name, string value) void AddWithoutCheckingName (string name, string value)
{ {
CheckRestricted (name); DoWithoutCheckingName (base.Add, name, value);
AddWithoutCheckingNameAndRestricted (name, value);
} }
void AddWithoutCheckingNameAndRestricted (string name, string value) void AddWithoutCheckingNameAndRestricted (string name, string value)
{ {
value = Trim (value); base.Add (name, CheckValue (value));
CheckValue (value);
base.Add (name, value);
} }
static int CheckColonSeparated (string header) static int CheckColonSeparated (string header)
@ -459,13 +453,28 @@ namespace WebSocketSharp.Net {
return i; return i;
} }
static void CheckName (string name) static HttpHeaderType CheckHeaderType (string name)
{ {
HttpHeaderInfo info;
return !TryGetHeaderInfo (name, out info)
? HttpHeaderType.Undefined
: info.IsRequest && !info.IsResponse
? HttpHeaderType.Request
: !info.IsRequest && info.IsResponse
? HttpHeaderType.Response
: HttpHeaderType.Undefined;
}
static string CheckName (string name)
{
name = Trim (name);
if (name.IsEmpty ()) if (name.IsEmpty ())
throw new ArgumentNullException ("name"); throw new ArgumentNullException ("name");
if (!IsHeaderName (name)) if (!IsHeaderName (name))
throw new ArgumentException ("Contains invalid characters.", "name"); throw new ArgumentException ("Contains invalid characters.", "name");
return name;
} }
void CheckRestricted (string name) void CheckRestricted (string name)
@ -486,22 +495,19 @@ namespace WebSocketSharp.Net {
throw new InvalidOperationException ("This collection has already been used to store the response headers."); throw new InvalidOperationException ("This collection has already been used to store the response headers.");
} }
void CheckState (HttpHeaderInfo info) static string CheckValue (string value)
{ {
if (info.IsRequest && !info.IsResponse) value = Trim (value);
CheckState (false); if (value.IsEmpty ())
return value;
if (!info.IsRequest && info.IsResponse)
CheckState (true);
}
static void CheckValue (string value)
{
if (value.Length > 65535) if (value.Length > 65535)
throw new ArgumentOutOfRangeException ("value", "The length must not be greater than 65535."); throw new ArgumentOutOfRangeException ("value", "The length must not be greater than 65535.");
if (!IsHeaderValue (value)) if (!IsHeaderValue (value))
throw new ArgumentException ("Contains invalid characters.", "value"); throw new ArgumentException ("Contains invalid characters.", "value");
return value;
} }
static string Convert (HttpRequestHeader header) static string Convert (HttpRequestHeader header)
@ -530,18 +536,31 @@ namespace WebSocketSharp.Net {
: false; : false;
} }
void DoWithCheckingState (Action <string, string> act, string name, string value) void DoWithCheckingState (
Action <string, string> act, string name, string value, bool setState)
{ {
HttpHeaderInfo info; var type = CheckHeaderType (name);
if (TryGetHeaderInfo (name, out info)) if (type == HttpHeaderType.Request)
{ DoWithCheckingState (act, name, value, false, setState);
CheckState (info); else if (type == HttpHeaderType.Response)
DoWithCheckingState (act, name, value, true, setState);
else
act (name, value); act (name, value);
SetState (info); }
return;
}
void DoWithCheckingState (
Action <string, string> act, string name, string value, bool response, bool setState)
{
CheckState (response);
act (name, value); act (name, value);
if (setState)
SetState (response);
}
void DoWithoutCheckingName (Action <string, string> act, string name, string value)
{
CheckRestricted (name);
act (name, CheckValue (value));
} }
static HttpHeaderInfo GetHeaderInfo (string name) static HttpHeaderInfo GetHeaderInfo (string name)
@ -551,7 +570,7 @@ namespace WebSocketSharp.Net {
select info).FirstOrDefault (); select info).FirstOrDefault ();
} }
void RemoveWithoutCheckingName (string name) void RemoveWithoutCheckingName (string name, string unuse)
{ {
CheckRestricted (name); CheckRestricted (name);
base.Remove (name); base.Remove (name);
@ -565,21 +584,9 @@ namespace WebSocketSharp.Net {
: HttpHeaderType.Request; : HttpHeaderType.Request;
} }
void SetState (HttpHeaderInfo info)
{
if (info.IsRequest && !info.IsResponse)
SetState (false);
if (!info.IsRequest && info.IsResponse)
SetState (true);
}
void SetWithoutCheckingName (string name, string value) void SetWithoutCheckingName (string name, string value)
{ {
CheckRestricted (name); DoWithoutCheckingName (base.Set, name, value);
value = Trim (value);
CheckValue (value);
base.Set (name, value);
} }
static string Trim (string value) static string Trim (string value)
@ -637,9 +644,7 @@ namespace WebSocketSharp.Net {
internal void SetInternal (string name, string value, bool response) internal void SetInternal (string name, string value, bool response)
{ {
value = Trim (value); value = CheckValue (value);
CheckValue (value);
if (IsMultiValue (name, response)) if (IsMultiValue (name, response))
base.Add (name, value); base.Add (name, value);
else else
@ -790,9 +795,7 @@ namespace WebSocketSharp.Net {
/// </exception> /// </exception>
public void Add (HttpRequestHeader header, string value) public void Add (HttpRequestHeader header, string value)
{ {
CheckState (false); DoWithCheckingState (AddWithoutCheckingName, Convert (header), value, false, true);
AddWithoutCheckingName (Convert (header), value);
SetState (false);
} }
/// <summary> /// <summary>
@ -823,9 +826,7 @@ namespace WebSocketSharp.Net {
/// </exception> /// </exception>
public void Add (HttpResponseHeader header, string value) public void Add (HttpResponseHeader header, string value)
{ {
CheckState (true); DoWithCheckingState (AddWithoutCheckingName, Convert (header), value, true, true);
AddWithoutCheckingName (Convert (header), value);
SetState (true);
} }
/// <summary> /// <summary>
@ -1031,9 +1032,7 @@ namespace WebSocketSharp.Net {
/// </exception> /// </exception>
public static bool IsRestricted (string headerName, bool response) public static bool IsRestricted (string headerName, bool response)
{ {
var name = Trim (headerName); return ContainsInRestricted (CheckName (headerName), response);
CheckName (name);
return ContainsInRestricted (name, response);
} }
/// <summary> /// <summary>
@ -1061,8 +1060,7 @@ namespace WebSocketSharp.Net {
/// </exception> /// </exception>
public void Remove (HttpRequestHeader header) public void Remove (HttpRequestHeader header)
{ {
CheckState (false); DoWithCheckingState (RemoveWithoutCheckingName, Convert (header), null, false, false);
RemoveWithoutCheckingName (Convert (header));
} }
/// <summary> /// <summary>
@ -1079,8 +1077,7 @@ namespace WebSocketSharp.Net {
/// </exception> /// </exception>
public void Remove (HttpResponseHeader header) public void Remove (HttpResponseHeader header)
{ {
CheckState (true); DoWithCheckingState (RemoveWithoutCheckingName, Convert (header), null, true, false);
RemoveWithoutCheckingName (Convert (header));
} }
/// <summary> /// <summary>
@ -1108,14 +1105,7 @@ namespace WebSocketSharp.Net {
/// </exception> /// </exception>
public override void Remove (string name) public override void Remove (string name)
{ {
name = Trim (name); DoWithCheckingState (RemoveWithoutCheckingName, CheckName (name), null, false);
CheckName (name);
HttpHeaderInfo info;
if (TryGetHeaderInfo (name, out info))
CheckState (info);
RemoveWithoutCheckingName (name);
} }
/// <summary> /// <summary>
@ -1146,9 +1136,7 @@ namespace WebSocketSharp.Net {
/// </exception> /// </exception>
public void Set (HttpRequestHeader header, string value) public void Set (HttpRequestHeader header, string value)
{ {
CheckState (false); DoWithCheckingState (SetWithoutCheckingName, Convert (header), value, false, true);
SetWithoutCheckingName (Convert (header), value);
SetState (false);
} }
/// <summary> /// <summary>
@ -1179,9 +1167,7 @@ namespace WebSocketSharp.Net {
/// </exception> /// </exception>
public void Set (HttpResponseHeader header, string value) public void Set (HttpResponseHeader header, string value)
{ {
CheckState (true); DoWithCheckingState (SetWithoutCheckingName, Convert (header), value, true, true);
SetWithoutCheckingName (Convert (header), value);
SetState (true);
} }
/// <summary> /// <summary>
@ -1215,9 +1201,7 @@ namespace WebSocketSharp.Net {
/// </exception> /// </exception>
public override void Set (string name, string value) public override void Set (string name, string value)
{ {
name = Trim (name); DoWithCheckingState (SetWithoutCheckingName, CheckName (name), value, true);
CheckName (name);
DoWithCheckingState (SetWithoutCheckingName, name, value);
} }
/// <summary> /// <summary>

Binary file not shown.