Skip to content

Add a bare minimum simpletest example #51

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
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions docs/examples.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Simple file serving
Simple Test
-------------------

Serving the content of index.html from the filesystem.
Serving a simple static text message.

.. literalinclude:: ../examples/httpserver_simple_serve.py
:caption: examples/httpserver_simple_serve.py
.. literalinclude:: ../examples/httpserver_simpletest.py
:caption: examples/httpserver_simpletest.py
:linenos:

If you want your code to do more than just serve web pages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
print("Connected to", ssid)

pool = socketpool.SocketPool(wifi.radio)
server = HTTPServer(pool, "/static")
server = HTTPServer(pool, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should keep the "/static" value for the root_path parameter.

Setting it to empty means that files in the root of the CIRCUITPY drive will be served which is potential security concern since settings.toml would get served.

The library was just recently updated to add additional restrictions to prevent that, so best practice is definitely to use a specific directory for the root path being served.

Even in cases where there is no static content used it can still be set to "/static".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally makes sense, thanks for the feedback. I'll get that updated shortly.



@server.route("/")
def base(request: HTTPRequest):
"""
Serve the default index.html file.
Serve a default static plain text message.
"""
with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response:
response.send_file("index.html")
with HTTPResponse(request, content_type=MIMEType.TYPE_TXT) as response:
message = "Hello from the CircuitPython HTTPServer!"
response.send(message)


print(f"Listening on http://{wifi.radio.ipv4_address}:80")
Expand Down