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