77 lines
3.1 KiB
C++
77 lines
3.1 KiB
C++
#ifndef PIHTTPSERVER_H
|
|
#define PIHTTPSERVER_H
|
|
|
|
#include "microhttpd_server.h"
|
|
|
|
//! ~english HTTP server
|
|
//! ~russian HTTP сервер
|
|
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 &)>;
|
|
|
|
|
|
//! ~english Registers handler for specific path and HTTP method
|
|
//! ~russian Регистрирует обработчик для указанного пути и HTTP метода
|
|
void registerPath(const PIString & path, PIHTTP::Method method, RequestFunction functor);
|
|
|
|
//! ~english Registers handler for specific path and HTTP method
|
|
//! ~russian Регистрирует обработчик для указанного пути и HTTP метода
|
|
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); });
|
|
}
|
|
|
|
|
|
//! ~english Registers handler for unhandled requests
|
|
//! ~russian Регистрирует обработчик для необработанных запросов
|
|
void registerUnhandled(RequestFunction functor);
|
|
|
|
//! ~english Registers handler for unhandled requests
|
|
//! ~russian Регистрирует обработчик для необработанных запросов
|
|
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); });
|
|
}
|
|
|
|
//! ~english Unregisters handler for specific path and method
|
|
//! ~russian Удаляет обработчик для указанного пути и метода
|
|
void unregisterPath(const PIString & path, PIHTTP::Method method);
|
|
|
|
//! ~english Unregisters all handlers for specific path
|
|
//! ~russian Удаляет все обработчики для указанного пути
|
|
void unregisterPath(const PIString & path);
|
|
|
|
|
|
//! ~english Adds header to all server responses
|
|
//! ~russian Добавляет заголовок ко всем ответам сервера
|
|
void addReplyHeader(const PIString & name, const PIString & value) { reply_headers[name] = value; }
|
|
|
|
//! ~english Removes header from server responses
|
|
//! ~russian Удаляет заголовок из ответов сервера
|
|
void removeReplyHeader(const PIString & name) { reply_headers.remove(name); }
|
|
|
|
//! ~english Clears all custom response headers
|
|
//! ~russian Очищает все пользовательские заголовки ответов
|
|
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
|