adafruit-circuitpython-httpserver 2.4.0__tar.gz → 3.0.0__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-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/PKG-INFO +1 -1
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/PKG-INFO +1 -1
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/SOURCES.txt +1 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/__init__.py +1 -1
- adafruit-circuitpython-httpserver-3.0.0/adafruit_httpserver/exceptions.py +52 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/response.py +71 -19
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/route.py +1 -1
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/server.py +47 -33
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/status.py +3 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/api.rst +3 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_chunked.py +4 -3
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_cpu_information.py +4 -3
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_mdns.py +4 -3
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_neopixel.py +4 -3
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_simple_poll.py +4 -3
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_simple_serve.py +4 -3
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_url_parameters.py +4 -3
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/pyproject.toml +1 -1
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/PULL_REQUEST_TEMPLATE/adafruit_circuitpython_pr.md +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/workflows/build.yml +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/workflows/failure-help-text.yml +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/workflows/release_gh.yml +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/workflows/release_pypi.yml +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.gitignore +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.pre-commit-config.yaml +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.pylintrc +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.readthedocs.yaml +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/CODE_OF_CONDUCT.md +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/LICENSE +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/LICENSES/CC-BY-4.0.txt +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/LICENSES/MIT.txt +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/LICENSES/Unlicense.txt +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/README.rst +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/README.rst.license +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/dependency_links.txt +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/requires.txt +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/top_level.txt +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/headers.py +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/methods.py +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/mime_type.py +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/request.py +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/_static/favicon.ico +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/_static/favicon.ico.license +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/api.rst.license +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/conf.py +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/examples.rst +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/examples.rst.license +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/index.rst +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/index.rst.license +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/requirements.txt +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/optional_requirements.txt +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/requirements.txt +0 -0
- {adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/setup.cfg +0 -0
|
@@ -23,6 +23,7 @@ adafruit_circuitpython_httpserver.egg-info/dependency_links.txt
|
|
|
23
23
|
adafruit_circuitpython_httpserver.egg-info/requires.txt
|
|
24
24
|
adafruit_circuitpython_httpserver.egg-info/top_level.txt
|
|
25
25
|
adafruit_httpserver/__init__.py
|
|
26
|
+
adafruit_httpserver/exceptions.py
|
|
26
27
|
adafruit_httpserver/headers.py
|
|
27
28
|
adafruit_httpserver/methods.py
|
|
28
29
|
adafruit_httpserver/mime_type.py
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2022 Dan Halbert for Adafruit Industries
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""
|
|
5
|
+
`adafruit_httpserver.exceptions`
|
|
6
|
+
====================================================
|
|
7
|
+
* Author(s): Michał Pokusa
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class InvalidPathError(Exception):
|
|
12
|
+
"""
|
|
13
|
+
Parent class for all path related errors.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ParentDirectoryReferenceError(InvalidPathError):
|
|
18
|
+
"""
|
|
19
|
+
Path contains ``..``, a reference to the parent directory.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, path: str) -> None:
|
|
23
|
+
"""Creates a new ``ParentDirectoryReferenceError`` for the ``path``."""
|
|
24
|
+
super().__init__(f"Parent directory reference in path: {path}")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class BackslashInPathError(InvalidPathError):
|
|
28
|
+
"""
|
|
29
|
+
Backslash ``\\`` in path.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, path: str) -> None:
|
|
33
|
+
"""Creates a new ``BackslashInPathError`` for the ``path``."""
|
|
34
|
+
super().__init__(f"Backslash in path: {path}")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ResponseAlreadySentError(Exception):
|
|
38
|
+
"""
|
|
39
|
+
Another ``HTTPResponse`` has already been sent. There can only be one per ``HTTPRequest``.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class FileNotExistsError(Exception):
|
|
44
|
+
"""
|
|
45
|
+
Raised when a file does not exist.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def __init__(self, path: str) -> None:
|
|
49
|
+
"""
|
|
50
|
+
Creates a new ``FileNotExistsError`` for the file at ``path``.
|
|
51
|
+
"""
|
|
52
|
+
super().__init__(f"File does not exist: {path}")
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
10
|
try:
|
|
11
|
-
from typing import Optional, Dict, Union, Tuple
|
|
11
|
+
from typing import Optional, Dict, Union, Tuple, Callable
|
|
12
12
|
from socket import socket
|
|
13
13
|
from socketpool import SocketPool
|
|
14
14
|
except ImportError:
|
|
@@ -17,12 +17,33 @@ except ImportError:
|
|
|
17
17
|
import os
|
|
18
18
|
from errno import EAGAIN, ECONNRESET
|
|
19
19
|
|
|
20
|
+
from .exceptions import (
|
|
21
|
+
BackslashInPathError,
|
|
22
|
+
FileNotExistsError,
|
|
23
|
+
ParentDirectoryReferenceError,
|
|
24
|
+
ResponseAlreadySentError,
|
|
25
|
+
)
|
|
20
26
|
from .mime_type import MIMEType
|
|
21
27
|
from .request import HTTPRequest
|
|
22
28
|
from .status import HTTPStatus, CommonHTTPStatus
|
|
23
29
|
from .headers import HTTPHeaders
|
|
24
30
|
|
|
25
31
|
|
|
32
|
+
def _prevent_multiple_send_calls(function: Callable):
|
|
33
|
+
"""
|
|
34
|
+
Decorator that prevents calling ``send`` or ``send_file`` more than once.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def wrapper(self: "HTTPResponse", *args, **kwargs):
|
|
38
|
+
if self._response_already_sent: # pylint: disable=protected-access
|
|
39
|
+
raise ResponseAlreadySentError
|
|
40
|
+
|
|
41
|
+
result = function(self, *args, **kwargs)
|
|
42
|
+
return result
|
|
43
|
+
|
|
44
|
+
return wrapper
|
|
45
|
+
|
|
46
|
+
|
|
26
47
|
class HTTPResponse:
|
|
27
48
|
"""
|
|
28
49
|
Response to a given `HTTPRequest`. Use in `HTTPServer.route` decorator functions.
|
|
@@ -73,8 +94,8 @@ class HTTPResponse:
|
|
|
73
94
|
"""
|
|
74
95
|
Defaults to ``text/plain`` if not set.
|
|
75
96
|
|
|
76
|
-
Can be explicitly provided in the constructor, in
|
|
77
|
-
implicitly determined from filename in
|
|
97
|
+
Can be explicitly provided in the constructor, in ``send()`` or
|
|
98
|
+
implicitly determined from filename in ``send_file()``.
|
|
78
99
|
|
|
79
100
|
Common MIME types are defined in `adafruit_httpserver.mime_type.MIMEType`.
|
|
80
101
|
"""
|
|
@@ -94,7 +115,7 @@ class HTTPResponse:
|
|
|
94
115
|
Sets `status`, ``headers`` and `http_version`
|
|
95
116
|
and optionally default ``content_type``.
|
|
96
117
|
|
|
97
|
-
To send the response, call
|
|
118
|
+
To send the response, call ``send`` or ``send_file``.
|
|
98
119
|
For chunked response use
|
|
99
120
|
``with HTTPRequest(request, content_type=..., chunked=True) as r:`` and `send_chunk`.
|
|
100
121
|
"""
|
|
@@ -115,7 +136,7 @@ class HTTPResponse:
|
|
|
115
136
|
) -> None:
|
|
116
137
|
"""
|
|
117
138
|
Sends headers.
|
|
118
|
-
Implicitly called by
|
|
139
|
+
Implicitly called by ``send`` and ``send_file`` and in
|
|
119
140
|
``with HTTPResponse(request, chunked=True) as response:`` context manager.
|
|
120
141
|
"""
|
|
121
142
|
headers = self.headers.copy()
|
|
@@ -141,6 +162,7 @@ class HTTPResponse:
|
|
|
141
162
|
self.request.connection, response_message_header.encode("utf-8")
|
|
142
163
|
)
|
|
143
164
|
|
|
165
|
+
@_prevent_multiple_send_calls
|
|
144
166
|
def send(
|
|
145
167
|
self,
|
|
146
168
|
body: str = "",
|
|
@@ -152,8 +174,6 @@ class HTTPResponse:
|
|
|
152
174
|
|
|
153
175
|
Should be called **only once** per response.
|
|
154
176
|
"""
|
|
155
|
-
if self._response_already_sent:
|
|
156
|
-
raise RuntimeError("Response was already sent")
|
|
157
177
|
|
|
158
178
|
if getattr(body, "encode", None):
|
|
159
179
|
encoded_response_message_body = body.encode("utf-8")
|
|
@@ -167,11 +187,41 @@ class HTTPResponse:
|
|
|
167
187
|
self._send_bytes(self.request.connection, encoded_response_message_body)
|
|
168
188
|
self._response_already_sent = True
|
|
169
189
|
|
|
170
|
-
|
|
190
|
+
@staticmethod
|
|
191
|
+
def _check_file_path_is_valid(file_path: str) -> bool:
|
|
192
|
+
"""
|
|
193
|
+
Checks if ``file_path`` is valid.
|
|
194
|
+
If not raises error corresponding to the problem.
|
|
195
|
+
"""
|
|
196
|
+
|
|
197
|
+
# Check for backslashes
|
|
198
|
+
if "\\" in file_path: # pylint: disable=anomalous-backslash-in-string
|
|
199
|
+
raise BackslashInPathError(file_path)
|
|
200
|
+
|
|
201
|
+
# Check each component of the path for parent directory references
|
|
202
|
+
for part in file_path.split("/"):
|
|
203
|
+
if part == "..":
|
|
204
|
+
raise ParentDirectoryReferenceError(file_path)
|
|
205
|
+
|
|
206
|
+
@staticmethod
|
|
207
|
+
def _get_file_length(file_path: str) -> int:
|
|
208
|
+
"""
|
|
209
|
+
Tries to get the length of the file at ``file_path``.
|
|
210
|
+
Raises ``FileNotExistsError`` if file does not exist.
|
|
211
|
+
"""
|
|
212
|
+
try:
|
|
213
|
+
return os.stat(file_path)[6]
|
|
214
|
+
except OSError:
|
|
215
|
+
raise FileNotExistsError(file_path) # pylint: disable=raise-missing-from
|
|
216
|
+
|
|
217
|
+
@_prevent_multiple_send_calls
|
|
218
|
+
def send_file( # pylint: disable=too-many-arguments
|
|
171
219
|
self,
|
|
172
220
|
filename: str = "index.html",
|
|
173
221
|
root_path: str = "./",
|
|
174
222
|
buffer_size: int = 1024,
|
|
223
|
+
head_only: bool = False,
|
|
224
|
+
safe: bool = True,
|
|
175
225
|
) -> None:
|
|
176
226
|
"""
|
|
177
227
|
Send response with content of ``filename`` located in ``root_path``.
|
|
@@ -180,26 +230,28 @@ class HTTPResponse:
|
|
|
180
230
|
|
|
181
231
|
Should be called **only once** per response.
|
|
182
232
|
"""
|
|
183
|
-
|
|
184
|
-
|
|
233
|
+
|
|
234
|
+
if safe:
|
|
235
|
+
self._check_file_path_is_valid(filename)
|
|
185
236
|
|
|
186
237
|
if not root_path.endswith("/"):
|
|
187
238
|
root_path += "/"
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
239
|
+
if filename.startswith("/"):
|
|
240
|
+
filename = filename[1:]
|
|
241
|
+
|
|
242
|
+
full_file_path = root_path + filename
|
|
243
|
+
|
|
244
|
+
file_length = self._get_file_length(full_file_path)
|
|
194
245
|
|
|
195
246
|
self._send_headers(
|
|
196
247
|
content_type=MIMEType.from_file_name(filename),
|
|
197
248
|
content_length=file_length,
|
|
198
249
|
)
|
|
199
250
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
251
|
+
if not head_only:
|
|
252
|
+
with open(full_file_path, "rb") as file:
|
|
253
|
+
while bytes_read := file.read(buffer_size):
|
|
254
|
+
self._send_bytes(self.request.connection, bytes_read)
|
|
203
255
|
self._response_already_sent = True
|
|
204
256
|
|
|
205
257
|
def send_chunk(self, chunk: str = "") -> None:
|
|
@@ -16,6 +16,7 @@ except ImportError:
|
|
|
16
16
|
|
|
17
17
|
from errno import EAGAIN, ECONNRESET, ETIMEDOUT
|
|
18
18
|
|
|
19
|
+
from .exceptions import FileNotExistsError, InvalidPathError
|
|
19
20
|
from .methods import HTTPMethod
|
|
20
21
|
from .request import HTTPRequest
|
|
21
22
|
from .response import HTTPResponse
|
|
@@ -26,18 +27,19 @@ from .status import CommonHTTPStatus
|
|
|
26
27
|
class HTTPServer:
|
|
27
28
|
"""A basic socket-based HTTP server."""
|
|
28
29
|
|
|
29
|
-
def __init__(self, socket_source: Protocol) -> None:
|
|
30
|
+
def __init__(self, socket_source: Protocol, root_path: str) -> None:
|
|
30
31
|
"""Create a server, and get it ready to run.
|
|
31
32
|
|
|
32
33
|
:param socket: An object that is a source of sockets. This could be a `socketpool`
|
|
33
34
|
in CircuitPython or the `socket` module in CPython.
|
|
35
|
+
:param str root_path: Root directory to serve files from
|
|
34
36
|
"""
|
|
35
37
|
self._buffer = bytearray(1024)
|
|
36
38
|
self._timeout = 1
|
|
37
39
|
self.routes = _HTTPRoutes()
|
|
38
40
|
self._socket_source = socket_source
|
|
39
41
|
self._sock = None
|
|
40
|
-
self.root_path =
|
|
42
|
+
self.root_path = root_path
|
|
41
43
|
|
|
42
44
|
def route(self, path: str, method: HTTPMethod = HTTPMethod.GET) -> Callable:
|
|
43
45
|
"""
|
|
@@ -63,14 +65,13 @@ class HTTPServer:
|
|
|
63
65
|
|
|
64
66
|
return route_decorator
|
|
65
67
|
|
|
66
|
-
def serve_forever(self, host: str, port: int = 80
|
|
68
|
+
def serve_forever(self, host: str, port: int = 80) -> None:
|
|
67
69
|
"""Wait for HTTP requests at the given host and port. Does not return.
|
|
68
70
|
|
|
69
71
|
:param str host: host name or IP address
|
|
70
72
|
:param int port: port
|
|
71
|
-
:param str root_path: root directory to serve files from
|
|
72
73
|
"""
|
|
73
|
-
self.start(host, port
|
|
74
|
+
self.start(host, port)
|
|
74
75
|
|
|
75
76
|
while True:
|
|
76
77
|
try:
|
|
@@ -78,17 +79,14 @@ class HTTPServer:
|
|
|
78
79
|
except OSError:
|
|
79
80
|
continue
|
|
80
81
|
|
|
81
|
-
def start(self, host: str, port: int = 80
|
|
82
|
+
def start(self, host: str, port: int = 80) -> None:
|
|
82
83
|
"""
|
|
83
84
|
Start the HTTP server at the given host and port. Requires calling
|
|
84
85
|
poll() in a while loop to handle incoming requests.
|
|
85
86
|
|
|
86
87
|
:param str host: host name or IP address
|
|
87
88
|
:param int port: port
|
|
88
|
-
:param str root_path: root directory to serve files from
|
|
89
89
|
"""
|
|
90
|
-
self.root_path = root_path
|
|
91
|
-
|
|
92
90
|
self._sock = self._socket_source.socket(
|
|
93
91
|
self._socket_source.AF_INET, self._socket_source.SOCK_STREAM
|
|
94
92
|
)
|
|
@@ -158,34 +156,50 @@ class HTTPServer:
|
|
|
158
156
|
conn, received_body_bytes, content_length
|
|
159
157
|
)
|
|
160
158
|
|
|
159
|
+
# Find a handler for the route
|
|
161
160
|
handler = self.routes.find_handler(
|
|
162
161
|
_HTTPRoute(request.path, request.method)
|
|
163
162
|
)
|
|
164
163
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
handler(
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
164
|
+
try:
|
|
165
|
+
# If a handler for route exists and is callable, call it.
|
|
166
|
+
if handler is not None and callable(handler):
|
|
167
|
+
handler(request)
|
|
168
|
+
|
|
169
|
+
# If no handler exists and request method is GET or HEAD, try to serve a file.
|
|
170
|
+
elif handler is None and request.method in (
|
|
171
|
+
HTTPMethod.GET,
|
|
172
|
+
HTTPMethod.HEAD,
|
|
173
|
+
):
|
|
174
|
+
filename = "index.html" if request.path == "/" else request.path
|
|
175
|
+
HTTPResponse(request).send_file(
|
|
176
|
+
filename=filename,
|
|
177
|
+
root_path=self.root_path,
|
|
178
|
+
buffer_size=self.request_buffer_size,
|
|
179
|
+
head_only=(request.method == HTTPMethod.HEAD),
|
|
180
|
+
)
|
|
181
|
+
else:
|
|
182
|
+
HTTPResponse(
|
|
183
|
+
request, status=CommonHTTPStatus.BAD_REQUEST_400
|
|
184
|
+
).send()
|
|
185
|
+
|
|
186
|
+
except InvalidPathError as error:
|
|
187
|
+
HTTPResponse(request, status=CommonHTTPStatus.FORBIDDEN_403).send(
|
|
188
|
+
str(error)
|
|
176
189
|
)
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
190
|
+
|
|
191
|
+
except FileNotExistsError as error:
|
|
192
|
+
HTTPResponse(request, status=CommonHTTPStatus.NOT_FOUND_404).send(
|
|
193
|
+
str(error)
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
except OSError as error:
|
|
197
|
+
# Handle EAGAIN and ECONNRESET
|
|
198
|
+
if error.errno == EAGAIN:
|
|
199
|
+
# There is no data available right now, try again later.
|
|
186
200
|
return
|
|
187
|
-
if
|
|
188
|
-
#
|
|
201
|
+
if error.errno == ECONNRESET:
|
|
202
|
+
# Connection reset by peer, try again later.
|
|
189
203
|
return
|
|
190
204
|
raise
|
|
191
205
|
|
|
@@ -200,7 +214,7 @@ class HTTPServer:
|
|
|
200
214
|
|
|
201
215
|
Example::
|
|
202
216
|
|
|
203
|
-
server = HTTPServer(pool)
|
|
217
|
+
server = HTTPServer(pool, "/static")
|
|
204
218
|
server.request_buffer_size = 2048
|
|
205
219
|
|
|
206
220
|
server.serve_forever(str(wifi.radio.ipv4_address))
|
|
@@ -222,7 +236,7 @@ class HTTPServer:
|
|
|
222
236
|
|
|
223
237
|
Example::
|
|
224
238
|
|
|
225
|
-
server = HTTPServer(pool)
|
|
239
|
+
server = HTTPServer(pool, "/static")
|
|
226
240
|
server.socket_timeout = 3
|
|
227
241
|
|
|
228
242
|
server.serve_forever(str(wifi.radio.ipv4_address))
|
|
@@ -39,6 +39,9 @@ class CommonHTTPStatus(HTTPStatus): # pylint: disable=too-few-public-methods
|
|
|
39
39
|
BAD_REQUEST_400 = HTTPStatus(400, "Bad Request")
|
|
40
40
|
"""400 Bad Request"""
|
|
41
41
|
|
|
42
|
+
FORBIDDEN_403 = HTTPStatus(403, "Forbidden")
|
|
43
|
+
"""403 Forbidden"""
|
|
44
|
+
|
|
42
45
|
NOT_FOUND_404 = HTTPStatus(404, "Not Found")
|
|
43
46
|
"""404 Not Found"""
|
|
44
47
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#
|
|
3
3
|
# SPDX-License-Identifier: Unlicense
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import os
|
|
6
6
|
|
|
7
7
|
import socketpool
|
|
8
8
|
import wifi
|
|
@@ -12,14 +12,15 @@ from adafruit_httpserver.response import HTTPResponse
|
|
|
12
12
|
from adafruit_httpserver.server import HTTPServer
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
ssid
|
|
15
|
+
ssid = os.getenv("WIFI_SSID")
|
|
16
|
+
password = os.getenv("WIFI_PASSWORD")
|
|
16
17
|
|
|
17
18
|
print("Connecting to", ssid)
|
|
18
19
|
wifi.radio.connect(ssid, password)
|
|
19
20
|
print("Connected to", ssid)
|
|
20
21
|
|
|
21
22
|
pool = socketpool.SocketPool(wifi.radio)
|
|
22
|
-
server = HTTPServer(pool)
|
|
23
|
+
server = HTTPServer(pool, "/static")
|
|
23
24
|
|
|
24
25
|
|
|
25
26
|
@server.route("/chunked")
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#
|
|
3
3
|
# SPDX-License-Identifier: Unlicense
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import os
|
|
6
6
|
|
|
7
7
|
import json
|
|
8
8
|
import microcontroller
|
|
@@ -15,14 +15,15 @@ from adafruit_httpserver.response import HTTPResponse
|
|
|
15
15
|
from adafruit_httpserver.server import HTTPServer
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
ssid
|
|
18
|
+
ssid = os.getenv("WIFI_SSID")
|
|
19
|
+
password = os.getenv("WIFI_PASSWORD")
|
|
19
20
|
|
|
20
21
|
print("Connecting to", ssid)
|
|
21
22
|
wifi.radio.connect(ssid, password)
|
|
22
23
|
print("Connected to", ssid)
|
|
23
24
|
|
|
24
25
|
pool = socketpool.SocketPool(wifi.radio)
|
|
25
|
-
server = HTTPServer(pool)
|
|
26
|
+
server = HTTPServer(pool, "/static")
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
@server.route("/cpu-information")
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#
|
|
3
3
|
# SPDX-License-Identifier: Unlicense
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import os
|
|
6
6
|
|
|
7
7
|
import mdns
|
|
8
8
|
import socketpool
|
|
@@ -14,7 +14,8 @@ from adafruit_httpserver.response import HTTPResponse
|
|
|
14
14
|
from adafruit_httpserver.server import HTTPServer
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
ssid
|
|
17
|
+
ssid = os.getenv("WIFI_SSID")
|
|
18
|
+
password = os.getenv("WIFI_PASSWORD")
|
|
18
19
|
|
|
19
20
|
print("Connecting to", ssid)
|
|
20
21
|
wifi.radio.connect(ssid, password)
|
|
@@ -25,7 +26,7 @@ mdns_server.hostname = "custom-mdns-hostname"
|
|
|
25
26
|
mdns_server.advertise_service(service_type="_http", protocol="_tcp", port=80)
|
|
26
27
|
|
|
27
28
|
pool = socketpool.SocketPool(wifi.radio)
|
|
28
|
-
server = HTTPServer(pool)
|
|
29
|
+
server = HTTPServer(pool, "/static")
|
|
29
30
|
|
|
30
31
|
|
|
31
32
|
@server.route("/")
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#
|
|
3
3
|
# SPDX-License-Identifier: Unlicense
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import os
|
|
6
6
|
|
|
7
7
|
import board
|
|
8
8
|
import neopixel
|
|
@@ -15,14 +15,15 @@ from adafruit_httpserver.response import HTTPResponse
|
|
|
15
15
|
from adafruit_httpserver.server import HTTPServer
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
ssid
|
|
18
|
+
ssid = os.getenv("WIFI_SSID")
|
|
19
|
+
password = os.getenv("WIFI_PASSWORD")
|
|
19
20
|
|
|
20
21
|
print("Connecting to", ssid)
|
|
21
22
|
wifi.radio.connect(ssid, password)
|
|
22
23
|
print("Connected to", ssid)
|
|
23
24
|
|
|
24
25
|
pool = socketpool.SocketPool(wifi.radio)
|
|
25
|
-
server = HTTPServer(pool)
|
|
26
|
+
server = HTTPServer(pool, "/static")
|
|
26
27
|
|
|
27
28
|
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
|
|
28
29
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#
|
|
3
3
|
# SPDX-License-Identifier: Unlicense
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import os
|
|
6
6
|
|
|
7
7
|
import socketpool
|
|
8
8
|
import wifi
|
|
@@ -13,14 +13,15 @@ from adafruit_httpserver.response import HTTPResponse
|
|
|
13
13
|
from adafruit_httpserver.server import HTTPServer
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
ssid
|
|
16
|
+
ssid = os.getenv("WIFI_SSID")
|
|
17
|
+
password = os.getenv("WIFI_PASSWORD")
|
|
17
18
|
|
|
18
19
|
print("Connecting to", ssid)
|
|
19
20
|
wifi.radio.connect(ssid, password)
|
|
20
21
|
print("Connected to", ssid)
|
|
21
22
|
|
|
22
23
|
pool = socketpool.SocketPool(wifi.radio)
|
|
23
|
-
server = HTTPServer(pool)
|
|
24
|
+
server = HTTPServer(pool, "/static")
|
|
24
25
|
|
|
25
26
|
|
|
26
27
|
@server.route("/")
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#
|
|
3
3
|
# SPDX-License-Identifier: Unlicense
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import os
|
|
6
6
|
|
|
7
7
|
import socketpool
|
|
8
8
|
import wifi
|
|
@@ -13,14 +13,15 @@ from adafruit_httpserver.response import HTTPResponse
|
|
|
13
13
|
from adafruit_httpserver.server import HTTPServer
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
ssid
|
|
16
|
+
ssid = os.getenv("WIFI_SSID")
|
|
17
|
+
password = os.getenv("WIFI_PASSWORD")
|
|
17
18
|
|
|
18
19
|
print("Connecting to", ssid)
|
|
19
20
|
wifi.radio.connect(ssid, password)
|
|
20
21
|
print("Connected to", ssid)
|
|
21
22
|
|
|
22
23
|
pool = socketpool.SocketPool(wifi.radio)
|
|
23
|
-
server = HTTPServer(pool)
|
|
24
|
+
server = HTTPServer(pool, "/static")
|
|
24
25
|
|
|
25
26
|
|
|
26
27
|
@server.route("/")
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#
|
|
3
3
|
# SPDX-License-Identifier: Unlicense
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import os
|
|
6
6
|
|
|
7
7
|
import socketpool
|
|
8
8
|
import wifi
|
|
@@ -13,14 +13,15 @@ from adafruit_httpserver.response import HTTPResponse
|
|
|
13
13
|
from adafruit_httpserver.server import HTTPServer
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
ssid
|
|
16
|
+
ssid = os.getenv("WIFI_SSID")
|
|
17
|
+
password = os.getenv("WIFI_PASSWORD")
|
|
17
18
|
|
|
18
19
|
print("Connecting to", ssid)
|
|
19
20
|
wifi.radio.connect(ssid, password)
|
|
20
21
|
print("Connected to", ssid)
|
|
21
22
|
|
|
22
23
|
pool = socketpool.SocketPool(wifi.radio)
|
|
23
|
-
server = HTTPServer(pool)
|
|
24
|
+
server = HTTPServer(pool, "/static")
|
|
24
25
|
|
|
25
26
|
|
|
26
27
|
class Device:
|
{adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/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 = "
|
|
15
|
+
version = "3.0.0"
|
|
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-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.gitignore
RENAMED
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/.pylintrc
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/LICENSES/MIT.txt
RENAMED
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/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
|
{adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/conf.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/index.rst
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/requirements.txt
RENAMED
|
File without changes
|
{adafruit-circuitpython-httpserver-2.4.0 → adafruit-circuitpython-httpserver-3.0.0}/setup.cfg
RENAMED
|
File without changes
|