|
- #pragma once
-
- #include <memory>
- #include <microhttpd.h>
- #include <cppcore/platform/fdset.h>
-
- #include "types.h"
- #include "request/request.h"
- #include "response/response.h"
-
- namespace cppmicrohttpd
- {
-
- /**
- * @brief A simple HTTP daemon.
- */
- struct daemon
- {
- public:
- using mhd_daemon_ptr_u = std::unique_ptr<MHD_Daemon, decltype(&MHD_stop_daemon)>;
-
- private:
- mhd_daemon_ptr_u _handle; //!< MHD handle
-
- public:
- /**
- * @brief Constructor.
- *
- * @param[in] port Port to open HTTP daemon on.
- * @param[in] flags Flags to create the daemon with.
- * @param[in] callbacks Flags that tell the daemon which callbacks should be registered.
- */
- daemon(
- uint16_t port,
- const daemon_flags& flags,
- const callback_flags& callbacks = callback_flags::empty());
-
- /**
- * @brief Destructor.
- */
- virtual ~daemon() = default;
-
- /**
- * @brief Get the internal MHD handle.
- */
- inline MHD_Daemon * handle() const;
-
- /**
- * @brief Run webserver operations (without blocking unless in client callbacks).
- */
- inline void run();
-
- /**
- * @brief Run webserver operations using the given sets of ready socket handles.
- *
- * @param[in] p_read File descriptor set with all file handles that are ready to read data from.
- * @param[in] p_write File descriptor set with all file handles that are ready to write data to.
- * @param[in] p_except File descriptor set with all file handles that are in a exceptional condition.
- */
- inline void run(
- const ::cppcore::fdset& p_read,
- const ::cppcore::fdset& p_write,
- const ::cppcore::fdset& p_except);
-
- /**
- * @brief Write all file descriptors known by the daemon to the passed file descriptor sets.
- *
- * @param[in] p_read File descriptor set to listen for read operations.
- * @param[in] p_write File descriptor set to listen for write operations.
- * @param[in] p_except File descriptor set to listen for exceptional condition.
- *
- * @return Greatest file descriptor.
- */
- inline int prepare_fdsets(
- ::cppcore::fdset& p_read,
- ::cppcore::fdset& p_write,
- ::cppcore::fdset& p_except) const;
-
- /**
- * @brief Get the timeout in milliseconds to wait for the file descriptors.
- *
- * @return Timeout how many milliseconds select should at most block (0r -1 if not relevant).
- */
- inline long long get_timeout() const;
-
- protected:
- /**
- * @brief Create a new request for the passed arguments
- *
- * @param[in] p_connection Connection this request was reveiced at.
- * @param[in] p_url The URL requested by the client.
- * @param[in] p_method The HTTP method used by the client.
- * @param[in] p_version The HTTP version string.
- *
- * @return The created request.
- */
- virtual request_ptr_u create_request(
- MHD_Connection * const p_connection,
- const char * const p_url,
- const char * const p_method,
- const char * const p_version);
-
- /**
- * @brief Create a response for the given request.
- *
- * @param[in] p_request Request to create response for.
- *
- * @return The created response.
- */
- virtual response_ptr_u create_response(
- request& p_request);
-
- /**
- * @brief Create a default response object.
- *
- * @param[in] p_request Request to create response for.
- * @param[in] p_error_only Only create responses for erroneous request.
- *
- * @return Created request or null if no request could be created.
- */
- response_ptr_u create_default_response(
- const request& p_request,
- bool p_error_only);
-
- public:
- /**
- * @brief Callback to handle requests.
- *
- * @param[in] cls User pointer passed to MHD.
- * @param[in] connection Connection the request was received at.
- * @param[in] url The URL requested by the client.
- * @param[in] method The HTTP method used by the client.
- * @param[in] version The HTTP version string.
- * @param[in] post_data The data being uploaded (excluding headers).
- * @param[in] data_size Number of bytes stored in post_data.
- * @param[in] con_cls User pointer to use for the connection.
- *
- * @return MHD_YES on success, MHD_NO on fatal error.
- */
- static int mhd_access_handler_callback(
- void * cls,
- struct MHD_Connection * connection,
- const char * url,
- const char * method,
- const char * version,
- const char * post_data,
- size_t * data_size,
- void ** con_cls);
- };
-
- }
-
- #include "daemon.inl"
|