def read_headers(self, event: events.ConnectionEvent) -> layer.CommandGenerator[None]: if isinstance(event, events.DataReceived): request_head = self.buf.maybe_extract_lines() if request_head: request_head = [bytes(x) for x in request_head] # TODO: Make url.parse compatible with bytearrays try: self.request = http1.read_request_head(request_head) expected_body_size = http1.expected_http_body_size(self.request, expect_continue_as_0=False) except ValueError as e: yield commands.Log(f"{human.format_address(self.conn.peername)}: {e}") yield commands.CloseConnection(self.conn) self.state = self.done return yield ReceiveHttp(RequestHeaders(self.stream_id, self.request, expected_body_size == 0)) self.body_reader = make_body_reader(expected_body_size) self.state = self.read_body yield from self.state(event) else: pass # FIXME: protect against header size DoS elif isinstance(event, events.ConnectionClosed): buf = bytes(self.buf) if buf.strip(): yield commands.Log(f"Client closed connection before completing request headers: {buf!r}") yield commands.CloseConnection(self.conn) else: raise AssertionError(f"Unexpected event: {event}")
def read_headers( self, event: events.ConnectionEvent) -> layer.CommandGenerator[None]: if isinstance(event, events.DataReceived): request_head = self.buf.maybe_extract_lines() if request_head: request_head = [ bytes(x) for x in request_head ] # TODO: Make url.parse compatible with bytearrays try: self.request = http1.read_request_head(request_head) if self.context.options.validate_inbound_headers: http1.validate_headers(self.request.headers) expected_body_size = http1.expected_http_body_size( self.request) except ValueError as e: yield commands.SendData(self.conn, make_error_response(400, str(e))) yield commands.CloseConnection(self.conn) if self.request: # we have headers that we can show in the ui yield ReceiveHttp( RequestHeaders(self.stream_id, self.request, False)) yield ReceiveHttp( RequestProtocolError(self.stream_id, str(e), 400)) else: yield commands.Log( f"{human.format_address(self.conn.peername)}: {e}") self.state = self.done return yield ReceiveHttp( RequestHeaders(self.stream_id, self.request, expected_body_size == 0)) self.body_reader = make_body_reader(expected_body_size) self.state = self.read_body yield from self.state(event) else: pass # FIXME: protect against header size DoS elif isinstance(event, events.ConnectionClosed): buf = bytes(self.buf) if buf.strip(): yield commands.Log( f"Client closed connection before completing request headers: {buf!r}" ) yield commands.CloseConnection(self.conn) else: raise AssertionError(f"Unexpected event: {event}")
def read_request_headers(self, flow): return http.HTTPRequest.wrap( http1.read_request_head(self.client_conn.rfile) )
def read_request_headers(self, flow): return http1.read_request_head(self.client_conn.rfile)
def read_request_headers(self, flow): return http.HTTPRequest.wrap( http1.read_request_head(self.client_conn.rfile))