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

Commit 487dcfc

Browse files
committed
Use ContentLength as primary data source
1 parent f32058c commit 487dcfc

File tree

9 files changed

+1527
-1573
lines changed

9 files changed

+1527
-1573
lines changed

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

Lines changed: 8 additions & 9 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

@@ -658,11 +657,11 @@ private void VerifyAndUpdateWrite(int count)
658657
if (responseHeaders != null &&
659658
!responseHeaders.HasTransferEncoding &&
660659
responseHeaders.HasContentLength &&
661-
_responseBytesWritten + count > responseHeaders.HeaderContentLengthValue.Value)
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;
@@ -680,7 +679,7 @@ private void CheckLastWrite()
680679
if (responseHeaders != null &&
681680
!responseHeaders.HasTransferEncoding &&
682681
responseHeaders.HasContentLength &&
683-
_responseBytesWritten == responseHeaders.HeaderContentLengthValue.Value)
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

@@ -926,7 +925,7 @@ private void CreateResponseHeader(
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)