49 lines
1.6 KiB
C++
49 lines
1.6 KiB
C++
#ifndef PIHTTPSERVER_H
|
|
#define PIHTTPSERVER_H
|
|
|
|
#include "microhttpd_server.h"
|
|
|
|
class PIP_HTTP_SERVER_EXPORT PIHTTPServer: public MicrohttpdServer {
|
|
PIOBJECT_SUBCLASS(PIHTTPServer, MicrohttpdServer)
|
|
|
|
public:
|
|
PIHTTPServer();
|
|
virtual ~PIHTTPServer();
|
|
|
|
using RequestFunction = std::function<PIHTTP::MessageMutable(const PIHTTP::MessageConst &)>;
|
|
|
|
void registerPath(const PIString & path, PIHTTP::Method method, RequestFunction functor);
|
|
template<typename T>
|
|
void
|
|
registerPath(const PIString & path, PIHTTP::Method method, T * o, PIHTTP::MessageMutable (T::*function)(const PIHTTP::MessageConst &)) {
|
|
registerPath(path, method, [o, function](const PIHTTP::MessageConst & m) { return (o->*function)(m); });
|
|
}
|
|
void registerUnhandled(RequestFunction functor);
|
|
template<typename T>
|
|
void registerUnhandled(T * o, PIHTTP::MessageMutable (T::*function)(const PIHTTP::MessageConst &)) {
|
|
registerUnhandled([o, function](const PIHTTP::MessageConst & m) { return (o->*function)(m); });
|
|
}
|
|
void unregisterPath(const PIString & path, PIHTTP::Method method);
|
|
void unregisterPath(const PIString & path);
|
|
|
|
// void registerBasicAuth() {}
|
|
|
|
void addReplyHeader(const PIString & name, const PIString & value) { reply_headers[name] = value; }
|
|
void removeReplyHeader(const PIString & name) { reply_headers.remove(name); }
|
|
void clearReplyHeaders() { reply_headers.clear(); }
|
|
|
|
private:
|
|
struct Endpoint {
|
|
bool match(const PIStringList & in_path) const;
|
|
PIStringList path;
|
|
PIHTTP::Method method = PIHTTP::Method::Unknown;
|
|
RequestFunction function;
|
|
};
|
|
PIMap<PIString, PIString> reply_headers;
|
|
PIMap<PIString, Endpoint> functions;
|
|
RequestFunction unhandled;
|
|
};
|
|
|
|
|
|
#endif
|