Skip to content
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

net: http_server: allow specifying a default resource #83811

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mrodgers-witekio
Copy link
Collaborator

The _detail parameter of HTTP_SERVICE_DEFINE is currently unused - use this to allow specifying a default resource detail which will be served if no other resource is matched. If the parameter is NULL, the existing behaviour is retained.

This could be used to:

  • Provide a custom 404 response
  • Better support a single-page app with routing handled in the frontend. This use-case is where I ran into issues at the moment - the routing in the frontend works fine until the page is refreshed, at which point the browser sends a request to the backend, the path doesn't exist, and a 404 is returned.

The _detail parameter of HTTP_SERVICE_DEFINE is used to optionally
specify a default resource detail, which will be served if no other
resource matches the URL.

Signed-off-by: Matt Rodgers <[email protected]>
@zephyrbot zephyrbot added area: HTTP HTTP client/server support area: Networking labels Jan 10, 2025
@zephyrbot zephyrbot requested review from jukkar and rlubos January 10, 2025 12:58
@jukkar jukkar requested a review from cfriedt January 10, 2025 13:16
@jukkar
Copy link
Member

jukkar commented Jan 10, 2025

Should we mention about this also in the API documentation which has examples of the usage?
I am talking about this file
https://github.com/zephyrproject-rtos/zephyr/blob/main/doc/connectivity/networking/api/http_server.rst

rlubos
rlubos previously approved these changes Jan 10, 2025
@mrodgers-witekio
Copy link
Collaborator Author

Added some documentation for this change

rlubos
rlubos previously approved these changes Jan 10, 2025
Add documentation showing how to use the _detail parameter when
registering an HTTP service to provide a default resource handling any
unknown path.

Signed-off-by: Matt Rodgers <[email protected]>
@cfriedt
Copy link
Member

cfriedt commented Jan 10, 2025

It would be great to provide a page that can be used to communicate any HTTP status code, and Zephyr could provide one that users could choose to use.

Default might not be the best terminology, but I think the idea is good.

const struct http_request_ctx *request_ctx,
struct http_response_ctx *response_ctx, void *user_data)
{
static const char response_404[] = "Oops, page not found!";
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to reuse this in a more generic way for any HTTP error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: HTTP HTTP client/server support area: Networking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants