adafruit-circuitpython-httpserver 4.4.5__tar.gz → 4.5.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/PKG-INFO +1 -1
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_circuitpython_httpserver.egg-info/PKG-INFO +1 -1
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_circuitpython_httpserver.egg-info/SOURCES.txt +1 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/__init__.py +1 -1
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/response.py +11 -1
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/route.py +65 -86
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/server.py +64 -38
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/examples.rst +18 -3
- adafruit-circuitpython-httpserver-4.5.1/examples/httpserver_cpython.py +22 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_handler_serves_file.py +3 -3
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/pyproject.toml +1 -1
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.github/PULL_REQUEST_TEMPLATE/adafruit_circuitpython_pr.md +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.github/workflows/build.yml +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.github/workflows/failure-help-text.yml +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.github/workflows/release_gh.yml +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.github/workflows/release_pypi.yml +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.gitignore +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.pre-commit-config.yaml +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.pylintrc +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.readthedocs.yaml +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/CODE_OF_CONDUCT.md +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/LICENSE +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/LICENSES/CC-BY-4.0.txt +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/LICENSES/MIT.txt +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/LICENSES/Unlicense.txt +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/README.rst +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/README.rst.license +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_circuitpython_httpserver.egg-info/dependency_links.txt +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_circuitpython_httpserver.egg-info/requires.txt +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_circuitpython_httpserver.egg-info/top_level.txt +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/authentication.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/exceptions.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/headers.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/interfaces.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/methods.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/mime_types.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/request.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/adafruit_httpserver/status.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/_static/favicon.ico +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/_static/favicon.ico.license +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/api.rst +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/api.rst.license +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/conf.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/examples.rst.license +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/index.rst +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/index.rst.license +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/requirements.txt +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/directory_listing.tpl.html +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/home.html +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_authentication_handlers.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_authentication_server.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_chunked.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_cookies.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_cpu_information.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_ethernet_simpletest.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_form_data.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_mdns.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_methods.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_multiple_servers.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_neopixel.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_redirects.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_simpletest_auto.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_simpletest_manual.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_sse.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_start_and_poll.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_start_and_poll_asyncio.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_static_files_serving.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_templates.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_url_parameters.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/httpserver_websocket.py +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/examples/settings.toml +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/optional_requirements.txt +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/requirements.txt +0 -0
- {adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/setup.cfg +0 -0
|
@@ -51,6 +51,7 @@ examples/httpserver_authentication_server.py
|
|
|
51
51
|
examples/httpserver_chunked.py
|
|
52
52
|
examples/httpserver_cookies.py
|
|
53
53
|
examples/httpserver_cpu_information.py
|
|
54
|
+
examples/httpserver_cpython.py
|
|
54
55
|
examples/httpserver_ethernet_simpletest.py
|
|
55
56
|
examples/httpserver_form_data.py
|
|
56
57
|
examples/httpserver_handler_serves_file.py
|
|
@@ -17,9 +17,19 @@ except ImportError:
|
|
|
17
17
|
import os
|
|
18
18
|
import json
|
|
19
19
|
from binascii import b2a_base64
|
|
20
|
-
import hashlib
|
|
21
20
|
from errno import EAGAIN, ECONNRESET, ETIMEDOUT, ENOTCONN
|
|
22
21
|
|
|
22
|
+
try:
|
|
23
|
+
try:
|
|
24
|
+
import hashlib
|
|
25
|
+
except ImportError:
|
|
26
|
+
import adafruit_hashlib as hashlib
|
|
27
|
+
except ImportError:
|
|
28
|
+
print(
|
|
29
|
+
"WARNING: hashlib module not available and adafruit_hashlib not installed.",
|
|
30
|
+
"Websocket support will not work.",
|
|
31
|
+
)
|
|
32
|
+
|
|
23
33
|
from .exceptions import (
|
|
24
34
|
BackslashInPathError,
|
|
25
35
|
FileNotExistsError,
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
10
|
try:
|
|
11
|
-
from typing import Callable,
|
|
11
|
+
from typing import Callable, Iterable, Union, Tuple, Literal, Dict, TYPE_CHECKING
|
|
12
12
|
|
|
13
13
|
if TYPE_CHECKING:
|
|
14
14
|
from .response import Response
|
|
@@ -23,6 +23,24 @@ from .methods import GET
|
|
|
23
23
|
class Route:
|
|
24
24
|
"""Route definition for different paths, see `adafruit_httpserver.server.Server.route`."""
|
|
25
25
|
|
|
26
|
+
@staticmethod
|
|
27
|
+
def _prepare_path_pattern(path: str, append_slash: bool) -> str:
|
|
28
|
+
# Escape all dots
|
|
29
|
+
path = re.sub(r"\.", r"\\.", path)
|
|
30
|
+
|
|
31
|
+
# Replace url parameters with regex groups
|
|
32
|
+
path = re.sub(r"<\w+>", r"([^/]+)", path)
|
|
33
|
+
|
|
34
|
+
# Replace wildcards with corresponding regex
|
|
35
|
+
path = path.replace(r"\.\.\.\.", r".+").replace(r"\.\.\.", r"[^/]+")
|
|
36
|
+
|
|
37
|
+
# Add optional slash at the end if append_slash is True
|
|
38
|
+
if append_slash:
|
|
39
|
+
path += r"/?"
|
|
40
|
+
|
|
41
|
+
# Add start and end of string anchors
|
|
42
|
+
return f"^{path}$"
|
|
43
|
+
|
|
26
44
|
def __init__(
|
|
27
45
|
self,
|
|
28
46
|
path: str = "",
|
|
@@ -33,80 +51,89 @@ class Route:
|
|
|
33
51
|
) -> None:
|
|
34
52
|
self._validate_path(path, append_slash)
|
|
35
53
|
|
|
36
|
-
self.
|
|
37
|
-
name[1:-1] for name in re.compile(r"/[^<>]*/?").split(path) if name != ""
|
|
38
|
-
]
|
|
39
|
-
self.path = re.sub(r"<\w+>", r"([^/]+)", path).replace("....", r".+").replace(
|
|
40
|
-
"...", r"[^/]+"
|
|
41
|
-
) + ("/?" if append_slash else "")
|
|
54
|
+
self.path = path
|
|
42
55
|
self.methods = (
|
|
43
56
|
set(methods) if isinstance(methods, (set, list, tuple)) else set([methods])
|
|
44
57
|
)
|
|
45
|
-
|
|
46
58
|
self.handler = handler
|
|
59
|
+
self.parameters_names = [
|
|
60
|
+
name[1:-1] for name in re.compile(r"/[^<>]*/?").split(path) if name != ""
|
|
61
|
+
]
|
|
62
|
+
self.path_pattern = re.compile(self._prepare_path_pattern(path, append_slash))
|
|
47
63
|
|
|
48
64
|
@staticmethod
|
|
49
65
|
def _validate_path(path: str, append_slash: bool) -> None:
|
|
50
66
|
if not path.startswith("/"):
|
|
51
67
|
raise ValueError("Path must start with a slash.")
|
|
52
68
|
|
|
69
|
+
if path.endswith("/") and append_slash:
|
|
70
|
+
raise ValueError("Cannot use append_slash=True when path ends with /")
|
|
71
|
+
|
|
72
|
+
if "//" in path:
|
|
73
|
+
raise ValueError("Path cannot contain double slashes.")
|
|
74
|
+
|
|
53
75
|
if "<>" in path:
|
|
54
76
|
raise ValueError("All URL parameters must be named.")
|
|
55
77
|
|
|
56
|
-
if
|
|
57
|
-
raise ValueError("
|
|
78
|
+
if re.search(r"[^/]<[^/]+>|<[^/]+>[^/]", path):
|
|
79
|
+
raise ValueError("All URL parameters must be between slashes.")
|
|
80
|
+
|
|
81
|
+
if re.search(r"[^/.]\.\.\.\.?|\.?\.\.\.[^/.]", path):
|
|
82
|
+
raise ValueError("... and .... must be between slashes")
|
|
58
83
|
|
|
59
|
-
|
|
84
|
+
if "....." in path:
|
|
85
|
+
raise ValueError("Path cannot contain more than 4 dots in a row.")
|
|
86
|
+
|
|
87
|
+
def matches(
|
|
88
|
+
self, method: str, path: str
|
|
89
|
+
) -> Union[Tuple[Literal[False], None], Tuple[Literal[True], Dict[str, str]]]:
|
|
60
90
|
"""
|
|
61
|
-
Checks if the route matches
|
|
91
|
+
Checks if the route matches given ``method`` and ``path``.
|
|
62
92
|
|
|
63
|
-
If the route contains parameters, it will check if the ``
|
|
93
|
+
If the route contains parameters, it will check if the ``path`` contains values for
|
|
64
94
|
them.
|
|
65
95
|
|
|
66
|
-
Returns tuple of a boolean
|
|
67
|
-
|
|
96
|
+
Returns tuple of a boolean that indicates if the routes matches and a dict containing
|
|
97
|
+
values for url parameters.
|
|
98
|
+
If the route does not match ``path`` or ``method`` if will return ``None`` instead of dict.
|
|
68
99
|
|
|
69
100
|
Examples::
|
|
70
101
|
|
|
71
|
-
route = Route("/example", GET, True)
|
|
102
|
+
route = Route("/example", GET, append_slash=True)
|
|
72
103
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
route.matches(other1a) # True, {}
|
|
76
|
-
route.matches(other1b) # True, {}
|
|
104
|
+
route.matches(GET, "/example") # True, {}
|
|
105
|
+
route.matches(GET, "/example/") # True, {}
|
|
77
106
|
|
|
78
|
-
|
|
79
|
-
route.matches(
|
|
107
|
+
route.matches(GET, "/other-example") # False, None
|
|
108
|
+
route.matches(POST, "/example/") # False, None
|
|
80
109
|
|
|
81
110
|
...
|
|
82
111
|
|
|
83
112
|
route = Route("/example/<parameter>", GET)
|
|
84
113
|
|
|
85
|
-
|
|
86
|
-
route.matches(other1) # True, {"parameter": "123"}
|
|
114
|
+
route.matches(GET, "/example/123") # True, {"parameter": "123"}
|
|
87
115
|
|
|
88
|
-
|
|
89
|
-
route.matches(other2) # False, {}
|
|
116
|
+
route.matches(GET, "/other-example") # False, None
|
|
90
117
|
|
|
91
118
|
...
|
|
92
119
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
route1.matches(other1) # True, {}
|
|
120
|
+
route = Route("/example/.../something", GET)
|
|
121
|
+
route.matches(GET, "/example/123/something") # True, {}
|
|
96
122
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
route2.matches(other2) # True, {}
|
|
123
|
+
route = Route("/example/..../something", GET)
|
|
124
|
+
route.matches(GET, "/example/123/456/something") # True, {}
|
|
100
125
|
"""
|
|
101
126
|
|
|
102
|
-
if not
|
|
103
|
-
return False,
|
|
127
|
+
if method not in self.methods:
|
|
128
|
+
return False, None
|
|
129
|
+
|
|
130
|
+
path_match = self.path_pattern.match(path)
|
|
131
|
+
if path_match is None:
|
|
132
|
+
return False, None
|
|
104
133
|
|
|
105
|
-
|
|
106
|
-
if regex_match is None:
|
|
107
|
-
return False, {}
|
|
134
|
+
url_parameters_values = path_match.groups()
|
|
108
135
|
|
|
109
|
-
return True, dict(zip(self.parameters_names,
|
|
136
|
+
return True, dict(zip(self.parameters_names, url_parameters_values))
|
|
110
137
|
|
|
111
138
|
def __repr__(self) -> str:
|
|
112
139
|
path = repr(self.path)
|
|
@@ -168,51 +195,3 @@ def as_route(
|
|
|
168
195
|
return Route(path, methods, func, append_slash=append_slash)
|
|
169
196
|
|
|
170
197
|
return route_decorator
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
class _Routes:
|
|
174
|
-
"""A collection of routes and their corresponding handlers."""
|
|
175
|
-
|
|
176
|
-
def __init__(self) -> None:
|
|
177
|
-
self._routes: List[Route] = []
|
|
178
|
-
|
|
179
|
-
def add(self, route: Route):
|
|
180
|
-
"""Adds a route and its handler to the collection."""
|
|
181
|
-
self._routes.append(route)
|
|
182
|
-
|
|
183
|
-
def find_handler(self, route: Route) -> Union[Callable["...", "Response"], None]:
|
|
184
|
-
"""
|
|
185
|
-
Finds a handler for a given route.
|
|
186
|
-
|
|
187
|
-
If route used URL parameters, the handler will be wrapped to pass the parameters to the
|
|
188
|
-
handler.
|
|
189
|
-
|
|
190
|
-
Example::
|
|
191
|
-
|
|
192
|
-
@server.route("/example/<my_parameter>", GET)
|
|
193
|
-
def route_func(request, my_parameter):
|
|
194
|
-
...
|
|
195
|
-
request.path == "/example/123" # True
|
|
196
|
-
my_parameter == "123" # True
|
|
197
|
-
"""
|
|
198
|
-
found_route, _route = False, None
|
|
199
|
-
|
|
200
|
-
for _route in self._routes:
|
|
201
|
-
matches, keyword_parameters = _route.match(route)
|
|
202
|
-
|
|
203
|
-
if matches:
|
|
204
|
-
found_route = True
|
|
205
|
-
break
|
|
206
|
-
|
|
207
|
-
if not found_route:
|
|
208
|
-
return None
|
|
209
|
-
|
|
210
|
-
handler = _route.handler
|
|
211
|
-
|
|
212
|
-
def wrapped_handler(request):
|
|
213
|
-
return handler(request, **keyword_parameters)
|
|
214
|
-
|
|
215
|
-
return wrapped_handler
|
|
216
|
-
|
|
217
|
-
def __repr__(self) -> str:
|
|
218
|
-
return f"_Routes({repr(self._routes)})"
|
|
@@ -30,7 +30,7 @@ from .headers import Headers
|
|
|
30
30
|
from .methods import GET, HEAD
|
|
31
31
|
from .request import Request
|
|
32
32
|
from .response import Response, FileResponse
|
|
33
|
-
from .route import
|
|
33
|
+
from .route import Route
|
|
34
34
|
from .status import BAD_REQUEST_400, UNAUTHORIZED_401, FORBIDDEN_403, NOT_FOUND_404
|
|
35
35
|
|
|
36
36
|
|
|
@@ -65,7 +65,7 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
65
65
|
self._auths = []
|
|
66
66
|
self._buffer = bytearray(1024)
|
|
67
67
|
self._timeout = 1
|
|
68
|
-
self._routes =
|
|
68
|
+
self._routes: "List[Route]" = []
|
|
69
69
|
self._socket_source = socket_source
|
|
70
70
|
self._sock = None
|
|
71
71
|
self.headers = Headers()
|
|
@@ -132,7 +132,7 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
132
132
|
"""
|
|
133
133
|
|
|
134
134
|
def route_decorator(func: Callable) -> Callable:
|
|
135
|
-
self._routes.
|
|
135
|
+
self._routes.append(Route(path, methods, func, append_slash=append_slash))
|
|
136
136
|
return func
|
|
137
137
|
|
|
138
138
|
return route_decorator
|
|
@@ -157,8 +157,7 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
157
157
|
external_route2,
|
|
158
158
|
]}
|
|
159
159
|
"""
|
|
160
|
-
|
|
161
|
-
self._routes.add(route)
|
|
160
|
+
self._routes.extend(routes)
|
|
162
161
|
|
|
163
162
|
def _verify_can_start(self, host: str, port: int) -> None:
|
|
164
163
|
"""Check if the server can be successfully started. Raises RuntimeError if not."""
|
|
@@ -172,7 +171,7 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
172
171
|
raise RuntimeError(f"Cannot start server on {host}:{port}") from error
|
|
173
172
|
|
|
174
173
|
def serve_forever(
|
|
175
|
-
self, host: str, port: int = 80, *, poll_interval: float =
|
|
174
|
+
self, host: str, port: int = 80, *, poll_interval: float = 0.1
|
|
176
175
|
) -> None:
|
|
177
176
|
"""
|
|
178
177
|
Wait for HTTP requests at the given host and port. Does not return.
|
|
@@ -187,16 +186,14 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
187
186
|
|
|
188
187
|
while not self.stopped:
|
|
189
188
|
try:
|
|
190
|
-
self.poll()
|
|
189
|
+
if self.poll() == NO_REQUEST and poll_interval is not None:
|
|
190
|
+
sleep(poll_interval)
|
|
191
191
|
except KeyboardInterrupt: # Exit on Ctrl-C e.g. during development
|
|
192
192
|
self.stop()
|
|
193
193
|
return
|
|
194
194
|
except Exception: # pylint: disable=broad-except
|
|
195
195
|
pass # Ignore exceptions in handler function
|
|
196
196
|
|
|
197
|
-
if poll_interval is not None:
|
|
198
|
-
sleep(poll_interval)
|
|
199
|
-
|
|
200
197
|
def start(self, host: str, port: int = 80) -> None:
|
|
201
198
|
"""
|
|
202
199
|
Start the HTTP server at the given host and port. Requires calling
|
|
@@ -234,32 +231,6 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
234
231
|
if self.debug:
|
|
235
232
|
_debug_stopped_server(self)
|
|
236
233
|
|
|
237
|
-
def _receive_request(
|
|
238
|
-
self,
|
|
239
|
-
sock: Union["SocketPool.Socket", "socket.socket"],
|
|
240
|
-
client_address: Tuple[str, int],
|
|
241
|
-
) -> Request:
|
|
242
|
-
"""Receive bytes from socket until the whole request is received."""
|
|
243
|
-
|
|
244
|
-
# Receiving data until empty line
|
|
245
|
-
header_bytes = self._receive_header_bytes(sock)
|
|
246
|
-
|
|
247
|
-
# Return if no data received
|
|
248
|
-
if not header_bytes:
|
|
249
|
-
return None
|
|
250
|
-
|
|
251
|
-
request = Request(self, sock, client_address, header_bytes)
|
|
252
|
-
|
|
253
|
-
content_length = int(request.headers.get_directive("Content-Length", 0))
|
|
254
|
-
received_body_bytes = request.body
|
|
255
|
-
|
|
256
|
-
# Receiving remaining body bytes
|
|
257
|
-
request.body = self._receive_body_bytes(
|
|
258
|
-
sock, received_body_bytes, content_length
|
|
259
|
-
)
|
|
260
|
-
|
|
261
|
-
return request
|
|
262
|
-
|
|
263
234
|
def _receive_header_bytes(
|
|
264
235
|
self, sock: Union["SocketPool.Socket", "socket.socket"]
|
|
265
236
|
) -> bytes:
|
|
@@ -296,6 +267,61 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
296
267
|
raise ex
|
|
297
268
|
return received_body_bytes[:content_length]
|
|
298
269
|
|
|
270
|
+
def _receive_request(
|
|
271
|
+
self,
|
|
272
|
+
sock: Union["SocketPool.Socket", "socket.socket"],
|
|
273
|
+
client_address: Tuple[str, int],
|
|
274
|
+
) -> Request:
|
|
275
|
+
"""Receive bytes from socket until the whole request is received."""
|
|
276
|
+
|
|
277
|
+
# Receiving data until empty line
|
|
278
|
+
header_bytes = self._receive_header_bytes(sock)
|
|
279
|
+
|
|
280
|
+
# Return if no data received
|
|
281
|
+
if not header_bytes:
|
|
282
|
+
return None
|
|
283
|
+
|
|
284
|
+
request = Request(self, sock, client_address, header_bytes)
|
|
285
|
+
|
|
286
|
+
content_length = int(request.headers.get_directive("Content-Length", 0))
|
|
287
|
+
received_body_bytes = request.body
|
|
288
|
+
|
|
289
|
+
# Receiving remaining body bytes
|
|
290
|
+
request.body = self._receive_body_bytes(
|
|
291
|
+
sock, received_body_bytes, content_length
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
return request
|
|
295
|
+
|
|
296
|
+
def _find_handler( # pylint: disable=cell-var-from-loop
|
|
297
|
+
self, method: str, path: str
|
|
298
|
+
) -> Union[Callable[..., "Response"], None]:
|
|
299
|
+
"""
|
|
300
|
+
Finds a handler for a given route.
|
|
301
|
+
|
|
302
|
+
If route used URL parameters, the handler will be wrapped to pass the parameters to the
|
|
303
|
+
handler.
|
|
304
|
+
|
|
305
|
+
Example::
|
|
306
|
+
|
|
307
|
+
@server.route("/example/<my_parameter>", GET)
|
|
308
|
+
def route_func(request, my_parameter):
|
|
309
|
+
...
|
|
310
|
+
request.path == "/example/123" # True
|
|
311
|
+
my_parameter == "123" # True
|
|
312
|
+
"""
|
|
313
|
+
for route in self._routes:
|
|
314
|
+
route_matches, url_parameters = route.matches(method, path)
|
|
315
|
+
|
|
316
|
+
if route_matches:
|
|
317
|
+
|
|
318
|
+
def wrapped_handler(request):
|
|
319
|
+
return route.handler(request, **url_parameters)
|
|
320
|
+
|
|
321
|
+
return wrapped_handler
|
|
322
|
+
|
|
323
|
+
return None
|
|
324
|
+
|
|
299
325
|
def _handle_request(
|
|
300
326
|
self, request: Request, handler: Union[Callable, None]
|
|
301
327
|
) -> Union[Response, None]:
|
|
@@ -371,8 +397,8 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
371
397
|
conn.close()
|
|
372
398
|
return CONNECTION_TIMED_OUT
|
|
373
399
|
|
|
374
|
-
# Find a
|
|
375
|
-
handler = self.
|
|
400
|
+
# Find a route that matches the request's method and path and get its handler
|
|
401
|
+
handler = self._find_handler(request.method, request.path)
|
|
376
402
|
|
|
377
403
|
# Handle the request
|
|
378
404
|
response = self._handle_request(request, handler)
|
|
@@ -5,7 +5,7 @@ Simple Test
|
|
|
5
5
|
**This mode is useful for development, but it is not recommended to use it in production.**
|
|
6
6
|
**More about Debug mode at the end of Examples section.**
|
|
7
7
|
|
|
8
|
-
This is the minimal example of using the library.
|
|
8
|
+
This is the minimal example of using the library with CircuitPython.
|
|
9
9
|
This example is serving a simple static text message.
|
|
10
10
|
|
|
11
11
|
It also manually connects to the WiFi network.
|
|
@@ -43,6 +43,17 @@ Note that we still need to import ``socketpool`` and ``wifi`` modules.
|
|
|
43
43
|
:emphasize-lines: 11
|
|
44
44
|
:linenos:
|
|
45
45
|
|
|
46
|
+
CPython usage
|
|
47
|
+
--------------------
|
|
48
|
+
|
|
49
|
+
Library can also be used in CPython, no changes other than changing the ``socket_source`` are necessary.
|
|
50
|
+
|
|
51
|
+
.. literalinclude:: ../examples/httpserver_cpython.py
|
|
52
|
+
:caption: examples/httpserver_cpython.py
|
|
53
|
+
:emphasize-lines: 5,10
|
|
54
|
+
:linenos:
|
|
55
|
+
|
|
56
|
+
|
|
46
57
|
Serving static files
|
|
47
58
|
--------------------
|
|
48
59
|
|
|
@@ -58,11 +69,15 @@ In order to save memory, we are unregistering unused MIME types and registering
|
|
|
58
69
|
:linenos:
|
|
59
70
|
|
|
60
71
|
You can also serve a specific file from the handler.
|
|
61
|
-
By default ``FileResponse`` looks for the file in the server's ``root_path`` directory
|
|
72
|
+
By default ``FileResponse`` looks for the file in the server's ``root_path`` directory
|
|
73
|
+
(``/default-static-directory`` in the example below), but you can change it manually in every ``FileResponse``
|
|
74
|
+
(to e.g. ``/other-static-directory``, as in example below).
|
|
75
|
+
|
|
76
|
+
By doing that, you can serve files from multiple directories, and decide exactly which files are accessible.
|
|
62
77
|
|
|
63
78
|
.. literalinclude:: ../examples/httpserver_handler_serves_file.py
|
|
64
79
|
:caption: examples/httpserver_handler_serves_file.py
|
|
65
|
-
:emphasize-lines: 22
|
|
80
|
+
:emphasize-lines: 13,22
|
|
66
81
|
:linenos:
|
|
67
82
|
|
|
68
83
|
.. literalinclude:: ../examples/home.html
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2024 Michał Pokusa
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Unlicense
|
|
4
|
+
|
|
5
|
+
import socket
|
|
6
|
+
|
|
7
|
+
from adafruit_httpserver import Server, Request, Response
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
pool = socket
|
|
11
|
+
server = Server(pool, "/static", debug=True)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@server.route("/")
|
|
15
|
+
def base(request: Request):
|
|
16
|
+
"""
|
|
17
|
+
Serve a default static plain text message.
|
|
18
|
+
"""
|
|
19
|
+
return Response(request, "Hello from the CircuitPython HTTP Server!")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
server.serve_forever("0.0.0.0")
|
|
@@ -10,16 +10,16 @@ from adafruit_httpserver import Server, Request, FileResponse
|
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
pool = socketpool.SocketPool(wifi.radio)
|
|
13
|
-
server = Server(pool, "/static", debug=True)
|
|
13
|
+
server = Server(pool, "/default-static-folder", debug=True)
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
@server.route("/home")
|
|
17
17
|
def home(request: Request):
|
|
18
18
|
"""
|
|
19
|
-
Serves the file /
|
|
19
|
+
Serves the file /other-static-folder/home.html.
|
|
20
20
|
"""
|
|
21
21
|
|
|
22
|
-
return FileResponse(request, "home.html", "/
|
|
22
|
+
return FileResponse(request, "home.html", "/other-static-folder")
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
server.serve_forever(str(wifi.radio.ipv4_address))
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/pyproject.toml
RENAMED
|
@@ -12,7 +12,7 @@ requires = [
|
|
|
12
12
|
[project]
|
|
13
13
|
name = "adafruit-circuitpython-httpserver"
|
|
14
14
|
description = "Simple HTTP Server for CircuitPython"
|
|
15
|
-
version = "4.
|
|
15
|
+
version = "4.5.1"
|
|
16
16
|
readme = "README.rst"
|
|
17
17
|
authors = [
|
|
18
18
|
{name = "Adafruit Industries", email = "circuitpython@adafruit.com"}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.gitignore
RENAMED
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/.pylintrc
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/LICENSES/MIT.txt
RENAMED
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/README.rst
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/api.rst
RENAMED
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/conf.py
RENAMED
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/docs/index.rst
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/requirements.txt
RENAMED
|
File without changes
|
{adafruit-circuitpython-httpserver-4.4.5 → adafruit-circuitpython-httpserver-4.5.1}/setup.cfg
RENAMED
|
File without changes
|