|
|
|
@ -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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|