src/http_worker.cpp

0.0% Lines (0/17) 0.0% List of functions (0/4) 0.0% Branches (0/3)
http_worker.cpp
f(x) Functions (4)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/beast2
8 //
9
10 #include <boost/beast2/http_worker.hpp>
11 #include <boost/http/error.hpp>
12 #include <boost/url/parse.hpp>
13 #include <iostream>
14
15 namespace boost {
16 namespace beast2 {
17
18 ✗ http_worker::
19 http_worker(
20 http::router<http::route_params> fr_,
21 http::shared_parser_config parser_cfg,
22 ✗ http::shared_serializer_config serializer_cfg)
23 ✗ : fr(std::move(fr_))
24 ✗ , parser(parser_cfg)
25 ✗ , serializer(serializer_cfg)
26 {
27 ✗ serializer.set_message(rp.res);
28 ✗ }
29
30 capy::task<void>
31 ✗ http_worker::
32 do_http_session()
33 {
34 struct guard
35 {
36 http_worker& self;
37
38 ✗ guard(http_worker& self_)
39 ✗ : self(self_)
40 {
41 ✗ }
42
43 ✗ ~guard()
44 {
45 ✗ self.parser.reset();
46 ✗ self.parser.start();
47 ✗ self.rp.session_data.clear();
48 ✗ }
49 };
50
51 guard g(*this); // clear things when session ends
52
53 // read request, send response loop
54 for(;;)
55 {
56 parser.reset();
57 parser.start();
58 rp.session_data.clear();
59
60 // Read HTTP request header
61 auto [ec] = co_await parser.read_header(stream);
62 if(ec)
63 {
64 std::cerr << "read_header error: " << ec.message() << "\n";
65 break;
66 }
67
68 // Process headers and dispatch
69 // Set up Request and Response objects
70 rp.req = parser.get();
71 rp.route_data.clear();
72 rp.res.set_start_line(
73 http::status::ok, rp.req.version());
74 rp.res.set_keep_alive(rp.req.keep_alive());
75 serializer.reset();
76
77 // Parse the URL
78 {
79 auto rv = urls::parse_uri_reference(rp.req.target());
80 if(rv.has_error())
81 {
82 rp.status(http::status::bad_request);
83 }
84 rp.url = rv.value();
85 }
86
87 {
88 auto rv = co_await fr.dispatch(rp.req.method(), rp.url, rp);
89 if(rv.failed())
90 {
91 // VFALCO log rv.error()
92 break;
93 }
94
95 if(! rp.res.keep_alive())
96 break;
97 }
98 }
99 ✗ }
100
101 } // beast2
102 } // boost
103