-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathserve_static.cpp
More file actions
336 lines (293 loc) · 8.04 KB
/
serve_static.cpp
File metadata and controls
336 lines (293 loc) · 8.04 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//
// 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/serve_static.hpp>
#include <boost/http/server/send_file.hpp>
#include <boost/http/field.hpp>
#include <boost/http/file.hpp>
#include <boost/http/status.hpp>
#include <filesystem>
#include <string>
namespace boost {
namespace http {
namespace {
// Append an HTTP rel-path to a local filesystem path.
void
path_cat(
std::string& result,
core::string_view prefix,
core::string_view suffix)
{
result = prefix;
#ifdef BOOST_MSVC
char constexpr path_separator = '\\';
#else
char constexpr path_separator = '/';
#endif
if(! result.empty() && result.back() == path_separator)
result.resize(result.size() - 1);
#ifdef BOOST_MSVC
for(auto& c : result)
if(c == '/')
c = path_separator;
#endif
for(auto const& c : suffix)
{
if(c == '/')
result.push_back(path_separator);
else
result.push_back(c);
}
}
// Check if path segment is a dotfile
bool
is_dotfile(core::string_view path) noexcept
{
auto pos = path.rfind('/');
if(pos == core::string_view::npos)
pos = 0;
else
++pos;
if(pos < path.size() && path[pos] == '.')
return true;
return false;
}
} // (anon)
struct serve_static::impl
{
std::string root;
serve_static_options opts;
impl(
core::string_view root_,
serve_static_options const& opts_)
: root(root_)
, opts(opts_)
{
}
};
serve_static::
~serve_static()
{
delete impl_;
}
serve_static::
serve_static(core::string_view root)
: serve_static(root, serve_static_options{})
{
}
serve_static::
serve_static(
core::string_view root,
serve_static_options const& opts)
: impl_(new impl(root, opts))
{
}
serve_static::
serve_static(serve_static&& other) noexcept
: impl_(other.impl_)
{
other.impl_ = nullptr;
}
route_task
serve_static::
operator()(route_params& rp) const
{
// Only handle GET and HEAD
if(rp.req.method() != method::get &&
rp.req.method() != method::head)
{
if(impl_->opts.fallthrough)
co_return route_next;
rp.res.set_status(status::method_not_allowed);
rp.res.set(field::allow, "GET, HEAD");
auto [ec] = co_await rp.send();
if(ec)
co_return route_error(ec);
co_return route_done;
}
// Get the request path
auto req_path = rp.url.path();
// Check for dotfiles
if(is_dotfile(req_path))
{
switch(impl_->opts.dotfiles)
{
case dotfiles_policy::deny:
{
rp.res.set_status(status::forbidden);
auto [ec] = co_await rp.send("Forbidden");
if(ec)
co_return route_error(ec);
co_return route_done;
}
case dotfiles_policy::ignore:
{
if(impl_->opts.fallthrough)
co_return route_next;
rp.res.set_status(status::not_found);
auto [ec] = co_await rp.send("Not Found");
if(ec)
co_return route_error(ec);
co_return route_done;
}
case dotfiles_policy::allow:
break;
}
}
// Build the file path
std::string path;
path_cat(path, impl_->root, req_path);
// Check if it's a directory
system::error_code fec;
bool is_dir = std::filesystem::is_directory(path, fec);
if(is_dir && ! fec.failed())
{
// Check for trailing slash
if(req_path.empty() || req_path.back() != '/')
{
if(impl_->opts.redirect)
{
// Redirect to add trailing slash
std::string location(req_path);
location += '/';
rp.res.set_status(status::moved_permanently);
rp.res.set(field::location, location);
auto [ec] = co_await rp.send("");
if(ec)
co_return route_error(ec);
co_return route_done;
}
}
// Try index file
if(impl_->opts.index)
{
#ifdef BOOST_MSVC
path += "\\index.html";
#else
path += "/index.html";
#endif
}
}
// Prepare file response using send_file utilities
send_file_options opts;
opts.etag = impl_->opts.etag;
opts.last_modified = impl_->opts.last_modified;
opts.max_age = impl_->opts.max_age;
send_file_info info;
send_file_init(info, rp, path, opts);
// Handle result
switch(info.result)
{
case send_file_result::not_found:
{
if(impl_->opts.fallthrough)
co_return route_next;
rp.res.set_status(status::not_found);
auto [ec] = co_await rp.send("Not Found");
if(ec)
co_return route_error(ec);
co_return route_done;
}
case send_file_result::not_modified:
{
rp.res.set_status(status::not_modified);
auto [ec] = co_await rp.send("");
if(ec)
co_return route_error(ec);
co_return route_done;
}
case send_file_result::error:
{
// Range error - headers already set by send_file_init
auto [ec] = co_await rp.send("");
if(ec)
co_return route_error(ec);
co_return route_done;
}
case send_file_result::ok:
break;
}
// Set Accept-Ranges if enabled
if(impl_->opts.accept_ranges)
rp.res.set(field::accept_ranges, "bytes");
// Set Cache-Control with immutable if configured
if(impl_->opts.immutable && opts.max_age > 0)
{
std::string cc = "public, max-age=" +
std::to_string(opts.max_age) + ", immutable";
rp.res.set(field::cache_control, cc);
}
// For HEAD requests, don't send body
if(rp.req.method() == method::head)
{
auto [ec] = co_await rp.send("");
if(ec)
co_return route_error(ec);
co_return route_done;
}
// Open and stream the file
file f;
system::error_code ec;
f.open(path.c_str(), file_mode::scan, ec);
if(ec)
{
if(impl_->opts.fallthrough)
co_return route_next;
rp.res.set_status(status::internal_server_error);
auto [ec2] = co_await rp.send("Internal Server Error");
if(ec2)
co_return route_error(ec2);
co_return route_done;
}
// Seek to range start if needed
if(info.is_range && info.range_start > 0)
{
f.seek(static_cast<std::uint64_t>(info.range_start), ec);
if(ec.failed())
{
rp.res.set_status(status::internal_server_error);
auto [ec2] = co_await rp.send("Internal Server Error");
if(ec2)
co_return route_error(ec2);
co_return route_done;
}
}
// Calculate how much to send
std::int64_t remaining = info.range_end - info.range_start + 1;
// Stream file content using serializer's internal buffer
while(remaining > 0)
{
capy::mutable_buffer arr[1];
auto bufs = rp.res_body.prepare(arr);
if(bufs.empty())
{
auto [ec2] = co_await rp.res_body.commit(0);
if(ec2)
co_return route_error(ec2);
continue;
}
auto const to_read = static_cast<std::size_t>(
(std::min)(remaining,
static_cast<std::int64_t>(bufs[0].size())));
auto const n1 = f.read(bufs[0].data(), to_read, ec);
if(ec.failed())
co_return route_error(ec);
if(n1 == 0)
break;
auto [ec2] = co_await rp.res_body.commit(n1);
if(ec2)
co_return route_error(ec2);
remaining -= static_cast<std::int64_t>(n1);
}
auto [ec3] = co_await rp.res_body.commit_eof(0);
if(ec3)
co_return route_error(ec3);
co_return route_done;
}
} // http
} // boost