adafruit-circuitpython-httpserver 4.5.5__py3-none-any.whl → 4.6.1__py3-none-any.whl
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.5.5.dist-info → adafruit_circuitpython_httpserver-4.6.1.dist-info}/METADATA +3 -2
- adafruit_circuitpython_httpserver-4.6.1.dist-info/RECORD +17 -0
- {adafruit_circuitpython_httpserver-4.5.5.dist-info → adafruit_circuitpython_httpserver-4.6.1.dist-info}/WHEEL +1 -1
- adafruit_httpserver/__init__.py +1 -1
- adafruit_httpserver/interfaces.py +61 -2
- adafruit_httpserver/request.py +14 -10
- adafruit_httpserver/response.py +7 -4
- adafruit_httpserver/route.py +4 -4
- adafruit_httpserver/server.py +121 -35
- adafruit_httpserver/status.py +7 -4
- adafruit_circuitpython_httpserver-4.5.5.dist-info/RECORD +0 -17
- {adafruit_circuitpython_httpserver-4.5.5.dist-info → adafruit_circuitpython_httpserver-4.6.1.dist-info}/LICENSE +0 -0
- {adafruit_circuitpython_httpserver-4.5.5.dist-info → adafruit_circuitpython_httpserver-4.6.1.dist-info}/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
2
|
Name: adafruit-circuitpython-httpserver
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.6.1
|
|
4
4
|
Summary: Simple HTTP Server for CircuitPython
|
|
5
5
|
Author-email: Adafruit Industries <circuitpython@adafruit.com>
|
|
6
6
|
License: MIT
|
|
@@ -51,6 +51,7 @@ HTTP Server for CircuitPython.
|
|
|
51
51
|
- Supports URL parameters and wildcard URLs.
|
|
52
52
|
- Supports HTTP Basic and Bearer Authentication on both server and route per level.
|
|
53
53
|
- Supports Websockets and Server-Sent Events.
|
|
54
|
+
- Limited support for HTTPS (only on selected microcontrollers with enough memory e.g. ESP32-S3).
|
|
54
55
|
|
|
55
56
|
|
|
56
57
|
Dependencies
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
adafruit_httpserver/__init__.py,sha256=lW4IkqzPvbQkckJRDrMXZeLSYE0ENqy7R9FsSQYKbsA,1994
|
|
2
|
+
adafruit_httpserver/authentication.py,sha256=cNWrsPUOAAepNFJzoP3r5ODLQx-X74LkMC_1CieMq2g,2080
|
|
3
|
+
adafruit_httpserver/exceptions.py,sha256=tmOoUecGaSXY9kUIIV3Is5YoChrEIEJqVHl4QgmEvrg,1597
|
|
4
|
+
adafruit_httpserver/headers.py,sha256=KT8hzJwf97lu3cT05ptm21soPVbywmOY23qdUC3NvVs,4576
|
|
5
|
+
adafruit_httpserver/interfaces.py,sha256=7ByLLu1EOhF1EiaC7iqa53qPAxqiNPMZteSDC-hhtxQ,4791
|
|
6
|
+
adafruit_httpserver/methods.py,sha256=6SnSWDFf2oDkxsfCUt07o00FgDQyiDACrNCK_XP82eI,364
|
|
7
|
+
adafruit_httpserver/mime_types.py,sha256=zI2geYB7DHDK0rgpfHEJwybZXszX4lXgfCPZEyFD-mA,7149
|
|
8
|
+
adafruit_httpserver/request.py,sha256=67tp_QjUpV7C_KrB9qE0OxbVPAcraU3kmWK7fmjsPUs,15099
|
|
9
|
+
adafruit_httpserver/response.py,sha256=0-UZxj7C5k0CF2mSe3CjZrIM1bYZ4C1tWrOjH1x6XCc,28371
|
|
10
|
+
adafruit_httpserver/route.py,sha256=VPWcfyJ196Jd8htWM8eTZRm66Su4qVUlL8iR31Xttas,6149
|
|
11
|
+
adafruit_httpserver/server.py,sha256=KPjRyzky712-LGvASFH23A20WRQM0T-p8aYqX5juLk4,22633
|
|
12
|
+
adafruit_httpserver/status.py,sha256=U3t5y3wXCYH3JUoWjta3Wd_ag05scK1njY0k-TLMBNM,1867
|
|
13
|
+
adafruit_circuitpython_httpserver-4.6.1.dist-info/LICENSE,sha256=gyhZwtwoqSsDGoSrV4MrLpKKWyG_jE3eROTZp1EjIUM,1102
|
|
14
|
+
adafruit_circuitpython_httpserver-4.6.1.dist-info/METADATA,sha256=JIPoirnlqN2_I9X1uaVkcHZl-v8H_8SY6mSKIpXypM4,4800
|
|
15
|
+
adafruit_circuitpython_httpserver-4.6.1.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
|
16
|
+
adafruit_circuitpython_httpserver-4.6.1.dist-info/top_level.txt,sha256=N8L9ONfZQ9bILxcOPVWXOjUDA-BNLPKFir7KqAbMQbA,20
|
|
17
|
+
adafruit_circuitpython_httpserver-4.6.1.dist-info/RECORD,,
|
adafruit_httpserver/__init__.py
CHANGED
|
@@ -8,11 +8,70 @@
|
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
10
|
try:
|
|
11
|
-
from typing import List, Dict, Union, Any
|
|
11
|
+
from typing import List, Tuple, Dict, Union, Any
|
|
12
12
|
except ImportError:
|
|
13
13
|
pass
|
|
14
14
|
|
|
15
15
|
|
|
16
|
+
class _ISocket: # pylint: disable=missing-function-docstring,no-self-use,unused-argument
|
|
17
|
+
"""A class for typing necessary methods for a socket object."""
|
|
18
|
+
|
|
19
|
+
def accept(self) -> Tuple["_ISocket", Tuple[str, int]]:
|
|
20
|
+
...
|
|
21
|
+
|
|
22
|
+
def bind(self, address: Tuple[str, int]) -> None:
|
|
23
|
+
...
|
|
24
|
+
|
|
25
|
+
def setblocking(self, flag: bool) -> None:
|
|
26
|
+
...
|
|
27
|
+
|
|
28
|
+
def settimeout(self, value: "Union[float, None]") -> None:
|
|
29
|
+
...
|
|
30
|
+
|
|
31
|
+
def setsockopt(self, level: int, optname: int, value: int) -> None:
|
|
32
|
+
...
|
|
33
|
+
|
|
34
|
+
def listen(self, backlog: int) -> None:
|
|
35
|
+
...
|
|
36
|
+
|
|
37
|
+
def send(self, data: bytes) -> int:
|
|
38
|
+
...
|
|
39
|
+
|
|
40
|
+
def recv_into(self, buffer: memoryview, nbytes: int) -> int:
|
|
41
|
+
...
|
|
42
|
+
|
|
43
|
+
def close(self) -> None:
|
|
44
|
+
...
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class _ISocketPool: # pylint: disable=missing-function-docstring,no-self-use,unused-argument
|
|
48
|
+
"""A class to typing necessary methods and properties for a socket pool object."""
|
|
49
|
+
|
|
50
|
+
AF_INET: int
|
|
51
|
+
SO_REUSEADDR: int
|
|
52
|
+
SOCK_STREAM: int
|
|
53
|
+
SOL_SOCKET: int
|
|
54
|
+
|
|
55
|
+
def socket( # pylint: disable=redefined-builtin
|
|
56
|
+
self,
|
|
57
|
+
family: int = ...,
|
|
58
|
+
type: int = ...,
|
|
59
|
+
proto: int = ...,
|
|
60
|
+
) -> _ISocket:
|
|
61
|
+
...
|
|
62
|
+
|
|
63
|
+
def getaddrinfo( # pylint: disable=redefined-builtin,too-many-arguments
|
|
64
|
+
self,
|
|
65
|
+
host: str,
|
|
66
|
+
port: int,
|
|
67
|
+
family: int = ...,
|
|
68
|
+
type: int = ...,
|
|
69
|
+
proto: int = ...,
|
|
70
|
+
flags: int = ...,
|
|
71
|
+
) -> Tuple[int, int, int, str, Tuple[str, int]]:
|
|
72
|
+
...
|
|
73
|
+
|
|
74
|
+
|
|
16
75
|
class _IFieldStorage:
|
|
17
76
|
"""Interface with shared methods for QueryParams, FormData and Headers."""
|
|
18
77
|
|
|
@@ -62,7 +121,7 @@ class _IFieldStorage:
|
|
|
62
121
|
return key in self._storage
|
|
63
122
|
|
|
64
123
|
def __repr__(self) -> str:
|
|
65
|
-
return f"{self.__class__.__name__}
|
|
124
|
+
return f"<{self.__class__.__name__} {repr(self._storage)}>"
|
|
66
125
|
|
|
67
126
|
|
|
68
127
|
def _encode_html_entities(value: Union[str, None]) -> Union[str, None]:
|
adafruit_httpserver/request.py
CHANGED
|
@@ -9,8 +9,6 @@
|
|
|
9
9
|
|
|
10
10
|
try:
|
|
11
11
|
from typing import List, Dict, Tuple, Union, Any, TYPE_CHECKING
|
|
12
|
-
from socket import socket
|
|
13
|
-
from socketpool import SocketPool
|
|
14
12
|
|
|
15
13
|
if TYPE_CHECKING:
|
|
16
14
|
from .server import Server
|
|
@@ -20,7 +18,7 @@ except ImportError:
|
|
|
20
18
|
import json
|
|
21
19
|
|
|
22
20
|
from .headers import Headers
|
|
23
|
-
from .interfaces import _IFieldStorage, _IXSSSafeFieldStorage
|
|
21
|
+
from .interfaces import _ISocket, _IFieldStorage, _IXSSSafeFieldStorage
|
|
24
22
|
from .methods import POST, PUT, PATCH, DELETE
|
|
25
23
|
|
|
26
24
|
|
|
@@ -127,11 +125,11 @@ class File:
|
|
|
127
125
|
|
|
128
126
|
def __repr__(self) -> str:
|
|
129
127
|
filename, content_type, size = (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
128
|
+
self.filename,
|
|
129
|
+
self.content_type,
|
|
130
|
+
self.size,
|
|
133
131
|
)
|
|
134
|
-
return f"{self.__class__.__name__}
|
|
132
|
+
return f"<{self.__class__.__name__} {filename=}, {content_type=}, {size=}>"
|
|
135
133
|
|
|
136
134
|
|
|
137
135
|
class Files(_IFieldStorage):
|
|
@@ -260,7 +258,9 @@ class FormData(_IXSSSafeFieldStorage):
|
|
|
260
258
|
|
|
261
259
|
def __repr__(self) -> str:
|
|
262
260
|
class_name = self.__class__.__name__
|
|
263
|
-
return
|
|
261
|
+
return (
|
|
262
|
+
f"<{class_name} {repr(self._storage)}, files={repr(self.files._storage)}>"
|
|
263
|
+
)
|
|
264
264
|
|
|
265
265
|
|
|
266
266
|
class Request: # pylint: disable=too-many-instance-attributes
|
|
@@ -274,7 +274,7 @@ class Request: # pylint: disable=too-many-instance-attributes
|
|
|
274
274
|
Server object that received the request.
|
|
275
275
|
"""
|
|
276
276
|
|
|
277
|
-
connection:
|
|
277
|
+
connection: _ISocket
|
|
278
278
|
"""
|
|
279
279
|
Socket object used to send and receive data on the connection.
|
|
280
280
|
"""
|
|
@@ -325,7 +325,7 @@ class Request: # pylint: disable=too-many-instance-attributes
|
|
|
325
325
|
def __init__(
|
|
326
326
|
self,
|
|
327
327
|
server: "Server",
|
|
328
|
-
connection:
|
|
328
|
+
connection: _ISocket,
|
|
329
329
|
client_address: Tuple[str, int],
|
|
330
330
|
raw_request: bytes = None,
|
|
331
331
|
) -> None:
|
|
@@ -481,6 +481,10 @@ class Request: # pylint: disable=too-many-instance-attributes
|
|
|
481
481
|
|
|
482
482
|
return method, path, query_params, http_version, headers
|
|
483
483
|
|
|
484
|
+
def __repr__(self) -> str:
|
|
485
|
+
path = self.path + (f"?{self.query_params}" if self.query_params else "")
|
|
486
|
+
return f'<{self.__class__.__name__} "{self.method} {path}">'
|
|
487
|
+
|
|
484
488
|
|
|
485
489
|
def _debug_unsupported_form_content_type(content_type: str) -> None:
|
|
486
490
|
"""Warns when an unsupported form content type is used."""
|
adafruit_httpserver/response.py
CHANGED
|
@@ -9,8 +9,6 @@
|
|
|
9
9
|
|
|
10
10
|
try:
|
|
11
11
|
from typing import Optional, Dict, Union, Tuple, Generator, Any
|
|
12
|
-
from socket import socket
|
|
13
|
-
from socketpool import SocketPool
|
|
14
12
|
except ImportError:
|
|
15
13
|
pass
|
|
16
14
|
|
|
@@ -47,6 +45,7 @@ from .status import (
|
|
|
47
45
|
PERMANENT_REDIRECT_308,
|
|
48
46
|
)
|
|
49
47
|
from .headers import Headers
|
|
48
|
+
from .interfaces import _ISocket
|
|
50
49
|
|
|
51
50
|
|
|
52
51
|
class Response: # pylint: disable=too-few-public-methods
|
|
@@ -132,7 +131,7 @@ class Response: # pylint: disable=too-few-public-methods
|
|
|
132
131
|
|
|
133
132
|
def _send_bytes(
|
|
134
133
|
self,
|
|
135
|
-
conn:
|
|
134
|
+
conn: _ISocket,
|
|
136
135
|
buffer: Union[bytes, bytearray, memoryview],
|
|
137
136
|
):
|
|
138
137
|
bytes_sent: int = 0
|
|
@@ -217,6 +216,10 @@ class FileResponse(Response): # pylint: disable=too-few-public-methods
|
|
|
217
216
|
)
|
|
218
217
|
self._filename = filename + "index.html" if filename.endswith("/") else filename
|
|
219
218
|
self._root_path = root_path or self._request.server.root_path
|
|
219
|
+
|
|
220
|
+
if self._root_path is None:
|
|
221
|
+
raise ValueError("root_path must be provided in Server or in FileResponse")
|
|
222
|
+
|
|
220
223
|
self._full_file_path = self._combine_path(self._root_path, self._filename)
|
|
221
224
|
self._content_type = content_type or MIMETypes.get_for_filename(self._filename)
|
|
222
225
|
self._file_length = self._get_file_length(self._full_file_path)
|
|
@@ -708,7 +711,7 @@ class Websocket(Response): # pylint: disable=too-few-public-methods
|
|
|
708
711
|
length -= min(payload_length, length)
|
|
709
712
|
|
|
710
713
|
if has_mask:
|
|
711
|
-
payload = bytes(
|
|
714
|
+
payload = bytes(byte ^ mask[idx % 4] for idx, byte in enumerate(payload))
|
|
712
715
|
|
|
713
716
|
return opcode, payload
|
|
714
717
|
|
adafruit_httpserver/route.py
CHANGED
|
@@ -136,11 +136,11 @@ class Route:
|
|
|
136
136
|
return True, dict(zip(self.parameters_names, url_parameters_values))
|
|
137
137
|
|
|
138
138
|
def __repr__(self) -> str:
|
|
139
|
-
path =
|
|
140
|
-
methods =
|
|
141
|
-
handler =
|
|
139
|
+
path = self.path
|
|
140
|
+
methods = self.methods
|
|
141
|
+
handler = self.handler
|
|
142
142
|
|
|
143
|
-
return f"Route
|
|
143
|
+
return f"<Route {path=}, {methods=}, {handler=}>"
|
|
144
144
|
|
|
145
145
|
|
|
146
146
|
def as_route(
|
adafruit_httpserver/server.py
CHANGED
|
@@ -8,12 +8,11 @@
|
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
10
|
try:
|
|
11
|
-
from typing import Callable,
|
|
12
|
-
from socket import socket
|
|
13
|
-
from socketpool import SocketPool
|
|
11
|
+
from typing import Callable, Union, List, Tuple, Dict, Iterable
|
|
14
12
|
except ImportError:
|
|
15
13
|
pass
|
|
16
14
|
|
|
15
|
+
from ssl import SSLContext, create_default_context
|
|
17
16
|
from errno import EAGAIN, ECONNRESET, ETIMEDOUT
|
|
18
17
|
from sys import implementation
|
|
19
18
|
from time import monotonic, sleep
|
|
@@ -28,18 +27,25 @@ from .exceptions import (
|
|
|
28
27
|
ServingFilesDisabledError,
|
|
29
28
|
)
|
|
30
29
|
from .headers import Headers
|
|
30
|
+
from .interfaces import _ISocketPool, _ISocket
|
|
31
31
|
from .methods import GET, HEAD
|
|
32
32
|
from .request import Request
|
|
33
33
|
from .response import Response, FileResponse
|
|
34
34
|
from .route import Route
|
|
35
35
|
from .status import BAD_REQUEST_400, UNAUTHORIZED_401, FORBIDDEN_403, NOT_FOUND_404
|
|
36
36
|
|
|
37
|
+
if implementation.name != "circuitpython":
|
|
38
|
+
from ssl import Purpose, CERT_NONE, SSLError # pylint: disable=ungrouped-imports
|
|
39
|
+
|
|
37
40
|
|
|
38
41
|
NO_REQUEST = "no_request"
|
|
39
42
|
CONNECTION_TIMED_OUT = "connection_timed_out"
|
|
40
43
|
REQUEST_HANDLED_NO_RESPONSE = "request_handled_no_response"
|
|
41
44
|
REQUEST_HANDLED_RESPONSE_SENT = "request_handled_response_sent"
|
|
42
45
|
|
|
46
|
+
# CircuitPython does not have these error codes
|
|
47
|
+
MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE = -30592
|
|
48
|
+
|
|
43
49
|
|
|
44
50
|
class Server: # pylint: disable=too-many-instance-attributes
|
|
45
51
|
"""A basic socket-based HTTP server."""
|
|
@@ -53,8 +59,50 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
53
59
|
root_path: str
|
|
54
60
|
"""Root directory to serve files from. ``None`` if serving files is disabled."""
|
|
55
61
|
|
|
62
|
+
@staticmethod
|
|
63
|
+
def _validate_https_cert_provided(
|
|
64
|
+
certfile: Union[str, None], keyfile: Union[str, None]
|
|
65
|
+
) -> None:
|
|
66
|
+
if certfile is None or keyfile is None:
|
|
67
|
+
raise ValueError("Both certfile and keyfile must be specified for HTTPS")
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def _create_circuitpython_ssl_context(certfile: str, keyfile: str) -> SSLContext:
|
|
71
|
+
ssl_context = create_default_context()
|
|
72
|
+
|
|
73
|
+
ssl_context.load_verify_locations(cadata="")
|
|
74
|
+
ssl_context.load_cert_chain(certfile, keyfile)
|
|
75
|
+
|
|
76
|
+
return ssl_context
|
|
77
|
+
|
|
78
|
+
@staticmethod
|
|
79
|
+
def _create_cpython_ssl_context(certfile: str, keyfile: str) -> SSLContext:
|
|
80
|
+
ssl_context = create_default_context(purpose=Purpose.CLIENT_AUTH)
|
|
81
|
+
|
|
82
|
+
ssl_context.load_cert_chain(certfile, keyfile)
|
|
83
|
+
|
|
84
|
+
ssl_context.verify_mode = CERT_NONE
|
|
85
|
+
ssl_context.check_hostname = False
|
|
86
|
+
|
|
87
|
+
return ssl_context
|
|
88
|
+
|
|
89
|
+
@classmethod
|
|
90
|
+
def _create_ssl_context(cls, certfile: str, keyfile: str) -> SSLContext:
|
|
91
|
+
return (
|
|
92
|
+
cls._create_circuitpython_ssl_context(certfile, keyfile)
|
|
93
|
+
if implementation.name == "circuitpython"
|
|
94
|
+
else cls._create_cpython_ssl_context(certfile, keyfile)
|
|
95
|
+
)
|
|
96
|
+
|
|
56
97
|
def __init__(
|
|
57
|
-
self,
|
|
98
|
+
self,
|
|
99
|
+
socket_source: _ISocketPool,
|
|
100
|
+
root_path: str = None,
|
|
101
|
+
*,
|
|
102
|
+
https: bool = False,
|
|
103
|
+
certfile: str = None,
|
|
104
|
+
keyfile: str = None,
|
|
105
|
+
debug: bool = False,
|
|
58
106
|
) -> None:
|
|
59
107
|
"""Create a server, and get it ready to run.
|
|
60
108
|
|
|
@@ -62,16 +110,30 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
62
110
|
in CircuitPython or the `socket` module in CPython.
|
|
63
111
|
:param str root_path: Root directory to serve files from
|
|
64
112
|
:param bool debug: Enables debug messages useful during development
|
|
113
|
+
:param bool https: If True, the server will use HTTPS
|
|
114
|
+
:param str certfile: Path to the certificate file, required if ``https`` is True
|
|
115
|
+
:param str keyfile: Path to the private key file, required if ``https`` is True
|
|
65
116
|
"""
|
|
66
|
-
self._auths = []
|
|
67
117
|
self._buffer = bytearray(1024)
|
|
68
118
|
self._timeout = 1
|
|
119
|
+
|
|
120
|
+
self._auths = []
|
|
69
121
|
self._routes: "List[Route]" = []
|
|
122
|
+
self.headers = Headers()
|
|
123
|
+
|
|
70
124
|
self._socket_source = socket_source
|
|
71
125
|
self._sock = None
|
|
72
|
-
|
|
126
|
+
|
|
73
127
|
self.host, self.port = None, None
|
|
74
128
|
self.root_path = root_path
|
|
129
|
+
self.https = https
|
|
130
|
+
|
|
131
|
+
if https:
|
|
132
|
+
self._validate_https_cert_provided(certfile, keyfile)
|
|
133
|
+
self._ssl_context = self._create_ssl_context(certfile, keyfile)
|
|
134
|
+
else:
|
|
135
|
+
self._ssl_context = None
|
|
136
|
+
|
|
75
137
|
if root_path in ["", "/"] and debug:
|
|
76
138
|
_debug_warning_exposed_files(root_path)
|
|
77
139
|
self.stopped = True
|
|
@@ -172,7 +234,7 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
172
234
|
raise RuntimeError(f"Cannot start server on {host}:{port}") from error
|
|
173
235
|
|
|
174
236
|
def serve_forever(
|
|
175
|
-
self, host: str, port: int =
|
|
237
|
+
self, host: str = "0.0.0.0", port: int = 5000, *, poll_interval: float = 0.1
|
|
176
238
|
) -> None:
|
|
177
239
|
"""
|
|
178
240
|
Wait for HTTP requests at the given host and port. Does not return.
|
|
@@ -195,15 +257,29 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
195
257
|
except Exception: # pylint: disable=broad-except
|
|
196
258
|
pass # Ignore exceptions in handler function
|
|
197
259
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
""
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
260
|
+
@staticmethod
|
|
261
|
+
def _create_server_socket(
|
|
262
|
+
socket_source: _ISocketPool,
|
|
263
|
+
ssl_context: "SSLContext | None",
|
|
264
|
+
host: str,
|
|
265
|
+
port: int,
|
|
266
|
+
) -> _ISocket:
|
|
267
|
+
sock = socket_source.socket(socket_source.AF_INET, socket_source.SOCK_STREAM)
|
|
268
|
+
|
|
269
|
+
# TODO: Temporary backwards compatibility, remove after CircuitPython 9.0.0 release
|
|
270
|
+
if implementation.version >= (9,) or implementation.name != "circuitpython":
|
|
271
|
+
sock.setsockopt(socket_source.SOL_SOCKET, socket_source.SO_REUSEADDR, 1)
|
|
205
272
|
|
|
206
|
-
|
|
273
|
+
if ssl_context is not None:
|
|
274
|
+
sock = ssl_context.wrap_socket(sock, server_side=True)
|
|
275
|
+
|
|
276
|
+
sock.bind((host, port))
|
|
277
|
+
sock.listen(10)
|
|
278
|
+
sock.setblocking(False) # Non-blocking socket
|
|
279
|
+
|
|
280
|
+
return sock
|
|
281
|
+
|
|
282
|
+
def start(self, host: str = "0.0.0.0", port: int = 5000) -> None:
|
|
207
283
|
"""
|
|
208
284
|
Start the HTTP server at the given host and port. Requires calling
|
|
209
285
|
``.poll()`` in a while loop to handle incoming requests.
|
|
@@ -216,17 +292,10 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
216
292
|
self.host, self.port = host, port
|
|
217
293
|
|
|
218
294
|
self.stopped = False
|
|
219
|
-
self._sock = self.
|
|
220
|
-
self._socket_source
|
|
295
|
+
self._sock = self._create_server_socket(
|
|
296
|
+
self._socket_source, self._ssl_context, host, port
|
|
221
297
|
)
|
|
222
298
|
|
|
223
|
-
if implementation.name != "circuitpython":
|
|
224
|
-
self._set_socket_level_to_reuse_address()
|
|
225
|
-
|
|
226
|
-
self._sock.bind((host, port))
|
|
227
|
-
self._sock.listen(10)
|
|
228
|
-
self._sock.setblocking(False) # Non-blocking socket
|
|
229
|
-
|
|
230
299
|
if self.debug:
|
|
231
300
|
_debug_started_server(self)
|
|
232
301
|
|
|
@@ -244,9 +313,7 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
244
313
|
if self.debug:
|
|
245
314
|
_debug_stopped_server(self)
|
|
246
315
|
|
|
247
|
-
def _receive_header_bytes(
|
|
248
|
-
self, sock: Union["SocketPool.Socket", "socket.socket"]
|
|
249
|
-
) -> bytes:
|
|
316
|
+
def _receive_header_bytes(self, sock: _ISocket) -> bytes:
|
|
250
317
|
"""Receive bytes until a empty line is received."""
|
|
251
318
|
received_bytes = bytes()
|
|
252
319
|
while b"\r\n\r\n" not in received_bytes:
|
|
@@ -263,7 +330,7 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
263
330
|
|
|
264
331
|
def _receive_body_bytes(
|
|
265
332
|
self,
|
|
266
|
-
sock:
|
|
333
|
+
sock: _ISocket,
|
|
267
334
|
received_body_bytes: bytes,
|
|
268
335
|
content_length: int,
|
|
269
336
|
) -> bytes:
|
|
@@ -282,7 +349,7 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
282
349
|
|
|
283
350
|
def _receive_request(
|
|
284
351
|
self,
|
|
285
|
-
sock:
|
|
352
|
+
sock: _ISocket,
|
|
286
353
|
client_address: Tuple[str, int],
|
|
287
354
|
) -> Request:
|
|
288
355
|
"""Receive bytes from socket until the whole request is received."""
|
|
@@ -388,7 +455,9 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
388
455
|
name, value
|
|
389
456
|
)
|
|
390
457
|
|
|
391
|
-
def poll(
|
|
458
|
+
def poll( # pylint: disable=too-many-branches,too-many-return-statements
|
|
459
|
+
self,
|
|
460
|
+
) -> str:
|
|
392
461
|
"""
|
|
393
462
|
Call this method inside your main loop to get the server to check for new incoming client
|
|
394
463
|
requests. When a request comes in, it will be handled by the handler function.
|
|
@@ -401,11 +470,12 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
401
470
|
|
|
402
471
|
conn = None
|
|
403
472
|
try:
|
|
473
|
+
if self.debug:
|
|
474
|
+
_debug_start_time = monotonic()
|
|
475
|
+
|
|
404
476
|
conn, client_address = self._sock.accept()
|
|
405
477
|
conn.settimeout(self._timeout)
|
|
406
478
|
|
|
407
|
-
_debug_start_time = monotonic()
|
|
408
|
-
|
|
409
479
|
# Receive the whole request
|
|
410
480
|
if (request := self._receive_request(conn, client_address)) is None:
|
|
411
481
|
conn.close()
|
|
@@ -426,9 +496,8 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
426
496
|
# Send the response
|
|
427
497
|
response._send() # pylint: disable=protected-access
|
|
428
498
|
|
|
429
|
-
_debug_end_time = monotonic()
|
|
430
|
-
|
|
431
499
|
if self.debug:
|
|
500
|
+
_debug_end_time = monotonic()
|
|
432
501
|
_debug_response_sent(response, _debug_end_time - _debug_start_time)
|
|
433
502
|
|
|
434
503
|
return REQUEST_HANDLED_RESPONSE_SENT
|
|
@@ -441,6 +510,15 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
441
510
|
# Connection reset by peer, try again later.
|
|
442
511
|
if error.errno == ECONNRESET:
|
|
443
512
|
return NO_REQUEST
|
|
513
|
+
# Handshake failed, try again later.
|
|
514
|
+
if error.errno == MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
|
|
515
|
+
return NO_REQUEST
|
|
516
|
+
|
|
517
|
+
# CPython specific SSL related errors
|
|
518
|
+
if implementation.name != "circuitpython" and isinstance(error, SSLError):
|
|
519
|
+
# Ignore unknown SSL certificate errors
|
|
520
|
+
if getattr(error, "reason", None) == "SSLV3_ALERT_CERTIFICATE_UNKNOWN":
|
|
521
|
+
return NO_REQUEST
|
|
444
522
|
|
|
445
523
|
if self.debug:
|
|
446
524
|
_debug_exception_in_handler(error)
|
|
@@ -530,6 +608,13 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
530
608
|
else:
|
|
531
609
|
raise ValueError("Server.socket_timeout must be a positive numeric value.")
|
|
532
610
|
|
|
611
|
+
def __repr__(self) -> str:
|
|
612
|
+
host = self.host
|
|
613
|
+
port = self.port
|
|
614
|
+
root_path = self.root_path
|
|
615
|
+
|
|
616
|
+
return f"<Server {host=}, {port=}, {root_path=}>"
|
|
617
|
+
|
|
533
618
|
|
|
534
619
|
def _debug_warning_exposed_files(root_path: str):
|
|
535
620
|
"""Warns about exposing all files on the device."""
|
|
@@ -542,9 +627,10 @@ def _debug_warning_exposed_files(root_path: str):
|
|
|
542
627
|
|
|
543
628
|
def _debug_started_server(server: "Server"):
|
|
544
629
|
"""Prints a message when the server starts."""
|
|
630
|
+
scheme = "https" if server.https else "http"
|
|
545
631
|
host, port = server.host, server.port
|
|
546
632
|
|
|
547
|
-
print(f"Started development server on
|
|
633
|
+
print(f"Started development server on {scheme}://{host}:{port}")
|
|
548
634
|
|
|
549
635
|
|
|
550
636
|
def _debug_response_sent(response: "Response", time_elapsed: float):
|
adafruit_httpserver/status.py
CHANGED
|
@@ -21,14 +21,17 @@ class Status: # pylint: disable=too-few-public-methods
|
|
|
21
21
|
self.code = code
|
|
22
22
|
self.text = text
|
|
23
23
|
|
|
24
|
-
def
|
|
25
|
-
return
|
|
24
|
+
def __eq__(self, other: "Status"):
|
|
25
|
+
return self.code == other.code and self.text == other.text
|
|
26
26
|
|
|
27
27
|
def __str__(self):
|
|
28
28
|
return f"{self.code} {self.text}"
|
|
29
29
|
|
|
30
|
-
def
|
|
31
|
-
|
|
30
|
+
def __repr__(self):
|
|
31
|
+
code = self.code
|
|
32
|
+
text = self.text
|
|
33
|
+
|
|
34
|
+
return f'<Status {code}, "{text}">'
|
|
32
35
|
|
|
33
36
|
|
|
34
37
|
SWITCHING_PROTOCOLS_101 = Status(101, "Switching Protocols")
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
adafruit_httpserver/__init__.py,sha256=FUGpkGPWij1M9KtDKTYx2PYBQvGnUNcJLG_kjbHxzm8,1994
|
|
2
|
-
adafruit_httpserver/authentication.py,sha256=cNWrsPUOAAepNFJzoP3r5ODLQx-X74LkMC_1CieMq2g,2080
|
|
3
|
-
adafruit_httpserver/exceptions.py,sha256=tmOoUecGaSXY9kUIIV3Is5YoChrEIEJqVHl4QgmEvrg,1597
|
|
4
|
-
adafruit_httpserver/headers.py,sha256=KT8hzJwf97lu3cT05ptm21soPVbywmOY23qdUC3NvVs,4576
|
|
5
|
-
adafruit_httpserver/interfaces.py,sha256=4r0AoVXf6GqcEejkw7m9Yd9tmZAOr1TXAa5u2H5O714,3292
|
|
6
|
-
adafruit_httpserver/methods.py,sha256=6SnSWDFf2oDkxsfCUt07o00FgDQyiDACrNCK_XP82eI,364
|
|
7
|
-
adafruit_httpserver/mime_types.py,sha256=zI2geYB7DHDK0rgpfHEJwybZXszX4lXgfCPZEyFD-mA,7149
|
|
8
|
-
adafruit_httpserver/request.py,sha256=dtRMkllCO8VRvI6v6eL8LEA-oGu_SB7kBVCRZCjdMeM,15036
|
|
9
|
-
adafruit_httpserver/response.py,sha256=jiEqyDAAixOXMA-U89lUuaFd4FiTm0r0u7vAlEvODXI,28305
|
|
10
|
-
adafruit_httpserver/route.py,sha256=09asPOs8GxyutYPELWwppY-X49AzhEOfR-L8VVrBKVg,6166
|
|
11
|
-
adafruit_httpserver/server.py,sha256=R-uDKyQgH9FYfKrc3r6t4ew7v5t0N9vIUlSP1FmAidM,19605
|
|
12
|
-
adafruit_httpserver/status.py,sha256=x6QNFagXgold9ebWWxghwvIsyQORKseFA9k0ySaXptc,1825
|
|
13
|
-
adafruit_circuitpython_httpserver-4.5.5.dist-info/LICENSE,sha256=gyhZwtwoqSsDGoSrV4MrLpKKWyG_jE3eROTZp1EjIUM,1102
|
|
14
|
-
adafruit_circuitpython_httpserver-4.5.5.dist-info/METADATA,sha256=AfgyexIMpgq21iJ3F-9j6gH1k2Cfk1_0QQnEBmriRSM,4702
|
|
15
|
-
adafruit_circuitpython_httpserver-4.5.5.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
16
|
-
adafruit_circuitpython_httpserver-4.5.5.dist-info/top_level.txt,sha256=N8L9ONfZQ9bILxcOPVWXOjUDA-BNLPKFir7KqAbMQbA,20
|
|
17
|
-
adafruit_circuitpython_httpserver-4.5.5.dist-info/RECORD,,
|
|
File without changes
|