]> git.tdb.fi Git - libs/net.git/blobdiff - source/http/server.cpp
Use string::size_type to store string offsets
[libs/net.git] / source / http / server.cpp
index 47de7dd381b6c84cd995e4dd56239ab64d91b2e2..f0474fd18e993c57de16b1a3f51c07dc260f069f 100644 (file)
@@ -5,6 +5,7 @@
 #include <msp/net/resolve.h>
 #include <msp/net/streamsocket.h>
 #include <msp/strings/format.h>
+#include <msp/strings/utils.h>
 #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()
@@ -84,6 +92,8 @@ void Server::client_data_available(Client &cl)
 
        char rbuf[4096];
        unsigned len = cl.sock->read(rbuf, sizeof(rbuf));
+       if(cl.stale)
+               return;
        cl.in_buf.append(rbuf, len);
 
        RefPtr<Response> response;
@@ -96,7 +106,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")
@@ -104,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)
                        {
@@ -121,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
                {
@@ -150,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;
        }
 }
@@ -171,6 +207,7 @@ Server::Client::Client(RefPtr<Net::StreamSocket> s):
        sock(s),
        request(0),
        response(0),
+       keepalive(false),
        async(false),
        stale(false)
 { }