parser.pyi 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from typing import Union, Any
  2. from array import array
  3. from .protocol import HTTPProtocol
  4. class HttpParser:
  5. def __init__(self, protocol: Union[HTTPProtocol, Any]) -> None:
  6. """
  7. protocol -- a Python object with the following methods
  8. (all optional):
  9. - on_message_begin()
  10. - on_url(url: bytes)
  11. - on_header(name: bytes, value: bytes)
  12. - on_headers_complete()
  13. - on_body(body: bytes)
  14. - on_message_complete()
  15. - on_chunk_header()
  16. - on_chunk_complete()
  17. - on_status(status: bytes)
  18. """
  19. def get_http_version(self) -> str:
  20. """Return an HTTP protocol version."""
  21. ...
  22. def should_keep_alive(self) -> bool:
  23. """Return ``True`` if keep-alive mode is preferred."""
  24. ...
  25. def should_upgrade(self) -> bool:
  26. """Return ``True`` if the parsed request is a valid Upgrade request.
  27. The method exposes a flag set just before on_headers_complete.
  28. Calling this method earlier will only yield `False`."""
  29. ...
  30. def feed_data(self, data: Union[bytes, bytearray, memoryview, array]) -> None:
  31. """Feed data to the parser.
  32. Will eventually trigger callbacks on the ``protocol``
  33. 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's side"""
  40. def get_method(self) -> bytes:
  41. """Return HTTP request method (GET, HEAD, etc)"""
  42. class HttpResponseParser(HttpParser):
  43. """Used for parsing http requests from the client's side"""
  44. def get_status_code(self) -> int:
  45. """Return the status code of the HTTP response"""