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

Reduce lifetime of header values #608

Merged
merged 4 commits into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Microsoft.AspNetCore.Server.Kestrel/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void Start()
}

// Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
if (ConnectionFilter == null)
if (ServerInformation.ConnectionFilter == null)
{
SocketInput = _rawSocketInput;
SocketOutput = _rawSocketOutput;
Expand All @@ -81,7 +81,7 @@ public void Start()

try
{
ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, state) =>
ServerInformation.ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, state) =>
{
var connection = (Connection)state;

Expand Down
132 changes: 90 additions & 42 deletions src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Copy link
Member

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.

Copy link
Member

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.

}


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;

Expand All @@ -207,8 +237,6 @@ public void Reset()
_autoChunk = false;
_applicationException = null;

_requestHeaders.Reset();
ResetResponseHeaders();
ResetFeatureCollection();

Scheme = null;
Expand All @@ -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;
Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this pattern is really achieving anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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>
Expand Down Expand Up @@ -292,8 +328,8 @@ public void Abort()
{
_requestProcessingStopping = true;

_requestBody?.Abort();
_responseBody?.Abort();
_frameStreams?.RequestBody.Abort();
_frameStreams?.ResponseBody.Abort();

try
{
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -626,7 +674,7 @@ private void CreateResponseHeader(
}
}

if (_keepAlive && !_responseHeaders.HasTransferEncoding && !_responseHeaders.HasContentLength)
if (_keepAlive && !responseHeaders.HasTransferEncoding && !responseHeaders.HasContentLength)
{
if (appCompleted)
{
Expand All @@ -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
{
Expand All @@ -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);
Expand Down
Loading