diff --git a/webserver.cc b/webserver.cc index d925503..9c6d42e 100644 --- a/webserver.cc +++ b/webserver.cc @@ -61,16 +61,25 @@ void Webserver::remove_url_handler(std::string url_regex) { _urls.erase(url_regex); } -void Webserver::deliver_file(boost::string_view target) +void Webserver::deliver404(tcp::socket *socket, Webserver::request req) +{ + http::response< http::string_body > res{http::status::not_found, req.version()}; + res.set(http::field::content_type, "text/html"); + res.body() = "404 file not found"; + res.keep_alive(req.keep_alive()); + res.prepare_payload(); +} + +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; - if (boost::regex_match(target.data(), m, file)) { + if (boost::regex_match(req.target().data(), m, file)) { fs::path p{_doc_root}; p += m[1]; } else { - + deliver404(socket, req); } } @@ -108,19 +117,21 @@ void Webserver::do_session(tcp::socket *socket) if (handler) { res = handler(m, req); + if (res) { + res->prepare_payload(); + http::write(*socket, *res, ec); + delete res; + if (ec) { + delete socket; + return fail(ec, "writing to stream"); + } + } + else { + deliver404(socket, req); + } } else { - deliver_file(req.target()); - } - - if (res) { - res->prepare_payload(); - http::write(*socket, *res, ec); - delete res; - if (ec) { - delete socket; - return fail(ec, "writing to stream"); - } + deliver_file(socket, req); } } diff --git a/webserver.hh b/webserver.hh index f2c571b..8010f14 100644 --- a/webserver.hh +++ b/webserver.hh @@ -52,7 +52,8 @@ namespace pEp { void shutdown(); protected: - void deliver_file(boost::string_view target); + void deliver404(tcp::socket *socket, request req); + void deliver_file(tcp::socket *socket, request req); handler_t find_handler(request& r, boost::cmatch& m); void do_session(tcp::socket *socket); };