parser.pyi 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from array import array
  2. from .protocol import HTTPProtocol
  3. class HttpParser:
  4. def __init__(self, protocol: HTTPProtocol | object) -> None:
  5. """The HTTP parser.
  6. Args:
  7. protocol (HTTPProtocol): Callback interface for the parser.
  8. """
  9. def set_dangerous_leniencies(
  10. self,
  11. lenient_headers: bool | None = None,
  12. lenient_chunked_length: bool | None = None,
  13. lenient_keep_alive: bool | None = None,
  14. lenient_transfer_encoding: bool | None = None,
  15. lenient_version: bool | None = None,
  16. lenient_data_after_close: bool | None = None,
  17. lenient_optional_lf_after_cr: bool | None = None,
  18. lenient_optional_cr_before_lf: bool | None = None,
  19. lenient_optional_crlf_after_chunk: bool | None = None,
  20. lenient_spaces_after_chunk_size: bool | None = None,
  21. ) -> None:
  22. """Set dangerous leniencies for the parser."""
  23. def get_http_version(self) -> str:
  24. """Retrieve the HTTP protocol version e.g. "1.1"."""
  25. def should_keep_alive(self) -> bool:
  26. """Return `True` if keep-alive mode is preferred."""
  27. def should_upgrade(self) -> bool:
  28. """Return `True` if the parsed request is a valid Upgrade request.
  29. The method exposes a flag set just before on_headers_complete.
  30. Calling this method earlier will only yield `False`."""
  31. def feed_data(self, data: bytes | bytearray | memoryview | array[int]) -> None:
  32. """Feed data to the parser.
  33. Will eventually trigger callbacks on the ``protocol`` object.
  34. On HTTP upgrade, this method will raise an
  35. ``HttpParserUpgrade`` exception, with its sole argument
  36. set to the offset of the non-HTTP data in ``data``.
  37. """
  38. class HttpRequestParser(HttpParser):
  39. """Used for parsing http requests from the server side."""
  40. def get_method(self) -> bytes:
  41. """Retrieve the HTTP method of the request."""
  42. class HttpResponseParser(HttpParser):
  43. """Used for parsing http responses from the client side."""
  44. def get_status_code(self) -> int:
  45. """Retrieve the status code of the HTTP response."""