libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
httphandler.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_HTTP
13#include <functional>
14#include <promeki/function.h>
15#include <promeki/namespace.h>
16#include <promeki/sharedptr.h>
17#include <promeki/list.h>
18#include <promeki/httprequest.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
36using HttpHandlerFunc = Function<void(const HttpRequest &request, HttpResponse &response)>;
37
39using HttpHandlerFuncList = ::promeki::List<HttpHandlerFunc>;
40
61using HttpMiddleware =
62 Function<void(const HttpRequest &request, HttpResponse &response, Function<void()> next)>;
63
65using HttpMiddlewareList = ::promeki::List<HttpMiddleware>;
66
87class HttpHandler {
88 public:
89 // Hand-rolled SHARED pattern instead of PROMEKI_SHARED_BASE:
90 // we want _promeki_clone() to be pure virtual so concrete
91 // leaves (with their own state) are forced at compile time
92 // to provide their own override. PROMEKI_SHARED_BASE
93 // would synthesize a non-pure body that aborts at runtime
94 // for an abstract type, which is silently weaker. Same
95 // approach used by MediaPayload and StringData.
96 RefCount _promeki_refct;
97 virtual HttpHandler *_promeki_clone() const = 0;
98
100 using Ptr = SharedPtr<HttpHandler, false>;
101
102 virtual ~HttpHandler() = default;
103
110 virtual void serve(const HttpRequest &request, HttpResponse &response) = 0;
111};
112
122class HttpFunctionHandler : public HttpHandler {
123 PROMEKI_SHARED_DERIVED(HttpFunctionHandler)
124 public:
129 explicit HttpFunctionHandler(HttpHandlerFunc func) : _func(std::move(func)) {}
130
131 void serve(const HttpRequest &request, HttpResponse &response) override {
132 if (_func) _func(request, response);
133 }
134
135 private:
136 HttpHandlerFunc _func;
137};
138
139PROMEKI_NAMESPACE_END
140
141#endif // PROMEKI_ENABLE_HTTP