X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=blobdiff_plain;f=source%2Fhttp%2Fserver.cpp;fp=source%2Fhttp%2Fserver.cpp;h=066fabe39218a47fe5a58de05450c696c110888d;hp=a7435d330350b41e60416dbc3eb632d840b742ab;hb=ede42d5bb352841e2e425972e12b8ef31ddf2123;hpb=1565dec0f4a86e4bda7095dd7415234f7c7a84e9 diff --git a/source/http/server.cpp b/source/http/server.cpp index a7435d3..066fabe 100644 --- a/source/http/server.cpp +++ b/source/http/server.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -34,7 +33,7 @@ Server::~Server() void Server::listen(unsigned port) { - RefPtr addr = Net::resolve("*", format("%d", port), Net::INET6); + unique_ptr addr(Net::resolve("*", format("%d", port), Net::INET6)); sock.listen(*addr, 8); sock.signal_data_available.connect(sigc::mem_fun(this, &Server::data_available)); } @@ -105,12 +104,13 @@ void Server::close_connections(const Time::TimeDelta &timeout) void Server::data_available() { - Net::StreamSocket *csock = sock.accept(); - clients.push_back(Client(csock)); - csock->signal_data_available.connect(sigc::bind(sigc::mem_fun(this, &Server::client_data_available), sigc::ref(clients.back()))); - csock->signal_end_of_file.connect(sigc::bind(sigc::mem_fun(this, &Server::client_end_of_file), sigc::ref(clients.back()))); + unique_ptr csock(sock.accept()); + clients.emplace_back(move(csock)); + Client &cl = clients.back(); + cl.sock->signal_data_available.connect(sigc::bind(sigc::mem_fun(this, &Server::client_data_available), sigc::ref(clients.back()))); + cl.sock->signal_end_of_file.connect(sigc::bind(sigc::mem_fun(this, &Server::client_end_of_file), sigc::ref(clients.back()))); if(event_disp) - event_disp->add(*csock); + event_disp->add(*cl.sock); } void Server::client_data_available(Client &cl) @@ -136,14 +136,14 @@ void Server::client_data_available(Client &cl) return; } - RefPtr response; + unique_ptr response; if(!cl.request) { if(cl.in_buf.find("\r\n\r\n")!=string::npos || cl.in_buf.find("\n\n")!=string::npos) { try { - cl.request = new Request(Request::parse(cl.in_buf)); + cl.request = make_unique(Request::parse(cl.in_buf)); string addr_str = cl.sock->get_peer_address().str(); string::size_type colon = addr_str.find(':'); @@ -151,18 +151,18 @@ void Server::client_data_available(Client &cl) if(cl.request->get_method()!="GET" && cl.request->get_method()!="POST") { - response = new Response(NOT_IMPLEMENTED); + response = make_unique(NOT_IMPLEMENTED); response->add_content("Method not implemented\n"); } else if(cl.request->get_path()[0]!='/') { - response = new Response(BAD_REQUEST); + response = make_unique(BAD_REQUEST); response->add_content("Path must be absolute\n"); } } catch(const exception &e) { - response = new Response(BAD_REQUEST); + response = make_unique(BAD_REQUEST); response->add_content(format("An error occurred while parsing request headers:\ntype: %s\nwhat: %s", Debug::demangle(typeid(e).name()), e.what())); } @@ -181,30 +181,28 @@ void Server::client_data_available(Client &cl) if(cl.request->has_header("Connection")) cl.keepalive = !strcasecmp(cl.request->get_header("Connection"), "keep-alive"); - response = new Response(NONE); + response = make_unique(NONE); try { - cl.response = response.get(); - responses[cl.response] = &cl; - signal_request.emit(*cl.request, *response); - if(cl.async) - response.release(); - else + cl.response = move(response); + responses[cl.response.get()] = &cl; + signal_request.emit(*cl.request, *cl.response); + if(!cl.async) { - responses.erase(cl.response); - cl.response = nullptr; + responses.erase(cl.response.get()); + response = move(cl.response); if(response->get_status()==NONE) { - response = new Response(NOT_FOUND); + response = make_unique(NOT_FOUND); response->add_content("The requested resource was not found\n"); } } } catch(const exception &e) { - responses.erase(cl.response); - cl.response = nullptr; - response = new Response(INTERNAL_ERROR); + responses.erase(cl.response.get()); + cl.response.reset(); + response = make_unique(INTERNAL_ERROR); response->add_content(format("An error occurred while processing the request:\ntype: %s\nwhat: %s", Debug::demangle(typeid(e).name()), e.what())); } @@ -232,10 +230,8 @@ void Server::send_response(Client &cl, Response &resp) cl.async = false; if(cl.keepalive) { - delete cl.request; - cl.request = nullptr; - delete cl.response; - cl.response = nullptr; + cl.request.reset(); + cl.response.reset(); } else { @@ -255,15 +251,9 @@ Server::Client &Server::get_client_by_response(Response &resp) } -Server::Client::Client(RefPtr s): - sock(s) +Server::Client::Client(unique_ptr s): + sock(move(s)) { } -Server::Client::~Client() -{ - delete request; - delete response; -} - } // namespace Http } // namespace Msp