Browse Source

adding factory function probing_port_range()

pull/1/head
Volker Birk 2 years ago
parent
commit
38698b900d
3 changed files with 37 additions and 39 deletions
  1. +15
    -6
      test_webserver.cc
  2. +17
    -29
      webserver.cc
  3. +5
    -4
      webserver.hh

+ 15
- 6
test_webserver.cc View File

@ -5,11 +5,19 @@
int main()
{
std::cout << "serving http://127.0.0.1:8080/test.html\n";
pEp::Webserver web{pEp::net::ip::address::from_string("127.0.0.1"), 8080, "htdocs"};
std::cout << "adding handler for http://127.0.0.1:8080/handler/sample\n";
web.add_url_handler("/handler/(?<name>\\w+)",
unsigned short port;
auto web =
pEp::Webserver::probing_port_range(pEp::net::ip::address::from_string("127.0.0.1"),
8080, 8089, port, "htdocs");
if (!web) {
std::cerr << "cannot start webserver, no ports free\n";
return 1;
}
std::cout << "serving http://127.0.0.1:" << port << "/test.html\n";
std::cout << "adding handler for http://127.0.0.1:" << port << "/handler/sample\n";
web->add_url_handler("/handler/(?<name>\\w+)",
[](boost::cmatch m, const pEp::Webserver::request& req)->pEp::Webserver::response {
auto res = pEp::Webserver::response{pEp::http::status::ok, req.version()};
@ -41,7 +49,8 @@ int main()
}
);
web.run();
web->run();
delete web;
return 0;
}


+ 17
- 29
webserver.cc View File

@ -22,35 +22,6 @@ namespace pEp {
, _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;
@ -122,6 +93,23 @@ Webserver::response Webserver::create_status_response(const request& req, http::
return res;
}
Webserver* Webserver::probing_port_range(net::ip::address addr, unsigned short
start, unsigned short end, unsigned short& port, const std::string&
doc_root)
{
pEp::Webserver *web = nullptr;
for (port = start; port <= end; ++port) {
try {
web = new pEp::Webserver{addr, port, doc_root};
break;
}
catch (boost::system::system_error& err) {
}
}
return web;
}
void Webserver::deliver_status(tcp::socket *socket, const request& req, http::status status)
{


+ 5
- 4
webserver.hh View File

@ -43,7 +43,7 @@ namespace pEp {
std::string _doc_root;
std::unordered_map< std::string, Handling > _urls;
handler_t _generic_handler;
int _port;
unsigned short _port;
bool _running;
std::mutex _mtx;
@ -52,9 +52,6 @@ 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;
@ -77,6 +74,10 @@ namespace pEp {
static
response create_status_response(const request& req, http::status status);
static Webserver* probing_port_range(net::ip::address addr,
unsigned short start, unsigned short end, unsigned short&
port, const std::string& doc_root = "");
protected:
void deliver_status(tcp::socket *socket, const request& req, http::status status);
void deliver_file (tcp::socket *socket, const request& req);


Loading…
Cancel
Save