a simple multithreaded webserver
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.

100 lines
3.4KB

  1. #pragma once
  2. #include <string>
  3. #include <unordered_map>
  4. #include <thread>
  5. #include <boost/asio/ip/tcp.hpp>
  6. #include <boost/regex.hpp>
  7. #include <boost/beast/http.hpp>
  8. namespace pEp {
  9. namespace beast = boost::beast;
  10. namespace http = beast::http;
  11. namespace net = boost::asio;
  12. using tcp = boost::asio::ip::tcp;
  13. // class Webserver
  14. //
  15. // when an URL handler is present it is called for each matching URL
  16. // otherwise this server is searching for static files in doc_root
  17. // only registered file types and no subdirectories are served for
  18. // static files
  19. //
  20. // to deliver 404 return nullptr from handler
  21. //
  22. // this server is supporting GET for static files and POST for handlers
  23. class Webserver {
  24. public:
  25. typedef boost::regex url_t;
  26. typedef http::request< http::string_body > request;
  27. typedef http::response< http::string_body > response;
  28. typedef std::function< response(boost::cmatch, const request&) > handler_t;
  29. private:
  30. struct Handling {
  31. boost::regex regex;
  32. handler_t handler;
  33. };
  34. net::io_context _ioc;
  35. tcp::acceptor _acceptor;
  36. std::string _doc_root;
  37. std::unordered_map< std::string, Handling > _urls;
  38. handler_t _generic_handler;
  39. unsigned short _port;
  40. bool _running;
  41. std::mutex _mtx;
  42. std::thread _runner;
  43. public:
  44. // if doc_root is empty, don't deliver arbitrary files.
  45. Webserver(net::ip::address addr, unsigned short port, const std::string& doc_root = "");
  46. Webserver(const Webserver&) = delete;
  47. Webserver& operator=(const Webserver&) = delete;
  48. virtual ~Webserver() = default;
  49. constexpr
  50. int port() const noexcept { return _port; }
  51. void add_url_handler (const std::string& url_regex, handler_t handler);
  52. void remove_url_handler(const std::string& url_regex);
  53. // the generic handler will be called if the URL does not match any registered handlers
  54. void add_generic_url_handler (handler_t handler);
  55. void remove_generic_url_handler();
  56. void run();
  57. void shutdown();
  58. static beast::string_view mime_type(beast::string_view path);
  59. static
  60. response create_status_response(const request& req, http::status status);
  61. static Webserver* probing_port_range(net::ip::address addr,
  62. unsigned short start, unsigned short end, unsigned short&
  63. port, const std::string& doc_root = "");
  64. protected:
  65. static void runner(Webserver *me);
  66. void deliver_status(tcp::socket *socket, const request& req, http::status status);
  67. void deliver_file (tcp::socket *socket, const request& req);
  68. handler_t find_handler(const request& req, boost::cmatch& m);
  69. // called at the beginning of a connection thread. Do nothing by default.
  70. virtual void thread_init() {}
  71. // called at the beginning of a connection thread. Do nothing by default.
  72. virtual void thread_done() {}
  73. // is called by run(), in a separate thread.
  74. void do_session(tcp::socket *socket);
  75. };
  76. };