Browse Source

Use 'response' instead of 'response*' and rely on NRVO. Errors in url_handler are signalled as exceptions and returned as status code 500 internal_server_error to client.

pull/1/head
roker 2 years ago
parent
commit
6f5a070828
3 changed files with 8 additions and 9 deletions
  1. +2
    -2
      test_webserver.cc
  2. +5
    -6
      webserver.cc
  3. +1
    -1
      webserver.hh

+ 2
- 2
test_webserver.cc View File

@ -10,8 +10,8 @@ int main()
std::cout << "adding handler for http://127.0.0.1:8080/handler/sample\n";
web.add_url_handler("/handler/(?<name>\\w+)",
[](boost::cmatch m, const pEp::Webserver::request& req)->pEp::Webserver::response* {
pEp::Webserver::response *res = new pEp::Webserver::response{pEp::http::status::ok, req.version()};
[](boost::cmatch m, const pEp::Webserver::request& req)->pEp::Webserver::response {
auto res = pEp::Webserver::response{pEp::http::status::ok, req.version()};
// https://www.boost.org/doc/libs/1_73_0/libs/beast/doc/html/beast/ref/boost__beast__http__response.html


+ 5
- 6
webserver.cc View File

@ -178,13 +178,12 @@ void Webserver::do_session(tcp::socket *socket)
Webserver::handler_t handler = find_handler(req, m);
if (handler) {
Webserver::response *res = handler(m, req);
if (res) {
http::write(*socket, *res, ec);
delete res;
try{
const Webserver::response res = handler(m, req);
http::write(*socket, res, ec);
}
else {
deliver_status(socket, req, http::status::not_found);
catch(...){
deliver_status(socket, req, http::status::internal_server_error);
}
}
else {


+ 1
- 1
webserver.hh View File

@ -30,7 +30,7 @@ namespace pEp {
typedef boost::regex url_t;
typedef http::request< http::string_body > request;
typedef http::response< http::string_body > response;
typedef std::function< response*(boost::cmatch, const request&) > handler_t;
typedef std::function< response(boost::cmatch, const request&) > handler_t;
private:
struct Handling {


Loading…
Cancel
Save