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

Add HTTP server support #64465

Merged
merged 5 commits into from
Apr 30, 2024
Merged

Conversation

jukkar
Copy link
Member

@jukkar jukkar commented Oct 26, 2023

This is based on the work done in #59669. I created a new PR as the original PR was kind of messy and long.

I fixed the tests so that they now use picolibc. Also the sample is tweaked to compile properly in native_sim.
Fixed also Posix support (basically removed it) so that it compiles properly with socket API.


#endif

#define CLIENT_BUFFER_SIZE CONFIG_NET_HTTP_SERVER_CLIENT_BUFFER_SIZE
Copy link
Member

Choose a reason for hiding this comment

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

These should probably not go in this header.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, agreed. I think we need to create internal http server header file for these.

Comment on lines 75 to 74
if (IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS)) {
proto = IPPROTO_TLS_1_2;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I've created a CoAP server implementation (#64265) inspired by the work that was done in the previous PR.
I did leave out the secure variant for now, since this way of doing it, forces it to either secure or non-secure. But no option to support both.

Copy link
Member Author

Choose a reason for hiding this comment

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

IMHO we need to allow supporting both at the same time so some tweaking is needed here. Initially I was thinking that application could do the socket initialization for TLS which would move the https burden from the lib to the application needing that support. For example, when registering the service, user could supply a function pointer that the server could then call to initialize the socket to use TLS.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I have been through the same process, I was working on a PoC for my CoAP server where the service is either secure or not, something like:

#define COAP_SERVICE_DEFINE(_name, _host, _port, _flags) /* Put NULL in the DTLS data */
#define COAP_SERVICE_DEFINE_SECURE(_name, _host, _port, _flags, _dtls_cfg) /* Some struct with DTLS info is provided */

The server implementation could then verify the secure data struct on how to initialize the sockets.

subsys/net/lib/http/http_server.c Outdated Show resolved Hide resolved
@jukkar
Copy link
Member Author

jukkar commented Oct 28, 2023

  • Updated according to comments
  • Squashed couple of commits together, the history is a bit messy so did not dive too deep into this
  • Added initial https support (does not work yet), I am proposing that TLS socket is done outside of the http_server library in order to avoid TLS complexities involved. This in mind I added a socket setup callback that the application can set when defining a service, and the library will then call it when needed

@pdgendt
Copy link
Collaborator

pdgendt commented Oct 30, 2023

  • Added initial https support (does not work yet), I am proposing that TLS socket is done outside of the http_server library in order to avoid TLS complexities involved. This in mind I added a socket setup callback that the application can set when defining a service, and the library will then call it when needed

While I like the customization level, moving the TLS complexities outside of the library seems like a step back to me. I would prefer, as a user, that the subsystem provides the implementation for a certain configuration. I see some benefits that way:

  • The same implementation across projects
  • Testable subsystem for the security features, and tracking of vulnerabilities
  • It just works ™
  • Avoid bugs/issues for a plethora of different uses of the socket_setup function

If the subsystem would expose a socket_setup function that is re-usable, this would already be a big plus. Some MACRO magic could hide most details for example.

Just my 2 cents :)

@jukkar
Copy link
Member Author

jukkar commented Oct 30, 2023

If the subsystem would expose a socket_setup function that is re-usable, this would already be a big plus. Some MACRO magic could hide most details for example.

Perhaps this is a way to go. So we could provide a default way to setup the TLS but if needed application could override that and do things its own way.

@jukkar
Copy link
Member Author

jukkar commented Nov 2, 2023

Worked on supporting multiple clients. Did not get it fully working yet.

@jukkar
Copy link
Member Author

jukkar commented Nov 3, 2023

This is now somewhat usable as a library. More than one client can be supported at the same time.

Still missing:

  • https support
  • dynamic resources
  • overall cleanup of the code

@jukkar
Copy link
Member Author

jukkar commented Nov 4, 2023

Changes:

  • fixed the multiple client support, client struct needed proper cleanup after connection was closed
  • preparation for https, removed the socket callback, user needs to supply the sec_tag list and the TLS socket will be created using that info

@jukkar jukkar force-pushed the http-server-support branch 2 times, most recently from c39edd5 to 5f76516 Compare November 4, 2023 22:55
@jukkar
Copy link
Member Author

jukkar commented Nov 4, 2023

HTTPS works now with attached certs which were copied from echo-server sample.

@jukkar
Copy link
Member Author

jukkar commented Nov 5, 2023

I am wondering about the dynamic resources, how they are defined and what kind of API between application and lib would work best. Any suggestion / ideas for that?

@cfriedt
Copy link
Member

cfriedt commented Nov 7, 2023

I am wondering about the dynamic resources, how they are defined and what kind of API between application and lib would work best. Any suggestion / ideas for that?

As in REST APIs? It would be a different kind of handler with a different "detail" pointer.

@jukkar
Copy link
Member Author

jukkar commented Nov 8, 2023

As in REST APIs? It would be a different kind of handler with a different "detail" pointer.

Yes, I was thinking that too and did some experiments. Will send changes after get something working.

@jukkar
Copy link
Member Author

jukkar commented Nov 9, 2023

Updated according to comments.

@jukkar
Copy link
Member Author

jukkar commented Apr 17, 2024

  • Minor refactoring done, Robert split the http_server.c into three parts, one for core functionality, one for HTTP/1 processing and one for HTTP/2
  • HTTP/1 header parsing is handled properly even if header is received in smaller pieces

@rlubos rlubos force-pushed the http-server-support branch 3 times, most recently from 394f1b2 to 7f78ce7 Compare April 26, 2024 11:03
@jukkar
Copy link
Member Author

jukkar commented Apr 26, 2024

@cfriedt @pdgendt @nxpadamm @MeisterBob and all the others in the review list.
Robert has done great job of fixing and implementing remaining known issues, also the commits are now squashed as the history was too messy again, so we believe the PR is ready for a merge. Please take a look at the code and if possible try it yourself and give feedback.

I have the original branch (with 75 commits) as a backup if we need it for anything.

@rlubos
Copy link
Contributor

rlubos commented Apr 26, 2024

fixing and implementing remaining known issues

Just to be clear - not all remaining/known issues, but the implementation is quite functional already and the PR is already pretty large. Further improvements/fixes can/will be submitted in separate PRs.

include/zephyr/net/http/service.h Outdated Show resolved Hide resolved
samples/net/sockets/http_server/CMakeLists.txt Outdated Show resolved Hide resolved
subsys/net/lib/http/Kconfig Outdated Show resolved Hide resolved
subsys/net/lib/CMakeLists.txt Outdated Show resolved Hide resolved
subsys/net/lib/http/http_server_core.c Outdated Show resolved Hide resolved
@cfriedt
Copy link
Member

cfriedt commented Apr 26, 2024

@Emna-Rekik 🪁🎉🥳

jukkar and others added 5 commits April 29, 2024 09:52
Have separate macros to setup a HTTPS service.

Signed-off-by: Jukka Rissanen <[email protected]>
Add HTTP/2 helper libraries to encode and decode HPACK encoded headers,
according to RFC7541.

HPACK string encoding requires to support certain set of Huffman codes,
therefore implement Huffman encoder/decoder as well.

Signed-off-by: Robert Lubos <[email protected]>
Original code developed as a GSoC 2023 project by Emna Rekik.

Code refactored in order to provide better bisectability
as the origical commits were not bisectable.

The server supports static and dynamic resources, managed by
HTTP_SERVICE/HTTP_RESOURCE macros.

Fixes zephyrproject-rtos#59685
Fixes zephyrproject-rtos#59686
Fixes zephyrproject-rtos#59688
Fixes zephyrproject-rtos#59690
Fixes zephyrproject-rtos#59670
Fixes zephyrproject-rtos#59700
Fixes zephyrproject-rtos#59684
Fixes zephyrproject-rtos#59693
Fixes zephyrproject-rtos#59693
Fixes zephyrproject-rtos#59694
Fixes zephyrproject-rtos#59699
Fixes zephyrproject-rtos#59696
Fixes zephyrproject-rtos#59688
Fixes zephyrproject-rtos#59690
Fixes zephyrproject-rtos#59670
Fixes zephyrproject-rtos#59700
Fixes zephyrproject-rtos#59685
Fixes zephyrproject-rtos#59686
Fixes zephyrproject-rtos#59688
Fixes zephyrproject-rtos#59691

Signed-off-by: Emna Rekik <[email protected]>
Signed-off-by: Jukka Rissanen <[email protected]>
Signed-off-by: Robert Lubos <[email protected]>
Tests for HTTP server support.

Signed-off-by: Emna Rekik <[email protected]>
Signed-off-by: Jukka Rissanen <[email protected]>
Signed-off-by: Robert Lubos <[email protected]>
A simple HTTP server sample application.

Signed-off-by: Emna Rekik <[email protected]>
Signed-off-by: Jukka Rissanen <[email protected]>
Signed-off-by: Robert Lubos <[email protected]>
@jukkar
Copy link
Member Author

jukkar commented Apr 29, 2024

  • updated according to comments
  • fixed sample readme file as it was describing things that were not correct any more

@jukkar
Copy link
Member Author

jukkar commented Apr 30, 2024

Related to discussion in #70817, @fabiobaltieri would you mind force merging this, there are binary certificate files that cause CI to fail.

@fabiobaltieri
Copy link
Member

Thanks @jukkar I don't have permissions to do it myself though, @carlescufi can you take care of this one?

@carlescufi
Copy link
Member

@jukkar shouldn't we wait for @rlubos' approval here?

@jukkar
Copy link
Member Author

jukkar commented Apr 30, 2024

@jukkar shouldn't we wait for @rlubos' approval here?

Well, Robert is one of the authors of these patches so I am not sure if that would change much, but if you want we can wait for that.

@carlescufi
Copy link
Member

@jukkar shouldn't we wait for @rlubos' approval here?

Well, Robert is one of the authors of these patches so I am not sure if that would change much, but if you want we can wait for that.

Right, you are right. No need to wait in that case, I will merge it.

@carlescufi carlescufi merged commit 660149d into zephyrproject-rtos:main Apr 30, 2024
24 of 25 checks passed
@jukkar jukkar deleted the http-server-support branch April 30, 2024 11:55
@kartben
Copy link
Collaborator

kartben commented May 3, 2024

@rlubos @jukkar As a follow-up to this PR, could you please look at adding minimal docs for this new feature?
Right now it doesn't even have Doxygen groups for the new headers, nor does it have any entry in the documentation (could be just an empty "placeholder" for listing the Doxygen group(s) under an API Reference section). Bonus points for using zephyr:code-sample:: in the code sample so that it cross-references the API. Thank you!

@jukkar
Copy link
Member Author

jukkar commented May 3, 2024

As a follow-up to this PR, could you please look at adding minimal docs for this new feature?

Yes, that is in the todo list, we will address this before 3.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.