This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 521
Reduce lifetime of header values #608
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,6 @@ public abstract partial class Frame : FrameContext, IFrameControl | |
private static readonly byte[] _bytesHttpVersion1_1 = Encoding.ASCII.GetBytes("HTTP/1.1 "); | ||
private static readonly byte[] _bytesContentLengthZero = Encoding.ASCII.GetBytes("\r\nContent-Length: 0"); | ||
private static readonly byte[] _bytesSpace = Encoding.ASCII.GetBytes(" "); | ||
private static readonly byte[] _bytesServer = Encoding.ASCII.GetBytes("\r\nServer: Kestrel"); | ||
private static readonly byte[] _bytesDate = Encoding.ASCII.GetBytes("Date: "); | ||
private static readonly byte[] _bytesEndHeaders = Encoding.ASCII.GetBytes("\r\n\r\n"); | ||
|
||
private static Vector<byte> _vectorCRs = new Vector<byte>((byte)'\r'); | ||
|
@@ -46,8 +44,10 @@ public abstract partial class Frame : FrameContext, IFrameControl | |
|
||
private readonly object _onStartingSync = new Object(); | ||
private readonly object _onCompletedSync = new Object(); | ||
protected readonly FrameRequestHeaders _requestHeaders = new FrameRequestHeaders(); | ||
private readonly FrameResponseHeaders _responseHeaders = new FrameResponseHeaders(); | ||
|
||
protected bool _poolingPermitted = true; | ||
private Headers _frameHeaders; | ||
private Streams _frameStreams; | ||
|
||
protected List<KeyValuePair<Func<object, Task>, object>> _onStarting; | ||
|
||
|
@@ -60,10 +60,6 @@ public abstract partial class Frame : FrameContext, IFrameControl | |
protected CancellationTokenSource _abortedCts; | ||
protected CancellationToken? _manuallySetRequestAbortToken; | ||
|
||
internal FrameRequestStream _requestBody; | ||
internal FrameResponseStream _responseBody; | ||
internal FrameDuplexStream _duplexStream; | ||
|
||
protected bool _responseStarted; | ||
protected bool _keepAlive; | ||
private bool _autoChunk; | ||
|
@@ -92,12 +88,6 @@ public Frame(ConnectionContext context, | |
_localEndPoint = localEndPoint; | ||
_prepareRequest = prepareRequest; | ||
_pathBase = context.ServerAddress.PathBase; | ||
if (ReuseStreams) | ||
{ | ||
_requestBody = new FrameRequestStream(); | ||
_responseBody = new FrameResponseStream(this); | ||
_duplexStream = new FrameDuplexStream(_requestBody, _responseBody); | ||
} | ||
|
||
FrameControl = this; | ||
Reset(); | ||
|
@@ -197,8 +187,48 @@ public bool HasResponseStarted | |
get { return _responseStarted; } | ||
} | ||
|
||
protected FrameRequestHeaders FrameRequestHeaders => _frameHeaders.RequestHeaders; | ||
|
||
public Frame InitializeHeaders() | ||
{ | ||
_frameHeaders = HttpComponentFactory.CreateHeaders(DateHeaderValueManager); | ||
RequestHeaders = _frameHeaders.RequestHeaders; | ||
ResponseHeaders = _frameHeaders.ResponseHeaders; | ||
return this; | ||
} | ||
|
||
|
||
public void InitializeStreams(MessageBody messageBody) | ||
{ | ||
_frameStreams = HttpComponentFactory.CreateStreams(this); | ||
|
||
RequestBody = _frameStreams.RequestBody.StartAcceptingReads(messageBody); | ||
ResponseBody = _frameStreams.ResponseBody.StartAcceptingWrites(); | ||
DuplexStream = _frameStreams.DuplexStream; | ||
} | ||
|
||
public void PauseStreams() | ||
{ | ||
_frameStreams.RequestBody.PauseAcceptingReads(); | ||
_frameStreams.ResponseBody.PauseAcceptingWrites(); | ||
} | ||
|
||
public void ResumeStreams() | ||
{ | ||
_frameStreams.RequestBody.ResumeAcceptingReads(); | ||
_frameStreams.ResponseBody.ResumeAcceptingWrites(); | ||
} | ||
|
||
public void StopStreams() | ||
{ | ||
_frameStreams.RequestBody.StopAcceptingReads(); | ||
_frameStreams.ResponseBody.StopAcceptingWrites(); | ||
} | ||
|
||
public void Reset() | ||
{ | ||
ResetComponents(poolingPermitted: true); | ||
|
||
_onStarting = null; | ||
_onCompleted = null; | ||
|
||
|
@@ -207,8 +237,6 @@ public void Reset() | |
_autoChunk = false; | ||
_applicationException = null; | ||
|
||
_requestHeaders.Reset(); | ||
ResetResponseHeaders(); | ||
ResetFeatureCollection(); | ||
|
||
Scheme = null; | ||
|
@@ -218,13 +246,8 @@ public void Reset() | |
Path = null; | ||
QueryString = null; | ||
_httpVersion = HttpVersionType.Unknown; | ||
RequestHeaders = _requestHeaders; | ||
RequestBody = null; | ||
StatusCode = 200; | ||
ReasonPhrase = null; | ||
ResponseHeaders = _responseHeaders; | ||
ResponseBody = null; | ||
DuplexStream = null; | ||
|
||
var httpConnectionFeature = this as IHttpConnectionFeature; | ||
httpConnectionFeature.RemoteIpAddress = _remoteEndPoint?.Address; | ||
|
@@ -239,15 +262,28 @@ public void Reset() | |
_abortedCts = null; | ||
} | ||
|
||
public void ResetResponseHeaders() | ||
protected void ResetComponents(bool poolingPermitted) | ||
{ | ||
_responseHeaders.Reset(); | ||
_responseHeaders.SetRawDate( | ||
DateHeaderValueManager.GetDateHeaderValue(), | ||
DateHeaderValueManager.GetDateHeaderValueBytes()); | ||
_responseHeaders.SetRawServer( | ||
"Kestrel", | ||
_bytesServer); | ||
if (_frameHeaders != null) | ||
{ | ||
var frameHeaders = _frameHeaders; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this pattern is really achieving anything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is to detect double call; also to drop references that have been returned to pool |
||
_frameHeaders = null; | ||
|
||
RequestHeaders = null; | ||
ResponseHeaders = null; | ||
HttpComponentFactory.DisposeHeaders(frameHeaders, poolingPermitted); | ||
} | ||
|
||
if (_frameStreams != null) | ||
{ | ||
var frameStreams = _frameStreams; | ||
_frameStreams = null; | ||
|
||
RequestBody = null; | ||
ResponseBody = null; | ||
DuplexStream = null; | ||
HttpComponentFactory.DisposeStreams(frameStreams, poolingPermitted); | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
@@ -292,8 +328,8 @@ public void Abort() | |
{ | ||
_requestProcessingStopping = true; | ||
|
||
_requestBody?.Abort(); | ||
_responseBody?.Abort(); | ||
_frameStreams?.RequestBody.Abort(); | ||
_frameStreams?.ResponseBody.Abort(); | ||
|
||
try | ||
{ | ||
|
@@ -560,8 +596,17 @@ protected Task ProduceEnd() | |
StatusCode = 500; | ||
ReasonPhrase = null; | ||
|
||
ResetResponseHeaders(); | ||
_responseHeaders.SetRawContentLength("0", _bytesContentLengthZero); | ||
var responseHeaders = _frameHeaders.ResponseHeaders; | ||
responseHeaders.Reset(); | ||
responseHeaders.SetRawDate( | ||
DateHeaderValueManager.GetDateHeaderValue(), | ||
DateHeaderValueManager.GetDateHeaderValueBytes()); | ||
responseHeaders.SetRawServer( | ||
"Kestrel", | ||
Headers.BytesServer); | ||
responseHeaders.SetRawContentLength("0", _bytesContentLengthZero); | ||
|
||
ResponseHeaders = responseHeaders; | ||
} | ||
} | ||
|
||
|
@@ -614,10 +659,13 @@ private void CreateResponseHeader( | |
byte[] statusBytes, | ||
bool appCompleted) | ||
{ | ||
var responseHeaders = _frameHeaders.ResponseHeaders; | ||
responseHeaders.SetReadOnly(); | ||
|
||
var end = SocketOutput.ProducingStart(); | ||
if (_keepAlive) | ||
{ | ||
foreach (var connectionValue in _responseHeaders.HeaderConnection) | ||
foreach (var connectionValue in responseHeaders.HeaderConnection) | ||
{ | ||
if (connectionValue.IndexOf("close", StringComparison.OrdinalIgnoreCase) != -1) | ||
{ | ||
|
@@ -626,7 +674,7 @@ private void CreateResponseHeader( | |
} | ||
} | ||
|
||
if (_keepAlive && !_responseHeaders.HasTransferEncoding && !_responseHeaders.HasContentLength) | ||
if (_keepAlive && !responseHeaders.HasTransferEncoding && !responseHeaders.HasContentLength) | ||
{ | ||
if (appCompleted) | ||
{ | ||
|
@@ -636,15 +684,15 @@ private void CreateResponseHeader( | |
{ | ||
// Since the app has completed and we are only now generating | ||
// the headers we can safely set the Content-Length to 0. | ||
_responseHeaders.SetRawContentLength("0", _bytesContentLengthZero); | ||
responseHeaders.SetRawContentLength("0", _bytesContentLengthZero); | ||
} | ||
} | ||
else | ||
{ | ||
if (_httpVersion == HttpVersionType.Http1_1) | ||
{ | ||
_autoChunk = true; | ||
_responseHeaders.SetRawTransferEncoding("chunked", _bytesTransferEncodingChunked); | ||
responseHeaders.SetRawTransferEncoding("chunked", _bytesTransferEncodingChunked); | ||
} | ||
else | ||
{ | ||
|
@@ -653,18 +701,18 @@ private void CreateResponseHeader( | |
} | ||
} | ||
|
||
if (_keepAlive == false && _responseHeaders.HasConnection == false && _httpVersion == HttpVersionType.Http1_1) | ||
if (_keepAlive == false && responseHeaders.HasConnection == false && _httpVersion == HttpVersionType.Http1_1) | ||
{ | ||
_responseHeaders.SetRawConnection("close", _bytesConnectionClose); | ||
responseHeaders.SetRawConnection("close", _bytesConnectionClose); | ||
} | ||
else if (_keepAlive && _responseHeaders.HasConnection == false && _httpVersion == HttpVersionType.Http1_0) | ||
else if (_keepAlive && responseHeaders.HasConnection == false && _httpVersion == HttpVersionType.Http1_0) | ||
{ | ||
_responseHeaders.SetRawConnection("keep-alive", _bytesConnectionKeepAlive); | ||
responseHeaders.SetRawConnection("keep-alive", _bytesConnectionKeepAlive); | ||
} | ||
|
||
end.CopyFrom(_httpVersion == HttpVersionType.Http1_1 ? _bytesHttpVersion1_1 : _bytesHttpVersion1_0); | ||
end.CopyFrom(statusBytes); | ||
_responseHeaders.CopyTo(ref end); | ||
responseHeaders.CopyTo(ref end); | ||
end.CopyFrom(_bytesEndHeaders, 0, _bytesEndHeaders.Length); | ||
|
||
SocketOutput.ProducingComplete(end); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't return
this
. I don't see you using this anywhere anyway.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, don't worry about this. I changed it while addressing my own PR feedback readying a merge.