METADATA 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. Metadata-Version: 2.4
  2. Name: httptools
  3. Version: 0.7.1
  4. Summary: A collection of framework independent HTTP protocol utils.
  5. Author-email: Yury Selivanov <yury@magic.io>
  6. License-Expression: MIT
  7. Project-URL: Homepage, https://github.com/MagicStack/httptools
  8. Platform: macOS
  9. Platform: POSIX
  10. Platform: Windows
  11. Classifier: Intended Audience :: Developers
  12. Classifier: Programming Language :: Python :: 3
  13. Classifier: Operating System :: POSIX
  14. Classifier: Operating System :: MacOS :: MacOS X
  15. Classifier: Environment :: Web Environment
  16. Classifier: Development Status :: 5 - Production/Stable
  17. Requires-Python: >=3.9
  18. Description-Content-Type: text/markdown
  19. License-File: LICENSE
  20. Provides-Extra: test
  21. Dynamic: license-file
  22. Dynamic: platform
  23. ![Tests](https://github.com/MagicStack/httptools/workflows/Tests/badge.svg)
  24. httptools is a Python binding for the nodejs HTTP parser.
  25. The package is available on PyPI: `pip install httptools`.
  26. # APIs
  27. httptools contains two classes `httptools.HttpRequestParser`,
  28. `httptools.HttpResponseParser` (fulfilled through
  29. [llhttp](https://github.com/nodejs/llhttp)) and a function for
  30. parsing URLs `httptools.parse_url` (through
  31. [http-parse](https://github.com/nodejs/http-parser) for now).
  32. See unittests for examples.
  33. ```python
  34. class HttpRequestParser:
  35. def __init__(self, protocol):
  36. """HttpRequestParser
  37. protocol -- a Python object with the following methods
  38. (all optional):
  39. - on_message_begin()
  40. - on_url(url: bytes)
  41. - on_header(name: bytes, value: bytes)
  42. - on_headers_complete()
  43. - on_body(body: bytes)
  44. - on_message_complete()
  45. - on_chunk_header()
  46. - on_chunk_complete()
  47. - on_status(status: bytes)
  48. """
  49. def get_http_version(self) -> str:
  50. """Return an HTTP protocol version."""
  51. def should_keep_alive(self) -> bool:
  52. """Return ``True`` if keep-alive mode is preferred."""
  53. def should_upgrade(self) -> bool:
  54. """Return ``True`` if the parsed request is a valid Upgrade request.
  55. The method exposes a flag set just before on_headers_complete.
  56. Calling this method earlier will only yield `False`.
  57. """
  58. def feed_data(self, data: bytes):
  59. """Feed data to the parser.
  60. Will eventually trigger callbacks on the ``protocol``
  61. object.
  62. On HTTP upgrade, this method will raise an
  63. ``HttpParserUpgrade`` exception, with its sole argument
  64. set to the offset of the non-HTTP data in ``data``.
  65. """
  66. def get_method(self) -> bytes:
  67. """Return HTTP request method (GET, HEAD, etc)"""
  68. class HttpResponseParser:
  69. """Has all methods except ``get_method()`` that
  70. HttpRequestParser has."""
  71. def get_status_code(self) -> int:
  72. """Return the status code of the HTTP response"""
  73. def parse_url(url: bytes):
  74. """Parse URL strings into a structured Python object.
  75. Returns an instance of ``httptools.URL`` class with the
  76. following attributes:
  77. - schema: bytes
  78. - host: bytes
  79. - port: int
  80. - path: bytes
  81. - query: bytes
  82. - fragment: bytes
  83. - userinfo: bytes
  84. """
  85. ```
  86. # Development
  87. 1. Clone this repository with
  88. `git clone --recursive git@github.com:MagicStack/httptools.git`
  89. 2. Create a virtual environment with Python 3:
  90. `python3 -m venv envname`
  91. 3. Activate the environment with `source envname/bin/activate`
  92. 4. Run `make` and `make test`.
  93. # License
  94. MIT.