+#include <exception>
+#include <msp/core/refptr.h>
+#include <msp/net/inet.h>
+#include <msp/net/resolve.h>
+#include <msp/net/streamsocket.h>
+#include <msp/strings/format.h>
+#include "request.h"
+#include "response.h"
+#include "server.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Http {
+
+Server::Server(unsigned port):
+ sock(Net::INET),
+ event_disp(0)
+{
+ sock.signal_data_available.connect(sigc::mem_fun(this, &Server::data_available));
+ RefPtr<Net::SockAddr> addr = Net::resolve("*", format("%d", port));
+ sock.listen(*addr, 8);
+}
+
+unsigned Server::get_port() const
+{
+ const Net::SockAddr &addr = sock.get_local_address();
+ if(addr.get_family()==Net::INET)
+ return static_cast<const Net::InetAddr &>(addr).get_port();
+ return 0;
+}
+
+void Server::use_event_dispatcher(IO::EventDispatcher *ed)
+{
+ if(event_disp)
+ {
+ event_disp->remove(sock);
+ for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
+ event_disp->remove(*i->sock);
+ }
+ event_disp = ed;
+ if(event_disp)
+ {
+ event_disp->add(sock);
+ for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
+ event_disp->add(*i->sock);
+ }
+}
+
+void Server::delay_response(Response &resp)
+{
+ get_client_by_response(resp).async = true;
+}
+
+void Server::submit_response(Response &resp)
+{
+ Client &cl = get_client_by_response(resp);
+ if(cl.async)
+ {
+ cl.sock->write(resp.str());
+ cl.stale = true;
+ }
+}
+
+void Server::data_available()
+{
+ Net::StreamSocket *csock = sock.accept();
+ clients.push_back(Client(csock));
+ csock->signal_data_available.connect(sigc::bind(sigc::mem_fun(this, &Server::client_data_available), sigc::ref(clients.back())));
+ csock->signal_end_of_file.connect(sigc::bind(sigc::mem_fun(this, &Server::client_end_of_file), sigc::ref(clients.back())));
+ if(event_disp)
+ event_disp->add(*csock);
+}
+
+void Server::client_data_available(Client &cl)
+{
+ for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
+ if(i->stale && &*i!=&cl)
+ {
+ clients.erase(i);
+ break;
+ }
+
+ char rbuf[4096];
+ unsigned len = cl.sock->read(rbuf, sizeof(rbuf));
+ cl.in_buf.append(rbuf, len);
+
+ RefPtr<Response> response;
+ if(!cl.request)
+ {
+ if(cl.in_buf.find("\r\n\r\n")!=string::npos || cl.in_buf.find("\n\n")!=string::npos)
+ {
+ try
+ {
+ cl.request = new Request(Request::parse(cl.in_buf));
+
+ string addr_str = cl.sock->get_peer_address().str();
+ unsigned 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")
+ {
+ response = new Response(NOT_IMPLEMENTED);
+ response->add_content("Method not implemented\n");
+ }
+ }
+ catch(const exception &e)
+ {
+ response = new Response(BAD_REQUEST);
+ response->add_content(e.what());
+ }
+ cl.in_buf = string();
+ }
+ }
+ else
+ {
+ len = cl.request->parse_content(cl.in_buf);
+ cl.in_buf.erase(0, len);
+ }
+
+ if(cl.request && cl.request->is_complete() && !response)
+ {
+ response = new Response(NONE);
+ try
+ {
+ cl.response = response.get();
+ signal_request.emit(*cl.request, *response);
+ if(cl.async)
+ response.release();
+ else
+ {
+ cl.response = 0;
+ if(response->get_status()==NONE)
+ {
+ response = new Response(NOT_FOUND);
+ response->add_content("The requested resource was not found\n");
+ }
+ }
+ }
+ catch(const exception &e)
+ {
+ cl.response = 0;
+ response = new Response(INTERNAL_ERROR);
+ response->add_content(e.what());
+ }
+ }
+
+ if(response)
+ {
+ cl.sock->write(response->str());
+ cl.stale = true;
+ }
+}
+
+void Server::client_end_of_file(Client &cl)
+{
+ cl.stale = true;
+}
+
+Server::Client &Server::get_client_by_response(Response &resp)
+{
+ for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
+ if(i->response==&resp)
+ return *i;
+
+ // XXX Do this differently
+ throw invalid_argument("Response does not belong to any client");
+}
+
+
+Server::Client::Client(RefPtr<Net::StreamSocket> s):
+ sock(s),
+ request(0),
+ response(0),
+ async(false),
+ stale(false)
+{ }
+
+Server::Client::~Client()
+{
+ delete request;
+ delete response;
+}
+
+} // namespace Http
+} // namespace Msp