-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhttp_worker.cpp
More file actions
90 lines (77 loc) · 2.03 KB
/
http_worker.cpp
File metadata and controls
90 lines (77 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/http
//
#include <boost/http/server/http_worker.hpp>
#include <boost/http/error.hpp>
#include <boost/url/parse.hpp>
#include <iostream>
namespace boost {
namespace http {
capy::task<void>
http_worker::
do_http_session()
{
struct guard
{
http_worker& self;
guard(http_worker& self_)
: self(self_)
{
}
~guard()
{
self.parser.reset();
self.parser.start();
self.rp.session_data.clear();
}
};
guard g(*this); // clear things when session ends
// read request, send response loop
for(;;)
{
parser.reset();
parser.start();
rp.session_data.clear();
// Read HTTP request header
auto [ec] = co_await parser.read_header(stream);
if(ec)
{
std::cerr << "read_header error: " << ec.message() << "\n";
break;
}
// Process headers and dispatch
// Set up Request and Response objects
rp.req = parser.get();
rp.route_data.clear();
rp.res.set_start_line(
http::status::ok, rp.req.version());
rp.res.set_keep_alive(rp.req.keep_alive());
serializer.reset();
// Parse the URL
{
auto rv = urls::parse_uri_reference(rp.req.target());
if(rv.has_error())
{
rp.status(http::status::bad_request);
}
rp.url = rv.value();
}
{
auto rv = co_await fr.dispatch(rp.req.method(), rp.url, rp);
if(rv.failed())
{
// VFALCO log rv.error()
break;
}
if(! rp.res.keep_alive())
break;
}
}
}
} // http
} // boost