|
|
@ -1,7 +1,7 @@ |
|
|
|
// this file is derived from a boost::beast sample
|
|
|
|
|
|
|
|
#include <boost/beast/core.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
@ -26,13 +26,13 @@ static beast::string_view mime_type(beast::string_view path) |
|
|
|
return beast::string_view{}; |
|
|
|
return path.substr(pos); |
|
|
|
}(); |
|
|
|
if(iequals(ext, ".htm")) return "text/html"; |
|
|
|
if(iequals(ext, ".html")) return "text/html"; |
|
|
|
if(iequals(ext, ".css")) return "text/css"; |
|
|
|
if(iequals(ext, ".txt")) return "text/plain"; |
|
|
|
if(iequals(ext, ".js")) return "application/javascript"; |
|
|
|
if(iequals(ext, ".json")) return "application/json"; |
|
|
|
if(iequals(ext, ".xml")) return "application/xml"; |
|
|
|
if(iequals(ext, ".htm")) return "text/html; charset=utf-8"; |
|
|
|
if(iequals(ext, ".html")) return "text/html; charset=utf-8"; |
|
|
|
if(iequals(ext, ".css")) return "text/css; charset=utf-8"; |
|
|
|
if(iequals(ext, ".txt")) return "text/plain; charset=utf-8"; |
|
|
|
if(iequals(ext, ".js")) return "application/javascript; charset=utf-8"; |
|
|
|
if(iequals(ext, ".json")) return "application/json; charset=utf-8"; |
|
|
|
if(iequals(ext, ".xml")) return "application/xml; charset=utf-8"; |
|
|
|
if(iequals(ext, ".png")) return "image/png"; |
|
|
|
if(iequals(ext, ".jpeg")) return "image/jpeg"; |
|
|
|
if(iequals(ext, ".jpg")) return "image/jpeg"; |
|
|
@ -40,7 +40,7 @@ static beast::string_view mime_type(beast::string_view path) |
|
|
|
if(iequals(ext, ".ico")) return "image/vnd.microsoft.icon"; |
|
|
|
if(iequals(ext, ".tiff")) return "image/tiff"; |
|
|
|
if(iequals(ext, ".tif")) return "image/tiff"; |
|
|
|
if(iequals(ext, ".svg")) return "image/svg+xml"; |
|
|
|
if(iequals(ext, ".svg")) return "image/svg+xml; charset=utf-8"; |
|
|
|
if(iequals(ext, ".svgz")) return "image/svg+xml"; |
|
|
|
return "application/octet-stream"; |
|
|
|
} |
|
|
@ -66,6 +66,9 @@ void Webserver::deliver_status(tcp::socket *socket, Webserver::request req, http |
|
|
|
http::response< http::string_body > res{status, req.version()}; |
|
|
|
res.set(http::field::content_type, "text/html"); |
|
|
|
res.keep_alive(req.keep_alive()); |
|
|
|
std::stringstream s; |
|
|
|
s << "<html><body>" << int(status) << " " << status << "</html></body>"; |
|
|
|
res.body() = s.str(); |
|
|
|
res.prepare_payload(); |
|
|
|
if (status != http::status::internal_server_error) |
|
|
|
res.keep_alive(req.keep_alive()); |
|
|
@ -75,11 +78,15 @@ void Webserver::deliver_status(tcp::socket *socket, Webserver::request req, http |
|
|
|
|
|
|
|
void Webserver::deliver_file(tcp::socket *socket, Webserver::request req) |
|
|
|
{ |
|
|
|
static boost::regex file{"/([\\w\\d]{1-100}\\.[\\w\\d]{1-4})"}; |
|
|
|
static boost::regex file{"/([\\w\\d]{1,100}\\.[\\w\\d]{1,4})"}; |
|
|
|
boost::cmatch m; |
|
|
|
if (boost::regex_match(req.target().data(), m, file)) { |
|
|
|
// there's a strange bug without this string variable d
|
|
|
|
// req.target().data() may end with '\x10' when directly being put into
|
|
|
|
// regex_match()
|
|
|
|
std::string d = req.target().data(); |
|
|
|
if (boost::regex_match(d.c_str(), m, file)) { |
|
|
|
fs::path p{_doc_root}; |
|
|
|
p += m[1]; |
|
|
|
p /= m[1]; |
|
|
|
|
|
|
|
beast::error_code ec; |
|
|
|
http::file_body::value_type body; |
|
|
@ -182,7 +189,7 @@ void Webserver::run() |
|
|
|
tcp::socket* socket = new tcp::socket{_ioc}; |
|
|
|
_acceptor.accept(*socket); |
|
|
|
|
|
|
|
std::function< void() > tf = [&](){do_session(socket);}; |
|
|
|
std::function< void() > tf = [=](){do_session(socket);}; |
|
|
|
std::thread{tf}.detach(); |
|
|
|
} |
|
|
|
} |
|
|
|