You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

154 rivejä
5.2 KiB

  1. #pragma once
  2. #include <memory>
  3. #include <microhttpd.h>
  4. #include <cppcore/platform/fdset.h>
  5. #include "types.h"
  6. #include "request/request.h"
  7. #include "response/response.h"
  8. namespace cppmicrohttpd
  9. {
  10. /**
  11. * @brief A simple HTTP daemon.
  12. */
  13. struct daemon
  14. {
  15. public:
  16. using mhd_daemon_ptr_u = std::unique_ptr<MHD_Daemon, decltype(&MHD_stop_daemon)>;
  17. private:
  18. mhd_daemon_ptr_u _handle; //!< MHD handle
  19. public:
  20. /**
  21. * @brief Constructor.
  22. *
  23. * @param[in] port Port to open HTTP daemon on.
  24. * @param[in] flags Flags to create the daemon with.
  25. * @param[in] callbacks Flags that tell the daemon which callbacks should be registered.
  26. */
  27. daemon(
  28. uint16_t port,
  29. const daemon_flags& flags,
  30. const callback_flags& callbacks = callback_flags::empty());
  31. /**
  32. * @brief Destructor.
  33. */
  34. virtual ~daemon() = default;
  35. /**
  36. * @brief Get the internal MHD handle.
  37. */
  38. inline MHD_Daemon * handle() const;
  39. /**
  40. * @brief Run webserver operations (without blocking unless in client callbacks).
  41. */
  42. inline void run();
  43. /**
  44. * @brief Run webserver operations using the given sets of ready socket handles.
  45. *
  46. * @param[in] p_read File descriptor set with all file handles that are ready to read data from.
  47. * @param[in] p_write File descriptor set with all file handles that are ready to write data to.
  48. * @param[in] p_except File descriptor set with all file handles that are in a exceptional condition.
  49. */
  50. inline void run(
  51. const ::cppcore::fdset& p_read,
  52. const ::cppcore::fdset& p_write,
  53. const ::cppcore::fdset& p_except);
  54. /**
  55. * @brief Write all file descriptors known by the daemon to the passed file descriptor sets.
  56. *
  57. * @param[in] p_read File descriptor set to listen for read operations.
  58. * @param[in] p_write File descriptor set to listen for write operations.
  59. * @param[in] p_except File descriptor set to listen for exceptional condition.
  60. *
  61. * @return Greatest file descriptor.
  62. */
  63. inline int prepare_fdsets(
  64. ::cppcore::fdset& p_read,
  65. ::cppcore::fdset& p_write,
  66. ::cppcore::fdset& p_except) const;
  67. /**
  68. * @brief Get the timeout in milliseconds to wait for the file descriptors.
  69. *
  70. * @return Timeout how many milliseconds select should at most block (0r -1 if not relevant).
  71. */
  72. inline long long get_timeout() const;
  73. protected:
  74. /**
  75. * @brief Create a new request for the passed arguments
  76. *
  77. * @param[in] p_connection Connection this request was reveiced at.
  78. * @param[in] p_url The URL requested by the client.
  79. * @param[in] p_method The HTTP method used by the client.
  80. * @param[in] p_version The HTTP version string.
  81. *
  82. * @return The created request.
  83. */
  84. virtual request_ptr_u create_request(
  85. MHD_Connection * const p_connection,
  86. const char * const p_url,
  87. const char * const p_method,
  88. const char * const p_version);
  89. /**
  90. * @brief Create a response for the given request.
  91. *
  92. * @param[in] p_request Request to create response for.
  93. *
  94. * @return The created response.
  95. */
  96. virtual response_ptr_u create_response(
  97. request& p_request);
  98. /**
  99. * @brief Create a default response object.
  100. *
  101. * @param[in] p_request Request to create response for.
  102. * @param[in] p_error_only Only create responses for erroneous request.
  103. *
  104. * @return Created request or null if no request could be created.
  105. */
  106. response_ptr_u create_default_response(
  107. const request& p_request,
  108. bool p_error_only);
  109. public:
  110. /**
  111. * @brief Callback to handle requests.
  112. *
  113. * @param[in] cls User pointer passed to MHD.
  114. * @param[in] connection Connection the request was received at.
  115. * @param[in] url The URL requested by the client.
  116. * @param[in] method The HTTP method used by the client.
  117. * @param[in] version The HTTP version string.
  118. * @param[in] post_data The data being uploaded (excluding headers).
  119. * @param[in] data_size Number of bytes stored in post_data.
  120. * @param[in] con_cls User pointer to use for the connection.
  121. *
  122. * @return MHD_YES on success, MHD_NO on fatal error.
  123. */
  124. static int mhd_access_handler_callback(
  125. void * cls,
  126. struct MHD_Connection * connection,
  127. const char * url,
  128. const char * method,
  129. const char * version,
  130. const char * post_data,
  131. size_t * data_size,
  132. void ** con_cls);
  133. };
  134. }
  135. #include "daemon.inl"