]> git.tdb.fi Git - libs/net.git/commitdiff
Use connection keepalive on async responses too
authorMikko Rasa <tdb@tdb.fi>
Tue, 19 Apr 2016 07:34:42 +0000 (10:34 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 19 Apr 2016 07:52:36 +0000 (10:52 +0300)
source/http/server.cpp
source/http/server.h

index 9bdfa61ac153cb188ea4688ad5a911df4114f539..fbacb10accfc6a1de8c4497503c357aa457a16ac 100644 (file)
@@ -62,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()
@@ -131,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
@@ -166,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;
        }
 }
 
@@ -198,6 +199,7 @@ Server::Client::Client(RefPtr<Net::StreamSocket> s):
        sock(s),
        request(0),
        response(0),
+       keepalive(false),
        async(false),
        stale(false)
 { }
index 04c2d06b5cf6d386fa7d3f357036f3efaabba4df..29b2b45a3d4cb95ad35a529139a64834e2e3c657 100644 (file)
@@ -23,6 +23,7 @@ private:
                std::string in_buf;
                Request *request;
                Response *response;
+               bool keepalive;
                bool async;
                bool stale;
 
@@ -46,6 +47,7 @@ public:
 private:
        void data_available();
        void client_data_available(Client &);
+       void send_response(Client &, Response &);
        void client_end_of_file(Client &);
        Client &get_client_by_response(Response &);
 };