Refactored WebHeaderCollection.cs

This commit is contained in:
sta 2014-04-20 17:21:38 +09:00
parent 117aa1089a
commit 5e19c5cb1a
6 changed files with 37 additions and 26 deletions

View File

@ -313,7 +313,7 @@ namespace WebSocketSharp
internal static void CloseWithAuthChallenge ( internal static void CloseWithAuthChallenge (
this HttpListenerResponse response, string challenge) this HttpListenerResponse response, string challenge)
{ {
response.Headers.SetInternal ("WWW-Authenticate", challenge, true); response.Headers.SetInternally ("WWW-Authenticate", challenge, true);
response.Close (HttpStatusCode.Unauthorized); response.Close (HttpStatusCode.Unauthorized);
} }

View File

@ -162,7 +162,7 @@ namespace WebSocketSharp
var headers = new WebHeaderCollection (); var headers = new WebHeaderCollection ();
for (int i = 1; i < headerParts.Length; i++) for (int i = 1; i < headerParts.Length; i++)
headers.SetInternal (headerParts [i], false); headers.SetInternally (headerParts [i], false);
return new HandshakeRequest { return new HandshakeRequest {
Headers = headers, Headers = headers,

View File

@ -140,7 +140,7 @@ namespace WebSocketSharp
var headers = new WebHeaderCollection (); var headers = new WebHeaderCollection ();
for (int i = 1; i < headerParts.Length; i++) for (int i = 1; i < headerParts.Length; i++)
headers.SetInternal (headerParts [i], true); headers.SetInternally (headerParts [i], true);
return new HandshakeResponse { return new HandshakeResponse {
Headers = headers, Headers = headers,

View File

@ -527,7 +527,7 @@ namespace WebSocketSharp.Net
var name = header.Substring (0, colon).Trim (); var name = header.Substring (0, colon).Trim ();
var val = header.Substring (colon + 1).Trim (); var val = header.Substring (colon + 1).Trim ();
var lower = name.ToLower (CultureInfo.InvariantCulture); var lower = name.ToLower (CultureInfo.InvariantCulture);
_headers.SetInternal (name, val, false); _headers.SetInternally (name, val, false);
if (lower == "accept") { if (lower == "accept") {
_acceptTypes = val.SplitHeaderValue (',').ToArray (); _acceptTypes = val.SplitHeaderValue (',').ToArray ();

View File

@ -538,20 +538,20 @@ namespace WebSocketSharp.Net
if (_contentEncoding != null && if (_contentEncoding != null &&
_contentType.IndexOf ("charset=", StringComparison.Ordinal) == -1) { _contentType.IndexOf ("charset=", StringComparison.Ordinal) == -1) {
var charset = _contentEncoding.WebName; var charset = _contentEncoding.WebName;
_headers.SetInternal ( _headers.SetInternally (
"Content-Type", _contentType + "; charset=" + charset, true); "Content-Type", _contentType + "; charset=" + charset, true);
} }
else { else {
_headers.SetInternal ("Content-Type", _contentType, true); _headers.SetInternally ("Content-Type", _contentType, true);
} }
} }
if (_headers ["Server"] == null) if (_headers ["Server"] == null)
_headers.SetInternal ("Server", "websocket-sharp/1.0", true); _headers.SetInternally ("Server", "websocket-sharp/1.0", true);
var provider = CultureInfo.InvariantCulture; var provider = CultureInfo.InvariantCulture;
if (_headers ["Date"] == null) if (_headers ["Date"] == null)
_headers.SetInternal ( _headers.SetInternally (
"Date", DateTime.UtcNow.ToString ("r", provider), true); "Date", DateTime.UtcNow.ToString ("r", provider), true);
if (!_chunked) { if (!_chunked) {
@ -561,7 +561,7 @@ namespace WebSocketSharp.Net
} }
if (_contentLengthSet) if (_contentLengthSet)
_headers.SetInternal ( _headers.SetInternally (
"Content-Length", _contentLength.ToString (provider), true); "Content-Length", _contentLength.ToString (provider), true);
} }
@ -591,36 +591,36 @@ namespace WebSocketSharp.Net
// They sent both KeepAlive: true and Connection: close!? // They sent both KeepAlive: true and Connection: close!?
if (!_keepAlive || connClose) { if (!_keepAlive || connClose) {
_headers.SetInternal ("Connection", "close", true); _headers.SetInternally ("Connection", "close", true);
connClose = true; connClose = true;
} }
if (_chunked) if (_chunked)
_headers.SetInternal ("Transfer-Encoding", "chunked", true); _headers.SetInternally ("Transfer-Encoding", "chunked", true);
int reuses = _context.Connection.Reuses; int reuses = _context.Connection.Reuses;
if (reuses >= 100) { if (reuses >= 100) {
_forceCloseChunked = true; _forceCloseChunked = true;
if (!connClose) { if (!connClose) {
_headers.SetInternal ("Connection", "close", true); _headers.SetInternally ("Connection", "close", true);
connClose = true; connClose = true;
} }
} }
if (!connClose) { if (!connClose) {
_headers.SetInternal ( _headers.SetInternally (
"Keep-Alive", "Keep-Alive",
String.Format ("timeout=15,max={0}", 100 - reuses), true); String.Format ("timeout=15,max={0}", 100 - reuses), true);
if (_context.Request.ProtocolVersion <= HttpVersion.Version10) if (_context.Request.ProtocolVersion <= HttpVersion.Version10)
_headers.SetInternal ("Connection", "keep-alive", true); _headers.SetInternally ("Connection", "keep-alive", true);
} }
if (_location != null) if (_location != null)
_headers.SetInternal ("Location", _location, true); _headers.SetInternally ("Location", _location, true);
if (_cookies != null) { if (_cookies != null) {
foreach (Cookie cookie in _cookies) foreach (Cookie cookie in _cookies)
_headers.SetInternal ("Set-Cookie", cookie.ToResponseString (), true); _headers.SetInternally ("Set-Cookie", cookie.ToResponseString (), true);
} }
var encoding = _contentEncoding ?? Encoding.Default; var encoding = _contentEncoding ?? Encoding.Default;

View File

@ -886,18 +886,18 @@ namespace WebSocketSharp.Net
return info != null && info.IsMultiValue (response); return info != null && info.IsMultiValue (response);
} }
internal void RemoveInternal (string name) internal void RemoveInternally (string name)
{ {
base.Remove (name); base.Remove (name);
} }
internal void SetInternal (string header, bool response) internal void SetInternally (string header, bool response)
{ {
var pos = checkColonSeparated (header); var pos = checkColonSeparated (header);
SetInternal (header.Substring (0, pos), header.Substring (pos + 1), response); SetInternally (header.Substring (0, pos), header.Substring (pos + 1), response);
} }
internal void SetInternal (string name, string value, bool response) internal void SetInternally (string name, string value, bool response)
{ {
value = checkValue (value); value = checkValue (value);
if (IsMultiValue (name, response)) if (IsMultiValue (name, response))
@ -1124,6 +1124,9 @@ namespace WebSocketSharp.Net
/// <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.
/// </param> /// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is out of allowable range of indexes for the collection.
/// </exception>
public override string Get (int index) public override string Get (int index)
{ {
return base.Get (index); return base.Get (index);
@ -1133,8 +1136,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. <see langword="null"/> if /// A <see cref="string"/> that receives the value of the header if found; otherwise,
/// there is no header with <paramref name="name"/> in the collection. /// <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.
@ -1165,21 +1168,28 @@ namespace WebSocketSharp.Net
/// <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.
/// </param> /// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is out of allowable range of indexes for the collection.
/// </exception>
public override string GetKey (int index) public override string GetKey (int index)
{ {
return base.GetKey (index); return base.GetKey (index);
} }
/// <summary> /// <summary>
/// Gets an array of header values stored in the specified <paramref name="index"/> position of /// Gets an array of header values stored in the specified <paramref name="index"/> position
/// the collection. /// of the collection.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// An array of <see cref="string"/> that receives the header values. /// An array of <see cref="string"/> that receives the header values if found; 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.
/// </param> /// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is out of allowable range of indexes for the collection.
/// </exception>
public override string [] GetValues (int index) public override string [] GetValues (int index)
{ {
var values = base.GetValues (index); var values = base.GetValues (index);
@ -1192,7 +1202,8 @@ namespace WebSocketSharp.Net
/// 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. /// An array of <see cref="string"/> that receives the header values if found; 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.