Skip to content

Add buffer size property #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion adafruit_httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def mime_type(filename):


class HTTPResponse:
"""Details of an HTTP response. Use in @`HTTPServer.route` decorator functions."""
"""Details of an HTTP response. Use in `HTTPServer.route` decorator functions."""

_HEADERS_FORMAT = (
"HTTP/1.1 {}\r\n"
Expand Down Expand Up @@ -361,3 +361,25 @@ def poll(self):
# connection reset by peer, try again later.
return
raise

@property
def request_buffer_size(self) -> int:
"""
The maximum size of the incoming request buffer. If the default size isn't
adequate to handle your incoming data you can set this after creating the
server instance.

Default size is 1024 bytes.

Example::

server = HTTPServer(pool)
server.request_buffer_size = 2048

server.serve_forever(str(wifi.radio.ipv4_address))
"""
return len(self._buffer)

@request_buffer_size.setter
def request_buffer_size(self, value: int) -> None:
self._buffer = bytearray(value)
19 changes: 19 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,22 @@ Ensure your device works with this simple test.
.. literalinclude:: ../examples/httpserver_simpletest.py
:caption: examples/httpserver_simpletest.py
:linenos:

Temperature test
--------------------

Send the microcontroller temperature back to the browser with this simple test.

.. literalinclude:: ../examples/httpserver_temperature.py
:caption: examples/httpserver_temperature.py
:linenos:

Simple polling test
-------------------

If you want your code to do more than just serve web pages,
use the start/poll methods as shown in this example.

.. literalinclude:: ../examples/httpserver_simplepolling.py
:caption: examples/httpserver_simplepolling.py
:linenos:
6 changes: 5 additions & 1 deletion examples/httpserver_simplepolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def base(request): # pylint: disable=unused-argument

while True:
try:
# processing any waiting requests
# do something useful in this section,
# for example read a sensor and capture an average,
# or a running total of the last 10 samples

# process any waiting requests
server.poll()
except OSError:
continue