- Se actualiza el controlserver para que use Command.
- Se agregan funciones split() y join() a String.
- Se corrigen bugs insignificantes.
--- /dev/null
+// vim: set noexpandtab tabstop=4 shiftwidth=4:
+//----------------------------------------------------------------------------
+// PlaQui
+//----------------------------------------------------------------------------
+// This file is part of PlaQui.
+//
+// PlaQui is free software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the Free Software
+// Foundation; either version 2 of the License, or (at your option) any later
+// version.
+//
+// PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+// details.
+//
+// You should have received a copy of the GNU General Public License along
+// with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
+// Place, Suite 330, Boston, MA 02111-1307 USA
+//----------------------------------------------------------------------------
+// Creado: sáb nov 8 17:02:44 ART 2003
+// Autores: Leandro Lucarella <llucare@fi.uba.ar>
+//----------------------------------------------------------------------------
+//
+// $Id$
+//
+
+#ifndef PLAQUI_COMMAND_H
+#define PLAQUI_COMMAND_H
+
+#include "httprequest.h"
+#include <string>
+#include <vector>
+#include <istream>
+#include <ostream>
+
+namespace PlaQui {
+
+namespace Server {
+
+ /// Pedido HTTP.
+ class Command: private HTTPRequest {
+
+ // Tipos.
+
+ public:
+
+ /// Tipo de métodos HTTP reconocidos.
+ typedef std::vector<std::string> Arguments;
+
+ // Atributos.
+
+ private:
+
+ /// Destinatario del comando.
+ std::string target;
+
+ /// Comando.
+ std::string command;
+
+ /// Lista de argumentos que recibe el comando.
+ Arguments args;
+
+ // Métodos.
+
+ private:
+
+ /**
+ * Construye el URI.
+ */
+ void build(void);
+
+ public:
+
+ /**
+ * Destructor.
+ */
+ virtual ~Command(void);
+
+ /**
+ * Constructor.
+ */
+ Command(const std::string& target = "",
+ const std::string& command = "");
+
+ /**
+ * Establece el destino.
+ */
+ void set_target(const std::string& _target);
+
+ /**
+ * Obtiene el destino.
+ */
+ const std::string& get_target(void);
+
+ /**
+ * Establece el comando.
+ */
+ void set_command(const std::string& _command);
+
+ /**
+ * Obtiene el comand.
+ */
+ const std::string& get_command(void);
+
+ /**
+ * Establece los argumentos.
+ */
+ void set_args(const Arguments& _args);
+
+ /**
+ * Obtiene los argumentos.
+ */
+ const Arguments& get_args(void);
+
+ /**
+ * Agrega un argumentos.
+ */
+ void add_arg(const std::string& arg);
+
+ /**
+ * Obtiene el comando desde un pedido HTTP completo.
+ */
+ friend std::istream& operator>>(std::istream& is,
+ Command& command);
+
+ /**
+ * Convierte el comando a un pedido HTTP completo.
+ */
+ friend std::ostream& operator<<(std::ostream& os,
+ const Command& command);
+
+ };
+
+}
+
+}
+
+#endif // PLAQUI_COMMAND_H
#include "httpmessage.h"
#include <string>
+#include <istream>
+#include <ostream>
namespace PlaQui {
#define PLAQUI_STRING_H
#include <string>
+#include <vector>
namespace PlaQui {
*/
String(const std::string& str);
+ /**
+ * Constructor a partir de un vector.
+ * Convierte el vector en string uniendo sus componentes a traves
+ * del separador.
+ *
+ * \param v Vector.
+ * \param sep Separador.
+ */
+ String(const std::vector<std::string>& v,
+ const std::string& sep);
+
/**
* Elmina caracteres al inicio y fin de un string.
*
*/
String& to_lower(void);
+ /**
+ * Fracciona una cadena convirtiendola en un vector.
+ *
+ * \param sep Caracter usado como separador.
+ */
+ std::vector<std::string> split(char sep) const;
+
+ /**
+ * Concatena los elementos de un vector.
+ *
+ * \param v Vector.
+ * \param sep Separador.
+ */
+ static String join(const std::vector<std::string>& v,
+ const std::string& sep);
+
};
}
objects+=httprequest.o
httprequest.o: $(httprequest_h) httprequest.cpp
+command_h=$(string_h) $(httprequest_h) $(INCLUDE_DIR)/command.h
+objects+=command.o
+command.o: $(command_h) command.cpp
+
httpresponse_h=$(string_h) $(httpmessage_h) $(INCLUDE_DIR)/httprequest.h
objects+=httpresponse.o
httpresponse.o: $(httpresponse_h) httpresponse.cpp
objects+=serverconnection.o
serverconnection.o: $(serverconnection_h) serverconnection.cpp
-controlclient_h=$(connection_h) $(INCLUDE_DIR)/controlclient.h
+controlclient_h=$(connection_h) $(command_h) $(INCLUDE_DIR)/controlclient.h
objects+=controlclient.o
controlclient.o: $(controlclient_h) controlclient.cpp
--- /dev/null
+// vim: set noexpandtab tabstop=4 shiftwidth=4:
+//----------------------------------------------------------------------------
+// PlaQui
+//----------------------------------------------------------------------------
+// This file is part of PlaQui.
+//
+// PlaQui is free software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the Free Software
+// Foundation; either version 2 of the License, or (at your option) any later
+// version.
+//
+// PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+// details.
+//
+// You should have received a copy of the GNU General Public License along
+// with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
+// Place, Suite 330, Boston, MA 02111-1307 USA
+//----------------------------------------------------------------------------
+// Creado: sáb nov 8 17:12:10 ART 2003
+// Autores: Leandro Lucarella <llucare@fi.uba.ar>
+//----------------------------------------------------------------------------
+//
+// $Id$
+//
+
+#include "plaqui/server/command.h"
+#include "plaqui/server/string.h"
+#include <algorithm>
+#ifdef DEBUG
+# include <iostream>
+#endif // DEBUG
+
+using namespace std;
+
+namespace PlaQui {
+
+namespace Server {
+
+Command::~Command(void) {
+#ifdef DEBUG
+ cerr << __FILE__ << ": destructor." << endl;
+#endif // DEBUG
+}
+
+Command::Command(const string& target, const string& command):
+ target(target), command(command) {
+#ifdef DEBUG
+ cerr << __FILE__ << ": target = " << target << " | "
+ << "command = " << command << endl;
+#endif // DEBUG
+}
+
+void Command::build(void) {
+ uri = string("/") + target + '/' + command + String::join(args, "/");
+#ifdef DEBUG
+ cerr << __FILE__ << ": build() = " << uri << endl;
+#endif // DEBUG
+}
+
+void Command::set_target(const std::string& _target) {
+ target = _target;
+ build();
+}
+
+const std::string& Command::get_target(void) {
+ return target;
+}
+
+void Command::set_command(const std::string& _command) {
+ command = _command;
+ build();
+}
+
+const std::string& Command::get_command(void) {
+ return command;
+}
+
+void Command::set_args(const Command::Arguments& _args) {
+ args = _args;
+ build();
+}
+
+const Command::Arguments& Command::get_args(void) {
+ return args;
+}
+
+void Command::add_arg(const std::string& arg) {
+ args.push_back(arg);
+ build();
+}
+
+istream& operator>>(istream& is, Command& command) {
+#ifdef DEBUG
+ cerr << __FILE__ << ": operator>>()" << endl;
+#endif // DEBUG
+ // Obtengo datos del Request HTTP.
+ is >> static_cast<HTTPRequest&>(command);
+ // Segun la URI, obtengo los fragmentos del comando.
+ String uri(command.uri);
+ command.args = uri.split('/');
+ // Borro el primero porque debe estar vacio porque la URI empieza con /.
+ command.args.erase(command.args.begin());
+ if (command.args.size() > 0) {
+ command.target = command.args[0];
+ command.args.erase(command.args.begin());
+ } else {
+ command.target = "";
+ }
+ if (command.args.size() > 1) {
+ command.command = command.args[1];
+ command.args.erase(command.args.begin());
+ } else {
+ command.command = "";
+ }
+ return is;
+}
+
+ostream& operator<<(ostream& os, const Command& command) {
+#ifdef DEBUG
+ cerr << __FILE__ << ": operator<<()" << endl;
+#endif // DEBUG
+ // Manda el request HTTP con la URI que representa el comando.
+ os << static_cast<const HTTPRequest&>(command);
+ return os;
+}
+
+} // namespace Server
+
+} // namespace PlaQui
+
//
#include "plaqui/server/controlserver.h"
-#include "plaqui/server/httprequest.h"
+#include "plaqui/server/command.h"
+#include "plaqui/server/string.h"
#include <cstring>
#include <sstream>
#ifdef DEBUG
#endif // DEBUG
//char buf[BUFSIZ];
while (!stop) {
- HTTPRequest request;
+ Command command;
try {
- socket >> request;
+ socket >> command;
} catch (const char* e) {
- cerr << "Error: " << e << endl;
+ cerr << " (" << __LINE__ << ") Error: " << e << endl;
stop = true;
continue;
} catch (string e) {
- cerr << "Error: " << e << endl;
+ cerr << " (" << __LINE__ << ") Error: " << e << endl;
stop = true;
continue;
} catch (...) {
- cerr << "Error desconocido!" << endl;
+ cerr << " (" << __LINE__ << ") Error desconocido!" << endl;
stop = true;
continue;
}
// Command command = parse_command(request.uri);
//signal_command_received().emit(command);
#ifdef DEBUG
- cerr << "Request: " << request << endl;
+ cerr << "Comando: target = " << command.get_target()
+ << " | command = " << command.get_command()
+ << " | args = [" << String::join(command.get_args(), ", ") << "]"
+ << endl;
//for (HTTPRequest::const_iterator i = request.begin();
// i != request.end(); i++) {
// cerr << " " << i->first << ": " << i->second << endl;
response_xml << " </head>" << endl;
response_xml << " <body>" << endl;
response_xml << " <h1>PlaQui</h1>" << endl;
- response_xml << " <p>versión 0.3</p>" << endl;
- response_xml << " <h2>Pedido HTTP</h2>" << endl;
+ response_xml << " <p>versión 0.4</p>" << endl;
+ response_xml << " <h2>Comando</h2>" << endl;
response_xml << " <ul>" << endl;
- response_xml << " <li><b>Versión:</b> " << request.version << endl;
+/* response_xml << " <li><b>Versión:</b> " << request.version << endl;
response_xml << " <li><b>Método:</b> " << (request.method ? "POST" : "GET") << endl;
response_xml << " <li><b>URI:</b> " << request.uri << endl;
response_xml << " <li><b>Query:</b> " << request.query << endl;
i != request.headers.end(); i++) {
response_xml << " <li><b>" << i->first << ":</b> "
<< i->second << endl;
+ }*/
+ response_xml << " <li><b>Target:</b> " << command.get_target() << endl;
+ response_xml << " <li><b>Command:</b> " << command.get_command() << endl;
+ response_xml << " <li><b>Argumentos:</b>" << endl;
+ response_xml << " <ol>" << endl;
+ for (Command::Arguments::const_iterator i = command.get_args().begin();
+ i != command.get_args().end(); i++) {
+ response_xml << " <li>" << *i << "</li>" << endl;
}
+ response_xml << " </ol>" << endl;
response_xml << " </ul>" << endl;
response_xml << " <h2>Desarrollado por</h2>" << endl;
response_xml << " <ul>" << endl;
#endif // DEBUG
}
-HTTPRequest::HTTPRequest(const string& uri, const HTTPMethod& method,
+HTTPRequest::HTTPRequest(const string& uri, const HTTPMethod& method,
const string& query, const string& version):
HTTPMessage(version), method(method), uri(uri), query(query) {
#ifdef DEBUG
#include "plaqui/server/string.h"
#include <cctype>
#include <algorithm>
+#include <iterator>
+#include <sstream>
#ifdef DEBUG
# include <iostream>
#endif // DEBUG
string(str.c_str()) {
}
+String::String(const vector<string>& v, const string& sep) {
+ (*this) = join(v, sep);
+}
+
String& String::trim(const String& clist) {
erase(0, find_first_not_of(clist));
erase(find_last_not_of(clist) + 1);
return *this;
}
+vector<string> String::split(char sep) const {
+ vector<string> v;
+ String::size_type start = 0;
+ String::size_type end = find(sep);
+ while (end != String::npos) {
+ v.push_back(substr(start, end - start));
+ start = end + 1;
+ end = find(sep, start);
+ }
+ if (start != length()) {
+ v.push_back(substr(start, end - start));
+ }
+ return v;
+}
+
+String String::join(const vector<string>& v, const string& sep) {
+ stringstream ss;
+ std::copy(v.begin(), v.end(), ostream_iterator<string>(ss, sep.c_str()));
+ return ss.str();
+}
+
} // namespace Server
} // namespace PlaQui