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

Improved RequestProcessingAsync legibility #424

Closed
wants to merge 1 commit into from
Closed
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
84 changes: 65 additions & 19 deletions src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,28 +263,15 @@ public async Task RequestProcessingAsync()
{
try
{
var terminated = false;
while (!terminated && !_requestProcessingStopping)
while (!_requestProcessingStopping)
{
while (!terminated && !_requestProcessingStopping && !TakeStartLine(SocketInput))
if (!await ReadStartLineAsync() ||
!await ReadHeadersAsync())
{
terminated = SocketInput.RemoteIntakeFin;
if (!terminated)
{
await SocketInput;
}
}

while (!terminated && !_requestProcessingStopping && !TakeMessageHeaders(SocketInput, _requestHeaders))
{
terminated = SocketInput.RemoteIntakeFin;
if (!terminated)
{
await SocketInput;
}
break;
}

if (!terminated && !_requestProcessingStopping)
if (!_requestProcessingStopping)
{
var messageBody = MessageBody.For(HttpVersion, _requestHeaders, this);
_keepAlive = messageBody.RequestKeepAlive;
Expand Down Expand Up @@ -337,7 +324,10 @@ public async Task RequestProcessingAsync()
_responseBody.StopAcceptingWrites();
}

terminated = !_keepAlive;
if (!_keepAlive)
{
break;
}
}

Reset();
Expand Down Expand Up @@ -372,6 +362,62 @@ public async Task RequestProcessingAsync()
}
}
}
private Task<bool> ReadStartLineAsync()
{
if (!_requestProcessingStopping && !TakeStartLine(SocketInput))
{
if (SocketInput.RemoteIntakeFin)
{
return TaskUtilities.CompletedFalseTask;
};
return ReadStartLineAwaitAsync();
}
return TaskUtilities.CompletedTrueTask;
}

private async Task<bool> ReadStartLineAwaitAsync()
{
await SocketInput;

while (!_requestProcessingStopping && !TakeStartLine(SocketInput))
{
if (SocketInput.RemoteIntakeFin)
{
return false;
};

await SocketInput;
}
return true;
}

private Task<bool> ReadHeadersAsync()
{
if (!_requestProcessingStopping && !TakeMessageHeaders(SocketInput, _requestHeaders))
{
if (SocketInput.RemoteIntakeFin)
{
return TaskUtilities.CompletedFalseTask;
};
return ReadHeadersAwaitAsync();
}
return TaskUtilities.CompletedTrueTask;
}

private async Task<bool> ReadHeadersAwaitAsync()
{
await SocketInput;

while (!_requestProcessingStopping && !TakeMessageHeaders(SocketInput, _requestHeaders))
{
if (SocketInput.RemoteIntakeFin)
{
return false;
};
await SocketInput;
}
return true;
}

public void OnStarting(Func<object, Task> callback, object state)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public static class TaskUtilities
#else
public static Task CompletedTask = Task.FromResult<object>(null);
#endif
public static Task<bool> CompletedTrueTask = Task.FromResult(true);
public static Task<bool> CompletedFalseTask = Task.FromResult(false);
}
}