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 3 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 requestbuffersize(self) -> int:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snake case will make this a little more readable:

Suggested change
def requestbuffersize(self) -> int:
def request_buffer_size(self) -> int:

Same thing for the setter of course!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the snake case and it's definitely easier to read. I wasn't sure if there were naming standards, so I just mirrored the other function names. I come from a camel case background, but that doesn't seem to apply in python except in class names.

Copy link
Member

@tekktrik tekktrik Jul 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! You've got it spot on. I don't think I've ever seen it explicitly said about the project, but generally we follow the PEP8 style guide (unless what we're making a library that should mirror CPython API like howadafruit_logging mirrors CPython's logging modules but that doesn't follow PEP8 itself).

"""
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.requestbuffersize = 2048

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

@requestbuffersize.setter
def requestbuffersize(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:
1 change: 1 addition & 0 deletions examples/httpserver_simplepolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def base(request): # pylint: disable=unused-argument

while True:
try:
# do something useful in this section
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It make be helpful to give an example of what you mean by "something useful", like "operate hardware" or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be easy to add some more details to that comment.

# processing any waiting requests
server.poll()
except OSError:
Expand Down