From d1d9d9e3bea087365fb1c2bbbb8b387ac187e38a Mon Sep 17 00:00:00 2001 From: Vizonex <114684698+Vizonex@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:02:22 -0500 Subject: [PATCH 1/5] Add static protocol object for type checking --- httptools/parser/protocol.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 httptools/parser/protocol.py diff --git a/httptools/parser/protocol.py b/httptools/parser/protocol.py new file mode 100644 index 0000000..5090f5c --- /dev/null +++ b/httptools/parser/protocol.py @@ -0,0 +1,15 @@ +from typing import Protocol + +class HTTPProtocol(Protocol): + """Used for providing static type-checking when parsing through the http protocol""" + + def on_message_begin() -> None:... + def on_url(url: bytes) -> None:... + def on_header(name: bytes, value: bytes) -> None:... + def on_headers_complete() -> None:... + def on_body(body: bytes) -> None:... + def on_message_complete() -> None:... + def on_chunk_header() -> None:... + def on_chunk_complete() -> None:... + def on_status(status: bytes) -> None:... + From 02e8f3db036c44b57a5c97df0c33422001a3999c Mon Sep 17 00:00:00 2001 From: Vizonex <114684698+Vizonex@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:02:56 -0500 Subject: [PATCH 2/5] Create url_parser.pyi --- httptools/parser/url_parser.pyi | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 httptools/parser/url_parser.pyi diff --git a/httptools/parser/url_parser.pyi b/httptools/parser/url_parser.pyi new file mode 100644 index 0000000..b74ba74 --- /dev/null +++ b/httptools/parser/url_parser.pyi @@ -0,0 +1,29 @@ +from typing import Union +from array import array + +class URL: + schema:bytes + host:bytes + port:int + path:bytes + query:bytes + fragment:bytes + userinfo:bytes + +def parse_url(url:Union[bytes, bytearray, memoryview, array]) -> URL: + """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 + """ + ... + + From 6011b5770dda9e8afa4736edea4e234517fb1285 Mon Sep 17 00:00:00 2001 From: Vizonex <114684698+Vizonex@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:03:59 -0500 Subject: [PATCH 3/5] Create parser.pyi --- httptools/parser/parser.pyi | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 httptools/parser/parser.pyi diff --git a/httptools/parser/parser.pyi b/httptools/parser/parser.pyi new file mode 100644 index 0000000..83fe34b --- /dev/null +++ b/httptools/parser/parser.pyi @@ -0,0 +1,58 @@ +from typing import Union, Any +from array import array +from .protocol import HTTPProtocol + + + +class HttpParser: + def __init__(self, protocol:Union[HTTPProtocol, Any]) -> None: + """ + 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:Union[bytes, bytearray, memoryview, array]) -> None: + """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``. + """ + + +class HttpRequestParser(HttpParser): + """Used for parsing http requests from the server's side""" + + def get_method(self) -> bytes: + """Return HTTP request method (GET, HEAD, etc)""" + + +class HttpResponseParser(HttpParser): + """Used for parsing http requests from the client's side""" + + def get_status_code(self) -> int: + """Return the status code of the HTTP response""" From a248458c7357155d5cce69c70d26478129f18332 Mon Sep 17 00:00:00 2001 From: Vizonex <114684698+Vizonex@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:05:10 -0500 Subject: [PATCH 4/5] add protocol module --- httptools/parser/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/httptools/parser/__init__.py b/httptools/parser/__init__.py index ba371f5..1d8df43 100644 --- a/httptools/parser/__init__.py +++ b/httptools/parser/__init__.py @@ -1,3 +1,4 @@ +from .protocol import HTTPProtocol from .parser import * # NoQA from .errors import * # NoQA from .url_parser import * # NoQA From 01308ee7c039958ca35eb805f0e9121244541e9f Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Tue, 15 Oct 2024 11:05:25 -0500 Subject: [PATCH 5/5] add spacing and semi black formatting --- httptools/parser/parser.pyi | 19 +++++++++---------- httptools/parser/protocol.py | 20 ++++++++++---------- httptools/parser/url_parser.pyi | 18 ++++++++---------- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/httptools/parser/parser.pyi b/httptools/parser/parser.pyi index 83fe34b..8a714bf 100644 --- a/httptools/parser/parser.pyi +++ b/httptools/parser/parser.pyi @@ -1,11 +1,9 @@ from typing import Union, Any -from array import array +from array import array from .protocol import HTTPProtocol - - class HttpParser: - def __init__(self, protocol:Union[HTTPProtocol, Any]) -> None: + def __init__(self, protocol: Union[HTTPProtocol, Any]) -> None: """ protocol -- a Python object with the following methods (all optional): @@ -24,15 +22,18 @@ class HttpParser: 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`.""" + The method exposes a flag set just before on_headers_complete. + Calling this method earlier will only yield `False`.""" ... - def feed_data(self, data:Union[bytes, bytearray, memoryview, array]) -> None: + + def feed_data(self, data: Union[bytes, bytearray, memoryview, array]) -> None: """Feed data to the parser. Will eventually trigger callbacks on the ``protocol`` @@ -43,14 +44,12 @@ class HttpParser: set to the offset of the non-HTTP data in ``data``. """ - class HttpRequestParser(HttpParser): """Used for parsing http requests from the server's side""" - + def get_method(self) -> bytes: """Return HTTP request method (GET, HEAD, etc)""" - class HttpResponseParser(HttpParser): """Used for parsing http requests from the client's side""" diff --git a/httptools/parser/protocol.py b/httptools/parser/protocol.py index 5090f5c..c3b4234 100644 --- a/httptools/parser/protocol.py +++ b/httptools/parser/protocol.py @@ -1,15 +1,15 @@ from typing import Protocol + class HTTPProtocol(Protocol): """Used for providing static type-checking when parsing through the http protocol""" - def on_message_begin() -> None:... - def on_url(url: bytes) -> None:... - def on_header(name: bytes, value: bytes) -> None:... - def on_headers_complete() -> None:... - def on_body(body: bytes) -> None:... - def on_message_complete() -> None:... - def on_chunk_header() -> None:... - def on_chunk_complete() -> None:... - def on_status(status: bytes) -> None:... - + def on_message_begin() -> None: ... + def on_url(url: bytes) -> None: ... + def on_header(name: bytes, value: bytes) -> None: ... + def on_headers_complete() -> None: ... + def on_body(body: bytes) -> None: ... + def on_message_complete() -> None: ... + def on_chunk_header() -> None: ... + def on_chunk_complete() -> None: ... + def on_status(status: bytes) -> None: ... diff --git a/httptools/parser/url_parser.pyi b/httptools/parser/url_parser.pyi index b74ba74..f3d3488 100644 --- a/httptools/parser/url_parser.pyi +++ b/httptools/parser/url_parser.pyi @@ -2,15 +2,15 @@ from typing import Union from array import array class URL: - schema:bytes - host:bytes - port:int - path:bytes - query:bytes - fragment:bytes - userinfo:bytes + schema: bytes + host: bytes + port: int + path: bytes + query: bytes + fragment: bytes + userinfo: bytes -def parse_url(url:Union[bytes, bytearray, memoryview, array]) -> URL: +def parse_url(url: Union[bytes, bytearray, memoryview, array]) -> URL: """Parse URL strings into a structured Python object. Returns an instance of ``httptools.URL`` class with the @@ -25,5 +25,3 @@ def parse_url(url:Union[bytes, bytearray, memoryview, array]) -> URL: - userinfo: bytes """ ... - -