forked from cppalliance/http
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute_handler.cpp
More file actions
87 lines (73 loc) · 1.81 KB
/
route_handler.cpp
File metadata and controls
87 lines (73 loc) · 1.81 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
//
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot 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_proto
//
#include <boost/http_proto/server/route_handler.hpp>
#include <boost/http_proto/string_body.hpp>
namespace boost {
namespace http_proto {
route_params::
~route_params()
{
}
route_params&
route_params::
status(
http_proto::status code)
{
res.set_start_line(code, res.version());
return *this;
}
route_params&
route_params::
set_body(std::string s)
{
if(! res.exists(http_proto::field::content_type))
{
// VFALCO TODO detect Content-Type
res.set(http_proto::field::content_type,
"text/plain; charset=UTF-8");
}
if(! res.exists(field::content_length))
{
res.set_payload_size(s.size());
}
serializer.start(res,
http_proto::string_body(std::string(s)));
return *this;
}
#ifdef BOOST_HTTP_PROTO_HAS_CORO
auto
route_params::
spawn(
capy::task<route_result> t) ->
route_result
{
return this->suspend(
[ex = this->ex, t = std::move(t)](resumer resume) mutable
{
auto h = t.release();
h.promise().on_done = [resume, h]()
{
auto& r = h.promise().result;
if(r.index() == 2)
{
auto ep = std::get<2>(r);
h.destroy();
resume(ep);
return;
}
auto rv = std::move(std::get<1>(r));
h.destroy();
resume(rv);
};
ex.post([h]() { h.resume(); });
});
}
#endif
} // http_proto
} // boost