Prevent modifying ContentLength after starting response #14056 (#15089)

This commit is contained in:
Chris Ross 2019-10-17 13:40:08 -07:00 committed by GitHub
parent 23f3a10965
commit 4dd9bfc492
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -27,6 +27,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
get { return _contentLength; }
set
{
if (_isReadOnly)
{
ThrowHeadersReadOnlyException();
}
if (value.HasValue && value.Value < 0)
{
ThrowInvalidContentLengthException(value.Value);

View File

@ -138,6 +138,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
Assert.Throws<InvalidOperationException>(() => ((IDictionary<string, StringValues>)headers).Add("my-header", new[] { "value" }));
}
[Fact]
public void ThrowsWhenSettingContentLengthPropertyAfterReadOnlyIsSet()
{
var headers = new HttpResponseHeaders();
headers.SetReadOnly();
Assert.Throws<InvalidOperationException>(() => headers.ContentLength = null);
}
[Fact]
public void ThrowsWhenChangingHeaderAfterReadOnlyIsSet()
{