18 #include <evhtp/evhtp.h> 19 #include <event2/thread.h> 22 #include <condition_variable> 29 #include <cex/cex_config.h> 31 #define IO_BUFFER_SIZE 128*1024 94 const char* getLibraryVersion();
100 typedef std::shared_ptr<Request> ReqPtr;
101 typedef std::shared_ptr<Response> ResPtr;
131 typedef std::pair<std::string,bool> MimeType;
132 typedef std::unordered_map<std::string, MimeType> MimeTypes;
133 typedef std::unique_ptr<std::thread, std::function<void(std::thread* t)>> ThreadPtr;
134 typedef std::unique_ptr<event_base, std::function<void(event_base*)>> EventBasePtr;
206 const char*
get(
const char* name);
240 static int keyValueIteratorCb(evhtp_kv_t * kv,
void * arg);
244 evhtp_authority_t* authority;
251 std::string middlewarePath;
252 std::vector<char> body;
294 fCompression= 0x000F,
308 void set(
const char* name,
const char* value);
314 void set(
const char* name,
int value);
320 int end(
const char*
string,
int status);
327 int end(
const char* buffer,
size_t bufLen,
int status);
368 static evhtp_res sendChunk(evhtp_connection_t* conn,
void* arg);
414 const char* getPath() {
return path.c_str(); }
423 MiddlewareFunction func;
424 UploadFunction uploadFunc;
478 evhtp_ssl_cfg_t* sslConfig;
496 int listen(
bool block=
true);
503 int listen(std::string address,
int port,
bool block=
true);
511 void reset() { middleWares.clear(); }
774 static MimeTypes* getMimeTypes() {
return mimeTypes.get(); }
775 static void registerMimeType(
const char* ext,
const char* mime,
bool binary);
780 void setSslOption(
const char* option,
const char* value);
781 void getSslClientInfo(Request* req);
784 static void libraryInit();
788 int start(
bool block);
790 static int initMimeTypes();
792 static void handleRequest(evhtp_request* req,
void* arg);
793 static evhtp_res handleHeaders(evhtp_request_t* request, evhtp_headers_t* hdr,
void* arg);
794 static evhtp_res handleBody(evhtp_request_t* req,
struct evbuffer* buf,
void* arg);
795 static evhtp_res handleFinished(evhtp_request_t* req,
void* arg);
798 static int verifyCert(
int ok, X509_STORE_CTX* store);
803 std::vector<std::unique_ptr<Middleware>> middleWares;
804 std::vector<std::unique_ptr<Middleware>> uploadWares;
810 EventBasePtr eventBase;
811 ThreadPtr backgroundThread;
812 std::mutex startMutex;
813 std::condition_variable startCond;
819 static bool initialized;
820 static std::mutex initMutex;
821 static std::unique_ptr<MimeTypes> mimeTypes;
827 #endif // __CORE_HPP_ int getFlags()
Returns the currently set flags of the response object.
Definition: core.hpp:364
bool sslEnabled
Flag indicating whether or not SSL is enabled on the listener (default: false).
Definition: core.hpp:473
Protocol
Enumeration of available HTTP versions (HTTP 1.0 or HTTP 1.1)
Definition: core.hpp:80
bool parseSslInfo
Flag indicating whether or not SSL client info shall be parsed for each request (default: true).
Definition: core.hpp:470
int stop()
Stops the listener. If it was started within a background thread, the background thread is terminated...
Definition: server.cc:222
void uploads(UploadFunction func)
Attaches a special middleware function which receives request body data uploads in chunks.
Definition: server.cc:424
int port
HTTP/HTTPS listener port of the server.
Definition: core.hpp:464
int listen(bool block=true)
Starts the server with listener on address and port specified in the server Config struct.
Definition: server.cc:87
void setFlags(int newFlags)
Sets the response object flags.
Definition: core.hpp:361
void propfind(MiddlewareFunction func)
Attaches a middleware function for HTTP WEBDAV PROPFIND requests.
Definition: server.cc:382
Internal helper struct for handling libevhtp callback functions.
Definition: core.hpp:442
Protocol getProtocol()
Returns the HTTP protocol version (1.0 or 1.1)
Definition: request.cc:121
bool isDone()
Checks if state of the response is stDone.
Definition: core.hpp:352
State
State of the response.
Definition: core.hpp:280
int getPort()
Returns the port, if present in the URI.
Definition: request.cc:111
bool isPending()
Checks if state of the response is not stDone.
Definition: core.hpp:357
Serves as interface to the response which will be sent to the client. Contains functions for modifyin...
Definition: core.hpp:275
void head(MiddlewareFunction func)
Attaches a middleware function for HTTP HEAD requests.
Definition: server.cc:291
Response(evhtp_request *req)
Constructs a new Response object.
Definition: response.cc:27
const char * getBody()
Definition: request.cc:101
const char * getQueryParam(const char *name)
Returns the value of a URL query parameter.
Definition: request.cc:61
void move(MiddlewareFunction func)
Attaches a middleware function for WEBDAV MOVE requests.
Definition: server.cc:372
Structure transporting all configuration options of the embedded server. Note that certain middleware...
Definition: core.hpp:458
Method getMethod()
Returns the request's HTTP method.
Definition: request.cc:116
size_t getBodyLength()
Definition: request.cc:106
void lock(MiddlewareFunction func)
Attaches a middleware function for WEBDAV LOCK requests.
Definition: server.cc:402
void put(MiddlewareFunction func)
Attaches a middleware function for HTTP PUT requests.
Definition: server.cc:271
Represents a single middleware.
Definition: core.hpp:383
const char * getUrl()
Returns the full URL of the request.
Definition: request.cc:131
void set(const char *name, const char *value)
Sets a HTTP header to a given value.
Definition: response.cc:33
void mkcol(MiddlewareFunction func)
Attaches a middleware function for WEBDAV MKCOL requests.
Definition: server.cc:352
void get(MiddlewareFunction func)
Attaches a middleware function for HTTP GET requests.
Definition: server.cc:261
void eachQueryParam(PairCallbackFunction cb)
Iterates all URL query parameters of the request with the given callback function.
Definition: request.cc:75
std::function< void(Request *req, const char *data, size_t len)> UploadFunction
A function which is called by an upload Middleware when an incoming request matches.
Definition: core.hpp:122
int stream(int status, std::istream *stream)
Streams a response to the client with the supplied HTTP code.
Definition: response.cc:110
Flags
Flags for controlling the path matching behaviour.
Definition: core.hpp:390
const char * getMiddlewarePath()
Definition: request.cc:146
Method
Enumeration of available HTTP methods.
Definition: core.hpp:53
std::function< void(Request *req, Response *res, std::function< void()> next)> MiddlewareFunction
A function which is called by a standard Middleware when an incoming request matches.
Definition: core.hpp:108
const char * getFile()
Returns the file-portion of the URL of the request.
Definition: request.cc:141
std::function< bool(const char *name, const char *value)> PairCallbackFunction
A callback function which receives a name and a value parameter.
Definition: core.hpp:129
void copy(MiddlewareFunction func)
Attaches a middleware function for WEBDAV COPY requests.
Definition: server.cc:362
Implementation of a simple propertylist based on std::unordered_map
bool compress
Globally enable compression of outgoing responses (default: true).
Definition: core.hpp:467
bool isState(State aState)
Queries the state of the response.
Definition: core.hpp:347
Server()
Constructs a new server with the default config.
Definition: server.cc:49
void trace(MiddlewareFunction func)
Attaches a middleware function for HTTP TRACE requests.
Definition: server.cc:331
void options(MiddlewareFunction func)
Attaches a middleware function for HTTP OPTIONS requests.
Definition: server.cc:321
int threadCount
Controls the number of worker threads the server is going to use (default: 4).
Definition: core.hpp:474
void proppatch(MiddlewareFunction func)
Attaches a middleware function for WEBDAV PROPPATCH requests.
Definition: server.cc:392
const char * get(const char *name)
Returns the value of a HTTP header.
Definition: request.cc:37
void connect(MiddlewareFunction func)
Attaches a middleware function for HTTP CONNECT requests.
Definition: server.cc:311
Contains the current request and all of its preparsed properties.
Definition: core.hpp:172
void unlock(MiddlewareFunction func)
Attaches a middleware function for WEBDAV UNLOCK requests.
Definition: server.cc:412
PropertyList properties
A list of properties of the current request.
Definition: core.hpp:234
void eachHeader(PairCallbackFunction cb)
Iterates all HTTP headers of the request with the given callback function.
Definition: request.cc:49
Type
Type of middleware.
Definition: core.hpp:399
const char * getHost()
Returns the hostname of the request.
Definition: request.cc:126
void reset()
Removes all attached middlewares.
Definition: core.hpp:511
A simple list of properties implemented using std::unordered_map
Definition: plist.hpp:76
int end(const char *string, int status)
Sends a response to the client with the supplied HTTP code and payload text.
Definition: response.cc:58
void patch(MiddlewareFunction func)
Attaches a middleware function for HTTP PATCH requests.
Definition: server.cc:341
Core class of the embedded webserver. Manages a single HTTP/HTTPS listener and performs routing as de...
Definition: core.hpp:434
void post(MiddlewareFunction func)
Attaches a middleware function for HTTP POST requests.
Definition: server.cc:281
Flags
Flags describing features of the response. Currently this affects only compression.
Definition: core.hpp:290
Middleware(const char *path, MiddlewareFunction func, int method=na, int flags=fMatchContain)
Constructs a new middleware with the given parameters.
Definition: middleware.cc:27
void del(MiddlewareFunction func)
Attaches a middleware function for HTTP DEL requests.
Definition: server.cc:301
void use(MiddlewareFunction func)
Attaches a middleware function with no conditions.
Definition: server.cc:243
Request(evhtp_request *req)
Constructs a new Response object.
Definition: request.cc:27
const char * getPath()
Returns the path-portion of the URL of the request.
Definition: request.cc:136
std::string address
Bind address of the server.
Definition: core.hpp:465