METADATA 3.6 KB

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