Skip to content

Commit 26e2a87

Browse files
committed
Tidy up file placement
1 parent 592e948 commit 26e2a87

File tree

6 files changed

+174
-49
lines changed

6 files changed

+174
-49
lines changed

include/boost/http/server/router.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#include <boost/http/detail/config.hpp>
1414
#include <boost/http/server/route_handler.hpp>
1515
#include <boost/http/server/detail/router_base.hpp>
16+
#include <boost/http/field.hpp>
1617
#include <boost/http/method.hpp>
18+
#include <boost/http/status.hpp>
1719
#include <boost/url/url_view.hpp>
1820
#include <boost/mp11/algorithm.hpp>
1921
#include <boost/assert.hpp>
@@ -598,6 +600,13 @@ class router : public detail::router_base
598600
router_options options = {})
599601
: detail::router_base(options.v_)
600602
{
603+
set_options_handler(
604+
[](P& rp, std::string_view allow) -> route_task {
605+
rp.status(status::no_content);
606+
rp.res.set(field::allow, allow);
607+
(void)(co_await rp.send());
608+
co_return route_done;
609+
});
601610
}
602611

603612
/** Construct a router from another router with compatible types.
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
// Official repository: https://github.com/cppalliance/http
88
//
99

10-
#include "src/server/detail/any_router.hpp"
10+
#include "src/server/detail/router_base.hpp"
1111
#include <boost/http/server/detail/router_base.hpp>
1212
#include <boost/http/detail/except.hpp>
1313
#include <boost/http/error.hpp>
14+
#include <boost/http/field.hpp>
15+
#include <boost/http/status.hpp>
1416
#include <boost/url/grammar/ci_string.hpp>
1517
#include <boost/url/grammar/hexdig_chars.hpp>
1618
#include "src/server/detail/pct_decode.hpp"
@@ -206,8 +208,8 @@ dispatch_loop(route_params& p, bool is_options) const
206208
std::size_t last_matched = SIZE_MAX;
207209
std::uint32_t current_depth = 0;
208210

209-
std::uint64_t options_methods = 0;
210-
std::vector<std::string> options_custom_verbs;
211+
std::uint64_t matched_methods = 0;
212+
std::vector<std::string> matched_custom_verbs;
211213

212214
std::size_t path_stack[router_base::max_path_depth];
213215
path_stack[0] = 0;
@@ -286,12 +288,12 @@ dispatch_loop(route_params& p, bool is_options) const
286288
if(!ancestors_ok)
287289
continue;
288290

289-
// Collect methods from matching end-route matchers for OPTIONS
290-
if(is_options && m.end_)
291+
// Collect methods from matching end-route matchers
292+
if(m.end_)
291293
{
292-
options_methods |= m.allowed_methods_;
294+
matched_methods |= m.allowed_methods_;
293295
for(auto const& v : m.custom_verbs_)
294-
options_custom_verbs.push_back(v);
296+
matched_custom_verbs.push_back(v);
295297
}
296298

297299
if(m.end_ && !e.match_method(
@@ -364,12 +366,23 @@ dispatch_loop(route_params& p, bool is_options) const
364366
co_return route_error(pv.ec_);
365367

366368
// OPTIONS fallback
367-
if(is_options && options_methods != 0 && options_handler_)
369+
if(is_options && matched_methods != 0 && options_handler_)
368370
{
369-
std::string allow = build_allow_header(options_methods, options_custom_verbs);
371+
std::string allow = build_allow_header(matched_methods, matched_custom_verbs);
370372
co_return co_await options_handler_->invoke(p, allow);
371373
}
372374

375+
// 405 fallback: path matched but method didn't
376+
if(!is_options &&
377+
(matched_methods != 0 || !matched_custom_verbs.empty()))
378+
{
379+
std::string allow = build_allow_header(matched_methods, matched_custom_verbs);
380+
p.res.set(field::allow, allow);
381+
p.res.set_status(status::method_not_allowed);
382+
(void)(co_await p.send());
383+
co_return route_done;
384+
}
385+
373386
co_return route_next;
374387
}
375388

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// Official repository: https://github.com/cppalliance/http
88
//
99

10-
#ifndef BOOST_HTTP_SRC_SERVER_DETAIL_ANY_ROUTER_HPP
11-
#define BOOST_HTTP_SRC_SERVER_DETAIL_ANY_ROUTER_HPP
10+
#ifndef BOOST_HTTP_SRC_SERVER_DETAIL_ROUTER_BASE_HPP
11+
#define BOOST_HTTP_SRC_SERVER_DETAIL_ROUTER_BASE_HPP
1212

1313
#include <boost/http/server/detail/router_base.hpp>
1414
#include <boost/http/detail/except.hpp>

src/server/http_worker.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,20 @@ do_http_session()
7474

7575
{
7676
auto rv = co_await fr.dispatch(rp.req.method(), rp.url, rp);
77+
78+
if(rv.what() == route_what::close)
79+
break;
80+
81+
if(rv.what() == route_what::next)
82+
{
83+
rp.status(http::status::not_found);
84+
(void)(co_await rp.send());
85+
}
86+
7787
if(rv.failed())
7888
{
79-
// VFALCO log rv.error()
80-
break;
89+
rp.status(http::status::internal_server_error);
90+
(void)(co_await rp.send());
8191
}
8292

8393
if(! rp.res.keep_alive())

0 commit comments

Comments
 (0)