Skip to content

Commit d743d1c

Browse files
committed
Minor docstring, typing and encoding fixes
temp to docs
1 parent bc579e8 commit d743d1c

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

adafruit_httpserver/headers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, headers: Dict[str, str] = None) -> None:
5252

5353
self._storage = {key.lower(): [key, value] for key, value in headers.items()}
5454

55-
def get(self, name: str, default: str = None):
55+
def get(self, name: str, default: str = None) -> Union[str, None]:
5656
"""Returns the value for the given header name, or default if not found."""
5757
return self._storage.get(name.lower(), [None, default])[1]
5858

@@ -66,11 +66,11 @@ def items(self):
6666

6767
def keys(self):
6868
"""Returns a list of header names."""
69-
return dict(self._storage.values()).keys()
69+
return list(dict(self._storage.values()).keys())
7070

7171
def values(self):
7272
"""Returns a list of header values."""
73-
return dict(self._storage.values()).values()
73+
return list(dict(self._storage.values()).values())
7474

7575
def update(self, headers: Dict[str, str]):
7676
"""Updates the headers with the given dict."""

adafruit_httpserver/request.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _add_field_value(self, field_name: str, value: Union[str, bytes]) -> None:
3434
self._storage[field_name].append(value)
3535

3636
@staticmethod
37-
def _encode_html_entities(value):
37+
def _encode_html_entities(value: str) -> str:
3838
"""Encodes unsafe HTML characters."""
3939
return (
4040
str(value)
@@ -84,11 +84,11 @@ def __repr__(self) -> str:
8484

8585
class QueryParams(_IFieldStorage):
8686
"""
87-
Class for parsing and storing GET quer parameters requests.
87+
Class for parsing and storing GET query parameters requests.
8888
8989
Examples::
9090
91-
query_params = QueryParams(b"foo=bar&baz=qux&baz=quux")
91+
query_params = QueryParams("foo=bar&baz=qux&baz=quux")
9292
# QueryParams({"foo": "bar", "baz": ["qux", "quux"]})
9393
9494
query_params.get("foo") # "bar"
@@ -99,7 +99,7 @@ class QueryParams(_IFieldStorage):
9999
query_params.fields # ["foo", "baz"]
100100
"""
101101

102-
_storage: Dict[str, List[Union[str, bytes]]]
102+
_storage: Dict[str, List[str]]
103103

104104
def __init__(self, query_string: str) -> None:
105105
self._storage = {}
@@ -151,7 +151,7 @@ def __init__(self, data: bytes, content_type: str) -> None:
151151
self._parse_text_plain(data)
152152

153153
def _parse_x_www_form_urlencoded(self, data: bytes) -> None:
154-
decoded_data = data.decode()
154+
decoded_data = data.decode("utf-8")
155155

156156
for field_name, value in [
157157
key_value.split("=", 1) for key_value in decoded_data.split("&")
@@ -169,12 +169,12 @@ def _parse_multipart_form_data(self, data: bytes, boundary: str) -> None:
169169
self._add_field_value(field_name, value)
170170

171171
def _parse_text_plain(self, data: bytes) -> None:
172-
lines = data.split(b"\r\n")[:-1]
172+
lines = data.decode("utf-8").split("\r\n")[:-1]
173173

174174
for line in lines:
175-
field_name, value = line.split(b"=", 1)
175+
field_name, value = line.split("=", 1)
176176

177-
self._add_field_value(field_name.decode(), value.decode())
177+
self._add_field_value(field_name, value)
178178

179179

180180
class Request:
@@ -231,7 +231,7 @@ class Request:
231231

232232
raw_request: bytes
233233
"""
234-
Raw 'bytes' that were received from the client.
234+
Raw ``bytes`` that were received from the client.
235235
236236
Should **not** be modified directly.
237237
"""
@@ -340,10 +340,10 @@ def _raw_body_bytes(self) -> bytes:
340340
return self.raw_request[empty_line_index + 4 :]
341341

342342
@staticmethod
343-
def _parse_start_line(header_bytes: bytes) -> Tuple[str, str, Dict[str, str], str]:
343+
def _parse_start_line(header_bytes: bytes) -> Tuple[str, str, QueryParams, str]:
344344
"""Parse HTTP Start line to method, path, query_params and http_version."""
345345

346-
start_line = header_bytes.decode("utf8").splitlines()[0]
346+
start_line = header_bytes.decode("utf-8").splitlines()[0]
347347

348348
method, path, http_version = start_line.split()
349349

@@ -359,7 +359,7 @@ def _parse_start_line(header_bytes: bytes) -> Tuple[str, str, Dict[str, str], st
359359
@staticmethod
360360
def _parse_headers(header_bytes: bytes) -> Headers:
361361
"""Parse HTTP headers from raw request."""
362-
header_lines = header_bytes.decode("utf8").splitlines()[1:]
362+
header_lines = header_bytes.decode("utf-8").splitlines()[1:]
363363

364364
return Headers(
365365
{

adafruit_httpserver/response.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ def __init__( # pylint: disable=too-many-arguments
178178
:param str filename: Name of the file to send.
179179
:param str root_path: Path to the root directory from which to serve files. Defaults to
180180
server's ``root_path``.
181-
:param Status status: Status code and text. Defaults to 200 OK.
181+
:param Status status: Status code and text. Defaults to ``200 OK``.
182182
:param Headers headers: Headers to include in response.
183183
:param str content_type: Content type of response.
184-
:param bool as_attachment: If True, the file will be sent as an attachment.
184+
:param bool as_attachment: If ``True``, the file will be sent as an attachment.
185185
:param str download_filename: Name of the file to send as an attachment.
186-
:param int buffer_size: Size of the buffer used to send the file. Defaults to 1024.
187-
:param bool head_only: If True, only headers will be sent. Defaults to False.
188-
:param bool safe: If True, checks if ``filename`` is valid. Defaults to True.
186+
:param int buffer_size: Size of the buffer used to send the file. Defaults to ``1024``.
187+
:param bool head_only: If ``True``, only headers will be sent. Defaults to ``False``.
188+
:param bool safe: If ``True``, checks if ``filename`` is valid. Defaults to ``True``.
189189
"""
190190
if safe:
191191
self._verify_file_path_is_valid(filename)

0 commit comments

Comments
 (0)