Cache the ContentLength header value
This commit is contained in:
parent
56bd85aaf2
commit
119945ca02
|
|
@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
_enableResponseCaching = enableResponseCaching;
|
_enableResponseCaching = enableResponseCaching;
|
||||||
|
|
||||||
// Pre-initialize any fields that are not lazy at the lower level.
|
// Pre-initialize any fields that are not lazy at the lower level.
|
||||||
_requestHeaders = new HeaderDictionary(Request.Headers);
|
_requestHeaders = Request.Headers;
|
||||||
_httpMethod = Request.Method;
|
_httpMethod = Request.Method;
|
||||||
_path = Request.Path;
|
_path = Request.Path;
|
||||||
_pathBase = Request.PathBase;
|
_pathBase = Request.PathBase;
|
||||||
|
|
@ -82,7 +82,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
_user = _requestContext.User;
|
_user = _requestContext.User;
|
||||||
|
|
||||||
_responseStream = new ResponseStream(requestContext.Response.Body, OnStart);
|
_responseStream = new ResponseStream(requestContext.Response.Body, OnStart);
|
||||||
_responseHeaders = new HeaderDictionary(Response.Headers);
|
_responseHeaders = Response.Headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IFeatureCollection Features => _features;
|
internal IFeatureCollection Features => _features;
|
||||||
|
|
|
||||||
|
|
@ -1,204 +0,0 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.Extensions.Primitives;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Server.HttpSys
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a wrapper for RequestHeaders and ResponseHeaders.
|
|
||||||
/// </summary>
|
|
||||||
internal class HeaderDictionary : IHeaderDictionary
|
|
||||||
{
|
|
||||||
public HeaderDictionary(IDictionary<string, StringValues> store)
|
|
||||||
{
|
|
||||||
Store = store;
|
|
||||||
}
|
|
||||||
|
|
||||||
private IDictionary<string, StringValues> Store { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get or sets the associated value from the collection as a single string.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The header name.</param>
|
|
||||||
/// <returns>the associated value from the collection as a StringValues or StringValues.Empty if the key is not present.</returns>
|
|
||||||
public StringValues this[string key]
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
StringValues value;
|
|
||||||
if (TryGetValue(key, out value))
|
|
||||||
{
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
return StringValues.Empty;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (key == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StringValues.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
Store.Remove(key);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Store[key] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Throws KeyNotFoundException if the key is not present.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The header name.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
StringValues IDictionary<string, StringValues>.this[string key]
|
|
||||||
{
|
|
||||||
get { return Store[key]; }
|
|
||||||
set { this[key] = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the number of elements contained in the <see cref="T:HeaderDictionary" />;.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The number of elements contained in the <see cref="T:HeaderDictionary" />.</returns>
|
|
||||||
public int Count
|
|
||||||
{
|
|
||||||
get { return Store.Count; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value that indicates whether the <see cref="T:HeaderDictionary" /> is in read-only mode.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>true if the <see cref="T:HeaderDictionary" /> is in read-only mode; otherwise, false.</returns>
|
|
||||||
public bool IsReadOnly
|
|
||||||
{
|
|
||||||
get { return Store.IsReadOnly; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICollection<string> Keys
|
|
||||||
{
|
|
||||||
get { return Store.Keys; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public ICollection<StringValues> Values
|
|
||||||
{
|
|
||||||
get { return Store.Values; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a new list of items to the collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The item to add.</param>
|
|
||||||
public void Add(KeyValuePair<string, StringValues> item)
|
|
||||||
{
|
|
||||||
Store.Add(item.Key, item.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the given header and values to the collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The header name.</param>
|
|
||||||
/// <param name="value">The header values.</param>
|
|
||||||
public void Add(string key, StringValues value)
|
|
||||||
{
|
|
||||||
Store.Add(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Clears the entire list of objects.
|
|
||||||
/// </summary>
|
|
||||||
public void Clear()
|
|
||||||
{
|
|
||||||
Store.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a value indicating whether the specified object occurs within this collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The item.</param>
|
|
||||||
/// <returns>true if the specified object occurs within this collection; otherwise, false.</returns>
|
|
||||||
public bool Contains(KeyValuePair<string, StringValues> item)
|
|
||||||
{
|
|
||||||
return Store.Contains(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines whether the <see cref="T:HeaderDictionary" /> contains a specific key.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The key.</param>
|
|
||||||
/// <returns>true if the <see cref="T:HeaderDictionary" /> contains a specific key; otherwise, false.</returns>
|
|
||||||
public bool ContainsKey(string key)
|
|
||||||
{
|
|
||||||
return Store.ContainsKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Copies the <see cref="T:HeaderDictionary" /> elements to a one-dimensional Array instance at the specified index.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="array">The one-dimensional Array that is the destination of the specified objects copied from
|
|
||||||
/// the <see cref="T:HeaderDictionary" />.</param>
|
|
||||||
/// <param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param>
|
|
||||||
public void CopyTo(KeyValuePair<string, StringValues>[] array, int arrayIndex)
|
|
||||||
{
|
|
||||||
Store.CopyTo(array, arrayIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes the given item from the the collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The item.</param>
|
|
||||||
/// <returns>true if the specified object was removed from the collection; otherwise, false.</returns>
|
|
||||||
public bool Remove(KeyValuePair<string, StringValues> item)
|
|
||||||
{
|
|
||||||
return Store.Remove(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes the given header from the collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The header name.</param>
|
|
||||||
/// <returns>true if the specified object was removed from the collection; otherwise, false.</returns>
|
|
||||||
public bool Remove(string key)
|
|
||||||
{
|
|
||||||
return Store.Remove(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Retrieves a value from the dictionary.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The header name.</param>
|
|
||||||
/// <param name="value">The value.</param>
|
|
||||||
/// <returns>true if the <see cref="T:HeaderDictionary" /> contains the key; otherwise, false.</returns>
|
|
||||||
public bool TryGetValue(string key, out StringValues value)
|
|
||||||
{
|
|
||||||
return Store.TryGetValue(key, out value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns an enumerator that iterates through a collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>An <see cref="T:IEnumerator" /> object that can be used to iterate through the collection.</returns>
|
|
||||||
public IEnumerator GetEnumerator()
|
|
||||||
{
|
|
||||||
return Store.GetEnumerator();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns an enumerator that iterates through a collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>An <see cref="T:IEnumerator" /> object that can be used to iterate through the collection.</returns>
|
|
||||||
IEnumerator<KeyValuePair<string, StringValues>> IEnumerable<KeyValuePair<string, StringValues>>.GetEnumerator()
|
|
||||||
{
|
|
||||||
return Store.GetEnumerator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -4,12 +4,17 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
|
using Microsoft.Net.Http.Headers;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Server.HttpSys
|
namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
{
|
{
|
||||||
internal class HeaderCollection : IDictionary<string, StringValues>
|
internal class HeaderCollection : IHeaderDictionary
|
||||||
{
|
{
|
||||||
|
private long? _contentLength;
|
||||||
|
private StringValues _contentLengthText;
|
||||||
|
|
||||||
public HeaderCollection()
|
public HeaderCollection()
|
||||||
: this(new Dictionary<string, StringValues>(4, StringComparer.OrdinalIgnoreCase))
|
: this(new Dictionary<string, StringValues>(4, StringComparer.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
|
|
@ -75,6 +80,52 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
get { return Store.Values; }
|
get { return Store.Values; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long? ContentLength
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
long value;
|
||||||
|
var rawValue = this[HttpKnownHeaderNames.ContentLength];
|
||||||
|
|
||||||
|
if (_contentLengthText.Equals(rawValue))
|
||||||
|
{
|
||||||
|
return _contentLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rawValue.Count == 1 &&
|
||||||
|
!string.IsNullOrWhiteSpace(rawValue[0]) &&
|
||||||
|
HeaderUtilities.TryParseInt64(new StringSegment(rawValue[0]).Trim(), out value))
|
||||||
|
{
|
||||||
|
_contentLengthText = rawValue;
|
||||||
|
_contentLength = value;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
ThrowIfReadOnly();
|
||||||
|
|
||||||
|
if (value.HasValue)
|
||||||
|
{
|
||||||
|
if (value.Value < 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException("value", value.Value, "Cannot be negative.");
|
||||||
|
}
|
||||||
|
_contentLengthText = HeaderUtilities.FormatInt64(value.Value);
|
||||||
|
this[HttpKnownHeaderNames.ContentLength] = _contentLengthText;
|
||||||
|
_contentLength = value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Remove(HttpKnownHeaderNames.ContentLength);
|
||||||
|
_contentLengthText = StringValues.Empty;
|
||||||
|
_contentLength = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Add(KeyValuePair<string, StringValues> item)
|
public void Add(KeyValuePair<string, StringValues> item)
|
||||||
{
|
{
|
||||||
ThrowIfReadOnly();
|
ThrowIfReadOnly();
|
||||||
|
|
|
||||||
|
|
@ -147,48 +147,8 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
// Header accessors
|
// Header accessors
|
||||||
public long? ContentLength
|
public long? ContentLength
|
||||||
{
|
{
|
||||||
get
|
get { return Headers.ContentLength; }
|
||||||
{
|
set { Headers.ContentLength = value; }
|
||||||
string contentLengthString = Headers[HttpKnownHeaderNames.ContentLength];
|
|
||||||
long contentLength;
|
|
||||||
if (!string.IsNullOrWhiteSpace(contentLengthString))
|
|
||||||
{
|
|
||||||
contentLengthString = contentLengthString.Trim();
|
|
||||||
if (string.Equals(Constants.Zero, contentLengthString, StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if (long.TryParse(contentLengthString, NumberStyles.None, CultureInfo.InvariantCulture.NumberFormat, out contentLength))
|
|
||||||
{
|
|
||||||
return contentLength;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
CheckResponseStarted();
|
|
||||||
if (!value.HasValue)
|
|
||||||
{
|
|
||||||
Headers.Remove(HttpKnownHeaderNames.ContentLength);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (value.Value < 0)
|
|
||||||
{
|
|
||||||
throw new ArgumentOutOfRangeException("value", value.Value, "Cannot be negative.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value.Value == 0)
|
|
||||||
{
|
|
||||||
Headers[HttpKnownHeaderNames.ContentLength] = Constants.Zero;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Headers[HttpKnownHeaderNames.ContentLength] = value.Value.ToString(CultureInfo.InvariantCulture);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
|
||||||
Assert.Equal("TEST", requestInfo.Method);
|
Assert.Equal("TEST", requestInfo.Method);
|
||||||
requestInfo.Body = new MemoryStream();
|
requestInfo.Body = new MemoryStream();
|
||||||
Assert.IsType<MemoryStream>(requestInfo.Body);
|
Assert.IsType<MemoryStream>(requestInfo.Body);
|
||||||
var customHeaders = new HeaderDictionary(new HeaderCollection());
|
var customHeaders = new HeaderCollection();
|
||||||
requestInfo.Headers = customHeaders;
|
requestInfo.Headers = customHeaders;
|
||||||
Assert.Same(customHeaders, requestInfo.Headers);
|
Assert.Same(customHeaders, requestInfo.Headers);
|
||||||
requestInfo.Scheme = "abcd";
|
requestInfo.Scheme = "abcd";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue