X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fhttp%2Fserver.cpp;h=f985bf994526bf871844b97cfd263f2ee56ee2ef;hb=d20870655aeca0614d9fc315fe5f9893ebdc7aa3;hp=fbacb10accfc6a1de8c4497503c357aa457a16ac;hpb=09858e5a153b0667b4885da81f6f979a0bf29c36;p=libs%2Fnet.git diff --git a/source/http/server.cpp b/source/http/server.cpp index fbacb10..f985bf9 100644 --- a/source/http/server.cpp +++ b/source/http/server.cpp @@ -1,10 +1,13 @@ #include +#include #include #include +#include #include #include #include #include +#include #include "request.h" #include "response.h" #include "server.h" @@ -65,6 +68,11 @@ void Server::submit_response(Response &resp) send_response(cl, *cl.response); } +void Server::cancel_keepalive(Response &resp) +{ + get_client_by_response(resp).keepalive = false; +} + void Server::data_available() { Net::StreamSocket *csock = sock.accept(); @@ -84,11 +92,19 @@ void Server::client_data_available(Client &cl) break; } - char rbuf[4096]; - unsigned len = cl.sock->read(rbuf, sizeof(rbuf)); - if(cl.stale) + try + { + char rbuf[4096]; + unsigned len = cl.sock->read(rbuf, sizeof(rbuf)); + if(cl.stale) + return; + cl.in_buf.append(rbuf, len); + } + catch(const exception &) + { + cl.stale = true; return; - cl.in_buf.append(rbuf, len); + } RefPtr response; if(!cl.request) @@ -100,7 +116,7 @@ void Server::client_data_available(Client &cl) cl.request = new Request(Request::parse(cl.in_buf)); string addr_str = cl.sock->get_peer_address().str(); - unsigned colon = addr_str.find(':'); + string::size_type colon = addr_str.find(':'); cl.request->set_header("-Client-Host", addr_str.substr(0, colon)); if(cl.request->get_method()!="GET" && cl.request->get_method()!="POST") @@ -117,14 +133,15 @@ void Server::client_data_available(Client &cl) catch(const exception &e) { response = new Response(BAD_REQUEST); - response->add_content(e.what()); + response->add_content(format("An error occurred while parsing request headers:\ntype: %s\nwhat: %s", + Debug::demangle(typeid(e).name()), e.what())); } cl.in_buf = string(); } } else { - len = cl.request->parse_content(cl.in_buf); + unsigned len = cl.request->parse_content(cl.in_buf); cl.in_buf.erase(0, len); } @@ -132,7 +149,7 @@ void Server::client_data_available(Client &cl) { cl.keepalive = false; if(cl.request->has_header("Connection")) - cl.keepalive = (cl.request->get_header("Connection")=="keep-alive"); + cl.keepalive = !strcasecmp(cl.request->get_header("Connection"), "keep-alive"); response = new Response(NONE); try @@ -158,7 +175,8 @@ void Server::client_data_available(Client &cl) responses.erase(cl.response); cl.response = 0; response = new Response(INTERNAL_ERROR); - response->add_content(e.what()); + response->add_content(format("An error occurred while processing the request:\ntype: %s\nwhat: %s", + Debug::demangle(typeid(e).name()), e.what())); } } @@ -168,7 +186,19 @@ void Server::client_data_available(Client &cl) void Server::send_response(Client &cl, Response &resp) { - cl.sock->write(resp.str()); + if(cl.keepalive) + resp.set_header("Connection", "keep-alive"); + + try + { + cl.sock->write(resp.str()); + } + catch(const exception &) + { + cl.stale = true; + return; + } + cl.async = false; if(cl.keepalive) {