]> git.tdb.fi Git - libs/net.git/blobdiff - source/http/server.cpp
Add a function to close all HTTP connections
[libs/net.git] / source / http / server.cpp
index ed3a2b9288339830ee89f0b56585960ddb24d398..667403e86788c9228e179e1f9677feb1552fb6cc 100644 (file)
@@ -1,10 +1,13 @@
 #include <exception>
+#include <typeinfo>
 #include <msp/core/maputils.h>
 #include <msp/core/refptr.h>
+#include <msp/debug/demangle.h>
 #include <msp/net/inet.h>
 #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"
@@ -14,13 +17,28 @@ using namespace std;
 namespace Msp {
 namespace Http {
 
+Server::Server():
+       sock(Net::INET6),
+       event_disp(0)
+{ }
+
 Server::Server(unsigned port):
-       sock(Net::INET),
+       sock(Net::INET6),
        event_disp(0)
 {
-       sock.signal_data_available.connect(sigc::mem_fun(this, &Server::data_available));
-       RefPtr<Net::SockAddr> addr = Net::resolve("*", format("%d", port));
+       listen(port);
+}
+
+// Avoid emitting sigc::signal destructor in files including server.h
+Server::~Server()
+{
+}
+
+void Server::listen(unsigned port)
+{
+       RefPtr<Net::SockAddr> addr = Net::resolve("*", format("%d", port), Net::INET6);
        sock.listen(*addr, 8);
+       sock.signal_data_available.connect(sigc::mem_fun(this, &Server::data_available));
 }
 
 unsigned Server::get_port() const
@@ -57,9 +75,34 @@ void Server::submit_response(Response &resp)
 {
        Client &cl = get_client_by_response(resp);
        if(cl.async)
+               send_response(cl, *cl.response);
+}
+
+void Server::cancel_keepalive(Response &resp)
+{
+       get_client_by_response(resp).keepalive = false;
+}
+
+void Server::close_connections(const Time::TimeDelta &timeout)
+{
+       IO::Poller poller;
+       for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
        {
-               cl.sock->write(resp.str());
-               cl.stale = true;
+               i->sock->shutdown(IO::M_WRITE);
+               poller.set_object(*i->sock, IO::P_INPUT);
+       }
+
+       while(!clients.empty() && poller.poll(timeout))
+       {
+               const vector<IO::Poller::PolledObject> &result = poller.get_result();
+               for(vector<IO::Poller::PolledObject>::const_iterator i=result.begin(); i!=result.end(); ++i)
+                       for(list<Client>::iterator j=clients.begin(); j!=clients.end(); ++j)
+                               if(j->sock.get()==i->object)
+                               {
+                                       poller.set_object(*j->sock, IO::P_NONE);
+                                       clients.erase(j);
+                                       break;
+                               }
        }
 }
 
@@ -82,11 +125,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> response;
        if(!cl.request)
@@ -98,7 +149,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")
@@ -115,22 +166,23 @@ 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);
        }
 
-       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 = !strcasecmp(cl.request->get_header("Connection"), "keep-alive");
 
                response = new Response(NONE);
                try
@@ -156,25 +208,42 @@ 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()));
                }
        }
 
        if(response)
+               send_response(cl, *response);
+}
+
+void Server::send_response(Client &cl, Response &resp)
+{
+       if(cl.keepalive)
+               resp.set_header("Connection", "keep-alive");
+
+       try
        {
-               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;
-               }
+               cl.sock->write(resp.str());
+       }
+       catch(const exception &)
+       {
+               cl.stale = true;
+               return;
+       }
+
+       cl.async = false;
+       if(cl.keepalive)
+       {
+               delete cl.request;
+               cl.request = 0;
+               delete cl.response;
+               cl.response = 0;
+       }
+       else
+       {
+               cl.sock->shutdown(IO::M_WRITE);
+               cl.stale = true;
        }
 }
 
@@ -193,6 +262,7 @@ Server::Client::Client(RefPtr<Net::StreamSocket> s):
        sock(s),
        request(0),
        response(0),
+       keepalive(false),
        async(false),
        stale(false)
 { }