(
self, event: events.ConnectionEvent
)
| 395 | raise AssertionError(f"Unexpected event: {event}") |
| 396 | |
| 397 | def read_headers( |
| 398 | self, event: events.ConnectionEvent |
| 399 | ) -> layer.CommandGenerator[None]: |
| 400 | if isinstance(event, events.DataReceived): |
| 401 | if not self.request: |
| 402 | # we just received some data for an unknown request. |
| 403 | yield commands.Log(f"Unexpected data from server: {bytes(self.buf)!r}") |
| 404 | yield commands.CloseConnection(self.conn) |
| 405 | return |
| 406 | assert self.stream_id is not None |
| 407 | |
| 408 | response_head = self.buf.maybe_extract_lines() |
| 409 | if response_head: |
| 410 | try: |
| 411 | self.response = http1.read_response_head( |
| 412 | [bytes(x) for x in response_head] |
| 413 | ) |
| 414 | expected_size = http1.expected_http_body_size( |
| 415 | self.request, self.response |
| 416 | ) |
| 417 | except ValueError as e: |
| 418 | yield commands.CloseConnection(self.conn) |
| 419 | yield ReceiveHttp( |
| 420 | ResponseProtocolError( |
| 421 | self.stream_id, |
| 422 | f"Cannot parse HTTP response: {e}", |
| 423 | ErrorCode.GENERIC_SERVER_ERROR, |
| 424 | ) |
| 425 | ) |
| 426 | return |
| 427 | yield ReceiveHttp( |
| 428 | ResponseHeaders(self.stream_id, self.response, expected_size == 0) |
| 429 | ) |
| 430 | self.body_reader = make_body_reader(expected_size) |
| 431 | |
| 432 | self.state = self.read_body |
| 433 | yield from self.state(event) |
| 434 | else: |
| 435 | pass # FIXME: protect against header size DoS |
| 436 | elif isinstance(event, events.ConnectionClosed): |
| 437 | if self.conn.state & ConnectionState.CAN_WRITE: |
| 438 | yield commands.CloseConnection(self.conn) |
| 439 | if self.stream_id: |
| 440 | if self.buf: |
| 441 | yield ReceiveHttp( |
| 442 | ResponseProtocolError( |
| 443 | self.stream_id, |
| 444 | f"unexpected server response: {bytes(self.buf)!r}", |
| 445 | ErrorCode.GENERIC_SERVER_ERROR, |
| 446 | ) |
| 447 | ) |
| 448 | else: |
| 449 | # The server has closed the connection to prevent us from continuing. |
| 450 | # We need to signal that to the stream. |
| 451 | # https://tools.ietf.org/html/rfc7231#section-6.5.11 |
| 452 | yield ReceiveHttp( |
| 453 | ResponseProtocolError( |
| 454 | self.stream_id, |
nothing calls this directly
no test coverage detected