X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fhttp%2Fserver.cpp;h=fbacb10accfc6a1de8c4497503c357aa457a16ac;hb=09858e5a153b0667b4885da81f6f979a0bf29c36;hp=e0b34b382e8a9d86e0a9c0a862a2a9250fa2220a;hpb=0165ed331051ae7fc64dfe85dd7ab8f5b11ba919;p=libs%2Fnet.git diff --git a/source/http/server.cpp b/source/http/server.cpp index e0b34b3..fbacb10 100644 --- a/source/http/server.cpp +++ b/source/http/server.cpp @@ -23,6 +23,11 @@ Server::Server(unsigned port): sock.listen(*addr, 8); } +// Avoid emitting sigc::signal destructor in files including server.h +Server::~Server() +{ +} + unsigned Server::get_port() const { const Net::SockAddr &addr = sock.get_local_address(); @@ -57,10 +62,7 @@ void Server::submit_response(Response &resp) { Client &cl = get_client_by_response(resp); if(cl.async) - { - cl.sock->write(resp.str()); - cl.stale = true; - } + send_response(cl, *cl.response); } void Server::data_available() @@ -106,6 +108,11 @@ void Server::client_data_available(Client &cl) response = new Response(NOT_IMPLEMENTED); response->add_content("Method not implemented\n"); } + else if(cl.request->get_path()[0]!='/') + { + response = new Response(BAD_REQUEST); + response->add_content("Path must be absolute\n"); + } } catch(const exception &e) { @@ -121,11 +128,11 @@ void Server::client_data_available(Client &cl) cl.in_buf.erase(0, len); } - bool keepalive = false; if(cl.request && cl.request->is_complete() && !response) { + cl.keepalive = false; if(cl.request->has_header("Connection")) - keepalive = (cl.request->get_header("Connection")=="keep-alive"); + cl.keepalive = (cl.request->get_header("Connection")=="keep-alive"); response = new Response(NONE); try @@ -156,20 +163,24 @@ void Server::client_data_available(Client &cl) } if(response) + send_response(cl, *response); +} + +void Server::send_response(Client &cl, Response &resp) +{ + cl.sock->write(resp.str()); + cl.async = false; + if(cl.keepalive) { - cl.sock->write(response->str()); - if(keepalive) - { - delete cl.request; - cl.request = 0; - delete cl.response; - cl.response = 0; - } - else - { - cl.sock->shutdown(IO::M_WRITE); - cl.stale = true; - } + delete cl.request; + cl.request = 0; + delete cl.response; + cl.response = 0; + } + else + { + cl.sock->shutdown(IO::M_WRITE); + cl.stale = true; } } @@ -188,6 +199,7 @@ Server::Client::Client(RefPtr s): sock(s), request(0), response(0), + keepalive(false), async(false), stale(false) { }