3#include "endpoint_http.hpp"
4#include "type_traits.hpp"
5#include "../../core/http/response.hpp"
6#include "../../core/type_traits.hpp"
12namespace malloy::server
19 matches_resource(
const boost::beast::http::request_header<>& req)
const = 0;
22 template<
typename Response, concepts::request_filter Handler,
bool WantsCapture>
27 template<
typename Derived>
28 using req_gen_t = std::shared_ptr<typename http::connection<Derived>::request_generator>;
30 template<
typename Req>
31 using handler_t = std::conditional_t<
33 std::function<Response(
const Req&,
const std::vector<std::string>&)>,
34 std::function<Response(
const Req&)>
39 std::regex resource_base;
40 handler_t<typename Handler::request_type> handler;
41 std::function<void(
const boost::beast::http::request_header<>&, Response&&,
const http::connection_t&)> writer;
45 matches_resource(
const req_header_t& req)
const override
48 return std::regex_match(req.target().begin(), req.target().end(), resource_base);
53 matches(
const req_header_t& req)
const override
56 if (!matches_resource(req))
66 handle(
const req_t& gens,
const http::connection_t& conn)
const override
70 [
this, conn]<
typename Generator>(Generator && gen) {
71 this->visit_bodies(std::forward<Generator>(gen), conn);
83 handle_req(
const auto& req,
const http::connection_t& conn)
const
85 if constexpr (WantsCapture) {
86 std::smatch url_matches;
87 std::string url{ req.target() };
88 std::regex_match(url, url_matches, resource_base);
90 if (url_matches.empty()) {
91 throw std::logic_error{
92 R
"(endpoint_http_regex passed request which does not match: )" +
93 std::string{req.target()} };
96 std::vector<std::string> matches{
99 url_matches.begin() + 1,
102 writer(req, handler(req,
matches), conn);
105 writer(req, handler(req), conn);
109 visit_bodies(
const auto& gen,
const http::connection_t& conn)
const
111 using body_t =
typename Handler::request_type::body_type;
112 gen->template body<body_t>(
113 [
this, conn](
const auto& req) {
114 this->handle_req(req, conn);
115 }, [&gen,
this](
auto& body) { filter.setup_body(gen->header(), body); });
static response server_error(std::string_view what)
Definition: generator.cpp:47
Definition: endpoint_http_regex.hpp:25
bool matches(const req_header_t &req) const override
Definition: endpoint_http_regex.hpp:53
handle_retr handle(const req_t &gens, const http::connection_t &conn) const override
Definition: endpoint_http_regex.hpp:66
Definition: endpoint_http.hpp:22
virtual bool matches(const req_header_t &req) const
Definition: endpoint_http.hpp:54
Definition: endpoint_http_regex.hpp:15