Skip to content

Commit b55f5fe

Browse files
VizonexKRRT7
andauthored
Static Type-checking for httptools (#100)
--------- Co-authored-by: Kevin Turcios <turcioskevinr@gmail.com>
1 parent 59bf94f commit b55f5fe

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

httptools/parser/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .protocol import HTTPProtocol
12
from .parser import * # NoQA
23
from .errors import * # NoQA
34
from .url_parser import * # NoQA

httptools/parser/parser.pyi

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from typing import Union, Any
2+
from array import array
3+
from .protocol import HTTPProtocol
4+
5+
class HttpParser:
6+
def __init__(self, protocol: Union[HTTPProtocol, Any]) -> None:
7+
"""
8+
protocol -- a Python object with the following methods
9+
(all optional):
10+
11+
- on_message_begin()
12+
- on_url(url: bytes)
13+
- on_header(name: bytes, value: bytes)
14+
- on_headers_complete()
15+
- on_body(body: bytes)
16+
- on_message_complete()
17+
- on_chunk_header()
18+
- on_chunk_complete()
19+
- on_status(status: bytes)
20+
"""
21+
22+
def get_http_version(self) -> str:
23+
"""Return an HTTP protocol version."""
24+
...
25+
26+
def should_keep_alive(self) -> bool:
27+
"""Return ``True`` if keep-alive mode is preferred."""
28+
...
29+
30+
def should_upgrade(self) -> bool:
31+
"""Return ``True`` if the parsed request is a valid Upgrade request.
32+
The method exposes a flag set just before on_headers_complete.
33+
Calling this method earlier will only yield `False`."""
34+
...
35+
36+
def feed_data(self, data: Union[bytes, bytearray, memoryview, array]) -> None:
37+
"""Feed data to the parser.
38+
39+
Will eventually trigger callbacks on the ``protocol``
40+
object.
41+
42+
On HTTP upgrade, this method will raise an
43+
``HttpParserUpgrade`` exception, with its sole argument
44+
set to the offset of the non-HTTP data in ``data``.
45+
"""
46+
47+
class HttpRequestParser(HttpParser):
48+
"""Used for parsing http requests from the server's side"""
49+
50+
def get_method(self) -> bytes:
51+
"""Return HTTP request method (GET, HEAD, etc)"""
52+
53+
class HttpResponseParser(HttpParser):
54+
"""Used for parsing http requests from the client's side"""
55+
56+
def get_status_code(self) -> int:
57+
"""Return the status code of the HTTP response"""

httptools/parser/protocol.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Protocol
2+
3+
4+
class HTTPProtocol(Protocol):
5+
"""Used for providing static type-checking when parsing through the http protocol"""
6+
7+
def on_message_begin() -> None: ...
8+
def on_url(url: bytes) -> None: ...
9+
def on_header(name: bytes, value: bytes) -> None: ...
10+
def on_headers_complete() -> None: ...
11+
def on_body(body: bytes) -> None: ...
12+
def on_message_complete() -> None: ...
13+
def on_chunk_header() -> None: ...
14+
def on_chunk_complete() -> None: ...
15+
def on_status(status: bytes) -> None: ...

httptools/parser/url_parser.pyi

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Union
2+
from array import array
3+
4+
class URL:
5+
schema: bytes
6+
host: bytes
7+
port: int
8+
path: bytes
9+
query: bytes
10+
fragment: bytes
11+
userinfo: bytes
12+
13+
def parse_url(url: Union[bytes, bytearray, memoryview, array]) -> URL:
14+
"""Parse URL strings into a structured Python object.
15+
16+
Returns an instance of ``httptools.URL`` class with the
17+
following attributes:
18+
19+
- schema: bytes
20+
- host: bytes
21+
- port: int
22+
- path: bytes
23+
- query: bytes
24+
- fragment: bytes
25+
- userinfo: bytes
26+
"""
27+
...

0 commit comments

Comments
 (0)