Skip to content

Commit 6011b57

Browse files
authored
Create parser.pyi
1 parent 02e8f3d commit 6011b57

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

httptools/parser/parser.pyi

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from typing import Union, Any
2+
from array import array
3+
from .protocol import HTTPProtocol
4+
5+
6+
7+
class HttpParser:
8+
def __init__(self, protocol:Union[HTTPProtocol, Any]) -> None:
9+
"""
10+
protocol -- a Python object with the following methods
11+
(all optional):
12+
13+
- on_message_begin()
14+
- on_url(url: bytes)
15+
- on_header(name: bytes, value: bytes)
16+
- on_headers_complete()
17+
- on_body(body: bytes)
18+
- on_message_complete()
19+
- on_chunk_header()
20+
- on_chunk_complete()
21+
- on_status(status: bytes)
22+
"""
23+
24+
def get_http_version(self) -> str:
25+
"""Return an HTTP protocol version."""
26+
...
27+
def should_keep_alive(self) -> bool:
28+
"""Return ``True`` if keep-alive mode is preferred."""
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+
def feed_data(self, data:Union[bytes, bytearray, memoryview, array]) -> None:
36+
"""Feed data to the parser.
37+
38+
Will eventually trigger callbacks on the ``protocol``
39+
object.
40+
41+
On HTTP upgrade, this method will raise an
42+
``HttpParserUpgrade`` exception, with its sole argument
43+
set to the offset of the non-HTTP data in ``data``.
44+
"""
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+
54+
class HttpResponseParser(HttpParser):
55+
"""Used for parsing http requests from the client's side"""
56+
57+
def get_status_code(self) -> int:
58+
"""Return the status code of the HTTP response"""

0 commit comments

Comments
 (0)