adafruit-circuitpython-httpserver 4.7.1__tar.gz → 4.8.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-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/PKG-INFO +1 -1
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_circuitpython_httpserver.egg-info/PKG-INFO +1 -1
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/__init__.py +1 -1
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/exceptions.py +13 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/response.py +241 -63
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/server.py +8 -8
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/pyproject.toml +1 -1
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.gitattributes +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.github/PULL_REQUEST_TEMPLATE/adafruit_circuitpython_pr.md +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.github/workflows/build.yml +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.github/workflows/failure-help-text.yml +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.github/workflows/release_gh.yml +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.github/workflows/release_pypi.yml +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.gitignore +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.pre-commit-config.yaml +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.readthedocs.yaml +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/CODE_OF_CONDUCT.md +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/LICENSE +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/LICENSES/CC-BY-4.0.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/LICENSES/MIT.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/LICENSES/Unlicense.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/README.rst +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/README.rst.license +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_circuitpython_httpserver.egg-info/SOURCES.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_circuitpython_httpserver.egg-info/dependency_links.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_circuitpython_httpserver.egg-info/requires.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_circuitpython_httpserver.egg-info/top_level.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/authentication.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/headers.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/interfaces.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/methods.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/mime_types.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/request.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/route.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/adafruit_httpserver/status.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/_static/custom.css +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/_static/favicon.ico +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/_static/favicon.ico.license +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/api.rst +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/api.rst.license +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/conf.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/examples.rst +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/examples.rst.license +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/index.rst +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/index.rst.license +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/requirements.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/starting_methods.rst +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/starting_methods.rst.license +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/directory_listing.tpl.html +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/home.html +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_authentication_handlers.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_authentication_server.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_chunked.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_cookies.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_cpu_information.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_cpython.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_form_data.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_handler_serves_file.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_https.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_mdns.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_methods.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_multiple_servers.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_neopixel.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_redirects.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_simpletest_auto_connection_manager.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_simpletest_auto_settings_toml.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_simpletest_manual_ap.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_simpletest_manual_ethernet.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_simpletest_manual_wifi.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_sse.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_start_and_poll.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_start_and_poll_asyncio.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_static_files_serving.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_templates.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_url_parameters.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_video_stream.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/httpserver_websocket.py +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/examples/settings.toml +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/optional_requirements.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/requirements.txt +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/ruff.toml +0 -0
- {adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/setup.cfg +0 -0
|
@@ -62,3 +62,16 @@ class FileNotExistsError(Exception):
|
|
|
62
62
|
Creates a new ``FileNotExistsError`` for the file at ``path``.
|
|
63
63
|
"""
|
|
64
64
|
super().__init__(f"File does not exist: {path}")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class WebsocketError(Exception):
|
|
68
|
+
"""
|
|
69
|
+
Raised when there is a error in WebSocket communication.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
def __init__(self, message: str, code: int = None) -> None:
|
|
73
|
+
"""
|
|
74
|
+
Creates a new ``WebsocketError`` with the given ``message``.
|
|
75
|
+
"""
|
|
76
|
+
self.code = code
|
|
77
|
+
super().__init__(f"WebSocket error: {message}")
|
|
@@ -16,6 +16,7 @@ import json
|
|
|
16
16
|
import os
|
|
17
17
|
from binascii import b2a_base64
|
|
18
18
|
from errno import EAGAIN, ECONNRESET, ENOTCONN, ETIMEDOUT
|
|
19
|
+
from time import monotonic_ns
|
|
19
20
|
|
|
20
21
|
try:
|
|
21
22
|
try:
|
|
@@ -32,6 +33,7 @@ from .exceptions import (
|
|
|
32
33
|
BackslashInPathError,
|
|
33
34
|
FileNotExistsError,
|
|
34
35
|
ParentDirectoryReferenceError,
|
|
36
|
+
WebsocketError,
|
|
35
37
|
)
|
|
36
38
|
from .headers import Headers
|
|
37
39
|
from .interfaces import _ISocket
|
|
@@ -414,8 +416,8 @@ class Redirect(Response):
|
|
|
414
416
|
*,
|
|
415
417
|
permanent: bool = False,
|
|
416
418
|
preserve_method: bool = False,
|
|
417
|
-
status: Union[Status, Tuple[int, str]] = None,
|
|
418
|
-
headers: Union[Headers, Dict[str, str]] = None,
|
|
419
|
+
status: Union[Status, Tuple[int, str], None] = None,
|
|
420
|
+
headers: Union[Headers, Dict[str, str], None] = None,
|
|
419
421
|
cookies: Dict[str, str] = None,
|
|
420
422
|
) -> None:
|
|
421
423
|
"""
|
|
@@ -593,13 +595,21 @@ class Websocket(Response):
|
|
|
593
595
|
FIN = 0b10000000 # FIN bit indicating the final fragment
|
|
594
596
|
|
|
595
597
|
# opcodes
|
|
596
|
-
CONT = 0 # Continuation frame
|
|
598
|
+
CONT = 0 # Continuation frame
|
|
597
599
|
TEXT = 1 # Frame contains UTF-8 text
|
|
598
600
|
BINARY = 2 # Frame contains binary data
|
|
599
601
|
CLOSE = 8 # Frame closes the connection
|
|
600
602
|
PING = 9 # Frame is a ping, expecting a pong
|
|
601
603
|
PONG = 10 # Frame is a pong, in response to a ping
|
|
602
604
|
|
|
605
|
+
PROTOCOL_ERROR = 1002
|
|
606
|
+
POLICY_VIOLATION = 1008
|
|
607
|
+
MESSAGE_TOO_BIG = 1009
|
|
608
|
+
|
|
609
|
+
MESSAGE_MAX_SIZE_BYTES = 4096
|
|
610
|
+
MESSAGE_MAX_FRAGMENTS = 16
|
|
611
|
+
MESSAGE_FRAGMENT_TIMEOUT_NS = 5 * (10**9)
|
|
612
|
+
|
|
603
613
|
@staticmethod
|
|
604
614
|
def _check_request_initiates_handshake(request: Request):
|
|
605
615
|
if not all(
|
|
@@ -650,80 +660,221 @@ class Websocket(Response):
|
|
|
650
660
|
self._headers.setdefault("Connection", "Upgrade")
|
|
651
661
|
self._headers.setdefault("Sec-WebSocket-Accept", sec_accept_key)
|
|
652
662
|
self._headers.setdefault("Content-Type", None)
|
|
653
|
-
self.
|
|
663
|
+
self._buffer = bytearray(buffer_size)
|
|
654
664
|
self.closed = False
|
|
655
665
|
|
|
666
|
+
self._reset_fragmented_message()
|
|
667
|
+
|
|
656
668
|
request.connection.setblocking(False)
|
|
657
669
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
opcode = header[0] & 0b00001111
|
|
662
|
-
has_mask = header[1] & 0b10000000
|
|
663
|
-
length = header[1] & 0b01111111
|
|
670
|
+
def _start_fragmented_message(self, opcode: Union[int, None], payload: Union[bytes, None]):
|
|
671
|
+
if self.MESSAGE_MAX_FRAGMENTS < 2:
|
|
672
|
+
raise WebsocketError("Fragmented messages not allowed", self.POLICY_VIOLATION)
|
|
664
673
|
|
|
665
|
-
if
|
|
666
|
-
|
|
667
|
-
elif length == 0b01111111:
|
|
668
|
-
length = -8
|
|
674
|
+
if len(payload) > self.MESSAGE_MAX_SIZE_BYTES:
|
|
675
|
+
raise WebsocketError("Message size too big", self.MESSAGE_TOO_BIG)
|
|
669
676
|
|
|
670
|
-
|
|
677
|
+
self._message_opcode: Union[int, None] = opcode
|
|
678
|
+
self._message_payload: Union[bytes, None] = payload
|
|
679
|
+
self._message_fragments: Union[int, None] = 1
|
|
671
680
|
|
|
672
|
-
|
|
673
|
-
|
|
681
|
+
now = monotonic_ns()
|
|
682
|
+
self._message_start_timestamp: Union[float, None] = now
|
|
683
|
+
self._message_last_frame_timestamp: Union[float, None] = now
|
|
684
|
+
|
|
685
|
+
def _reset_fragmented_message(self):
|
|
686
|
+
self._message_opcode = None
|
|
687
|
+
self._message_payload = None
|
|
688
|
+
self._message_fragments = None
|
|
689
|
+
self._message_start_timestamp = None
|
|
690
|
+
self._message_last_frame_timestamp = None
|
|
691
|
+
|
|
692
|
+
def _cont_fragmented_message(self, payload: bytes):
|
|
693
|
+
if self._message_fragments + 1 > self.MESSAGE_MAX_FRAGMENTS:
|
|
694
|
+
raise WebsocketError("Too many fragments in message", self.POLICY_VIOLATION)
|
|
695
|
+
|
|
696
|
+
if len(self._message_payload) + len(payload) > self.MESSAGE_MAX_SIZE_BYTES:
|
|
697
|
+
raise WebsocketError("Message size too big", self.MESSAGE_TOO_BIG)
|
|
698
|
+
|
|
699
|
+
now = monotonic_ns()
|
|
700
|
+
time_since_last_frame = now - self._message_last_frame_timestamp
|
|
701
|
+
|
|
702
|
+
if time_since_last_frame > self.MESSAGE_FRAGMENT_TIMEOUT_NS:
|
|
703
|
+
raise WebsocketError("Fragment timeout exceeded", self.POLICY_VIOLATION)
|
|
704
|
+
|
|
705
|
+
self._message_payload += payload
|
|
706
|
+
self._message_fragments += 1
|
|
707
|
+
self._message_last_frame_timestamp = now
|
|
708
|
+
|
|
709
|
+
def _fragmented_message_in_progress(self):
|
|
710
|
+
return self._message_opcode is not None
|
|
711
|
+
|
|
712
|
+
def _recv_exact(self, buffer: bytearray, size: int) -> bytes:
|
|
713
|
+
received = 0
|
|
714
|
+
view = memoryview(buffer)
|
|
715
|
+
while received < size:
|
|
716
|
+
remaining = size - received
|
|
717
|
+
try:
|
|
718
|
+
count = self._request.connection.recv_into(view[received : received + remaining])
|
|
719
|
+
except OSError as error:
|
|
720
|
+
if error.errno == EAGAIN and received == 0:
|
|
721
|
+
raise
|
|
722
|
+
if error.errno == EAGAIN:
|
|
723
|
+
continue
|
|
724
|
+
raise
|
|
725
|
+
if count == 0:
|
|
726
|
+
if received == 0:
|
|
727
|
+
raise OSError(ENOTCONN)
|
|
728
|
+
break
|
|
729
|
+
received += count
|
|
730
|
+
return bytes(view[:received])
|
|
731
|
+
|
|
732
|
+
def _read_frame_header(self):
|
|
733
|
+
header_bytes = self._recv_exact(self._buffer, 2)
|
|
734
|
+
|
|
735
|
+
if len(header_bytes) < 2:
|
|
736
|
+
raise OSError(ENOTCONN)
|
|
737
|
+
|
|
738
|
+
fin = header_bytes[0] & Websocket.FIN
|
|
739
|
+
opcode = header_bytes[0] & 0b00001111
|
|
740
|
+
|
|
741
|
+
mask = header_bytes[1] & 0b10000000
|
|
742
|
+
if not mask:
|
|
743
|
+
raise WebsocketError("Client frame not masked", self.PROTOCOL_ERROR)
|
|
744
|
+
|
|
745
|
+
payload_length = header_bytes[1] & 0b01111111
|
|
746
|
+
|
|
747
|
+
if 125 < payload_length:
|
|
748
|
+
if payload_length == 126:
|
|
749
|
+
payload_length_bytes = self._recv_exact(self._buffer, 2) # Read next 16 bits
|
|
750
|
+
if len(payload_length_bytes) < 2:
|
|
751
|
+
raise WebsocketError("Incomplete payload length", self.PROTOCOL_ERROR)
|
|
752
|
+
|
|
753
|
+
elif payload_length == 127:
|
|
754
|
+
payload_length_bytes = self._recv_exact(self._buffer, 8) # Read next 64 bits
|
|
755
|
+
if len(payload_length_bytes) < 8:
|
|
756
|
+
raise WebsocketError("Incomplete payload length", self.PROTOCOL_ERROR)
|
|
757
|
+
else:
|
|
758
|
+
raise WebsocketError("Invalid payload length", self.PROTOCOL_ERROR)
|
|
759
|
+
|
|
760
|
+
payload_length = int.from_bytes(payload_length_bytes, "big")
|
|
761
|
+
|
|
762
|
+
# In 64-bit payload length, most significant bit must be 0
|
|
763
|
+
if payload_length & (1 << 63):
|
|
764
|
+
raise WebsocketError("Invalid payload length", self.PROTOCOL_ERROR)
|
|
765
|
+
|
|
766
|
+
if opcode == Websocket.CONT and self._fragmented_message_in_progress():
|
|
767
|
+
if len(self._message_payload) + payload_length > self.MESSAGE_MAX_SIZE_BYTES:
|
|
768
|
+
raise WebsocketError("Message size too big", self.MESSAGE_TOO_BIG)
|
|
769
|
+
|
|
770
|
+
elif opcode in {Websocket.TEXT, Websocket.BINARY}:
|
|
771
|
+
if payload_length > self.MESSAGE_MAX_SIZE_BYTES:
|
|
772
|
+
raise WebsocketError("Message size too big", self.MESSAGE_TOO_BIG)
|
|
674
773
|
|
|
675
|
-
|
|
676
|
-
header_bytes = buffer[:header_length]
|
|
774
|
+
return fin, opcode, payload_length
|
|
677
775
|
|
|
678
|
-
|
|
776
|
+
def _read_frame(self):
|
|
777
|
+
fin, opcode, payload_length = self._read_frame_header()
|
|
679
778
|
|
|
680
|
-
|
|
681
|
-
if
|
|
682
|
-
|
|
779
|
+
masking_key = self._recv_exact(self._buffer, 4)
|
|
780
|
+
if len(masking_key) < 4:
|
|
781
|
+
raise WebsocketError("Incomplete mask", self.PROTOCOL_ERROR)
|
|
683
782
|
|
|
684
783
|
payload = b""
|
|
685
|
-
if fin == Websocket.FIN and opcode == Websocket.CLOSE:
|
|
686
|
-
return Websocket.CLOSE, payload
|
|
687
784
|
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
785
|
+
while 0 < payload_length:
|
|
786
|
+
chunk = self._recv_exact(self._buffer, min(payload_length, len(self._buffer)))
|
|
787
|
+
if not chunk:
|
|
788
|
+
break
|
|
789
|
+
payload += chunk
|
|
790
|
+
payload_length -= len(chunk)
|
|
691
791
|
|
|
692
|
-
|
|
693
|
-
mask_length = self._request.connection.recv_into(buffer, 4)
|
|
694
|
-
mask = buffer[:mask_length]
|
|
792
|
+
payload = bytes(byte ^ masking_key[idx % 4] for idx, byte in enumerate(payload))
|
|
695
793
|
|
|
696
|
-
|
|
697
|
-
payload_length = self._request.connection.recv_into(buffer, length)
|
|
698
|
-
payload += buffer[: min(payload_length, length)]
|
|
699
|
-
length -= min(payload_length, length)
|
|
794
|
+
return fin, opcode, payload
|
|
700
795
|
|
|
701
|
-
|
|
702
|
-
|
|
796
|
+
def _is_control_frame(self, opcode: int) -> bool:
|
|
797
|
+
return opcode in {Websocket.CLOSE, Websocket.PING, Websocket.PONG}
|
|
703
798
|
|
|
704
|
-
|
|
799
|
+
def _handle_control_frame(self, fin: int, opcode: int, payload: bytes):
|
|
800
|
+
if 125 < len(payload):
|
|
801
|
+
raise WebsocketError("Control frame payload too large", self.PROTOCOL_ERROR)
|
|
705
802
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
if opcode == Websocket.CONT:
|
|
709
|
-
return None
|
|
803
|
+
if fin != Websocket.FIN:
|
|
804
|
+
raise WebsocketError("Control frame not final", self.PROTOCOL_ERROR)
|
|
710
805
|
|
|
711
806
|
if opcode == Websocket.CLOSE:
|
|
712
|
-
|
|
713
|
-
|
|
807
|
+
if len(payload) == 1:
|
|
808
|
+
raise WebsocketError("Invalid close payload length", self.PROTOCOL_ERROR)
|
|
809
|
+
close_code = None
|
|
810
|
+
close_reason = None
|
|
811
|
+
if 2 <= len(payload):
|
|
812
|
+
close_code = int.from_bytes(payload[:2], "big")
|
|
813
|
+
if 2 < len(payload):
|
|
814
|
+
try:
|
|
815
|
+
close_reason = payload[2:].decode("utf-8")
|
|
816
|
+
except UnicodeError as error:
|
|
817
|
+
raise WebsocketError(
|
|
818
|
+
"Invalid close reason encoding", self.PROTOCOL_ERROR
|
|
819
|
+
) from error
|
|
820
|
+
self.close(code=close_code, reason=close_reason)
|
|
821
|
+
return
|
|
822
|
+
elif opcode == Websocket.PING:
|
|
823
|
+
self.send_message(payload, Websocket.PONG)
|
|
824
|
+
return
|
|
825
|
+
elif opcode == Websocket.PONG:
|
|
826
|
+
return
|
|
827
|
+
|
|
828
|
+
def _handle_frame(self, fin: int, opcode: int, payload: bytes) -> Union[str, bytes, None]:
|
|
829
|
+
if self._is_control_frame(opcode):
|
|
830
|
+
return self._handle_control_frame(fin, opcode, payload)
|
|
831
|
+
|
|
832
|
+
if not self._fragmented_message_in_progress():
|
|
833
|
+
if opcode not in {Websocket.TEXT, Websocket.BINARY}:
|
|
834
|
+
raise WebsocketError(
|
|
835
|
+
"Invalid frame received when no fragmented message in progress",
|
|
836
|
+
self.PROTOCOL_ERROR,
|
|
837
|
+
)
|
|
838
|
+
|
|
839
|
+
if fin == Websocket.FIN:
|
|
840
|
+
if len(payload) > self.MESSAGE_MAX_SIZE_BYTES:
|
|
841
|
+
raise WebsocketError("Message size too big", self.MESSAGE_TOO_BIG)
|
|
842
|
+
|
|
843
|
+
if opcode == Websocket.TEXT:
|
|
844
|
+
try:
|
|
845
|
+
return payload.decode("utf-8")
|
|
846
|
+
except UnicodeError as error:
|
|
847
|
+
raise WebsocketError(
|
|
848
|
+
"Invalid UTF-8 in text message", self.PROTOCOL_ERROR
|
|
849
|
+
) from error
|
|
850
|
+
return payload
|
|
851
|
+
|
|
852
|
+
else:
|
|
853
|
+
self._start_fragmented_message(opcode, payload)
|
|
854
|
+
return None
|
|
855
|
+
|
|
856
|
+
if opcode != Websocket.CONT:
|
|
857
|
+
raise WebsocketError(
|
|
858
|
+
"New data frame received while fragmented message in progress",
|
|
859
|
+
self.PROTOCOL_ERROR,
|
|
860
|
+
)
|
|
714
861
|
|
|
715
|
-
|
|
862
|
+
self._cont_fragmented_message(payload)
|
|
863
|
+
|
|
864
|
+
if fin != Websocket.FIN:
|
|
716
865
|
return None
|
|
717
|
-
if opcode == Websocket.PING:
|
|
718
|
-
self.send_message(payload, Websocket.PONG)
|
|
719
|
-
return payload
|
|
720
866
|
|
|
721
867
|
try:
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
868
|
+
if self._message_opcode == Websocket.TEXT:
|
|
869
|
+
try:
|
|
870
|
+
return self._message_payload.decode("utf-8")
|
|
871
|
+
except UnicodeError as error:
|
|
872
|
+
raise WebsocketError(
|
|
873
|
+
"Invalid UTF-8 in text message", self.PROTOCOL_ERROR
|
|
874
|
+
) from error
|
|
875
|
+
return self._message_payload
|
|
876
|
+
finally:
|
|
877
|
+
self._reset_fragmented_message()
|
|
727
878
|
|
|
728
879
|
def receive(self, fail_silently: bool = False) -> Union[str, bytes, None]:
|
|
729
880
|
"""
|
|
@@ -737,12 +888,21 @@ class Websocket(Response):
|
|
|
737
888
|
raise RuntimeError("Websocket connection is closed, cannot receive messages")
|
|
738
889
|
|
|
739
890
|
try:
|
|
740
|
-
opcode, payload = self._read_frame()
|
|
741
|
-
frame_data = self._handle_frame(opcode, payload)
|
|
891
|
+
fin, opcode, payload = self._read_frame()
|
|
742
892
|
|
|
743
|
-
return
|
|
893
|
+
return self._handle_frame(fin, opcode, payload)
|
|
894
|
+
except WebsocketError as error:
|
|
895
|
+
self.close(code=error.code or self.PROTOCOL_ERROR)
|
|
896
|
+
return None
|
|
744
897
|
except OSError as error:
|
|
745
|
-
if error.errno == EAGAIN: # No
|
|
898
|
+
if error.errno == EAGAIN: # No message/frame available
|
|
899
|
+
if not self._fragmented_message_in_progress():
|
|
900
|
+
return None
|
|
901
|
+
|
|
902
|
+
time_since_last_frame = monotonic_ns() - self._message_last_frame_timestamp
|
|
903
|
+
|
|
904
|
+
if time_since_last_frame > self.MESSAGE_FRAGMENT_TIMEOUT_NS:
|
|
905
|
+
self.close(code=self.POLICY_VIOLATION)
|
|
746
906
|
return None
|
|
747
907
|
if error.errno == ETIMEDOUT: # Connection timed out
|
|
748
908
|
return None
|
|
@@ -785,7 +945,7 @@ class Websocket(Response):
|
|
|
785
945
|
"""
|
|
786
946
|
Send a message to the client.
|
|
787
947
|
|
|
788
|
-
:param str message: Message to be sent.
|
|
948
|
+
:param Union[str, bytes] message: Message to be sent.
|
|
789
949
|
:param int opcode: Opcode of the message. Defaults to TEXT if message is a string and
|
|
790
950
|
BINARY for bytes.
|
|
791
951
|
:param bool fail_silently: If True, no error will be raised if the connection is closed.
|
|
@@ -814,13 +974,31 @@ class Websocket(Response):
|
|
|
814
974
|
def _send(self) -> None:
|
|
815
975
|
self._send_headers()
|
|
816
976
|
|
|
817
|
-
def
|
|
977
|
+
def _prepare_close_payload(self, code: int = None, reason: str = None) -> bytes:
|
|
978
|
+
if code is None:
|
|
979
|
+
return b""
|
|
980
|
+
payload = bytearray(code.to_bytes(2, "big"))
|
|
981
|
+
if reason:
|
|
982
|
+
payload.extend(reason.encode("utf-8"))
|
|
983
|
+
if 125 < len(payload):
|
|
984
|
+
payload = payload[:125]
|
|
985
|
+
return bytes(payload)
|
|
986
|
+
|
|
987
|
+
def close(self, code: int = None, reason: str = None):
|
|
818
988
|
"""
|
|
819
989
|
Close the connection.
|
|
820
990
|
|
|
821
991
|
**Always call this method when you are done sending events.**
|
|
822
992
|
"""
|
|
823
|
-
if
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
993
|
+
if self.closed:
|
|
994
|
+
return
|
|
995
|
+
|
|
996
|
+
self._reset_fragmented_message()
|
|
997
|
+
|
|
998
|
+
payload = self._prepare_close_payload(code, reason)
|
|
999
|
+
try:
|
|
1000
|
+
self.send_message(payload, Websocket.CLOSE, fail_silently=True)
|
|
1001
|
+
except (BrokenPipeError, OSError):
|
|
1002
|
+
pass
|
|
1003
|
+
self._close_connection()
|
|
1004
|
+
self.closed = True
|
|
@@ -36,12 +36,8 @@ from .status import BAD_REQUEST_400, FORBIDDEN_403, NOT_FOUND_404, UNAUTHORIZED_
|
|
|
36
36
|
try:
|
|
37
37
|
from ssl import SSLContext, create_default_context
|
|
38
38
|
|
|
39
|
-
try: # ssl imports for
|
|
40
|
-
from ssl import
|
|
41
|
-
CERT_NONE,
|
|
42
|
-
Purpose,
|
|
43
|
-
SSLError,
|
|
44
|
-
)
|
|
39
|
+
try: # ssl imports for CPython
|
|
40
|
+
from ssl import CERT_NONE, Purpose, SSLError
|
|
45
41
|
except ImportError:
|
|
46
42
|
pass
|
|
47
43
|
SSL_AVAILABLE = True
|
|
@@ -129,7 +125,7 @@ class Server:
|
|
|
129
125
|
self._timeout = 1
|
|
130
126
|
|
|
131
127
|
self._auths = []
|
|
132
|
-
self._routes:
|
|
128
|
+
self._routes: List[Route] = []
|
|
133
129
|
self.headers = Headers()
|
|
134
130
|
|
|
135
131
|
self._socket_source = socket_source
|
|
@@ -331,6 +327,8 @@ class Server:
|
|
|
331
327
|
try:
|
|
332
328
|
length = sock.recv_into(self._buffer, len(self._buffer))
|
|
333
329
|
received_bytes += self._buffer[:length]
|
|
330
|
+
except TimeoutError:
|
|
331
|
+
break
|
|
334
332
|
except OSError as ex:
|
|
335
333
|
if ex.errno == ETIMEDOUT:
|
|
336
334
|
break
|
|
@@ -350,6 +348,8 @@ class Server:
|
|
|
350
348
|
try:
|
|
351
349
|
length = sock.recv_into(self._buffer, len(self._buffer))
|
|
352
350
|
received_body_bytes += self._buffer[:length]
|
|
351
|
+
except TimeoutError:
|
|
352
|
+
break
|
|
353
353
|
except OSError as ex:
|
|
354
354
|
if ex.errno == ETIMEDOUT:
|
|
355
355
|
break
|
|
@@ -647,7 +647,7 @@ def _debug_response_sent(response: "Response", time_elapsed: float):
|
|
|
647
647
|
req_size = len(response._request.raw_request)
|
|
648
648
|
status = response._status
|
|
649
649
|
res_size = response._size
|
|
650
|
-
time_elapsed_ms = f"{round(time_elapsed*1000)}ms"
|
|
650
|
+
time_elapsed_ms = f"{round(time_elapsed * 1000)}ms"
|
|
651
651
|
|
|
652
652
|
print(
|
|
653
653
|
f'{client_ip} -- "{method} {path}" {req_size} -- "{status}" {res_size} -- {time_elapsed_ms}'
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.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 = "4.
|
|
15
|
+
version = "4.8.0"
|
|
16
16
|
readme = "README.rst"
|
|
17
17
|
authors = [
|
|
18
18
|
{name = "Adafruit Industries", email = "circuitpython@adafruit.com"}
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.gitattributes
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/.gitignore
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/LICENSES/MIT.txt
RENAMED
|
File without changes
|
|
File without changes
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/api.rst
RENAMED
|
File without changes
|
|
File without changes
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/docs/conf.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/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
|
|
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.7.1 → adafruit_circuitpython_httpserver-4.8.0}/requirements.txt
RENAMED
|
File without changes
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/ruff.toml
RENAMED
|
File without changes
|
{adafruit_circuitpython_httpserver-4.7.1 → adafruit_circuitpython_httpserver-4.8.0}/setup.cfg
RENAMED
|
File without changes
|