Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit adfa582

Browse files
committed
#407 Add ContentLength to IHeaderDictionary
1 parent b7d2f8c commit adfa582

File tree

6 files changed

+73
-52
lines changed

6 files changed

+73
-52
lines changed

src/Microsoft.AspNetCore.Http.Features/IHeaderDictionary.cs

+5
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ public interface IHeaderDictionary : IDictionary<string, StringValues>
1717
/// <param name="key"></param>
1818
/// <returns>The stored value, or StringValues.Empty if the key is not present.</returns>
1919
new StringValues this[string key] { get; set; }
20+
21+
/// <summary>
22+
/// Strongly typed access to the Content-Type header. Implementations must keep this in sync with the string representation.
23+
/// </summary>
24+
long? ContentLength { get; set; }
2025
}
2126
}

src/Microsoft.AspNetCore.Http/HeaderDictionary.cs

+29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Collections.Generic;
77
using Microsoft.Extensions.Primitives;
8+
using Microsoft.Net.Http.Headers;
89

910
namespace Microsoft.AspNetCore.Http
1011
{
@@ -97,6 +98,34 @@ StringValues IDictionary<string, StringValues>.this[string key]
9798
set { this[key] = value; }
9899
}
99100

101+
public long? ContentLength
102+
{
103+
get
104+
{
105+
long value;
106+
var rawValue = this[HeaderNames.ContentLength];
107+
if (rawValue.Count == 1 &&
108+
!string.IsNullOrWhiteSpace(rawValue[0]) &&
109+
HeaderUtilities.TryParseInt64(new StringSegment(rawValue[0]).Trim(), out value))
110+
{
111+
return value;
112+
}
113+
114+
return null;
115+
}
116+
set
117+
{
118+
if (value.HasValue)
119+
{
120+
this[HeaderNames.ContentLength] = HeaderUtilities.FormatInt64(value.Value);
121+
}
122+
else
123+
{
124+
this.Remove(HeaderNames.ContentLength);
125+
}
126+
}
127+
}
128+
100129
/// <summary>
101130
/// Gets the number of elements contained in the <see cref="HeaderDictionary" />;.
102131
/// </summary>

src/Microsoft.AspNetCore.Http/Internal/DefaultHttpRequest.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,8 @@ public override QueryString QueryString
7272

7373
public override long? ContentLength
7474
{
75-
get
76-
{
77-
return ParsingHelpers.GetContentLength(Headers);
78-
}
79-
set
80-
{
81-
ParsingHelpers.SetContentLength(Headers, value);
82-
}
75+
get { return Headers.ContentLength; }
76+
set { Headers.ContentLength = value; }
8377
}
8478

8579
public override Stream Body

src/Microsoft.AspNetCore.Http/Internal/DefaultHttpResponse.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,8 @@ public override Stream Body
6363

6464
public override long? ContentLength
6565
{
66-
get
67-
{
68-
return ParsingHelpers.GetContentLength(Headers);
69-
}
70-
set
71-
{
72-
ParsingHelpers.SetContentLength(Headers, value);
73-
}
66+
get { return Headers.ContentLength; }
67+
set { Headers.ContentLength = value; }
7468
}
7569

7670
public override string ContentType

src/Microsoft.AspNetCore.Http/Internal/ParsingHelpers.cs

-36
Original file line numberDiff line numberDiff line change
@@ -403,41 +403,5 @@ private static string DeQuote(string value)
403403

404404
return value;
405405
}
406-
407-
public static long? GetContentLength(IHeaderDictionary headers)
408-
{
409-
if (headers == null)
410-
{
411-
throw new ArgumentNullException(nameof(headers));
412-
}
413-
414-
long value;
415-
var rawValue = headers[HeaderNames.ContentLength];
416-
if (rawValue.Count == 1 &&
417-
!string.IsNullOrWhiteSpace(rawValue[0]) &&
418-
HeaderUtilities.TryParseInt64(new StringSegment(rawValue[0]).Trim(), out value))
419-
{
420-
return value;
421-
}
422-
423-
return null;
424-
}
425-
426-
public static void SetContentLength(IHeaderDictionary headers, long? value)
427-
{
428-
if (headers == null)
429-
{
430-
throw new ArgumentNullException(nameof(headers));
431-
}
432-
433-
if (value.HasValue)
434-
{
435-
headers[HeaderNames.ContentLength] = HeaderUtilities.FormatInt64(value.Value);
436-
}
437-
else
438-
{
439-
headers.Remove(HeaderNames.ContentLength);
440-
}
441-
}
442406
}
443407
}

src/Microsoft.AspNetCore.Owin/DictionaryStringValuesWrapper.cs

+35
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.Extensions.Primitives;
9+
using Microsoft.Net.Http.Headers;
910

1011
namespace Microsoft.AspNetCore.Owin
1112
{
@@ -42,6 +43,40 @@ StringValues IDictionary<string, StringValues>.this[string key]
4243
set { Inner[key] = value; }
4344
}
4445

46+
public long? ContentLength
47+
{
48+
get
49+
{
50+
long value;
51+
52+
string[] rawValue;
53+
if (!Inner.TryGetValue(HeaderNames.ContentLength, out rawValue))
54+
{
55+
return null;
56+
}
57+
58+
if (rawValue.Length == 1 &&
59+
!string.IsNullOrWhiteSpace(rawValue[0]) &&
60+
HeaderUtilities.TryParseInt64(new StringSegment(rawValue[0]).Trim(), out value))
61+
{
62+
return value;
63+
}
64+
65+
return null;
66+
}
67+
set
68+
{
69+
if (value.HasValue)
70+
{
71+
Inner[HeaderNames.ContentLength] = (StringValues)HeaderUtilities.FormatInt64(value.Value);
72+
}
73+
else
74+
{
75+
Inner.Remove(HeaderNames.ContentLength);
76+
}
77+
}
78+
}
79+
4580
int ICollection<KeyValuePair<string, StringValues>>.Count => Inner.Count;
4681

4782
bool ICollection<KeyValuePair<string, StringValues>>.IsReadOnly => Inner.IsReadOnly;

0 commit comments

Comments
 (0)