|
|
@ -16,7 +16,7 @@ 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) { } |
|
|
|
|
|
|
|
static beast::string_view mime_type(beast::string_view path) |
|
|
|
beast::string_view Webserver::mime_type(beast::string_view path) |
|
|
|
{ |
|
|
|
using beast::iequals; |
|
|
|
auto const ext = [&path] |
|
|
@ -45,11 +45,6 @@ static beast::string_view mime_type(beast::string_view path) |
|
|
|
return "application/octet-stream"; |
|
|
|
} |
|
|
|
|
|
|
|
static void fail(beast::error_code ec, char const* what) |
|
|
|
{ |
|
|
|
std::cerr << what << ": " << ec.message() << "\n"; |
|
|
|
;} |
|
|
|
|
|
|
|
void Webserver::add_url_handler(std::string url_regex, handler_t handler) |
|
|
|
{ |
|
|
|
std::lock_guard< std::mutex > lock(_mtx); |
|
|
@ -81,9 +76,10 @@ void Webserver::deliver_file(tcp::socket *socket, Webserver::request req) |
|
|
|
static boost::regex file{"/([\\w\\d]{1,100}\\.[\\w\\d]{1,4})"}; |
|
|
|
boost::cmatch m; |
|
|
|
// there's a strange bug without this string variable d
|
|
|
|
// req.target().data() may end with '\x10' when directly being put into
|
|
|
|
// regex_match()
|
|
|
|
// req.target().data() may end with '\x10'
|
|
|
|
std::string d = req.target().data(); |
|
|
|
if (d.back() == '\x10') |
|
|
|
d.pop_back(); |
|
|
|
if (boost::regex_match(d.c_str(), m, file)) { |
|
|
|
fs::path p{_doc_root}; |
|
|
|
p /= m[1]; |
|
|
@ -139,7 +135,7 @@ void Webserver::do_session(tcp::socket *socket) |
|
|
|
break; |
|
|
|
if (ec) { |
|
|
|
delete socket; |
|
|
|
return fail(ec, "reading from stream"); |
|
|
|
throw std::ios_base::failure(ec.message()); |
|
|
|
} |
|
|
|
|
|
|
|
switch (req.method()) { |
|
|
@ -173,7 +169,7 @@ void Webserver::do_session(tcp::socket *socket) |
|
|
|
|
|
|
|
if (ec) { |
|
|
|
delete socket; |
|
|
|
return fail(ec, "writing to stream"); |
|
|
|
throw std::ios_base::failure(ec.message()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|