]> git.llucax.com Git - z.facultad/75.42/plaqui.git/commitdiff
- Se agrega el método HTTPRequest::method_str() para obtener el método como un
authorLeandro Lucarella <llucax@gmail.com>
Thu, 13 Nov 2003 05:27:37 +0000 (05:27 +0000)
committerLeandro Lucarella <llucax@gmail.com>
Thu, 13 Nov 2003 05:27:37 +0000 (05:27 +0000)
  string.
- Se arreglan algunos bugs de llamadas a constructores con parametros viejos.
- Se agrega un test minimamente funcional del ControlClient (tiene un bug
  soportable).
- Se arreglan algunas otras cosas para que el ControlClient ande (aunque sea
  mal :-/).
- Se agrega una TODO list :)

12 files changed:
Server/TODO [new file with mode: 0644]
Server/include/plaqui/server/command.h
Server/include/plaqui/server/httprequest.h
Server/include/plaqui/server/httpresponse.h
Server/src/command.cpp
Server/src/controlclient.cpp
Server/src/httpheaders.cpp
Server/src/httpmessage.cpp
Server/src/httprequest.cpp
Server/src/httpresponse.cpp
Server/src/server.cpp
Server/tests/Makefile

diff --git a/Server/TODO b/Server/TODO
new file mode 100644 (file)
index 0000000..20674f4
--- /dev/null
@@ -0,0 +1,6 @@
+$Id$
+- Agregar timeouts (recvtimeout y sendtimeout).
+- Darle bola al header Connection para saber si cerramos la conexión al
+  finalizar el request o no.
+- Ver por que el ControlClient tira una señal de error despues de una respuesta
+  buena.
index fafa951771fad114699cb73b4f0fce2dfe59a92c..ac43f473faa32fcbdb528d765d4232ed2a9746c5 100644 (file)
@@ -80,8 +80,8 @@ namespace Server {
                        /**
                         * Constructor.
                         */
-                       Command(const std::string& target = "",
-                                       const std::string& command = "");
+                       Command(const std::string& _target = "",
+                                       const std::string& _command = "");
 
                        /**
                         * Establece el destino.
index a5a3647f29a5f15c8b6f6e4ace553c37620f0dc7..5056d15cfe8ee68f59465d1f5165cb568ca6ca8f 100644 (file)
@@ -110,8 +110,14 @@ namespace Server {
                        HTTPRequest(const std::string& uri = "/",
                                        const HTTPMethod& method = GET,
                                        const std::string& query = "",
+                                       const std::string& body = "",
                                        const std::string& version = "1.1");
 
+                       /**
+                        * Obtiene el método como un string.
+                        */
+                       std::string method_str(void) const;
+
                        /**
                         * Obtiene los datos del pedido HTTP desde un texto.
                         */
index 462c0829c879a81ca95e503720bed9ec7353fd37..f4a2db093da3406a94cfc9e8e2ff044974d0833e 100644 (file)
@@ -73,7 +73,8 @@ namespace Server {
                        /**
                         * Constructor.
                         */
-                       HTTPResponse(const std::string& version = "1.1");
+                       HTTPResponse(const std::string& body = "",
+                                       const std::string& version = "1.1");
 
                        /**
                         * Constructor.
index 2db14f0095aef36f00f3b17c63f0298fcbc30264..08a73c9b36ae4a9bd5cb18bec05325d667acd2ff 100644 (file)
@@ -44,12 +44,13 @@ Command::~Command(void) {
 #endif // DEBUG
 }
 
-Command::Command(const string& target, const string& command):
-               target(target), command(command) {
+Command::Command(const string& _target, const string& _command) {
 #ifdef DEBUG
        cerr << __FILE__ << ": target = " << target << " | "
                        << "command = " << command << endl;
 #endif // DEBUG
+       set_target(_target);
+       set_command(_command);
 }
 
 void Command::build(void) {
index afdd073f3b61bac933e484b4e8018d60e30d76e5..3a3221bfdc71d673d1aadab980af13ac61198aa1 100644 (file)
@@ -46,13 +46,13 @@ ControlClient::~ControlClient(void) {
 ControlClient::ControlClient(string host, int port):
                Connection(sockbuf::sock_stream) {
 #ifdef DEBUG
-       cerr << __FILE__ << ": host" << host
+       cerr << __FILE__ << ": host = " << host
                << " | port = " << port << endl;
 #endif // DEBUG
        socket->connect(host.c_str(), port);
-       if (!socket->is_open()) {
-               throw ios::failure("Can't connect!");
-       }
+       //if (!socket->is_open()) {
+       //      throw ios::failure("Can't connect!");
+       //}
 }
 
 void ControlClient::real_run(void) {
index 0ec62e1e7301e87047a323e8786b61ffeea50f17..04bd6c823de5f41bf37a62d90f4d9380e7088415 100644 (file)
@@ -71,7 +71,7 @@ ostream& operator<<(ostream& os, const HTTPHeaders& h) {
 #endif // DEBUG
        for (HTTPHeaders::const_iterator i = h.begin();
                        i != h.end(); ++i) {
-               os << i->first << ": " << i->second << "\n\r";
+               os << i->first << ": " << i->second << "\r\n";
        }
        return os;
 }
index 6aad1efa531c4473629b7906d9e3a8df134dc0f2..578f563645ffeb8b6a41a02435d3a0daa1db5b23 100644 (file)
@@ -48,8 +48,8 @@ HTTPMessage::~HTTPMessage(void) {
 HTTPMessage::HTTPMessage(const string& _body, const string& _version):
                version(_version) {
 #ifdef DEBUG
-       cerr << __FILE__ << ": version = " << version << " | body.length = "
-               << _body.length() << endl;
+       cerr << __FILE__ << ": version = " << version << " | body ("
+               << _body.length() << ") = " << _body << endl;
 #endif // DEBUG
        set_body(_body);
 }
@@ -58,7 +58,7 @@ void HTTPMessage::set_body(const string& _body) {
        body = _body;
        if (body.length()) {
                stringstream ss; // TODO ver forma mas linda de convertir
-               ss << (body.length()+1); // FIXME No se por que tengo que sumarle 1.
+               ss << (body.length()); // FIXME No se por que tengo que sumarle 1.
                headers["Accept-Ranges"] = "bytes";
                headers["Content-Length"] = ss.str();
        }
@@ -105,7 +105,7 @@ ostream& operator<<(ostream& os, const HTTPMessage& m) {
 #ifdef DEBUG
        cerr << __FILE__ << ": operator<<()" << endl;
 #endif // DEBUG
-       return os << m.headers << "\n\r" // Fin de cabeceras
+       return os << m.headers << "\r\n" // Fin de cabeceras
                << m.body;
 }
 
index f9fa56c47e654825b6855270b2b6b99bb5b94870..d62b7859f1aa320f61f6651e69f698e2b1388ba2 100644 (file)
@@ -44,16 +44,27 @@ HTTPRequest::~HTTPRequest(void) {
 }
 
 HTTPRequest::HTTPRequest(const string& uri, const HTTPMethod& method,
-               const string& query, const string& version):
-               HTTPMessage(version), method(method), uri(uri), query(query) {
+               const string& query, const string& body, const string& version):
+               HTTPMessage(body, version), method(method), uri(uri), query(query) {
 #ifdef DEBUG
        cerr << __FILE__ << ": uri = " << uri << " | "
-                       << "method = " << ((method == GET) ? "GET" : "POST") << " | "
+                       << "method = " << method_str() << " | "
                        << "query = " << query << " | "
+                       << "body.length = " << body.length() << " | "
                        << "version = " << version << endl;
 #endif // DEBUG
 }
 
+string HTTPRequest::method_str(void) const {
+       switch (method) {
+               case POST:
+                       return "POST";
+               case GET:
+               default:
+                       return "GET";
+       }
+}
+
 istream& operator>>(istream& is, HTTPRequest& req)
                throw(HTTPError, ios::failure) {
 #ifdef DEBUG
@@ -137,12 +148,12 @@ ostream& operator<<(ostream& os, const HTTPRequest& req) {
 #ifdef DEBUG
        cerr << __FILE__ << ": operator<<()" << endl;
 #endif // DEBUG
-       os << req.method << " " << req.uri;
+       os << req.method_str() << " " << req.uri;
        if (req.query.length()) {
                os << "?" << req.query;
        }
-       // TODO ver que este bien el \n/r
-       os << " HTTP/" << req.version << "\n\r" << static_cast<const HTTPMessage&>(req);
+       os << " HTTP/" << req.version << "\r\n"
+               << static_cast<const HTTPMessage&>(req);
        return os;
 }
 
index 562dbf9881f200291da337745bab5ad400ecd610..46cc55e11db4fe6adc760eb980fad69c409110ba 100644 (file)
@@ -44,10 +44,11 @@ HTTPResponse::~HTTPResponse(void) {
 #endif // DEBUG
 }
 
-HTTPResponse::HTTPResponse(const string& version):
-               HTTPMessage(version) {
+HTTPResponse::HTTPResponse(const string& body, const string& version):
+               HTTPMessage(body, version) {
 #ifdef DEBUG
-       cerr << __FILE__ << ": version = " << version << endl;
+       cerr << __FILE__ << ": body.length = " << body.length()
+               << " | version = " << version << endl;
 #endif // DEBUG
 }
 
@@ -90,7 +91,7 @@ istream& operator>>(istream& is, HTTPResponse& resp)
        }
        // Averiguo la versión.
        string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5);
-       string ver = line.substr(5, pos); 
+       string ver = line.substr(5, pos - 5); 
        if ((ver == "1.1") || (ver == "1.0")) {
                resp.version = ver;
        } else {
@@ -127,8 +128,7 @@ ostream& operator<<(ostream& os, const HTTPResponse& resp) {
        cerr << __FILE__ << ": operator<<()" << endl;
 #endif // DEBUG
        os << "HTTP/" << resp.version << " " << resp.status_code << " "
-               << HTTPMessage::reason(resp.status_code) << "\n\r";
-       // TODO ver que este bien el \r\l
+               << HTTPMessage::reason(resp.status_code) << "\r\n";
        os << static_cast<const HTTPMessage&>(resp);
        return os;
 }
index 375087a169f7de12dfa894ba62e5d3cdf3ee460c..37f5c1c75b7d26d7ca1e6d763d6a21a2ee02f36b 100644 (file)
@@ -186,7 +186,11 @@ void Server::on_control_command_received(const Command& command,
                        response.set_body("Invalid command for 'connection' taget!");
                }
        } else if (command.get_target() == "transmission") {
+                       response.status_code = HTTPMessage::NOT_FOUND;
+                       response.set_body("Invalid command for 'transmission' taget!");
        } else if (command.get_target() == "plant") {
+                       response.status_code = HTTPMessage::NOT_FOUND;
+                       response.set_body("Invalid command for 'plant' taget!");
        } else {
                response.status_code = HTTPMessage::NOT_FOUND;
                response.set_body("Invalid Target!");
@@ -195,7 +199,9 @@ void Server::on_control_command_received(const Command& command,
        response.headers["Content-Type"] = "text/html; charset=iso-8859-1";
        response.headers["Connection"] = "close";
        server->send(response);
-       server->finish();
+       // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
+       // para saber si hay que finish()earlo o no.
+       // server->finish();
 }
 
 } // namespace Server
index 0210ffe18fc530832936fa5424454aedf7ab4a48..c5bfc639a1c37f99a09e0ea3b885b698eb5c26a0 100644 (file)
@@ -43,7 +43,7 @@ CXXFLAGS+=-g -DDEBUG
 #CXXFLAGS+=-O3
 LDFLAGS=-lsocket++ `pkg-config --libs glibmm-2.0 gthread-2.0` #-L$(LIB_FILES)
 
-TARGETS=server_test
+TARGETS=server_test client_test
 
 # Regla por defecto.
 all: $(TARGETS)
@@ -55,6 +55,9 @@ $(LIB_FILES)/server.a:
 # Tests
 server_test: $(LIB_FILES)/server.a server_test.o
 
+# Tests
+client_test: $(LIB_FILES)/server.a client_test.o
+
 clean:
        rm -f $(TARGETS) *.o