Browse Source

add Webserver constructor that probes a port range. Necessary if multiple instances are running.

master
roker 2 years ago
parent
commit
89e3e9854b
2 changed files with 36 additions and 0 deletions
  1. +29
    -0
      webserver.cc
  2. +7
    -0
      webserver.hh

+ 29
- 0
webserver.cc View File

@ -18,10 +18,39 @@ namespace pEp {
, _acceptor{_ioc, {addr, port}}
, _doc_root{doc_root}
, _generic_handler{}
, _port{port}
, _running{false}
{ }
Webserver::Webserver(net::ip::address addr, unsigned short start_port, unsigned short end_port)
: _ioc{1}
, _acceptor{_ioc}
, _doc_root{} // no doc_root
, _generic_handler{}
, _port{-1}
, _running{false}
{
boost::system::error_code ec{};
for(unsigned short port = start_port; port<=end_port; ++port)
{
_acceptor.bind( {addr, port }, ec );
if(!ec)
{
_port = port;
break;
}
}
if(_port<0)
{
throw boost::system::system_error(ec, "Cannot bind to any port from "
+ std::to_string(start_port) + " to " + std::to_string(end_port) + ".");
}
}
beast::string_view Webserver::mime_type(beast::string_view path)
{
using beast::iequals;


+ 7
- 0
webserver.hh View File

@ -43,6 +43,7 @@ namespace pEp {
std::string _doc_root;
std::unordered_map< std::string, Handling > _urls;
handler_t _generic_handler;
int _port;
bool _running;
std::mutex _mtx;
@ -51,9 +52,15 @@ namespace pEp {
// if doc_root is empty, don't deliver arbitrary files.
Webserver(net::ip::address addr, unsigned short port, const std::string& doc_root = "");
// probes all ports from start_port ... end_port, inclusive.
Webserver(net::ip::address addr, unsigned short start_port, unsigned short end_port);
Webserver(const Webserver&) = delete;
Webserver& operator=(const Webserver&) = delete;
virtual ~Webserver() = default;
constexpr
int port() const noexcept { return _port; }
void add_url_handler (const std::string& url_regex, handler_t handler);
void remove_url_handler(const std::string& url_regex);


Loading…
Cancel
Save