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

Commit e04235a

Browse files
committed
Improved RequestProcessingAsync legibility
With precomputed tasks Hopefully #391, the good bits...
1 parent 07c0b41 commit e04235a

File tree

2 files changed

+67
-19
lines changed

2 files changed

+67
-19
lines changed

src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,28 +263,15 @@ public async Task RequestProcessingAsync()
263263
{
264264
try
265265
{
266-
var terminated = false;
267-
while (!terminated && !_requestProcessingStopping)
266+
while (!_requestProcessingStopping)
268267
{
269-
while (!terminated && !_requestProcessingStopping && !TakeStartLine(SocketInput))
268+
if (!await ReadStartLineAsync() ||
269+
!await ReadHeadersAsync())
270270
{
271-
terminated = SocketInput.RemoteIntakeFin;
272-
if (!terminated)
273-
{
274-
await SocketInput;
275-
}
276-
}
277-
278-
while (!terminated && !_requestProcessingStopping && !TakeMessageHeaders(SocketInput, _requestHeaders))
279-
{
280-
terminated = SocketInput.RemoteIntakeFin;
281-
if (!terminated)
282-
{
283-
await SocketInput;
284-
}
271+
break;
285272
}
286273

287-
if (!terminated && !_requestProcessingStopping)
274+
if (!_requestProcessingStopping)
288275
{
289276
var messageBody = MessageBody.For(HttpVersion, _requestHeaders, this);
290277
_keepAlive = messageBody.RequestKeepAlive;
@@ -337,7 +324,10 @@ public async Task RequestProcessingAsync()
337324
_responseBody.StopAcceptingWrites();
338325
}
339326

340-
terminated = !_keepAlive;
327+
if (!_keepAlive)
328+
{
329+
break;
330+
}
341331
}
342332

343333
Reset();
@@ -372,6 +362,62 @@ public async Task RequestProcessingAsync()
372362
}
373363
}
374364
}
365+
private Task<bool> ReadStartLineAsync()
366+
{
367+
if (!_requestProcessingStopping && !TakeStartLine(SocketInput))
368+
{
369+
if (SocketInput.RemoteIntakeFin)
370+
{
371+
return TaskUtilities.CompletedFalseTask;
372+
};
373+
return ReadStartLineAwaitAsync();
374+
}
375+
return TaskUtilities.CompletedTrueTask;
376+
}
377+
378+
private async Task<bool> ReadStartLineAwaitAsync()
379+
{
380+
await SocketInput;
381+
382+
while (!_requestProcessingStopping && !TakeStartLine(SocketInput))
383+
{
384+
if (SocketInput.RemoteIntakeFin)
385+
{
386+
return false;
387+
};
388+
389+
await SocketInput;
390+
}
391+
return true;
392+
}
393+
394+
private Task<bool> ReadHeadersAsync()
395+
{
396+
if (!_requestProcessingStopping && !TakeMessageHeaders(SocketInput, _requestHeaders))
397+
{
398+
if (SocketInput.RemoteIntakeFin)
399+
{
400+
return TaskUtilities.CompletedFalseTask;
401+
};
402+
return ReadHeadersAwaitAsync();
403+
}
404+
return TaskUtilities.CompletedTrueTask;
405+
}
406+
407+
private async Task<bool> ReadHeadersAwaitAsync()
408+
{
409+
await SocketInput;
410+
411+
while (!_requestProcessingStopping && !TakeMessageHeaders(SocketInput, _requestHeaders))
412+
{
413+
if (SocketInput.RemoteIntakeFin)
414+
{
415+
return false;
416+
};
417+
await SocketInput;
418+
}
419+
return true;
420+
}
375421

376422
public void OnStarting(Func<object, Task> callback, object state)
377423
{

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/TaskUtilities.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ public static class TaskUtilities
1212
#else
1313
public static Task CompletedTask = Task.FromResult<object>(null);
1414
#endif
15+
public static Task<bool> CompletedTrueTask = Task.FromResult(true);
16+
public static Task<bool> CompletedFalseTask = Task.FromResult(false);
1517
}
1618
}

0 commit comments

Comments
 (0)