-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrouter_types.cpp
More file actions
208 lines (179 loc) · 4.72 KB
/
router_types.cpp
File metadata and controls
208 lines (179 loc) · 4.72 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// Copyright (c) 2025 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/route_handler.hpp>
#include <boost/http/server/etag.hpp>
#include <boost/http/server/fresh.hpp>
#include <boost/http/detail/except.hpp>
#include <boost/url/grammar/ci_string.hpp>
#include <boost/capy/buffers/make_buffer.hpp>
#include <boost/assert.hpp>
#include <cstring>
namespace boost {
namespace system {
template<>
struct is_error_code_enum<
::boost::http::route_what>
{
static bool const value = true;
};
} // system
} // boost
namespace boost {
namespace http {
namespace {
struct route_what_cat_type
: system::error_category
{
constexpr route_what_cat_type()
: error_category(0x7a8b3c4d5e6f1029)
{
}
const char* name() const noexcept override
{
return "boost.http.route_what";
}
std::string message(int code) const override
{
return message(code, nullptr, 0);
}
char const* message(
int code,
char*,
std::size_t) const noexcept override
{
switch(static_cast<route_what>(code))
{
case route_what::done: return "done";
case route_what::error: return "error";
case route_what::next: return "next";
case route_what::next_route: return "next_route";
case route_what::close: return "close";
default:
return "?";
}
}
};
route_what_cat_type route_what_cat;
} // (anon)
system::error_code
make_error_code(
route_what w) noexcept
{
return system::error_code{
static_cast<int>(w), route_what_cat};
}
//----------------------------------------------------------
route_result::
route_result(
system::error_code ec)
: ec_(ec)
{
if(! ec.failed())
detail::throw_invalid_argument();
}
void
route_result::
set(route_what w)
{
ec_ = make_error_code(w);
}
auto
route_result::
what() const noexcept ->
route_what
{
if(! ec_.failed())
return route_what::done;
if(&ec_.category() != &route_what_cat)
return route_what::error;
if( ec_ == route_what::next)
return route_what::next;
if( ec_ == route_what::next_route)
return route_what::next_route;
//if( ec_ == route_what::close)
return route_what::close;
}
auto
route_result::
error() const noexcept ->
system::error_code
{
if(&ec_.category() != &route_what_cat)
return ec_;
return {};
}
//------------------------------------------------
bool
route_params::
is_method(
core::string_view s) const noexcept
{
auto m = http::string_to_method(s);
if(m != http::method::unknown)
return priv_.verb_ == m;
return s == priv_.verb_str_;
}
//------------------------------------------------
capy::io_task<>
route_params::
send(std::string_view body)
{
auto const sc = res.status();
// 204 No Content / 304 Not Modified: strip headers, no body
if(sc == status::no_content ||
sc == status::not_modified)
{
res.erase(field::content_type);
res.erase(field::content_length);
res.erase(field::transfer_encoding);
co_return co_await res_body.write_eof();
}
// 205 Reset Content: Content-Length=0, no body
if(sc == status::reset_content)
{
res.erase(field::transfer_encoding);
res.set_payload_size(0);
co_return co_await res_body.write_eof();
}
// Set Content-Type if not already set
if(! res.exists(field::content_type))
{
if(! body.empty() && body[0] == '<')
res.set(field::content_type,
"text/html; charset=utf-8");
else
res.set(field::content_type,
"text/plain; charset=utf-8");
}
// Generate ETag if not already set
if(! res.exists(field::etag))
res.set(field::etag, etag(body));
// Set Content-Length if not already set
if(! res.exists(field::content_length))
res.set_payload_size(body.size());
// Freshness check: auto-304 for conditional GET
if(is_fresh(req, res))
{
res.set_status(status::not_modified);
res.erase(field::content_type);
res.erase(field::content_length);
res.erase(field::transfer_encoding);
co_return co_await res_body.write_eof();
}
// HEAD: send headers only, skip body
if(req.method() == method::head)
{
co_return co_await res_body.write_eof();
}
auto [ec, n] = co_await res_body.write_eof(
capy::make_buffer(body));
co_return {ec};
}
} // http
} // boost