Skip to main content

llhttp in python

Project description

pyllhttp

Python wrapper for llhttp

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

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_header_field(self, field):
        if self.current_header_value is not None:
            assert self.current_header_field is not None
            self.headers[self.current_header_field] = self.current_header_value
            self.current_header_field = None
            self.current_header_value = None

        if self.current_header_field is None:
            self.current_header_field = bytes(field)
        else:
            self.current_header_field += field

    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 = bytes(value)
        else:
            self.current_header_value += value

    def on_headers_complete(self):
        if self.current_header_value is not None:
            assert self.current_header_field is not None
            self.headers[self.current_header_field] = self.current_header_value
            self.current_header_field = None
            self.current_header_value = None

    def on_message_complete(self):
        assert self.current_header_field is None
        assert self.current_header_value is None
        print("MESSAGE COMPLETE")

parser = request_parser()

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,
})

This project is a toy, started to reacquaint myself with Python c-api modules. If you find it useful, 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

llhttp-2.2.0.2.tar.gz (37.4 kB view details)

Uploaded Source

File details

Details for the file llhttp-2.2.0.2.tar.gz.

File metadata

  • Download URL: llhttp-2.2.0.2.tar.gz
  • Upload date:
  • Size: 37.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.6

File hashes

Hashes for llhttp-2.2.0.2.tar.gz
Algorithm Hash digest
SHA256 b081e93b82757073813ef6625be8c0b073af58791abd4b739a3e3239b45c2269
MD5 e024a8bb229e4ce106690612845c4b5e
BLAKE2b-256 2191ad3d7208e948e3ccb14d04ea39a6fe0b12283a884fea154acfe41d8128fb

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