mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-18 11:09:39 +00:00
Add Server::CustomRoute() for HTTP methods outside the built-in set (#2553)
* Add Server::CustomRoute() for HTTP methods outside the built-in set parse_request_line validates the request method against a fixed whitelist and rejects anything else with 400 before routing runs. That blocks WebDAV, where PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK and UNLOCK are ordinary methods defined by RFC 4918, and it blocks extension methods such as UPnP's SUBSCRIBE. The need has been open since #847. Registering a handler is now what makes the server accept a method: svr.CustomRoute("PROPFIND", "/dav/:id", handler); Because custom methods go through the normal dispatch path, patterns work the way they do for Get() and friends, and the request body is available in req.body. Serving these methods through set_pre_routing_handler was never enough: the body has not been read at that point, so PROPPATCH and LOCK, which require one, could not be implemented at all. A HandlerWithContentReader overload is available too. The content reader gate in routing() also fires when a custom method carries no body, matching what expect_content() does unconditionally for POST/PUT/PATCH/DELETE, so a body-less PROPFIND (RFC 4918 treats one as allprop) reaches its handler instead of falling through to 404. Method names are validated as RFC 9110 tokens, and the ten built-in methods are refused. Seven of them are dispatched by the if/else chain in routing() before the custom tables are consulted, so a route registered for one could never fire; CONNECT, TRACE and PRI carry protocol-level meaning this library does not route. A refused registration makes is_valid() return false, so listen() fails rather than starting a server holding a handler that would never run. This is also why SSLServer::is_valid() now chains to Server::is_valid() instead of only checking ctx_. Servers that never call CustomRoute() keep the previous per-request cost: the built-in method set is checked first and short-circuits, and the custom lookup returns early on an empty map. * Add cookbook recipe for custom HTTP methods The CustomRoute() docs were a section inside S01, which pushed that page to 90 lines, the longest in the cookbook, and mixed a separate feature into a page about registering GET/POST/PUT/DELETE handlers. Move the section into its own recipe and give it room for the part that was missing: the OPTIONS handler returning DAV: and Allow, which WebDAV clients probe for before anything else. S01 goes back to 68 lines and keeps a pointer to the new page. The recipe is titled after the API rather than after WebDAV, and says outright that generating the 207 Multi-Status XML, interpreting Depth and managing locks are the reader's job. Routing the method is all the library does. S23 takes order 42, so the TLS, SSE and WebSocket recipes shift to 43-57. That only moves the sort key. Filenames, the T01/E01/W01 labels, the published URLs and every cross-reference are untouched.
This commit is contained in:
1 parent
af75a4160f
commit
254e576b50
39 files changed
+588
-39
No files matched your search
@@ -2107,6 +2107,17 @@ public:
|
||||
Server &Delete(const std::string &pattern, HandlerWithContentReader handler);
|
||||
Server &Options(const std::string &pattern, Handler handler);
|
||||
|
||||
// Register a handler for an HTTP method outside the built-in set (e.g. the
|
||||
// WebDAV methods from RFC 4918). Registering a method here is what makes the
|
||||
// server accept it; an unregistered method is still rejected with 400.
|
||||
// `method` must be a valid HTTP method token and must not be one of the
|
||||
// built-in methods, which have their own registration functions above. A
|
||||
// rejected registration makes is_valid() return false, so listen() fails.
|
||||
Server &CustomRoute(const std::string &method, const std::string &pattern,
|
||||
Handler handler);
|
||||
Server &CustomRoute(const std::string &method, const std::string &pattern,
|
||||
HandlerWithContentReader handler);
|
||||
|
||||
Server &WebSocket(const std::string &pattern, WebSocketHandler handler);
|
||||
Server &WebSocket(const std::string &pattern, WebSocketHandler handler,
|
||||
SubProtocolSelector sub_protocol_selector);
|
||||
@@ -2226,9 +2237,21 @@ private:
|
||||
std::vector<std::pair<std::unique_ptr<detail::MatcherBase>,
|
||||
HandlerWithContentReader>>;
|
||||
|
||||
// Both handler tables for one custom method live in a single entry, so that
|
||||
// routing() needs only one map lookup per request to reach either of them.
|
||||
struct CustomHandlerEntry {
|
||||
Handlers handlers;
|
||||
HandlersForContentReader handlers_for_content_reader;
|
||||
};
|
||||
using CustomHandlers = std::map<std::string, CustomHandlerEntry>;
|
||||
|
||||
static std::unique_ptr<detail::MatcherBase>
|
||||
make_matcher(const std::string &pattern);
|
||||
|
||||
static const std::set<std::string> &builtin_methods();
|
||||
CustomHandlerEntry *custom_entry_for_registration(const std::string &method);
|
||||
const CustomHandlerEntry *find_custom_entry(const std::string &method) const;
|
||||
|
||||
template <typename H>
|
||||
Server &add_handler(
|
||||
std::vector<std::pair<std::unique_ptr<detail::MatcherBase>, H>> &handlers,
|
||||
@@ -2292,6 +2315,10 @@ private:
|
||||
std::atomic<bool> is_running_{false};
|
||||
std::atomic<bool> is_decommissioned{false};
|
||||
|
||||
// Set when CustomRoute() refuses a registration. Written before listen(),
|
||||
// read by is_valid() on the same thread, so it needs no synchronization.
|
||||
bool has_invalid_registration_ = false;
|
||||
|
||||
struct MountPointEntry {
|
||||
std::string mount_point;
|
||||
std::string base_dir;
|
||||
@@ -2313,6 +2340,7 @@ private:
|
||||
Handlers delete_handlers_;
|
||||
HandlersForContentReader delete_handlers_for_content_reader_;
|
||||
Handlers options_handlers_;
|
||||
CustomHandlers custom_handlers_;
|
||||
|
||||
struct WebSocketHandlerEntry {
|
||||
std::unique_ptr<detail::MatcherBase> matcher;
|
||||
@@ -12441,6 +12469,57 @@ inline Server &Server::Options(const std::string &pattern, Handler handler) {
|
||||
return add_handler(options_handlers_, pattern, std::move(handler));
|
||||
}
|
||||
|
||||
inline const std::set<std::string> &Server::builtin_methods() {
|
||||
thread_local const std::set<std::string> methods{
|
||||
"GET", "HEAD", "POST", "PUT", "DELETE",
|
||||
"CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"};
|
||||
return methods;
|
||||
}
|
||||
|
||||
inline Server::CustomHandlerEntry *
|
||||
Server::custom_entry_for_registration(const std::string &method) {
|
||||
// Built-in methods are refused for two different reasons. GET, HEAD, POST,
|
||||
// PUT, DELETE, OPTIONS and PATCH are dispatched by the if/else chain in
|
||||
// routing() before the custom tables are consulted, so a route registered
|
||||
// for one of them could never fire. CONNECT, TRACE and PRI have no branch
|
||||
// there and would be reachable, but they carry protocol-level meaning
|
||||
// (tunnel setup, request echo, the HTTP/2 connection preface) that this
|
||||
// library does not route.
|
||||
if (!detail::fields::is_token(method) || builtin_methods().count(method)) {
|
||||
output_error_log(Error::InvalidHTTPMethod, nullptr);
|
||||
has_invalid_registration_ = true;
|
||||
return nullptr;
|
||||
}
|
||||
return &custom_handlers_[method];
|
||||
}
|
||||
|
||||
inline Server &Server::CustomRoute(const std::string &method,
|
||||
const std::string &pattern,
|
||||
Handler handler) {
|
||||
auto *entry = custom_entry_for_registration(method);
|
||||
if (!entry) { return *this; }
|
||||
return add_handler(entry->handlers, pattern, std::move(handler));
|
||||
}
|
||||
|
||||
inline Server &Server::CustomRoute(const std::string &method,
|
||||
const std::string &pattern,
|
||||
HandlerWithContentReader handler) {
|
||||
auto *entry = custom_entry_for_registration(method);
|
||||
if (!entry) { return *this; }
|
||||
return add_handler(entry->handlers_for_content_reader, pattern,
|
||||
std::move(handler));
|
||||
}
|
||||
|
||||
inline const Server::CustomHandlerEntry *
|
||||
Server::find_custom_entry(const std::string &method) const {
|
||||
// find() alone would be correct here. The empty() check is what keeps the
|
||||
// per-request cost off servers that never call CustomRoute(), which is the
|
||||
// overwhelmingly common case; keep it rather than walking into the tree.
|
||||
if (custom_handlers_.empty()) { return nullptr; }
|
||||
auto it = custom_handlers_.find(method);
|
||||
return it == custom_handlers_.end() ? nullptr : &it->second;
|
||||
}
|
||||
|
||||
inline Server &Server::WebSocket(const std::string &pattern,
|
||||
WebSocketHandler handler) {
|
||||
websocket_handlers_.push_back(
|
||||
@@ -12733,11 +12812,12 @@ inline bool Server::parse_request_line(const char *s, Request &req) const {
|
||||
if (count != 3) { return false; }
|
||||
}
|
||||
|
||||
thread_local const std::set<std::string> methods{
|
||||
"GET", "HEAD", "POST", "PUT", "DELETE",
|
||||
"CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"};
|
||||
// A method outside the built-in set is accepted only when a handler has been
|
||||
// registered for it with CustomRoute().
|
||||
const auto &methods = builtin_methods();
|
||||
|
||||
if (methods.find(req.method) == methods.end()) {
|
||||
if (methods.find(req.method) == methods.end() &&
|
||||
!find_custom_entry(req.method)) {
|
||||
output_error_log(Error::InvalidHTTPMethod, &req);
|
||||
return false;
|
||||
}
|
||||
@@ -13372,7 +13452,14 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (detail::expect_content(req)) {
|
||||
const auto *custom = find_custom_entry(req.method);
|
||||
|
||||
// The second clause mirrors what expect_content() does unconditionally for
|
||||
// POST/PUT/PATCH/DELETE: a content reader route fires even when the request
|
||||
// carries no body. Without it a body-less PROPFIND (RFC 4918 treats one as
|
||||
// `allprop`) would skip its handler and fall through to 404.
|
||||
if (detail::expect_content(req) ||
|
||||
(custom && !custom->handlers_for_content_reader.empty())) {
|
||||
// Content reader handler
|
||||
{
|
||||
// Track whether the ContentReader was aborted due to the decompressed
|
||||
@@ -13419,6 +13506,9 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) {
|
||||
} else if (req.method == "DELETE") {
|
||||
dispatched = dispatch_request_for_content_reader(
|
||||
req, res, std::move(reader), delete_handlers_for_content_reader_);
|
||||
} else if (custom) {
|
||||
dispatched = dispatch_request_for_content_reader(
|
||||
req, res, std::move(reader), custom->handlers_for_content_reader);
|
||||
}
|
||||
|
||||
if (dispatched) {
|
||||
@@ -13455,6 +13545,8 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) {
|
||||
return dispatch_request(req, res, options_handlers_, strm);
|
||||
} else if (req.method == "PATCH") {
|
||||
return dispatch_request(req, res, patch_handlers_, strm);
|
||||
} else if (custom) {
|
||||
return dispatch_request(req, res, custom->handlers, strm);
|
||||
}
|
||||
|
||||
res.status = StatusCode::BadRequest_400;
|
||||
@@ -13972,7 +14064,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline bool Server::is_valid() const { return true; }
|
||||
inline bool Server::is_valid() const { return !has_invalid_registration_; }
|
||||
|
||||
inline bool Server::process_and_close_socket(socket_t sock) {
|
||||
std::string remote_addr;
|
||||
@@ -17313,7 +17405,9 @@ inline SSLServer::~SSLServer() {
|
||||
if (ctx_) { tls::free_context(ctx_); }
|
||||
}
|
||||
|
||||
inline bool SSLServer::is_valid() const { return ctx_ != nullptr; }
|
||||
inline bool SSLServer::is_valid() const {
|
||||
return ctx_ != nullptr && Server::is_valid();
|
||||
}
|
||||
|
||||
inline bool SSLServer::process_and_close_socket(socket_t sock) {
|
||||
using namespace tls;
|
||||
|
||||
Reference in new issue
Block a user