Skip to main content

A collection of framework independent HTTP protocol utils.

Project description

Tests

httptools is a Python binding for the nodejs HTTP parser.

The package is available on PyPI: pip install httptools.

APIs

httptools contains two classes httptools.HttpRequestParser, httptools.HttpResponseParser (fulfilled through llhttp) and a function for parsing URLs httptools.parse_url (through http-parse for now). See unittests for examples.

class HttpRequestParser:

    def __init__(self, protocol):
        """HttpRequestParser

        protocol -- a Python object with the following methods
        (all optional):

          - on_message_begin()
          - on_url(url: bytes)
          - on_header(name: bytes, value: bytes)
          - on_headers_complete()
          - on_body(body: bytes)
          - on_message_complete()
          - on_chunk_header()
          - on_chunk_complete()
          - on_status(status: bytes)
        """

    def get_http_version(self) -> str:
        """Return an HTTP protocol version."""

    def should_keep_alive(self) -> bool:
        """Return ``True`` if keep-alive mode is preferred."""

    def should_upgrade(self) -> bool:
        """Return ``True`` if the parsed request is a valid Upgrade request.
	The method exposes a flag set just before on_headers_complete.
	Calling this method earlier will only yield `False`.
	"""

    def feed_data(self, data: bytes):
        """Feed data to the parser.

        Will eventually trigger callbacks on the ``protocol``
        object.

        On HTTP upgrade, this method will raise an
        ``HttpParserUpgrade`` exception, with its sole argument
        set to the offset of the non-HTTP data in ``data``.
        """

    def get_method(self) -> bytes:
        """Return HTTP request method (GET, HEAD, etc)"""


class HttpResponseParser:

    """Has all methods except ``get_method()`` that
    HttpRequestParser has."""

    def get_status_code(self) -> int:
        """Return the status code of the HTTP response"""


def parse_url(url: bytes):
    """Parse URL strings into a structured Python object.

    Returns an instance of ``httptools.URL`` class with the
    following attributes:

      - schema: bytes
      - host: bytes
      - port: int
      - path: bytes
      - query: bytes
      - fragment: bytes
      - userinfo: bytes
    """

Development

  1. Clone this repository with git clone --recursive git@github.com:MagicStack/httptools.git

  2. Create a virtual environment with Python 3: python3 -m venv envname

  3. Activate the environment with source envname/bin/activate

  4. Install development requirements with pip install -e .[test]

  5. Run make and make test.

License

MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

httptools-0.6.4.tar.gz (240.6 kB view hashes)

Uploaded Source

Built Distributions

httptools-0.6.4-cp313-cp313-win_amd64.whl (87.7 kB view hashes)

Uploaded CPython 3.13 Windows x86-64

httptools-0.6.4-cp313-cp313-musllinux_1_2_x86_64.whl (452.0 kB view hashes)

Uploaded CPython 3.13 musllinux: musl 1.2+ x86-64

httptools-0.6.4-cp313-cp313-musllinux_1_2_aarch64.whl (448.9 kB view hashes)

Uploaded CPython 3.13 musllinux: musl 1.2+ ARM64

httptools-0.6.4-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (473.1 kB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ ARM64

httptools-0.6.4-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (473.8 kB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

httptools-0.6.4-cp313-cp313-macosx_11_0_arm64.whl (102.4 kB view hashes)

Uploaded CPython 3.13 macOS 11.0+ ARM64

httptools-0.6.4-cp313-cp313-macosx_10_13_universal2.whl (197.2 kB view hashes)

Uploaded CPython 3.13 macOS 10.13+ universal2 (ARM64, x86-64)

httptools-0.6.4-cp312-cp312-win_amd64.whl (88.6 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

httptools-0.6.4-cp312-cp312-musllinux_1_2_x86_64.whl (489.8 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

httptools-0.6.4-cp312-cp312-musllinux_1_2_aarch64.whl (485.3 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARM64

httptools-0.6.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (508.8 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

httptools-0.6.4-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (510.8 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

httptools-0.6.4-cp312-cp312-macosx_11_0_arm64.whl (104.3 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

httptools-0.6.4-cp312-cp312-macosx_10_13_universal2.whl (200.7 kB view hashes)

Uploaded CPython 3.12 macOS 10.13+ universal2 (ARM64, x86-64)

httptools-0.6.4-cp311-cp311-win_amd64.whl (88.6 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

httptools-0.6.4-cp311-cp311-musllinux_1_2_x86_64.whl (434.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

httptools-0.6.4-cp311-cp311-musllinux_1_2_aarch64.whl (433.2 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARM64

httptools-0.6.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (462.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

httptools-0.6.4-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (459.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

httptools-0.6.4-cp311-cp311-macosx_11_0_arm64.whl (103.5 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

httptools-0.6.4-cp311-cp311-macosx_10_9_universal2.whl (199.0 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

httptools-0.6.4-cp310-cp310-win_amd64.whl (88.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

httptools-0.6.4-cp310-cp310-musllinux_1_2_x86_64.whl (417.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

httptools-0.6.4-cp310-cp310-musllinux_1_2_aarch64.whl (415.9 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ARM64

httptools-0.6.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (443.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

httptools-0.6.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (442.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

httptools-0.6.4-cp310-cp310-macosx_11_0_arm64.whl (103.3 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

httptools-0.6.4-cp310-cp310-macosx_10_9_universal2.whl (198.8 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

httptools-0.6.4-cp39-cp39-win_amd64.whl (89.6 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

httptools-0.6.4-cp39-cp39-musllinux_1_2_x86_64.whl (423.9 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

httptools-0.6.4-cp39-cp39-musllinux_1_2_aarch64.whl (422.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ARM64

httptools-0.6.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (449.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

httptools-0.6.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (448.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

httptools-0.6.4-cp39-cp39-macosx_11_0_arm64.whl (104.5 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

httptools-0.6.4-cp39-cp39-macosx_10_9_universal2.whl (201.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

httptools-0.6.4-cp38-cp38-win_amd64.whl (89.9 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

httptools-0.6.4-cp38-cp38-musllinux_1_2_x86_64.whl (434.3 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ x86-64

httptools-0.6.4-cp38-cp38-musllinux_1_2_aarch64.whl (434.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ ARM64

httptools-0.6.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (456.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

httptools-0.6.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (453.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

httptools-0.6.4-cp38-cp38-macosx_11_0_arm64.whl (105.1 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

httptools-0.6.4-cp38-cp38-macosx_10_9_universal2.whl (202.4 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page