From 117aa1089a168ba02f0030da714718c0cf6b7bab Mon Sep 17 00:00:00 2001 From: sta Date: Sat, 19 Apr 2014 17:35:49 +0900 Subject: [PATCH] Refactored WebHeaderCollection.cs --- websocket-sharp/Net/WebHeaderCollection.cs | 207 ++++++++++----------- 1 file changed, 100 insertions(+), 107 deletions(-) diff --git a/websocket-sharp/Net/WebHeaderCollection.cs b/websocket-sharp/Net/WebHeaderCollection.cs index 47c4c259..ebf7242a 100644 --- a/websocket-sharp/Net/WebHeaderCollection.cs +++ b/websocket-sharp/Net/WebHeaderCollection.cs @@ -44,13 +44,12 @@ using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; -using System.Linq; using System.Net; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Security.Permissions; using System.Text; - + namespace WebSocketSharp.Net { /// @@ -615,16 +614,12 @@ namespace WebSocketSharp.Net /// Gets or sets the specified request in the collection. /// /// - /// A that represents the value of the specified request - /// . + /// A that represents the value of the request . /// /// - /// A that represents the request header. + /// One of the enum values, represents the request header + /// to get or set. /// - /// - /// The current instance doesn't allow any of - /// enum values. - /// /// /// /// is a restricted header. @@ -639,6 +634,10 @@ namespace WebSocketSharp.Net /// /// The length of is greater than 65535. /// + /// + /// The current instance doesn't allow the request + /// . + /// public string this [HttpRequestHeader header] { get { return Get (Convert (header)); @@ -653,16 +652,12 @@ namespace WebSocketSharp.Net /// Gets or sets the specified response in the collection. /// /// - /// A that represents the value of the specified response - /// . + /// A that represents the value of the response . /// /// - /// A that represents the response header. + /// One of the enum values, represents the response header + /// to get or set. /// - /// - /// The current instance doesn't allow any of - /// enum values. - /// /// /// /// is a restricted header. @@ -677,6 +672,10 @@ namespace WebSocketSharp.Net /// /// The length of is greater than 65535. /// + /// + /// The current instance doesn't allow the response + /// . + /// public string this [HttpResponseHeader header] { get { return Get (Convert (header)); @@ -735,8 +734,8 @@ namespace WebSocketSharp.Net private static HttpHeaderType checkHeaderType (string name) { - HttpHeaderInfo info; - return !tryGetHeaderInfo (name, out info) + var info = getHeaderInfo (name); + return info == null ? HttpHeaderType.Unspecified : info.IsRequest && !info.IsResponse ? HttpHeaderType.Request @@ -747,7 +746,7 @@ namespace WebSocketSharp.Net private static string checkName (string name) { - if (name.IsNullOrEmpty ()) + if (name == null || name.Length == 0) throw new ArgumentNullException ("name"); name = name.Trim (); @@ -779,7 +778,7 @@ namespace WebSocketSharp.Net private static string checkValue (string value) { - if (value.IsNullOrEmpty ()) + if (value == null || value.Length == 0) return String.Empty; value = value.Trim (); @@ -818,8 +817,8 @@ namespace WebSocketSharp.Net { checkState (response); action (name, value); - if (setState) - setDefaultState (response); + if (setState && _state == HttpHeaderType.Unspecified) + _state = response ? HttpHeaderType.Response : HttpHeaderType.Request; } private void doWithoutCheckingName (Action action, string name, string value) @@ -830,15 +829,17 @@ namespace WebSocketSharp.Net private static HttpHeaderInfo getHeaderInfo (string name) { - return (from HttpHeaderInfo info in _headers.Values - where info.Name.Equals (name, StringComparison.InvariantCultureIgnoreCase) - select info).FirstOrDefault (); + foreach (var info in _headers.Values) + if (info.Name.Equals (name, StringComparison.InvariantCultureIgnoreCase)) + return info; + + return null; } private static bool isRestricted (string name, bool response) { - HttpHeaderInfo info; - return tryGetHeaderInfo (name, out info) && info.IsRestricted (response); + var info = getHeaderInfo (name); + return info != null && info.IsRestricted (response); } private void removeWithoutCheckingName (string name, string unuse) @@ -847,23 +848,11 @@ namespace WebSocketSharp.Net base.Remove (name); } - private void setDefaultState (bool response) - { - if (_state == HttpHeaderType.Unspecified) - _state = response ? HttpHeaderType.Response : HttpHeaderType.Request; - } - private void setWithoutCheckingName (string name, string value) { doWithoutCheckingName (base.Set, name, value); } - private static bool tryGetHeaderInfo (string name, out HttpHeaderInfo info) - { - info = getHeaderInfo (name); - return info != null; - } - #endregion #region Internal Methods @@ -880,7 +869,7 @@ namespace WebSocketSharp.Net internal static bool IsHeaderName (string name) { - return !name.IsNullOrEmpty () && name.IsToken (); + return name != null && name.Length > 0 && name.IsToken (); } internal static bool IsHeaderValue (string value) @@ -890,11 +879,11 @@ namespace WebSocketSharp.Net internal static bool IsMultiValue (string headerName, bool response) { - if (headerName.IsNullOrEmpty ()) + if (headerName == null || headerName.Length == 0) return false; - HttpHeaderInfo info; - return tryGetHeaderInfo (headerName, out info) && info.IsMultiValue (response); + var info = getHeaderInfo (headerName); + return info != null && info.IsMultiValue (response); } internal void RemoveInternal (string name) @@ -1018,15 +1007,12 @@ namespace WebSocketSharp.Net /// to the collection. /// /// - /// A that represents the request header to add. + /// One of the enum values, represents the request header + /// to add. /// /// /// A that represents the value of the header to add. /// - /// - /// The current instance doesn't allow any of - /// enum values. - /// /// /// /// is a restricted header. @@ -1041,6 +1027,10 @@ namespace WebSocketSharp.Net /// /// The length of is greater than 65535. /// + /// + /// The current instance doesn't allow the request + /// . + /// public void Add (HttpRequestHeader header, string value) { doWithCheckingState (addWithoutCheckingName, Convert (header), value, false, true); @@ -1051,15 +1041,12 @@ namespace WebSocketSharp.Net /// to the collection. /// /// - /// A that represents the response header to add. + /// One of the enum values, represents the response header + /// to add. /// /// /// A that represents the value of the header to add. /// - /// - /// The current instance doesn't allow any of - /// enum values. - /// /// /// /// is a restricted header. @@ -1074,14 +1061,18 @@ namespace WebSocketSharp.Net /// /// The length of is greater than 65535. /// + /// + /// The current instance doesn't allow the response + /// . + /// public void Add (HttpResponseHeader header, string value) { doWithCheckingState (addWithoutCheckingName, Convert (header), value, true, true); } /// - /// Adds a header with the specified and to - /// the collection. + /// Adds a header with the specified and + /// to the collection. /// /// /// A that represents the name of the header to add. @@ -1125,13 +1116,13 @@ namespace WebSocketSharp.Net } /// - /// Get the value of the header with the specified in the collection. + /// Get the value of the header at the specified in the collection. /// /// /// A that receives the value of the header. /// /// - /// An that is the zero-based index of the header to get. + /// An that represents the zero-based index of the header to find. /// public override string Get (int index) { @@ -1142,11 +1133,11 @@ namespace WebSocketSharp.Net /// Get the value of the header with the specified in the collection. /// /// - /// A that receives the value of the header. - /// if there is no header with in the collection. + /// A that receives the value of the header. if + /// there is no header with in the collection. /// /// - /// A that represents the name of the header to get. + /// A that represents the name of the header to find. /// public override string Get (string name) { @@ -1166,36 +1157,19 @@ namespace WebSocketSharp.Net } /// - /// Get the header name at the specified position in the collection. + /// Get the name of the header at the specified in the collection. /// /// /// A that receives the header name. /// /// - /// An is the zero-based index of the key to get from the collection. + /// An that represents the zero-based index of the header to find. /// public override string GetKey (int index) { return base.GetKey (index); } - /// - /// Gets an array of header values stored in the specified . - /// - /// - /// An array of that receives the header values. - /// - /// - /// A that represents the name of the header. - /// - public override string [] GetValues (string header) - { - var values = base.GetValues (header); - return values != null && values.Length > 0 - ? values - : null; - } - /// /// Gets an array of header values stored in the specified position of /// the collection. @@ -1204,7 +1178,7 @@ namespace WebSocketSharp.Net /// An array of that receives the header values. /// /// - /// An is the zero-based index of the header in the collection. + /// An that represents the zero-based index of the header to find. /// public override string [] GetValues (int index) { @@ -1214,6 +1188,23 @@ namespace WebSocketSharp.Net : null; } + /// + /// Gets an array of header values stored in the specified . + /// + /// + /// An array of that receives the header values. + /// + /// + /// A that represents the name of the header to find. + /// + public override string [] GetValues (string header) + { + var values = base.GetValues (header); + return values != null && values.Length > 0 + ? values + : null; + } + /// /// Populates the specified with the data needed to serialize /// the . @@ -1302,38 +1293,38 @@ namespace WebSocketSharp.Net } /// - /// Removes the specified request header from the collection. + /// Removes the specified request from the collection. /// /// - /// A that represents the request header to remove from - /// the collection. + /// One of the enum values, represents the request header + /// to remove. /// - /// - /// The current instance doesn't allow any of - /// enum values. - /// /// /// is a restricted header. /// + /// + /// The current instance doesn't allow the request + /// . + /// public void Remove (HttpRequestHeader header) { doWithCheckingState (removeWithoutCheckingName, Convert (header), null, false, false); } /// - /// Removes the specified response header from the collection. + /// Removes the specified response from the collection. /// /// - /// A that represents the response header to remove from - /// the collection. + /// One of the enum values, represents the response header + /// to remove. /// - /// - /// The current instance doesn't allow any of - /// enum values. - /// /// /// is a restricted header. /// + /// + /// The current instance doesn't allow the response + /// . + /// public void Remove (HttpResponseHeader header) { doWithCheckingState (removeWithoutCheckingName, Convert (header), null, true, false); @@ -1343,7 +1334,7 @@ namespace WebSocketSharp.Net /// Removes the specified header from the collection. /// /// - /// A that represents the name of the header to remove from the collection. + /// A that represents the name of the header to remove. /// /// /// is or empty. @@ -1369,18 +1360,15 @@ namespace WebSocketSharp.Net } /// - /// Sets the specified request header to the specified value. + /// Sets the specified request to the specified value. /// /// - /// A that represents the request header to set. + /// One of the enum values, represents the request header + /// to set. /// /// /// A that represents the value of the request header to set. /// - /// - /// The current instance doesn't allow any of - /// enum values. - /// /// /// /// is a restricted header. @@ -1395,24 +1383,25 @@ namespace WebSocketSharp.Net /// /// The length of is greater than 65535. /// + /// + /// The current instance doesn't allow the request + /// . + /// public void Set (HttpRequestHeader header, string value) { doWithCheckingState (setWithoutCheckingName, Convert (header), value, false, true); } /// - /// Sets the specified response header to the specified value. + /// Sets the specified response to the specified value. /// /// - /// A that represents the response header to set. + /// One of the enum values, represents the response header + /// to set. /// /// /// A that represents the value of the response header to set. /// - /// - /// The current instance doesn't allow any of - /// enum values. - /// /// /// /// is a restricted header. @@ -1427,6 +1416,10 @@ namespace WebSocketSharp.Net /// /// The length of is greater than 65535. /// + /// + /// The current instance doesn't allow the response + /// . + /// public void Set (HttpResponseHeader header, string value) { doWithCheckingState (setWithoutCheckingName, Convert (header), value, true, true);