2 #include <msp/core/maputils.h>
3 #include <msp/core/refptr.h>
4 #include <msp/net/inet.h>
5 #include <msp/net/resolve.h>
6 #include <msp/net/streamsocket.h>
7 #include <msp/strings/format.h>
17 Server::Server(unsigned port):
21 sock.signal_data_available.connect(sigc::mem_fun(this, &Server::data_available));
22 RefPtr<Net::SockAddr> addr = Net::resolve("*", format("%d", port));
23 sock.listen(*addr, 8);
26 // Avoid emitting sigc::signal destructor in files including server.h
31 unsigned Server::get_port() const
33 const Net::SockAddr &addr = sock.get_local_address();
34 if(addr.get_family()==Net::INET)
35 return static_cast<const Net::InetAddr &>(addr).get_port();
39 void Server::use_event_dispatcher(IO::EventDispatcher *ed)
43 event_disp->remove(sock);
44 for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
45 event_disp->remove(*i->sock);
50 event_disp->add(sock);
51 for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
52 event_disp->add(*i->sock);
56 void Server::delay_response(Response &resp)
58 get_client_by_response(resp).async = true;
61 void Server::submit_response(Response &resp)
63 Client &cl = get_client_by_response(resp);
65 send_response(cl, *cl.response);
68 void Server::data_available()
70 Net::StreamSocket *csock = sock.accept();
71 clients.push_back(Client(csock));
72 csock->signal_data_available.connect(sigc::bind(sigc::mem_fun(this, &Server::client_data_available), sigc::ref(clients.back())));
73 csock->signal_end_of_file.connect(sigc::bind(sigc::mem_fun(this, &Server::client_end_of_file), sigc::ref(clients.back())));
75 event_disp->add(*csock);
78 void Server::client_data_available(Client &cl)
80 for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
81 if(i->stale && &*i!=&cl)
88 unsigned len = cl.sock->read(rbuf, sizeof(rbuf));
91 cl.in_buf.append(rbuf, len);
93 RefPtr<Response> response;
96 if(cl.in_buf.find("\r\n\r\n")!=string::npos || cl.in_buf.find("\n\n")!=string::npos)
100 cl.request = new Request(Request::parse(cl.in_buf));
102 string addr_str = cl.sock->get_peer_address().str();
103 unsigned colon = addr_str.find(':');
104 cl.request->set_header("-Client-Host", addr_str.substr(0, colon));
106 if(cl.request->get_method()!="GET" && cl.request->get_method()!="POST")
108 response = new Response(NOT_IMPLEMENTED);
109 response->add_content("Method not implemented\n");
111 else if(cl.request->get_path()[0]!='/')
113 response = new Response(BAD_REQUEST);
114 response->add_content("Path must be absolute\n");
117 catch(const exception &e)
119 response = new Response(BAD_REQUEST);
120 response->add_content(e.what());
122 cl.in_buf = string();
127 len = cl.request->parse_content(cl.in_buf);
128 cl.in_buf.erase(0, len);
131 if(cl.request && cl.request->is_complete() && !response)
133 cl.keepalive = false;
134 if(cl.request->has_header("Connection"))
135 cl.keepalive = (cl.request->get_header("Connection")=="keep-alive");
137 response = new Response(NONE);
140 cl.response = response.get();
141 responses[cl.response] = &cl;
142 signal_request.emit(*cl.request, *response);
147 responses.erase(cl.response);
149 if(response->get_status()==NONE)
151 response = new Response(NOT_FOUND);
152 response->add_content("The requested resource was not found\n");
156 catch(const exception &e)
158 responses.erase(cl.response);
160 response = new Response(INTERNAL_ERROR);
161 response->add_content(e.what());
166 send_response(cl, *response);
169 void Server::send_response(Client &cl, Response &resp)
171 cl.sock->write(resp.str());
182 cl.sock->shutdown(IO::M_WRITE);
187 void Server::client_end_of_file(Client &cl)
192 Server::Client &Server::get_client_by_response(Response &resp)
194 return *get_item(responses, &resp);
198 Server::Client::Client(RefPtr<Net::StreamSocket> s):
207 Server::Client::~Client()