private async Task OnReadInternal(int offset) { _timer.Change(Timeout.Infinite, Timeout.Infinite); // Continue reading until full header is received. // Especially important for multipart requests when the second part of the header arrives after a tiny delay // because the web browser has to measure the content length first. var parsedBytes = 0; while (true) { try { await _ms.WriteAsync(_buffer, parsedBytes, offset - parsedBytes).ConfigureAwait(false); if (_ms.Length > 32768) { Close(true); return; } } catch { CloseSocket(); Unbind(); return; } if (offset == 0) { CloseSocket(); Unbind(); return; } if (ProcessInput(_ms)) { if (!_context.HaveError) { _context.HttpListenerRequest.FinishInitialization(); } if (_context.HaveError || !_epl.BindContext(_context)) { Close(true); return; } var listener = _context.Listener; if (_lastListener != listener) { RemoveConnection(); listener.AddConnection(this); _lastListener = listener; } _contextBound = true; listener.RegisterContext(_context); return; } parsedBytes = offset; offset += await Stream.ReadAsync(_buffer, offset, BufferSize - offset).ConfigureAwait(false); } }
private void OnReadInternal(IAsyncResult ares) { _timer.Change(Timeout.Infinite, Timeout.Infinite); var nread = -1; try { nread = _stream.EndRead(ares); _ms.Write(_buffer, 0, nread); if (_ms.Length > 32768) { SendError("Bad request", 400); Close(true); return; } } catch { if (_ms != null && _ms.Length > 0) { SendError(); } if (_sock != null) { CloseSocket(); Unbind(); } return; } if (nread == 0) { //if (ms.Length > 0) // SendError (); // Why bother? CloseSocket(); Unbind(); return; } if (ProcessInput(_ms)) { if (!_context.HaveError) { _context.Request.FinishInitialization(); } if (_context.HaveError) { SendError(); Close(true); return; } if (!_epl.BindContext(_context)) { SendError("Invalid host", 400); Close(true); return; } var listener = _context.Listener; if (_lastListener != listener) { RemoveConnection(); listener.AddConnection(this); _lastListener = listener; } _contextBound = true; listener.RegisterContext(_context); return; } _stream.BeginRead(_buffer, 0, BufferSize, _onreadCb, this); }