X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fserver.cpp;h=45bf69b285bef06da567dc599160bdd44f30dc36;hb=c3bc44e20b80683ec1e8296a449804ff6c5f7aff;hp=04eea524ec895637ae34d7c302a2fe5b621d89a0;hpb=077408f98f08fac1a098a501fffdb22728a57a46;p=libs%2Fnet.git diff --git a/source/server.cpp b/source/server.cpp index 04eea52..45bf69b 100644 --- a/source/server.cpp +++ b/source/server.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmsphttp -Copyright © 2008 Mikkosoft Productions, Mikko Rasa -Distributed under the LGPL -*/ - #include #include #include @@ -28,7 +21,7 @@ Server::Server(unsigned port): unsigned Server::get_port() const { - const Net::SockAddr &addr=sock.get_local_address(); + const Net::SockAddr &addr = sock.get_local_address(); if(addr.get_family()==Net::INET) return static_cast(addr).get_port(); return 0; @@ -42,7 +35,7 @@ void Server::use_event_dispatcher(IO::EventDispatcher *ed) for(list::iterator i=clients.begin(); i!=clients.end(); ++i) event_disp->remove(*i->sock); } - event_disp=ed; + event_disp = ed; if(event_disp) { event_disp->add(sock); @@ -53,23 +46,23 @@ void Server::use_event_dispatcher(IO::EventDispatcher *ed) void Server::delay_response(Response &resp) { - get_client_by_response(resp).async=true; + get_client_by_response(resp).async = true; } void Server::submit_response(Response &resp) { - Client &cl=get_client_by_response(resp); + Client &cl = get_client_by_response(resp); if(cl.async) { cl.sock->write(resp.str()); cl.sock->close(); - cl.stale=true; + cl.stale = true; } } void Server::data_available() { - Net::StreamSocket *csock=sock.accept(); + 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()))); @@ -87,7 +80,7 @@ void Server::client_data_available(Client &cl) } char rbuf[4096]; - unsigned len=cl.sock->read(rbuf, sizeof(rbuf)); + unsigned len = cl.sock->read(rbuf, sizeof(rbuf)); cl.in_buf.append(rbuf, len); RefPtr response; @@ -97,55 +90,55 @@ void Server::client_data_available(Client &cl) { try { - cl.request=new Request(Request::parse(cl.in_buf)); + cl.request = new Request(Request::parse(cl.in_buf)); - string addr_str=cl.sock->get_peer_address().str(); - unsigned colon=addr_str.find(':'); + 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") + if(cl.request->get_method()!="GET" && cl.request->get_method()!="POST") { - response=new Response(NOT_IMPLEMENTED); + response = new Response(NOT_IMPLEMENTED); response->add_content("Method not implemented"); } } catch(const exception &e) { - response=new Response(BAD_REQUEST); + response = new Response(BAD_REQUEST); response->add_content(e.what()); } - cl.in_buf=string(); + cl.in_buf = string(); } } else { - len=cl.request->parse_content(cl.in_buf); + 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); + response = new Response(NONE); try { - cl.response=response.get(); + cl.response = response.get(); signal_request.emit(*cl.request, *response); if(cl.async) response.release(); else { - cl.response=0; + cl.response = 0; if(response->get_status()==NONE) { - response=new Response(NOT_FOUND); + response = new Response(NOT_FOUND); response->add_content("The requested resource was not found"); } } } catch(const exception &e) { - cl.response=0; - response=new Response(INTERNAL_ERROR); + cl.response = 0; + response = new Response(INTERNAL_ERROR); response->add_content(e.what()); } } @@ -154,13 +147,13 @@ void Server::client_data_available(Client &cl) { cl.sock->write(response->str()); cl.sock->close(); - cl.stale=true; + cl.stale = true; } } void Server::client_end_of_file(Client &cl) { - cl.stale=true; + cl.stale = true; } Server::Client &Server::get_client_by_response(Response &resp)