|
|
@ -59,7 +59,7 @@ void Webserver::remove_url_handler(std::string 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"); |
|
|
|
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>"; |
|
|
@ -75,11 +75,7 @@ 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'
|
|
|
|
std::string d = req.target().data(); |
|
|
|
if (d.back() == '\x10') |
|
|
|
d.pop_back(); |
|
|
|
std::string d{req.target().data(), req.target().length()}; |
|
|
|
if (boost::regex_match(d.c_str(), m, file)) { |
|
|
|
fs::path p{_doc_root}; |
|
|
|
p /= m[1]; |
|
|
@ -110,12 +106,13 @@ void Webserver::deliver_file(tcp::socket *socket, Webserver::request req) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Webserver::handler_t Webserver::find_handler(request& r, boost::cmatch& m) |
|
|
|
Webserver::handler_t Webserver::find_handler(request& req, boost::cmatch& m) |
|
|
|
{ |
|
|
|
std::lock_guard< std::mutex > lock(_mtx); |
|
|
|
|
|
|
|
for (auto it=_urls.begin(); it!=_urls.end(); ++it) { |
|
|
|
if (boost::regex_match(r.target().data(), m, it->second.regex)) |
|
|
|
std::string d{req.target().data(), req.target().length()}; |
|
|
|
if (boost::regex_match(d.c_str(), m, it->second.regex)) |
|
|
|
return it->second.handler; |
|
|
|
} |
|
|
|
|
|
|
@ -159,9 +156,25 @@ void Webserver::do_session(tcp::socket *socket) |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case http::verb::get: |
|
|
|
deliver_file(socket, req); |
|
|
|
break; |
|
|
|
case http::verb::get: { |
|
|
|
boost::cmatch m; |
|
|
|
Webserver::handler_t handler = find_handler(req, m); |
|
|
|
|
|
|
|
if (handler) { |
|
|
|
Webserver::response *res = handler(m, req); |
|
|
|
if (!res) { |
|
|
|
deliver_status(socket, req, http::status::not_found); |
|
|
|
} |
|
|
|
else { |
|
|
|
http::write(*socket, *res, ec); |
|
|
|
delete res; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
deliver_file(socket, req); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
deliver_status(socket, req, http::status::method_not_allowed); |
|
|
|