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