#407 Add ContentLength to IHeaderDictionary

This commit is contained in:
Chris R 2017-01-17 13:27:21 -08:00
parent b7d2f8c905
commit 779115b1ad
8 changed files with 101 additions and 52 deletions

View File

@ -17,5 +17,10 @@ namespace Microsoft.AspNetCore.Http
/// <param name="key"></param> /// <param name="key"></param>
/// <returns>The stored value, or StringValues.Empty if the key is not present.</returns> /// <returns>The stored value, or StringValues.Empty if the key is not present.</returns>
new StringValues this[string key] { get; set; } new StringValues this[string key] { get; set; }
/// <summary>
/// Strongly typed access to the Content-Length header. Implementations must keep this in sync with the string representation.
/// </summary>
long? ContentLength { get; set; }
} }
} }

View File

@ -0,0 +1,14 @@
[
{
"OldTypeId": "public interface Microsoft.AspNetCore.Http.IHeaderDictionary : System.Collections.Generic.IDictionary<System.String, Microsoft.Extensions.Primitives.StringValues>",
"NewTypeId": "public interface Microsoft.AspNetCore.Http.IHeaderDictionary : System.Collections.Generic.IDictionary<System.String, Microsoft.Extensions.Primitives.StringValues>",
"NewMemberId": "System.Nullable<System.Int64> get_ContentLength()",
"Kind": "Addition"
},
{
"OldTypeId": "public interface Microsoft.AspNetCore.Http.IHeaderDictionary : System.Collections.Generic.IDictionary<System.String, Microsoft.Extensions.Primitives.StringValues>",
"NewTypeId": "public interface Microsoft.AspNetCore.Http.IHeaderDictionary : System.Collections.Generic.IDictionary<System.String, Microsoft.Extensions.Primitives.StringValues>",
"NewMemberId": "System.Void set_ContentLength(System.Nullable<System.Int64> value)",
"Kind": "Addition"
}
]

View File

@ -0,0 +1,14 @@
[
{
"OldTypeId": "public interface Microsoft.AspNetCore.Http.IHeaderDictionary : System.Collections.Generic.IDictionary<System.String, Microsoft.Extensions.Primitives.StringValues>",
"NewTypeId": "public interface Microsoft.AspNetCore.Http.IHeaderDictionary : System.Collections.Generic.IDictionary<System.String, Microsoft.Extensions.Primitives.StringValues>",
"NewMemberId": "System.Nullable<System.Int64> get_ContentLength()",
"Kind": "Addition"
},
{
"OldTypeId": "public interface Microsoft.AspNetCore.Http.IHeaderDictionary : System.Collections.Generic.IDictionary<System.String, Microsoft.Extensions.Primitives.StringValues>",
"NewTypeId": "public interface Microsoft.AspNetCore.Http.IHeaderDictionary : System.Collections.Generic.IDictionary<System.String, Microsoft.Extensions.Primitives.StringValues>",
"NewMemberId": "System.Void set_ContentLength(System.Nullable<System.Int64> value)",
"Kind": "Addition"
}
]

View File

@ -5,6 +5,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Http namespace Microsoft.AspNetCore.Http
{ {
@ -97,6 +98,34 @@ namespace Microsoft.AspNetCore.Http
set { this[key] = value; } set { this[key] = value; }
} }
public long? ContentLength
{
get
{
long value;
var rawValue = this[HeaderNames.ContentLength];
if (rawValue.Count == 1 &&
!string.IsNullOrWhiteSpace(rawValue[0]) &&
HeaderUtilities.TryParseInt64(new StringSegment(rawValue[0]).Trim(), out value))
{
return value;
}
return null;
}
set
{
if (value.HasValue)
{
this[HeaderNames.ContentLength] = HeaderUtilities.FormatInt64(value.Value);
}
else
{
this.Remove(HeaderNames.ContentLength);
}
}
}
/// <summary> /// <summary>
/// Gets the number of elements contained in the <see cref="HeaderDictionary" />;. /// Gets the number of elements contained in the <see cref="HeaderDictionary" />;.
/// </summary> /// </summary>

View File

@ -72,14 +72,8 @@ namespace Microsoft.AspNetCore.Http.Internal
public override long? ContentLength public override long? ContentLength
{ {
get get { return Headers.ContentLength; }
{ set { Headers.ContentLength = value; }
return ParsingHelpers.GetContentLength(Headers);
}
set
{
ParsingHelpers.SetContentLength(Headers, value);
}
} }
public override Stream Body public override Stream Body

View File

@ -63,14 +63,8 @@ namespace Microsoft.AspNetCore.Http.Internal
public override long? ContentLength public override long? ContentLength
{ {
get get { return Headers.ContentLength; }
{ set { Headers.ContentLength = value; }
return ParsingHelpers.GetContentLength(Headers);
}
set
{
ParsingHelpers.SetContentLength(Headers, value);
}
} }
public override string ContentType public override string ContentType

View File

@ -403,41 +403,5 @@ namespace Microsoft.AspNetCore.Http.Internal
return value; return value;
} }
public static long? GetContentLength(IHeaderDictionary headers)
{
if (headers == null)
{
throw new ArgumentNullException(nameof(headers));
}
long value;
var rawValue = headers[HeaderNames.ContentLength];
if (rawValue.Count == 1 &&
!string.IsNullOrWhiteSpace(rawValue[0]) &&
HeaderUtilities.TryParseInt64(new StringSegment(rawValue[0]).Trim(), out value))
{
return value;
}
return null;
}
public static void SetContentLength(IHeaderDictionary headers, long? value)
{
if (headers == null)
{
throw new ArgumentNullException(nameof(headers));
}
if (value.HasValue)
{
headers[HeaderNames.ContentLength] = HeaderUtilities.FormatInt64(value.Value);
}
else
{
headers.Remove(HeaderNames.ContentLength);
}
}
} }
} }

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Owin namespace Microsoft.AspNetCore.Owin
{ {
@ -42,6 +43,40 @@ namespace Microsoft.AspNetCore.Owin
set { Inner[key] = value; } set { Inner[key] = value; }
} }
public long? ContentLength
{
get
{
long value;
string[] rawValue;
if (!Inner.TryGetValue(HeaderNames.ContentLength, out rawValue))
{
return null;
}
if (rawValue.Length == 1 &&
!string.IsNullOrWhiteSpace(rawValue[0]) &&
HeaderUtilities.TryParseInt64(new StringSegment(rawValue[0]).Trim(), out value))
{
return value;
}
return null;
}
set
{
if (value.HasValue)
{
Inner[HeaderNames.ContentLength] = (StringValues)HeaderUtilities.FormatInt64(value.Value);
}
else
{
Inner.Remove(HeaderNames.ContentLength);
}
}
}
int ICollection<KeyValuePair<string, StringValues>>.Count => Inner.Count; int ICollection<KeyValuePair<string, StringValues>>.Count => Inner.Count;
bool ICollection<KeyValuePair<string, StringValues>>.IsReadOnly => Inner.IsReadOnly; bool ICollection<KeyValuePair<string, StringValues>>.IsReadOnly => Inner.IsReadOnly;