Browse Source

add thread_init() and thread_done(). some smaller cleanups and minor fixes.

pull/1/head
roker 2 years ago
parent
commit
513d5a8200
2 changed files with 49 additions and 15 deletions
  1. +32
    -9
      webserver.cc
  2. +17
    -6
      webserver.hh

+ 32
- 9
webserver.cc View File

@ -13,8 +13,13 @@ namespace fs = boost::filesystem;
#include "webserver.hh"
namespace pEp {
Webserver::Webserver(net::ip::address addr, unsigned short port, std::string doc_root)
: _ioc(1), _acceptor(_ioc, {addr, port}), _doc_root(doc_root), _running(false) { }
Webserver::Webserver(net::ip::address addr, unsigned short port, const std::string& doc_root)
: _ioc(1)
, _acceptor(_ioc, {addr, port})
, _doc_root(doc_root)
, _running(false)
{ }
beast::string_view Webserver::mime_type(beast::string_view path)
{
@ -45,24 +50,28 @@ beast::string_view Webserver::mime_type(beast::string_view path)
return "application/octet-stream";
}
void Webserver::add_url_handler(std::string url_regex, handler_t handler)
void Webserver::add_url_handler(const std::string& url_regex, handler_t handler)
{
std::lock_guard< std::mutex > lock(_mtx);
_urls.emplace(std::pair< std::string, Handling >(url_regex, {boost::regex(url_regex), handler}));
}
void Webserver::remove_url_handler(std::string url_regex) {
void Webserver::remove_url_handler(const std::string& url_regex)
{
std::lock_guard< std::mutex > lock(_mtx);
_urls.erase(url_regex);
}
void Webserver::deliver_status(tcp::socket *socket, Webserver::request req, http::status status)
{
http::response< http::string_body > res{status, req.version()};
res.set(http::field::content_type, "text/html; charset=utf-8");
res.keep_alive(req.keep_alive());
std::stringstream s;
s << "<html><body>" << int(status) << " " << status << "</html></body>";
s << "<html><body>" << int(status) << " " << status << "</body></html>";
res.body() = s.str();
res.prepare_payload();
if (status != http::status::internal_server_error)
@ -71,6 +80,7 @@ void Webserver::deliver_status(tcp::socket *socket, Webserver::request req, http
http::write(*socket, res, ec);
}
void Webserver::deliver_file(tcp::socket *socket, Webserver::request req)
{
static boost::regex file{"/([\\w\\d]{1,100}\\.[\\w\\d]{1,4})"};
@ -106,6 +116,7 @@ void Webserver::deliver_file(tcp::socket *socket, Webserver::request req)
}
}
Webserver::handler_t Webserver::find_handler(request& req, boost::cmatch& m)
{
std::lock_guard< std::mutex > lock(_mtx);
@ -141,7 +152,7 @@ void Webserver::do_session(tcp::socket *socket)
Webserver::handler_t handler = find_handler(req, m);
if (handler) {
Webserver::response *res = handler(m, req);
Webserver::response *res = handler(m, req);
if (!res) {
deliver_status(socket, req, http::status::not_found);
}
@ -161,7 +172,7 @@ void Webserver::do_session(tcp::socket *socket)
Webserver::handler_t handler = find_handler(req, m);
if (handler) {
Webserver::response *res = handler(m, req);
Webserver::response *res = handler(m, req);
if (!res) {
deliver_status(socket, req, http::status::not_found);
}
@ -171,7 +182,12 @@ void Webserver::do_session(tcp::socket *socket)
}
}
else {
deliver_file(socket, req);
if(!_doc_root.empty())
{
deliver_file(socket, req);
}else{
deliver_status(socket, req, http::status::not_found);
}
}
}
break;
@ -190,6 +206,7 @@ void Webserver::do_session(tcp::socket *socket)
delete socket;
}
void Webserver::run()
{
_running = true;
@ -198,7 +215,13 @@ void Webserver::run()
tcp::socket* socket = new tcp::socket{_ioc};
_acceptor.accept(*socket);
std::function< void() > tf = [=](){do_session(socket);};
std::function< void() > tf = [=]()
{
thread_init();
do_session(socket);
thread_done();
};
std::thread{tf}.detach();
}
}


+ 17
- 6
webserver.hh View File

@ -26,7 +26,7 @@ namespace pEp {
class Webserver {
public:
typedef boost::regex url_t;
typedef http::request< http::string_body > request;
typedef http::response< http::string_body > response;
@ -46,13 +46,16 @@ namespace pEp {
std::mutex _mtx;
public:
Webserver(net::ip::address addr, unsigned short port, std::string doc_root);
// if doc_root is empty, don't deliver arbitrary files.
Webserver(net::ip::address addr, unsigned short port, const std::string& doc_root = "");
Webserver(const Webserver&) = delete;
Webserver& operator=(const Webserver&) = delete;
~Webserver() { }
virtual ~Webserver() = default;
void add_url_handler(std::string url_regex, handler_t handler);
void remove_url_handler(std::string url_regex);
void add_url_handler (const std::string& url_regex, handler_t handler);
void remove_url_handler(const std::string& url_regex);
void run();
void shutdown();
@ -61,8 +64,16 @@ namespace pEp {
protected:
void deliver_status(tcp::socket *socket, request req, http::status status);
void deliver_file(tcp::socket *socket, request req);
void deliver_file (tcp::socket *socket, request req);
handler_t find_handler(request& req, boost::cmatch& m);
// called at the beginning of a connection thread. Do nothing by default.
virtual void thread_init() {}
// called at the beginning of a connection thread. Do nothing by default.
virtual void thread_done() {}
// is called by run(), in a separate thread.
void do_session(tcp::socket *socket);
};
};


Loading…
Cancel
Save