adafruit-circuitpython-httpserver 2.5.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.
Files changed (53) hide show
  1. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/PKG-INFO +1 -1
  2. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/PKG-INFO +1 -1
  3. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/SOURCES.txt +1 -0
  4. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/__init__.py +1 -1
  5. adafruit-circuitpython-httpserver-3.0.0/adafruit_httpserver/exceptions.py +52 -0
  6. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/response.py +67 -17
  7. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/server.py +47 -37
  8. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/status.py +3 -0
  9. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/api.rst +3 -0
  10. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_chunked.py +4 -3
  11. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_cpu_information.py +4 -3
  12. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_mdns.py +4 -3
  13. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_neopixel.py +4 -3
  14. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_simple_poll.py +4 -3
  15. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_simple_serve.py +4 -3
  16. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/examples/httpserver_url_parameters.py +4 -3
  17. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/pyproject.toml +1 -1
  18. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/PULL_REQUEST_TEMPLATE/adafruit_circuitpython_pr.md +0 -0
  19. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/workflows/build.yml +0 -0
  20. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/workflows/failure-help-text.yml +0 -0
  21. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/workflows/release_gh.yml +0 -0
  22. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/.github/workflows/release_pypi.yml +0 -0
  23. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/.gitignore +0 -0
  24. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/.pre-commit-config.yaml +0 -0
  25. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/.pylintrc +0 -0
  26. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/.readthedocs.yaml +0 -0
  27. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/CODE_OF_CONDUCT.md +0 -0
  28. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/LICENSE +0 -0
  29. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/LICENSES/CC-BY-4.0.txt +0 -0
  30. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/LICENSES/MIT.txt +0 -0
  31. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/LICENSES/Unlicense.txt +0 -0
  32. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/README.rst +0 -0
  33. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/README.rst.license +0 -0
  34. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/dependency_links.txt +0 -0
  35. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/requires.txt +0 -0
  36. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_circuitpython_httpserver.egg-info/top_level.txt +0 -0
  37. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/headers.py +0 -0
  38. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/methods.py +0 -0
  39. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/mime_type.py +0 -0
  40. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/request.py +0 -0
  41. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/adafruit_httpserver/route.py +0 -0
  42. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/_static/favicon.ico +0 -0
  43. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/_static/favicon.ico.license +0 -0
  44. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/api.rst.license +0 -0
  45. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/conf.py +0 -0
  46. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/examples.rst +0 -0
  47. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/examples.rst.license +0 -0
  48. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/index.rst +0 -0
  49. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/index.rst.license +0 -0
  50. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/docs/requirements.txt +0 -0
  51. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/optional_requirements.txt +0 -0
  52. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/requirements.txt +0 -0
  53. {adafruit-circuitpython-httpserver-2.5.0 → adafruit-circuitpython-httpserver-3.0.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: adafruit-circuitpython-httpserver
3
- Version: 2.5.0
3
+ Version: 3.0.0
4
4
  Summary: Simple HTTP Server for CircuitPython
5
5
  Author-email: Adafruit Industries <circuitpython@adafruit.com>
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: adafruit-circuitpython-httpserver
3
- Version: 2.5.0
3
+ Version: 3.0.0
4
4
  Summary: Simple HTTP Server for CircuitPython
5
5
  Author-email: Adafruit Industries <circuitpython@adafruit.com>
6
6
  License: MIT
@@ -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
@@ -19,5 +19,5 @@ Implementation Notes
19
19
  https://github.com/adafruit/circuitpython/releases
20
20
  """
21
21
 
22
- __version__ = "2.5.0"
22
+ __version__ = "3.0.0"
23
23
  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer.git"
@@ -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 `send()` or
77
- implicitly determined from filename in `send_file()`.
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 `send` or `send_file`.
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 `send` and `send_file` and in
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,12 +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
- def send_file(
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,
175
223
  head_only: bool = False,
224
+ safe: bool = True,
176
225
  ) -> None:
177
226
  """
178
227
  Send response with content of ``filename`` located in ``root_path``.
@@ -181,17 +230,18 @@ class HTTPResponse:
181
230
 
182
231
  Should be called **only once** per response.
183
232
  """
184
- if self._response_already_sent:
185
- raise RuntimeError("Response was already sent")
233
+
234
+ if safe:
235
+ self._check_file_path_is_valid(filename)
186
236
 
187
237
  if not root_path.endswith("/"):
188
238
  root_path += "/"
189
- try:
190
- file_length = os.stat(root_path + filename)[6]
191
- except OSError:
192
- # If the file doesn't exist, return 404.
193
- HTTPResponse(self.request, status=CommonHTTPStatus.NOT_FOUND_404).send()
194
- return
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)
195
245
 
196
246
  self._send_headers(
197
247
  content_type=MIMEType.from_file_name(filename),
@@ -199,7 +249,7 @@ class HTTPResponse:
199
249
  )
200
250
 
201
251
  if not head_only:
202
- with open(root_path + filename, "rb") as file:
252
+ with open(full_file_path, "rb") as file:
203
253
  while bytes_read := file.read(buffer_size):
204
254
  self._send_bytes(self.request.connection, bytes_read)
205
255
  self._response_already_sent = True
@@ -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, root_path: str = "") -> None:
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, root_path)
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, root_path: str = "") -> None:
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,38 +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
- # 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, 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),
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)
180
189
  )
181
- else:
182
- HTTPResponse(
183
- request, status=CommonHTTPStatus.BAD_REQUEST_400
184
- ).send()
185
-
186
- except OSError as ex:
187
- # handle EAGAIN and ECONNRESET
188
- if ex.errno == EAGAIN:
189
- # there is no data available right now, try again later.
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.
190
200
  return
191
- if ex.errno == ECONNRESET:
192
- # connection reset by peer, try again later.
201
+ if error.errno == ECONNRESET:
202
+ # Connection reset by peer, try again later.
193
203
  return
194
204
  raise
195
205
 
@@ -204,7 +214,7 @@ class HTTPServer:
204
214
 
205
215
  Example::
206
216
 
207
- server = HTTPServer(pool)
217
+ server = HTTPServer(pool, "/static")
208
218
  server.request_buffer_size = 2048
209
219
 
210
220
  server.serve_forever(str(wifi.radio.ipv4_address))
@@ -226,7 +236,7 @@ class HTTPServer:
226
236
 
227
237
  Example::
228
238
 
229
- server = HTTPServer(pool)
239
+ server = HTTPServer(pool, "/static")
230
240
  server.socket_timeout = 3
231
241
 
232
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
 
@@ -27,3 +27,6 @@
27
27
 
28
28
  .. automodule:: adafruit_httpserver.mime_type
29
29
  :members:
30
+
31
+ .. automodule:: adafruit_httpserver.exceptions
32
+ :members:
@@ -2,7 +2,7 @@
2
2
  #
3
3
  # SPDX-License-Identifier: Unlicense
4
4
 
5
- import secrets # pylint: disable=no-name-in-module
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, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD # pylint: disable=no-member
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 secrets # pylint: disable=no-name-in-module
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, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD # pylint: disable=no-member
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 secrets # pylint: disable=no-name-in-module
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, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD # pylint: disable=no-member
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 secrets # pylint: disable=no-name-in-module
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, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD # pylint: disable=no-member
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 secrets # pylint: disable=no-name-in-module
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, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD # pylint: disable=no-member
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 secrets # pylint: disable=no-name-in-module
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, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD # pylint: disable=no-member
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 secrets # pylint: disable=no-name-in-module
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, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD # pylint: disable=no-member
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:
@@ -12,7 +12,7 @@ requires = [
12
12
  [project]
13
13
  name = "adafruit-circuitpython-httpserver"
14
14
  description = "Simple HTTP Server for CircuitPython"
15
- version = "2.5.0"
15
+ version = "3.0.0"
16
16
  readme = "README.rst"
17
17
  authors = [
18
18
  {name = "Adafruit Industries", email = "circuitpython@adafruit.com"}