private async Task ReadBody() { Exception?error = null; try { while (true) { var memory = _bodyInputPipe !.Writer.GetMemory(); var read = await AsyncIO !.ReadAsync(memory); // End of body if (read == 0) { break; } // Read was not canceled because of incoming write or IO stopping if (read != -1) { _consumedBytes += read; _bodyInputPipe.Writer.Advance(read); } if (_consumedBytes > MaxRequestBodySize) { IISBadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge); } var result = await _bodyInputPipe.Writer.FlushAsync(); if (result.IsCompleted || result.IsCanceled) { break; } } } catch (ConnectionResetException ex) { AbortIO(clientDisconnect: true); error = ex; } catch (Http.BadHttpRequestException ex) { // Similar to a ConnectionResetException, this shouldn't be logged as an "Unexpected exception." // This should be logged by whatever catches it. Likely IISHttpContextOfT.ProcessRequestsAsync(). error = ex; } catch (Exception ex) { error = ex; Log.UnexpectedError(_logger, nameof(IISHttpContext), ex); } finally { _bodyInputPipe !.Writer.Complete(error); } }
private void InitializeRequestIO() { Debug.Assert(!HasStartedConsumingRequestBody); if (RequestHeaders.ContentLength > MaxRequestBodySize) { IISBadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge); } HasStartedConsumingRequestBody = true; EnsureIOInitialized(); _bodyInputPipe = new Pipe(new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.ThreadPool, minimumSegmentSize: MinAllocBufferSize)); _readBodyTask = ReadBody(); }