X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fhttp%2Fserver.cpp;h=fc6d61f14c8c065e43bf951a4cc8f52b5a7eac48;hb=065568e3a84b596ea15f80f67574ea7708d4e111;hp=b18ed4c76b02dec85572e41cfecac26f2f186852;hpb=2d106c4711c2277b9b3a7e53ad66c4bc22a6569f;p=libs%2Fnet.git diff --git a/source/http/server.cpp b/source/http/server.cpp index b18ed4c..fc6d61f 100644 --- a/source/http/server.cpp +++ b/source/http/server.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "request.h" #include "response.h" #include "server.h" @@ -23,6 +24,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 +63,12 @@ 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::cancel_keepalive(Response &resp) +{ + get_client_by_response(resp).keepalive = false; } void Server::data_available() @@ -106,6 +114,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) { @@ -123,6 +136,10 @@ void Server::client_data_available(Client &cl) if(cl.request && cl.request->is_complete() && !response) { + cl.keepalive = false; + if(cl.request->has_header("Connection")) + cl.keepalive = !strcasecmp(cl.request->get_header("Connection"), "keep-alive"); + response = new Response(NONE); try { @@ -152,8 +169,25 @@ void Server::client_data_available(Client &cl) } if(response) + send_response(cl, *response); +} + +void Server::send_response(Client &cl, Response &resp) +{ + if(cl.keepalive) + resp.set_header("Connection", "keep-alive"); + cl.sock->write(resp.str()); + cl.async = false; + if(cl.keepalive) + { + delete cl.request; + cl.request = 0; + delete cl.response; + cl.response = 0; + } + else { - cl.sock->write(response->str()); + cl.sock->shutdown(IO::M_WRITE); cl.stale = true; } } @@ -173,6 +207,7 @@ Server::Client::Client(RefPtr s): sock(s), request(0), response(0), + keepalive(false), async(false), stale(false) { }