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

Commit ecca980

Browse files
benaadamsTratcher
authored andcommitted
Implement IHeaderDictionary.ContentLength
1 parent 815db3d commit ecca980

File tree

18 files changed

+3854
-5366
lines changed

18 files changed

+3854
-5366
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/BadHttpRequestException.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ internal static BadHttpRequestException GetException(RequestRejectionReason reas
4343
case RequestRejectionReason.MalformedRequestInvalidHeaders:
4444
ex = new BadHttpRequestException("Malformed request: invalid headers.", StatusCodes.Status400BadRequest);
4545
break;
46+
case RequestRejectionReason.MultipleContentLengths:
47+
ex = new BadHttpRequestException("Multiple Content-Length headers.", StatusCodes.Status400BadRequest);
48+
break;
4649
case RequestRejectionReason.UnexpectedEndOfRequestContent:
4750
ex = new BadHttpRequestException("Unexpected end of request content.", StatusCodes.Status400BadRequest);
4851
break;

src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public abstract partial class Frame : IFrameControl
4141
private static readonly byte[] _bytesConnectionKeepAlive = Encoding.ASCII.GetBytes("\r\nConnection: keep-alive");
4242
private static readonly byte[] _bytesTransferEncodingChunked = Encoding.ASCII.GetBytes("\r\nTransfer-Encoding: chunked");
4343
private static readonly byte[] _bytesHttpVersion11 = Encoding.ASCII.GetBytes("HTTP/1.1 ");
44-
private static readonly byte[] _bytesContentLengthZero = Encoding.ASCII.GetBytes("\r\nContent-Length: 0");
4544
private static readonly byte[] _bytesEndHeaders = Encoding.ASCII.GetBytes("\r\n\r\n");
4645
private static readonly byte[] _bytesServer = Encoding.ASCII.GetBytes("\r\nServer: Kestrel");
4746

@@ -657,12 +656,12 @@ private void VerifyAndUpdateWrite(int count)
657656

658657
if (responseHeaders != null &&
659658
!responseHeaders.HasTransferEncoding &&
660-
responseHeaders.HasContentLength &&
661-
_responseBytesWritten + count > responseHeaders.HeaderContentLengthValue.Value)
659+
responseHeaders.ContentLength.HasValue &&
660+
_responseBytesWritten + count > responseHeaders.ContentLength.Value)
662661
{
663662
_keepAlive = false;
664663
throw new InvalidOperationException(
665-
$"Response Content-Length mismatch: too many bytes written ({_responseBytesWritten + count} of {responseHeaders.HeaderContentLengthValue.Value}).");
664+
$"Response Content-Length mismatch: too many bytes written ({_responseBytesWritten + count} of {responseHeaders.ContentLength.Value}).");
666665
}
667666

668667
_responseBytesWritten += count;
@@ -679,8 +678,8 @@ private void CheckLastWrite()
679678
// Called after VerifyAndUpdateWrite(), so _responseBytesWritten has already been updated.
680679
if (responseHeaders != null &&
681680
!responseHeaders.HasTransferEncoding &&
682-
responseHeaders.HasContentLength &&
683-
_responseBytesWritten == responseHeaders.HeaderContentLengthValue.Value)
681+
responseHeaders.ContentLength.HasValue &&
682+
_responseBytesWritten == responseHeaders.ContentLength.Value)
684683
{
685684
_abortedCts = null;
686685
}
@@ -692,8 +691,8 @@ protected void VerifyResponseContentLength()
692691

693692
if (!HttpMethods.IsHead(Method) &&
694693
!responseHeaders.HasTransferEncoding &&
695-
responseHeaders.HeaderContentLengthValue.HasValue &&
696-
_responseBytesWritten < responseHeaders.HeaderContentLengthValue.Value)
694+
responseHeaders.ContentLength.HasValue &&
695+
_responseBytesWritten < responseHeaders.ContentLength.Value)
697696
{
698697
// We need to close the connection if any bytes were written since the client
699698
// cannot be certain of how many bytes it will receive.
@@ -703,7 +702,7 @@ protected void VerifyResponseContentLength()
703702
}
704703

705704
ReportApplicationError(new InvalidOperationException(
706-
$"Response Content-Length mismatch: too few bytes written ({_responseBytesWritten} of {responseHeaders.HeaderContentLengthValue.Value})."));
705+
$"Response Content-Length mismatch: too few bytes written ({_responseBytesWritten} of {responseHeaders.ContentLength.Value})."));
707706
}
708707
}
709708

@@ -920,13 +919,13 @@ private void CreateResponseHeader(
920919
// automatically for HEAD requests or 204, 205, 304 responses.
921920
if (_canHaveBody)
922921
{
923-
if (!hasTransferEncoding && !responseHeaders.HasContentLength)
922+
if (!hasTransferEncoding && !responseHeaders.ContentLength.HasValue)
924923
{
925924
if (appCompleted && StatusCode != StatusCodes.Status101SwitchingProtocols)
926925
{
927926
// Since the app has completed and we are only now generating
928927
// the headers we can safely set the Content-Length to 0.
929-
responseHeaders.SetRawContentLength("0", _bytesContentLengthZero);
928+
responseHeaders.ContentLength = 0;
930929
}
931930
else
932931
{
@@ -1468,7 +1467,7 @@ private void SetErrorResponseHeaders(int statusCode)
14681467
var dateHeaderValues = DateHeaderValueManager.GetDateHeaderValues();
14691468

14701469
responseHeaders.SetRawDate(dateHeaderValues.String, dateHeaderValues.Bytes);
1471-
responseHeaders.SetRawContentLength("0", _bytesContentLengthZero);
1470+
responseHeaders.ContentLength = 0;
14721471

14731472
if (ServerOptions.AddServerHeader)
14741473
{

0 commit comments

Comments
 (0)