Skip to content

Refactor HttpHandlers to use fewer arguments and to stream responses #10491

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

Closed
wants to merge 8 commits into from

Conversation

jschmidt-icinga
Copy link
Contributor

@jschmidt-icinga jschmidt-icinga commented Jun 27, 2025

This PR is a work in progress. It fixes #10142 and is a step towards #10409.

This is based on the improvements to @yhabteab's improvements to the JSON encoder in #10414.

Description

  • Added a new class HttpRequest that incorporates all of the derived data, like the authenticated user, url and parameters, that the handlers need, so the don't have to be passed as individual arguments, as per the suggestion in Simplify HttpHandler::HandleRequest() signature by grouping arguments into structs #10142.
  • Added the new class HttpResponse, supported by a new custom message body type that makes it easy to use the boost::beast::http::serializer to start sending some responses with chunked encoding, before the complete data and content_length information is available.
  • These classes derive from the respective boost::beast:message types and are usable in functions that require these types.
  • In regard to the HttpHandler interface parameters, this leaves only the WaitGroup, the request and response objects and the yield_context being passed to the handlers.
  • To support seamless disconnect in EventsHandler but without adding a solution specific to it (like StartStreaming did previously), I've added a third coroutine to HttpServerConnection that waits for the stream to be shut down by the remote side and then initiates a Disconnect().
  • I added unit-tests for HttpServerConnection, HttpRequest and HttpResponse. These add lots of functionality that can be used to extend unit-tests to other components (such as the individual HTTP handlers, JsonRpcConnection, etc.) in the future.

Todo

  • The EventsHandler currently still blocks formally disconnecting the connection until the next event is returned by the EventsInbox. Before this PR, that was handled in HttpServerConnection::StartStreaming() by trying to read from the connection in another coroutine and calling HttpServerConnection::Disconnect() when the connection is closed. The handler was still sitting there, but it could simply return when it got the next event while the connection was already disconnected.
  • The signalling between the coroutines needs some more work. Possibly this can be done more implicitly. Specifically I want to get rid of HttpResponse::Begin() and HttpResponse::Done().
  • Get distros using boost-1.66 (opensuse/leap, rockylinux:8) to build. 1.66 was the first version boost::beast was introduced and there are some pesky differences to the interface of the message body implementation needs to satisfy.
  • Look into adding test-cases for Http(Request|Response) and possibly HttpServerConnection (minimal version without handlers)
  • Look at more handlers and see if they can benefit from using these new features.
  • Some other smaller things (cleaning up commits, improving documentation, etc.)

@cla-bot cla-bot bot added the cla/signed label Jun 27, 2025
@jschmidt-icinga jschmidt-icinga changed the title Refactor HttpHandlers to use less arguments and stream responses Refactor HttpHandlers to use fewer arguments and to stream responses Jun 27, 2025
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch 2 times, most recently from 20f44e0 to 9d518af Compare June 30, 2025 08:49
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch 2 times, most recently from b01c538 to 04dc8f2 Compare June 30, 2025 13:52
@jschmidt-icinga jschmidt-icinga changed the base branch from master to support-json-serialization-into-ostream July 1, 2025 08:21
@yhabteab yhabteab force-pushed the support-json-serialization-into-ostream branch 2 times, most recently from 24d6d3e to cb42cdd Compare July 1, 2025 09:20
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch 2 times, most recently from 7a55570 to d64106c Compare July 1, 2025 14:38
@yhabteab yhabteab force-pushed the support-json-serialization-into-ostream branch from c3bc75b to 46951a9 Compare July 1, 2025 14:41
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch from d64106c to 883ea0e Compare July 1, 2025 14:48
@yhabteab yhabteab force-pushed the support-json-serialization-into-ostream branch from 46951a9 to 43a9980 Compare July 1, 2025 14:55
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch from 883ea0e to 3098345 Compare July 1, 2025 14:59
@yhabteab yhabteab force-pushed the support-json-serialization-into-ostream branch from 43a9980 to 251e43a Compare July 1, 2025 15:25
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch 3 times, most recently from 0d7b984 to fa99c02 Compare July 2, 2025 09:59
@yhabteab yhabteab force-pushed the support-json-serialization-into-ostream branch 2 times, most recently from b52e873 to ace70d8 Compare July 2, 2025 15:32
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch 4 times, most recently from a1dc496 to 5d731a3 Compare July 3, 2025 07:01
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch 7 times, most recently from efd0d0b to 10ac1b0 Compare July 16, 2025 12:57
Copy link
Member

@Al2Klimov Al2Klimov left a comment

Choose a reason for hiding this comment

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

In addition to #10491 (review):

@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch 8 times, most recently from 0fdced2 to 3c260a5 Compare July 21, 2025 10:26
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch from 3c260a5 to 151d8a8 Compare July 21, 2025 12:45
@jschmidt-icinga jschmidt-icinga force-pushed the http-handlers-stream-refactor branch from 151d8a8 to a0d56ed Compare July 22, 2025 14:36
@jschmidt-icinga
Copy link
Contributor Author

I'm addressing the remaining comments and then I'm going to close this draft PR and open an new one for proper review, mostly so we don't have to scroll past 100 force pushes and resolved conversions.

@yhabteab yhabteab deleted the http-handlers-stream-refactor branch July 23, 2025 07:56
@yhabteab yhabteab restored the http-handlers-stream-refactor branch July 23, 2025 07:56
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.

Simplify HttpHandler::HandleRequest() signature by grouping arguments into structs
4 participants