From 6822fdce36822e1ca410a1d70baaea55ada794b9 Mon Sep 17 00:00:00 2001 From: sta Date: Wed, 29 Oct 2014 14:41:30 +0900 Subject: [PATCH] Refactored WebHeaderCollection.cs --- websocket-sharp/Ext.cs | 2 +- websocket-sharp/HttpRequest.cs | 2 +- websocket-sharp/HttpResponse.cs | 2 +- websocket-sharp/Net/HttpListenerRequest.cs | 2 +- websocket-sharp/Net/HttpListenerResponse.cs | 22 ++-- websocket-sharp/Net/WebHeaderCollection.cs | 108 +++++++++----------- 6 files changed, 65 insertions(+), 73 deletions(-) diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 441476d4..00d29ef0 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -323,7 +323,7 @@ namespace WebSocketSharp internal static void CloseWithAuthChallenge ( this HttpListenerResponse response, string challenge) { - response.Headers.SetInternally ("WWW-Authenticate", challenge, true); + response.Headers.InternalSet ("WWW-Authenticate", challenge, true); response.Close (HttpStatusCode.Unauthorized); } diff --git a/websocket-sharp/HttpRequest.cs b/websocket-sharp/HttpRequest.cs index 9a69abcb..41f700e2 100644 --- a/websocket-sharp/HttpRequest.cs +++ b/websocket-sharp/HttpRequest.cs @@ -154,7 +154,7 @@ namespace WebSocketSharp var headers = new WebHeaderCollection (); for (int i = 1; i < headerParts.Length; i++) - headers.SetInternally (headerParts[i], false); + headers.InternalSet (headerParts[i], false); return new HttpRequest ( requestLine[0], requestLine[1], new Version (requestLine[2].Substring (5)), headers); diff --git a/websocket-sharp/HttpResponse.cs b/websocket-sharp/HttpResponse.cs index a8262f47..9cfe26f7 100644 --- a/websocket-sharp/HttpResponse.cs +++ b/websocket-sharp/HttpResponse.cs @@ -168,7 +168,7 @@ namespace WebSocketSharp var headers = new WebHeaderCollection (); for (int i = 1; i < headerParts.Length; i++) - headers.SetInternally (headerParts[i], true); + headers.InternalSet (headerParts[i], true); return new HttpResponse ( statusLine[1], statusLine[2], new Version (statusLine[0].Substring (5)), headers); diff --git a/websocket-sharp/Net/HttpListenerRequest.cs b/websocket-sharp/Net/HttpListenerRequest.cs index 3aa90f35..e28e6254 100644 --- a/websocket-sharp/Net/HttpListenerRequest.cs +++ b/websocket-sharp/Net/HttpListenerRequest.cs @@ -491,7 +491,7 @@ namespace WebSocketSharp.Net var name = header.Substring (0, colon).Trim (); var val = header.Substring (colon + 1).Trim (); - _headers.SetInternally (name, val, false); + _headers.InternalSet (name, val, false); var lower = name.ToLower (CultureInfo.InvariantCulture); if (lower == "accept") { diff --git a/websocket-sharp/Net/HttpListenerResponse.cs b/websocket-sharp/Net/HttpListenerResponse.cs index 6e0ee7c9..4231089e 100644 --- a/websocket-sharp/Net/HttpListenerResponse.cs +++ b/websocket-sharp/Net/HttpListenerResponse.cs @@ -518,15 +518,15 @@ namespace WebSocketSharp.Net "{0}; charset={1}", _contentType, _contentEncoding.WebName) : _contentType; - _headers.SetInternally ("Content-Type", contentType, true); + _headers.InternalSet ("Content-Type", contentType, true); } if (_headers["Server"] == null) - _headers.SetInternally ("Server", "websocket-sharp/1.0", true); + _headers.InternalSet ("Server", "websocket-sharp/1.0", true); var provider = CultureInfo.InvariantCulture; if (_headers["Date"] == null) - _headers.SetInternally ("Date", DateTime.UtcNow.ToString ("r", provider), true); + _headers.InternalSet ("Date", DateTime.UtcNow.ToString ("r", provider), true); if (!_chunked) { if (!_contentLengthWasSet && closing) { @@ -535,7 +535,7 @@ namespace WebSocketSharp.Net } if (_contentLengthWasSet) - _headers.SetInternally ("Content-Length", _contentLength.ToString (provider), true); + _headers.InternalSet ("Content-Length", _contentLength.ToString (provider), true); } var reqVer = _context.Request.ProtocolVersion; @@ -565,36 +565,36 @@ namespace WebSocketSharp.Net // They sent both KeepAlive: true and Connection: close!? if (!_keepAlive || connClose) { - _headers.SetInternally ("Connection", "close", true); + _headers.InternalSet ("Connection", "close", true); connClose = true; } if (_chunked) - _headers.SetInternally ("Transfer-Encoding", "chunked", true); + _headers.InternalSet ("Transfer-Encoding", "chunked", true); var reuses = _context.Connection.Reuses; if (reuses >= 100) { _forceCloseChunked = true; if (!connClose) { - _headers.SetInternally ("Connection", "close", true); + _headers.InternalSet ("Connection", "close", true); connClose = true; } } if (!connClose) { - _headers.SetInternally ( + _headers.InternalSet ( "Keep-Alive", String.Format ("timeout=15,max={0}", 100 - reuses), true); if (reqVer < HttpVersion.Version11) - _headers.SetInternally ("Connection", "keep-alive", true); + _headers.InternalSet ("Connection", "keep-alive", true); } if (_location != null) - _headers.SetInternally ("Location", _location, true); + _headers.InternalSet ("Location", _location, true); if (_cookies != null) foreach (Cookie cookie in _cookies) - _headers.SetInternally ("Set-Cookie", cookie.ToResponseString (), true); + _headers.InternalSet ("Set-Cookie", cookie.ToResponseString (), true); var enc = _contentEncoding ?? Encoding.Default; var writer = new StreamWriter (stream, enc, 256); diff --git a/websocket-sharp/Net/WebHeaderCollection.cs b/websocket-sharp/Net/WebHeaderCollection.cs index d7e899ee..b48111f2 100644 --- a/websocket-sharp/Net/WebHeaderCollection.cs +++ b/websocket-sharp/Net/WebHeaderCollection.cs @@ -59,16 +59,11 @@ namespace WebSocketSharp.Net [ComVisible (true)] public class WebHeaderCollection : NameValueCollection, ISerializable { - #region Private Static Fields - - private static readonly Dictionary _headers; - - #endregion - #region Private Fields - private bool _internallyCreated; - private HttpHeaderType _state; + private static readonly Dictionary _headers; + private bool _internallyCreated; + private HttpHeaderType _state; #endregion @@ -557,11 +552,11 @@ namespace WebSocketSharp.Net _internallyCreated = serializationInfo.GetBoolean ("InternallyCreated"); _state = (HttpHeaderType) serializationInfo.GetInt32 ("State"); - var count = serializationInfo.GetInt32 ("Count"); - for (int i = 0; i < count; i++) { + var cnt = serializationInfo.GetInt32 ("Count"); + for (var i = 0; i < cnt; i++) { base.Add ( serializationInfo.GetString (i.ToString ()), - serializationInfo.GetString ((count + i).ToString ())); + serializationInfo.GetString ((cnt + i).ToString ())); } } catch (SerializationException ex) { @@ -578,7 +573,6 @@ namespace WebSocketSharp.Net /// public WebHeaderCollection () { - _internallyCreated = false; _state = HttpHeaderType.Unspecified; } @@ -592,7 +586,7 @@ namespace WebSocketSharp.Net /// /// An array of that contains all header names in the collection. /// - public override string [] AllKeys { + public override string[] AllKeys { get { return base.AllKeys; } @@ -638,7 +632,7 @@ namespace WebSocketSharp.Net /// The current instance doesn't allow the request /// . /// - public string this [HttpRequestHeader header] { + public string this[HttpRequestHeader header] { get { return Get (Convert (header)); } @@ -676,7 +670,7 @@ namespace WebSocketSharp.Net /// The current instance doesn't allow the response /// . /// - public string this [HttpResponseHeader header] { + public string this[HttpResponseHeader header] { get { return Get (Convert (header)); } @@ -705,11 +699,9 @@ namespace WebSocketSharp.Net private void add (string name, string value, bool ignoreRestricted) { - Action act; - if (ignoreRestricted) - act = addWithoutCheckingNameAndRestricted; - else - act = addWithoutCheckingName; + var act = ignoreRestricted + ? (Action ) addWithoutCheckingNameAndRestricted + : addWithoutCheckingName; doWithCheckingState (act, checkName (name), value, true); } @@ -728,7 +720,7 @@ namespace WebSocketSharp.Net { var i = header.IndexOf (':'); if (i == -1) - throw new ArgumentException ("No colon found.", "header"); + throw new ArgumentException ("No colon could be found.", "header"); return i; } @@ -867,6 +859,26 @@ namespace WebSocketSharp.Net return convert (header.ToString ()); } + internal void InternalRemove (string name) + { + base.Remove (name); + } + + internal void InternalSet (string header, bool response) + { + var pos = checkColonSeparated (header); + InternalSet (header.Substring (0, pos), header.Substring (pos + 1), response); + } + + internal void InternalSet (string name, string value, bool response) + { + value = checkValue (value); + if (IsMultiValue (name, response)) + base.Add (name, value); + else + base.Set (name, value); + } + internal static bool IsHeaderName (string name) { return name != null && name.Length > 0 && name.IsToken (); @@ -886,26 +898,6 @@ namespace WebSocketSharp.Net return info != null && info.IsMultiValue (response); } - internal void RemoveInternally (string name) - { - base.Remove (name); - } - - internal void SetInternally (string header, bool response) - { - var pos = checkColonSeparated (header); - SetInternally (header.Substring (0, pos), header.Substring (pos + 1), response); - } - - internal void SetInternally (string name, string value, bool response) - { - value = checkValue (value); - if (IsMultiValue (name, response)) - base.Add (name, value); - else - base.Set (name, value); - } - internal string ToStringMultiValue (bool response) { var buff = new StringBuilder (); @@ -913,8 +905,8 @@ namespace WebSocketSharp.Net i => { var key = GetKey (i); if (IsMultiValue (key, response)) - foreach (var value in GetValues (i)) - buff.AppendFormat ("{0}: {1}\r\n", key, value); + foreach (var val in GetValues (i)) + buff.AppendFormat ("{0}: {1}\r\n", key, val); else buff.AppendFormat ("{0}: {1}\r\n", key, Get (i)); }); @@ -963,7 +955,7 @@ namespace WebSocketSharp.Net /// /// /// A that represents the header with the name and value separated by - /// a colon (:). + /// a colon (':'). /// /// /// is , empty, or the name part of @@ -1189,11 +1181,11 @@ namespace WebSocketSharp.Net /// /// is out of allowable range of indexes for the collection. /// - public override string [] GetValues (int index) + public override string[] GetValues (int index) { - var values = base.GetValues (index); - return values != null && values.Length > 0 - ? values + var vals = base.GetValues (index); + return vals != null && vals.Length > 0 + ? vals : null; } @@ -1207,11 +1199,11 @@ namespace WebSocketSharp.Net /// /// A that represents the name of the header to find. /// - public override string [] GetValues (string header) + public override string[] GetValues (string header) { - var values = base.GetValues (header); - return values != null && values.Length > 0 - ? values + var vals = base.GetValues (header); + return vals != null && vals.Length > 0 + ? vals : null; } @@ -1239,12 +1231,12 @@ namespace WebSocketSharp.Net serializationInfo.AddValue ("InternallyCreated", _internallyCreated); serializationInfo.AddValue ("State", (int) _state); - var count = Count; - serializationInfo.AddValue ("Count", count); - count.Times ( + var cnt = Count; + serializationInfo.AddValue ("Count", cnt); + cnt.Times ( i => { serializationInfo.AddValue (i.ToString (), GetKey (i)); - serializationInfo.AddValue ((count + i).ToString (), Get (i)); + serializationInfo.AddValue ((cnt + i).ToString (), Get (i)); }); } @@ -1477,7 +1469,7 @@ namespace WebSocketSharp.Net /// An array of that receives the converted current /// . /// - public byte [] ToByteArray () + public byte[] ToByteArray () { return Encoding.UTF8.GetBytes (ToString ()); } @@ -1499,7 +1491,7 @@ namespace WebSocketSharp.Net #endregion - #region Explicit Interface Implementation + #region Explicit Interface Implementations /// /// Populates the specified with the data needed to serialize