Skip to main content

llhttp in python

Project description

pyllhttp

Python wrapper for llhttp

A fork of llhttp compatible with subinterpreters

======

A simple Python wrapper around llhttp, the HTTP parser for Node.js.

Install

pyllhttp via PyPI, or pip install pyllhttp

Usage

import llhttp
from pprint import pprint

pprint({"version": llhttp.version})

class request_parser(llhttp.Request):
    headers = {}

    url = b''
    current_header_field = None
    current_header_value = None

    def on_message_begin(self):
        print(f"MESSAGE BEGIN")

    def on_url(self, url):
        self.url += url
        self.pause()

    def on_url_complete(self):
        print(f"URL {self.url}")

    def on_header_field(self, field):
        assert self.current_header_value is None
        if self.current_header_field is None:
            self.current_header_field = bytearray(field)
        else:
            self.current_header_field += field

    def on_header_field_complete(self):
        self.current_header_field = self.current_header_field.decode('iso-8859-1').lower()
        assert self.current_header_field not in self.headers

    def on_header_value(self, value):
        assert self.current_header_field is not None
        if self.current_header_value is None:
            self.current_header_value = bytearray(value)
        else:
            self.current_header_value += value

    def on_header_value_complete(self):
        assert self.current_header_field is not None
        self.current_header_value = bytes(self.current_header_value)
        print(f"HEADER {self.current_header_field}: {self.current_header_value}")
        self.headers[self.current_header_field] = self.current_header_value
        self.current_header_field = None
        self.current_header_value = None

    def on_headers_complete(self):
        assert self.current_header_field is None
        assert self.current_header_value is None

    def on_message_complete(self):
        print("MESSAGE COMPLETE")

parser = request_parser()

assert parser.lenient_headers is not True
parser.lenient_headers = True
parser.reset()
assert parser.lenient_headers is True

buffer = b"GET /test HTTP/1.1\r\nlOl:wut\r\nOH: hai\r\n\r\n"
while buffer:
    consumed = parser.execute(buffer[:2])
    buffer = buffer[consumed:]
    if parser.is_paused:
        print("UNPAUSING")
        parser.unpause()

parser.finish()
pprint({
    "method": parser.method,
    "url": parser.url,
    "version": f"{parser.major}.{parser.minor}",
    "headers": parser.headers,
})
{'version': '3.0.0'}
MESSAGE BEGIN
UNPAUSING
UNPAUSING
UNPAUSING
URL b'/test'
HEADER lol: b'wut'
HEADER oh: b'hai'
MESSAGE COMPLETE
{'headers': {'lol': b'wut', 'oh': b'hai'},
 'method': 'GET',
 'url': b'/test',
 'version': '1.1'}

Extra

This project is a toy, started to reacquaint myself with Python c-api modules. If you find it useful, please let me know.

The version number tracks the version of the incorporated llhttp.

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

pyllhttp-6.7.1.tar.gz (40.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyllhttp-6.7.1-cp313-cp313-macosx_15_0_arm64.whl (41.2 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

File details

Details for the file pyllhttp-6.7.1.tar.gz.

File metadata

  • Download URL: pyllhttp-6.7.1.tar.gz
  • Upload date:
  • Size: 40.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for pyllhttp-6.7.1.tar.gz
Algorithm Hash digest
SHA256 58ff283847bb731121f3a37fb11b182c1149001402ca863675b3bda6e63ce935
MD5 cabf7a6ff514956eb66496490c4c9461
BLAKE2b-256 e4ac791ae0fe2ca8f396741a7c575e7a30ce4010a3885cf343400440bfce1ef5

See more details on using hashes here.

File details

Details for the file pyllhttp-6.7.1-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyllhttp-6.7.1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 ac44b66cf036c4d49f364fd08e374b2adaf735cf8f2aecf47742e396bc30fcc0
MD5 5449da5e9eaeaaa4fee5dcc219389b88
BLAKE2b-256 cd56254f77aa264b9b9cc57cd46e47856a816d7e6bc6fb59407742fb73c41f70

See more details on using hashes here.

Supported by

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