Skip to content

Case insensitive HTTPHeaders, HTTPResponse context manager and some fixes #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
aad7010
Added HTTPHeaders
michalpokusa Dec 19, 2022
f1878b3
Replacing dict with HTTPHeaders in other modules
michalpokusa Dec 19, 2022
d547c7f
Fixed and extended docstrings
michalpokusa Dec 19, 2022
d0d5b80
Changed order of Docs References and updated version
michalpokusa Dec 19, 2022
d3adfd8
Fixed wrong content length for multibyte characters
michalpokusa Dec 19, 2022
be20bb1
Black format changes
michalpokusa Dec 19, 2022
aeb95a2
Encoding body only once when constructing response bytes
michalpokusa Dec 20, 2022
c1d2f55
Merge remote-tracking branch 'origin/main' into test
michalpokusa Dec 23, 2022
00d3247
Refactor for unifying the HTTPResponse API
michalpokusa Dec 23, 2022
2ca4756
Changed HTTPResponse to use context managers
michalpokusa Dec 23, 2022
a12d4ab
Minor changes to docstrings
michalpokusa Dec 23, 2022
c222d09
Changed send_chunk_body to send_chunk for unification
michalpokusa Dec 24, 2022
4b61d74
Fix: Missing chunk length in ending message due to .lstrip()
michalpokusa Dec 26, 2022
f0b61a7
Prevented from calling .send() multiple times and added deprecation e…
michalpokusa Dec 25, 2022
e2e396b
Updated existing and added more examples
michalpokusa Dec 26, 2022
287c5e0
Updated README.rst
michalpokusa Dec 26, 2022
21aa09e
Fixed CI for examples/
michalpokusa Dec 26, 2022
d14a13a
Fix: Content type not setting properly
michalpokusa Dec 28, 2022
c758e51
Changed root to root_path in docstrings
michalpokusa Dec 28, 2022
140c0e1
Minor adjustments to examples
michalpokusa Jan 2, 2023
c609a82
Changed address to client_address to match CPython's http.server modu…
michalpokusa Jan 2, 2023
5b0135a
Changed version in docs/conf.py from 1.1.0 to 1.0, similar to other l…
michalpokusa Jan 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions adafruit_httpserver/headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# SPDX-FileCopyrightText: Copyright (c) 2022 Dan Halbert for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_httpserver.headers.HTTPHeaders`
====================================================
* Author(s): Michał Pokusa
"""

try:
from typing import Dict, Tuple
except ImportError:
pass


class HTTPHeaders:
"""
A dict-like class for storing HTTP headers.

Allows access to headers using **case insensitive** names.

Does **not** implement all dict methods.

Examples::

headers = HTTPHeaders({"Content-Type": "text/html", "Content-Length": "1024"})

len(headers)
# 2

headers.setdefault("Access-Control-Allow-Origin", "*")
headers["Access-Control-Allow-Origin"]
# '*'

headers["Content-Length"]
# '1024'

headers["content-type"]
# 'text/html'

"CONTENT-TYPE" in headers
# True
"""

_storage: Dict[str, Tuple[str, str]]

def __init__(self, headers: Dict[str, str] = None) -> None:

headers = headers or {}

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

def get(self, name: str, default: str = None):
"""Returns the value for the given header name, or default if not found."""
return self._storage.get(name.lower(), [None, default])[1]

def setdefault(self, name: str, default: str = None):
"""Sets the value for the given header name if it does not exist."""
return self._storage.setdefault(name.lower(), [name, default])[1]

def items(self):
"""Returns a list of (name, value) tuples."""
return dict(self._storage.values()).items()

def keys(self):
"""Returns a list of header names."""
return dict(self._storage.values()).keys()

def values(self):
"""Returns a list of header values."""
return dict(self._storage.values()).values()

def update(self, headers: Dict[str, str]):
"""Updates the headers with the given dict."""
return self._storage.update(
{key.lower(): [key, value] for key, value in headers.items()}
)

def copy(self):
"""Returns a copy of the headers."""
return HTTPHeaders(dict(self._storage.values()))

def __getitem__(self, name: str):
return self._storage[name.lower()][1]

def __setitem__(self, name: str, value: str):
self._storage[name.lower()] = [name, value]

def __delitem__(self, name: str):
del self._storage[name.lower()]

def __iter__(self):
return iter(dict(self._storage.values()))

def __len__(self):
return len(self._storage)

def __contains__(self, key: str):
return key.lower() in self._storage.keys()

def __repr__(self):
return f"{self.__class__.__name__}({dict(self._storage.values())})"
37 changes: 17 additions & 20 deletions adafruit_httpserver/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
except ImportError:
pass

from .headers import HTTPHeaders


class HTTPRequest:
"""
Expand Down Expand Up @@ -39,24 +41,17 @@ class HTTPRequest:
http_version: str
"""HTTP version, e.g. "HTTP/1.1"."""

headers: Dict[str, str]
headers: HTTPHeaders
"""
Headers from the request as `dict`.

Values should be accessed using **lower case header names**.

Example::

request.headers
# {'connection': 'keep-alive', 'content-length': '64' ...}
request.headers["content-length"]
# '64'
request.headers["Content-Length"]
# KeyError: 'Content-Length'
Headers from the request.
"""

raw_request: bytes
"""Raw bytes passed to the constructor."""
"""
Raw 'bytes' passed to the constructor and body 'bytes' received later.

Should **not** be modified directly.
"""

def __init__(self, raw_request: bytes = None) -> None:
self.raw_request = raw_request
Expand Down Expand Up @@ -120,12 +115,14 @@ def _parse_start_line(header_bytes: bytes) -> Tuple[str, str, Dict[str, str], st
return method, path, query_params, http_version

@staticmethod
def _parse_headers(header_bytes: bytes) -> Dict[str, str]:
def _parse_headers(header_bytes: bytes) -> HTTPHeaders:
"""Parse HTTP headers from raw request."""
header_lines = header_bytes.decode("utf8").splitlines()[1:]

return {
name.lower(): value
for header_line in header_lines
for name, value in [header_line.split(": ", 1)]
}
return HTTPHeaders(
{
name: value
for header_line in header_lines
for name, value in [header_line.split(": ", 1)]
}
)
28 changes: 15 additions & 13 deletions adafruit_httpserver/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

from .mime_type import MIMEType
from .status import HTTPStatus, CommonHTTPStatus
from .headers import HTTPHeaders


class HTTPResponse:
"""Details of an HTTP response. Use in `HTTPServer.route` decorator functions."""

http_version: str
status: HTTPStatus
headers: Dict[str, str]
headers: HTTPHeaders
content_type: str

filename: Optional[str]
Expand All @@ -39,7 +40,7 @@ def __init__( # pylint: disable=too-many-arguments
self,
status: Union[HTTPStatus, Tuple[int, str]] = CommonHTTPStatus.OK_200,
body: str = "",
headers: Dict[str, str] = None,
headers: Union[HTTPHeaders, Dict[str, str]] = None,
content_type: str = MIMEType.TYPE_TXT,
filename: Optional[str] = None,
root_path: str = "",
Expand All @@ -52,7 +53,9 @@ def __init__( # pylint: disable=too-many-arguments
"""
self.status = status if isinstance(status, HTTPStatus) else HTTPStatus(*status)
self.body = body
self.headers = headers or {}
self.headers = (
headers.copy() if isinstance(headers, HTTPHeaders) else HTTPHeaders(headers)
)
self.content_type = content_type
self.filename = filename
self.root_path = root_path
Expand All @@ -64,21 +67,20 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments
status: HTTPStatus = CommonHTTPStatus.OK_200,
content_type: str = MIMEType.TYPE_TXT,
content_length: Union[int, None] = None,
headers: Dict[str, str] = None,
headers: HTTPHeaders = None,
body: str = "",
) -> bytes:
"""Constructs the response bytes from the given parameters."""

response = f"{http_version} {status.code} {status.text}\r\n"

# Make a copy of the headers so that we don't modify the incoming dict
response_headers = {} if headers is None else headers.copy()

response_headers.setdefault("Content-Type", content_type)
response_headers.setdefault("Content-Length", content_length or len(body))
response_headers.setdefault("Connection", "close")
headers.setdefault("Content-Type", content_type)
headers.setdefault(
"Content-Length", content_length or len(body.encode("utf-8"))
)
headers.setdefault("Connection", "close")

for header, value in response_headers.items():
for header, value in headers.items():
response += f"{header}: {value}\r\n"

response += f"\r\n{body}"
Expand Down Expand Up @@ -122,7 +124,7 @@ def _send_response( # pylint: disable=too-many-arguments
status: HTTPStatus,
content_type: str,
body: str,
headers: Dict[str, str] = None,
headers: HTTPHeaders = None,
):
self._send_bytes(
conn,
Expand All @@ -140,7 +142,7 @@ def _send_file_response( # pylint: disable=too-many-arguments
filename: str,
root_path: str,
file_length: int,
headers: Dict[str, str] = None,
headers: HTTPHeaders = None,
):
self._send_bytes(
conn,
Expand Down
5 changes: 3 additions & 2 deletions adafruit_httpserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ def request_buffer_size(self, value: int) -> None:
def socket_timeout(self) -> int:
"""
Timeout after which the socket will stop waiting for more incoming data.
When exceeded, raises `OSError` with `errno.ETIMEDOUT`.

Default timeout is 0, which means socket is in non-blocking mode.
Must be set to positive integer or float. Default is 1 second.

When exceeded, raises `OSError` with `errno.ETIMEDOUT`.

Example::

Expand Down
13 changes: 8 additions & 5 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
.. automodule:: adafruit_httpserver
:members:

.. automodule:: adafruit_httpserver.methods
:members:

.. automodule:: adafruit_httpserver.mime_type
.. automodule:: adafruit_httpserver.server
:members:

.. automodule:: adafruit_httpserver.request
Expand All @@ -19,8 +16,14 @@
.. automodule:: adafruit_httpserver.response
:members:

.. automodule:: adafruit_httpserver.server
.. automodule:: adafruit_httpserver.headers
:members:

.. automodule:: adafruit_httpserver.status
:members:

.. automodule:: adafruit_httpserver.methods
:members:

.. automodule:: adafruit_httpserver.mime_type
:members:
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
# built documents.
#
# The short X.Y version.
version = "1.0"
version = "1.1.0"
# The full version, including alpha/beta/rc tags.
release = "1.0"
release = "1.1.0"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down