Refactored a few for WebHeaderCollection.cs

This commit is contained in:
sta 2015-06-14 17:20:30 +09:00
parent 5641184ee6
commit 33684e9fde

View File

@ -61,7 +61,7 @@ namespace WebSocketSharp.Net
#region Private Fields #region Private Fields
private static readonly Dictionary<string, HttpHeaderInfo> _headers; private static readonly Dictionary<string, HttpHeaderInfo> _headers;
private bool _internallyCreated; private bool _internallyUsed;
private HttpHeaderType _state; private HttpHeaderType _state;
#endregion #endregion
@ -451,10 +451,10 @@ namespace WebSocketSharp.Net
#region Internal Constructors #region Internal Constructors
internal WebHeaderCollection (bool internallyCreated) internal WebHeaderCollection (HttpHeaderType state, bool internallyUsed)
{ {
_internallyCreated = internallyCreated; _state = state;
_state = HttpHeaderType.Unspecified; _internallyUsed = internallyUsed;
} }
#endregion #endregion
@ -484,7 +484,7 @@ namespace WebSocketSharp.Net
throw new ArgumentNullException ("serializationInfo"); throw new ArgumentNullException ("serializationInfo");
try { try {
_internallyCreated = serializationInfo.GetBoolean ("InternallyCreated"); _internallyUsed = serializationInfo.GetBoolean ("InternallyUsed");
_state = (HttpHeaderType) serializationInfo.GetInt32 ("State"); _state = (HttpHeaderType) serializationInfo.GetInt32 ("State");
var cnt = serializationInfo.GetInt32 ("Count"); var cnt = serializationInfo.GetInt32 ("Count");
@ -508,7 +508,16 @@ namespace WebSocketSharp.Net
/// </summary> /// </summary>
public WebHeaderCollection () public WebHeaderCollection ()
{ {
_state = HttpHeaderType.Unspecified; }
#endregion
#region Internal Properties
internal HttpHeaderType State {
get {
return _state;
}
} }
#endregion #endregion
@ -546,8 +555,8 @@ namespace WebSocketSharp.Net
/// A <see cref="string"/> that represents the value of the request <paramref name="header"/>. /// A <see cref="string"/> that represents the value of the request <paramref name="header"/>.
/// </value> /// </value>
/// <param name="header"> /// <param name="header">
/// One of the <see cref="HttpRequestHeader"/> enum values, represents the request header /// One of the <see cref="HttpRequestHeader"/> enum values, represents
/// to get or set. /// the request header to get or set.
/// </param> /// </param>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// <para> /// <para>
@ -564,8 +573,8 @@ namespace WebSocketSharp.Net
/// The length of <paramref name="value"/> is greater than 65,535 characters. /// The length of <paramref name="value"/> is greater than 65,535 characters.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the request /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="header"/>. /// the request <paramref name="header"/>.
/// </exception> /// </exception>
public string this[HttpRequestHeader header] { public string this[HttpRequestHeader header] {
get { get {
@ -584,8 +593,8 @@ namespace WebSocketSharp.Net
/// A <see cref="string"/> that represents the value of the response <paramref name="header"/>. /// A <see cref="string"/> that represents the value of the response <paramref name="header"/>.
/// </value> /// </value>
/// <param name="header"> /// <param name="header">
/// One of the <see cref="HttpResponseHeader"/> enum values, represents the response header /// One of the <see cref="HttpResponseHeader"/> enum values, represents
/// to get or set. /// the response header to get or set.
/// </param> /// </param>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// <para> /// <para>
@ -602,8 +611,8 @@ namespace WebSocketSharp.Net
/// The length of <paramref name="value"/> is greater than 65,535 characters. /// The length of <paramref name="value"/> is greater than 65,535 characters.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the response /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="header"/>. /// the response <paramref name="header"/>.
/// </exception> /// </exception>
public string this[HttpResponseHeader header] { public string this[HttpResponseHeader header] {
get { get {
@ -619,8 +628,8 @@ namespace WebSocketSharp.Net
/// Gets a collection of header names in the collection. /// Gets a collection of header names in the collection.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="NameObjectCollectionBase.KeysCollection"/> that contains all header names /// A <see cref="NameObjectCollectionBase.KeysCollection"/> that contains
/// in the collection. /// all header names in the collection.
/// </value> /// </value>
public override NameObjectCollectionBase.KeysCollection Keys { public override NameObjectCollectionBase.KeysCollection Keys {
get { get {
@ -653,11 +662,11 @@ namespace WebSocketSharp.Net
private static int checkColonSeparated (string header) private static int checkColonSeparated (string header)
{ {
var i = header.IndexOf (':'); var idx = header.IndexOf (':');
if (i == -1) if (idx == -1)
throw new ArgumentException ("No colon could be found.", "header"); throw new ArgumentException ("No colon could be found.", "header");
return i; return idx;
} }
private static HttpHeaderType checkHeaderType (string name) private static HttpHeaderType checkHeaderType (string name)
@ -686,7 +695,7 @@ namespace WebSocketSharp.Net
private void checkRestricted (string name) private void checkRestricted (string name)
{ {
if (!_internallyCreated && isRestricted (name, true)) if (!_internallyUsed && isRestricted (name, true))
throw new ArgumentException ("This header must be modified with the appropiate property."); throw new ArgumentException ("This header must be modified with the appropiate property.");
} }
@ -722,9 +731,7 @@ namespace WebSocketSharp.Net
private static string convert (string key) private static string convert (string key)
{ {
HttpHeaderInfo info; HttpHeaderInfo info;
return _headers.TryGetValue (key, out info) return _headers.TryGetValue (key, out info) ? info.Name : String.Empty;
? info.Name
: String.Empty;
} }
private void doWithCheckingState ( private void doWithCheckingState (
@ -854,8 +861,8 @@ namespace WebSocketSharp.Net
#region Protected Methods #region Protected Methods
/// <summary> /// <summary>
/// Adds a header to the collection without checking whether the header is on the restricted /// Adds a header to the collection without checking if the header is on
/// header list. /// the restricted header list.
/// </summary> /// </summary>
/// <param name="headerName"> /// <param name="headerName">
/// A <see cref="string"/> that represents the name of the header to add. /// A <see cref="string"/> that represents the name of the header to add.
@ -922,7 +929,7 @@ namespace WebSocketSharp.Net
/// </exception> /// </exception>
public void Add (string header) public void Add (string header)
{ {
if (header.IsNullOrEmpty ()) if (header == null || header.Length == 0)
throw new ArgumentNullException ("header"); throw new ArgumentNullException ("header");
var pos = checkColonSeparated (header); var pos = checkColonSeparated (header);
@ -930,12 +937,12 @@ namespace WebSocketSharp.Net
} }
/// <summary> /// <summary>
/// Adds the specified request <paramref name="header"/> with the specified /// Adds the specified request <paramref name="header"/> with
/// <paramref name="value"/> to the collection. /// the specified <paramref name="value"/> to the collection.
/// </summary> /// </summary>
/// <param name="header"> /// <param name="header">
/// One of the <see cref="HttpRequestHeader"/> enum values, represents the request header /// One of the <see cref="HttpRequestHeader"/> enum values, represents
/// to add. /// the request header to add.
/// </param> /// </param>
/// <param name="value"> /// <param name="value">
/// A <see cref="string"/> that represents the value of the header to add. /// A <see cref="string"/> that represents the value of the header to add.
@ -955,8 +962,8 @@ namespace WebSocketSharp.Net
/// The length of <paramref name="value"/> is greater than 65,535 characters. /// The length of <paramref name="value"/> is greater than 65,535 characters.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the request /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="header"/>. /// the request <paramref name="header"/>.
/// </exception> /// </exception>
public void Add (HttpRequestHeader header, string value) public void Add (HttpRequestHeader header, string value)
{ {
@ -964,12 +971,12 @@ namespace WebSocketSharp.Net
} }
/// <summary> /// <summary>
/// Adds the specified response <paramref name="header"/> with the specified /// Adds the specified response <paramref name="header"/> with
/// <paramref name="value"/> to the collection. /// the specified <paramref name="value"/> to the collection.
/// </summary> /// </summary>
/// <param name="header"> /// <param name="header">
/// One of the <see cref="HttpResponseHeader"/> enum values, represents the response header /// One of the <see cref="HttpResponseHeader"/> enum values, represents
/// to add. /// the response header to add.
/// </param> /// </param>
/// <param name="value"> /// <param name="value">
/// A <see cref="string"/> that represents the value of the header to add. /// A <see cref="string"/> that represents the value of the header to add.
@ -989,8 +996,8 @@ namespace WebSocketSharp.Net
/// The length of <paramref name="value"/> is greater than 65,535 characters. /// The length of <paramref name="value"/> is greater than 65,535 characters.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the response /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="header"/>. /// the response <paramref name="header"/>.
/// </exception> /// </exception>
public void Add (HttpResponseHeader header, string value) public void Add (HttpResponseHeader header, string value)
{ {
@ -998,8 +1005,8 @@ namespace WebSocketSharp.Net
} }
/// <summary> /// <summary>
/// Adds a header with the specified <paramref name="name"/> and <paramref name="value"/> /// Adds a header with the specified <paramref name="name"/> and
/// to the collection. /// <paramref name="value"/> to the collection.
/// </summary> /// </summary>
/// <param name="name"> /// <param name="name">
/// A <see cref="string"/> that represents the name of the header to add. /// A <see cref="string"/> that represents the name of the header to add.
@ -1025,8 +1032,8 @@ namespace WebSocketSharp.Net
/// The length of <paramref name="value"/> is greater than 65,535 characters. /// The length of <paramref name="value"/> is greater than 65,535 characters.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the header /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="name"/>. /// the header <paramref name="name"/>.
/// </exception> /// </exception>
public override void Add (string name, string value) public override void Add (string name, string value)
{ {
@ -1063,8 +1070,8 @@ namespace WebSocketSharp.Net
/// Get the value of the header with the specified <paramref name="name"/> in the collection. /// Get the value of the header with the specified <paramref name="name"/> in the collection.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A <see cref="string"/> that receives the value of the header if found; otherwise, /// A <see cref="string"/> that receives the value of the header if found;
/// <see langword="null"/>. /// otherwise, <see langword="null"/>.
/// </returns> /// </returns>
/// <param name="name"> /// <param name="name">
/// A <see cref="string"/> that represents the name of the header to find. /// A <see cref="string"/> that represents the name of the header to find.
@ -1103,12 +1110,12 @@ namespace WebSocketSharp.Net
} }
/// <summary> /// <summary>
/// Gets an array of header values stored in the specified <paramref name="index"/> position /// Gets an array of header values stored in the specified <paramref name="index"/> position of
/// of the collection. /// the collection.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// An array of <see cref="string"/> that receives the header values if found; otherwise, /// An array of <see cref="string"/> that receives the header values if found;
/// <see langword="null"/>. /// otherwise, <see langword="null"/>.
/// </returns> /// </returns>
/// <param name="index"> /// <param name="index">
/// An <see cref="int"/> that represents the zero-based index of the header to find. /// An <see cref="int"/> that represents the zero-based index of the header to find.
@ -1119,17 +1126,15 @@ namespace WebSocketSharp.Net
public override string[] GetValues (int index) public override string[] GetValues (int index)
{ {
var vals = base.GetValues (index); var vals = base.GetValues (index);
return vals != null && vals.Length > 0 return vals != null && vals.Length > 0 ? vals : null;
? vals
: null;
} }
/// <summary> /// <summary>
/// Gets an array of header values stored in the specified <paramref name="header"/>. /// Gets an array of header values stored in the specified <paramref name="header"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// An array of <see cref="string"/> that receives the header values if found; otherwise, /// An array of <see cref="string"/> that receives the header values if found;
/// <see langword="null"/>. /// otherwise, <see langword="null"/>.
/// </returns> /// </returns>
/// <param name="header"> /// <param name="header">
/// A <see cref="string"/> that represents the name of the header to find. /// A <see cref="string"/> that represents the name of the header to find.
@ -1137,9 +1142,7 @@ namespace WebSocketSharp.Net
public override string[] GetValues (string header) public override string[] GetValues (string header)
{ {
var vals = base.GetValues (header); var vals = base.GetValues (header);
return vals != null && vals.Length > 0 return vals != null && vals.Length > 0 ? vals : null;
? vals
: null;
} }
/// <summary> /// <summary>
@ -1163,7 +1166,7 @@ namespace WebSocketSharp.Net
if (serializationInfo == null) if (serializationInfo == null)
throw new ArgumentNullException ("serializationInfo"); throw new ArgumentNullException ("serializationInfo");
serializationInfo.AddValue ("InternallyCreated", _internallyCreated); serializationInfo.AddValue ("InternallyUsed", _internallyUsed);
serializationInfo.AddValue ("State", (int) _state); serializationInfo.AddValue ("State", (int) _state);
var cnt = Count; var cnt = Count;
@ -1233,15 +1236,15 @@ namespace WebSocketSharp.Net
/// Removes the specified request <paramref name="header"/> from the collection. /// Removes the specified request <paramref name="header"/> from the collection.
/// </summary> /// </summary>
/// <param name="header"> /// <param name="header">
/// One of the <see cref="HttpRequestHeader"/> enum values, represents the request header /// One of the <see cref="HttpRequestHeader"/> enum values, represents
/// to remove. /// the request header to remove.
/// </param> /// </param>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// <paramref name="header"/> is a restricted header. /// <paramref name="header"/> is a restricted header.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the request /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="header"/>. /// the request <paramref name="header"/>.
/// </exception> /// </exception>
public void Remove (HttpRequestHeader header) public void Remove (HttpRequestHeader header)
{ {
@ -1252,15 +1255,15 @@ namespace WebSocketSharp.Net
/// Removes the specified response <paramref name="header"/> from the collection. /// Removes the specified response <paramref name="header"/> from the collection.
/// </summary> /// </summary>
/// <param name="header"> /// <param name="header">
/// One of the <see cref="HttpResponseHeader"/> enum values, represents the response header /// One of the <see cref="HttpResponseHeader"/> enum values, represents
/// to remove. /// the response header to remove.
/// </param> /// </param>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// <paramref name="header"/> is a restricted header. /// <paramref name="header"/> is a restricted header.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the response /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="header"/>. /// the response <paramref name="header"/>.
/// </exception> /// </exception>
public void Remove (HttpResponseHeader header) public void Remove (HttpResponseHeader header)
{ {
@ -1288,8 +1291,8 @@ namespace WebSocketSharp.Net
/// </para> /// </para>
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the header /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="name"/>. /// the header <paramref name="name"/>.
/// </exception> /// </exception>
public override void Remove (string name) public override void Remove (string name)
{ {
@ -1300,8 +1303,8 @@ namespace WebSocketSharp.Net
/// Sets the specified request <paramref name="header"/> to the specified value. /// Sets the specified request <paramref name="header"/> to the specified value.
/// </summary> /// </summary>
/// <param name="header"> /// <param name="header">
/// One of the <see cref="HttpRequestHeader"/> enum values, represents the request header /// One of the <see cref="HttpRequestHeader"/> enum values, represents
/// to set. /// the request header to set.
/// </param> /// </param>
/// <param name="value"> /// <param name="value">
/// A <see cref="string"/> that represents the value of the request header to set. /// A <see cref="string"/> that represents the value of the request header to set.
@ -1321,8 +1324,8 @@ namespace WebSocketSharp.Net
/// The length of <paramref name="value"/> is greater than 65,535 characters. /// The length of <paramref name="value"/> is greater than 65,535 characters.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the request /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="header"/>. /// the request <paramref name="header"/>.
/// </exception> /// </exception>
public void Set (HttpRequestHeader header, string value) public void Set (HttpRequestHeader header, string value)
{ {
@ -1333,8 +1336,8 @@ namespace WebSocketSharp.Net
/// Sets the specified response <paramref name="header"/> to the specified value. /// Sets the specified response <paramref name="header"/> to the specified value.
/// </summary> /// </summary>
/// <param name="header"> /// <param name="header">
/// One of the <see cref="HttpResponseHeader"/> enum values, represents the response header /// One of the <see cref="HttpResponseHeader"/> enum values, represents
/// to set. /// the response header to set.
/// </param> /// </param>
/// <param name="value"> /// <param name="value">
/// A <see cref="string"/> that represents the value of the response header to set. /// A <see cref="string"/> that represents the value of the response header to set.
@ -1354,8 +1357,8 @@ namespace WebSocketSharp.Net
/// The length of <paramref name="value"/> is greater than 65,535 characters. /// The length of <paramref name="value"/> is greater than 65,535 characters.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the response /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="header"/>. /// the response <paramref name="header"/>.
/// </exception> /// </exception>
public void Set (HttpResponseHeader header, string value) public void Set (HttpResponseHeader header, string value)
{ {
@ -1389,8 +1392,8 @@ namespace WebSocketSharp.Net
/// The length of <paramref name="value"/> is greater than 65,535 characters. /// The length of <paramref name="value"/> is greater than 65,535 characters.
/// </exception> /// </exception>
/// <exception cref="InvalidOperationException"> /// <exception cref="InvalidOperationException">
/// The current <see cref="WebHeaderCollection"/> instance doesn't allow the header /// The current <see cref="WebHeaderCollection"/> instance doesn't allow
/// <paramref name="name"/>. /// the header <paramref name="name"/>.
/// </exception> /// </exception>
public override void Set (string name, string value) public override void Set (string name, string value)
{ {