1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
7 // PlaQui is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the Free Software
9 // Foundation; either version 2 of the License, or (at your option) any later
12 // PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 // You should have received a copy of the GNU General Public License along
18 // with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA 02111-1307 USA
20 //----------------------------------------------------------------------------
21 // Creado: Sat Oct 18 18:18:36 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
28 #include "plaqui/server/controlserver.h"
29 #include "plaqui/server/command.h"
30 #include "plaqui/server/string.h"
43 ControlServer::~ControlServer(void) {
45 cerr << __FILE__ << ": destructor." << endl;
49 ControlServer::ControlServer(const sockbuf::sockdesc& sd):
52 cerr << __FILE__ << ": sd = " << sd.sock << endl;
56 void ControlServer::real_run(void) {
58 cerr << __FILE__ << ": real_run()" << endl;
65 } catch (const char* e) {
66 cerr << " (" << __LINE__ << ") Error: " << e << endl;
70 cerr << " (" << __LINE__ << ") Error: " << e << endl;
74 cerr << " (" << __LINE__ << ") Error desconocido!" << endl;
78 // TODO agregar las verificaciones de abajo a HTTPRequest y padres.
80 // Primera línea no vacía (que debe ser el request).
82 while (!stop && socket.getline(buf, BUFSIZ)) {
84 cerr << __FILE__ << " Recibiendo inea: " << buf << endl;
86 int len = strlen(buf);
87 // Si tiene un retorno de carro, lo elimina.
88 if (len && (buf[len-1] == '\r')) {
91 // Si tiene contenido, lo agrego a la información del request.
93 // Si es la primera línea, es el request.
95 request.set_request(buf, socket->peerhost(),
99 // TODO request.parse_header(buf);
101 // Si viene la línea vacía
103 // Si no es la primera, terminan las cabeceras HTTP.
105 // Podría ir un break.
109 // Si es la primera, no pasa nada, sigue esperando un request.
114 command_received.emit(command);
116 cerr << "Comando: target = " << command.get_target()
117 << " | command = " << command.get_command()
118 << " | args = [" << String::join(command.get_args(), ", ") << "]"
121 // FIXME - hacer respuesta XML.
122 // La respuesta hay que mandarla asincrónicamente porque no puedo
123 // responder hasta que la planta no se termine de actualizar, por
125 stringstream response_xml;
126 socket << "HTTP/1.0 200 OK" << endl;
128 Date: Sun, 19 Oct 2003 15:11:14 GMT
129 Server: Apache/1.3.28 (Debian GNU/Linux)
130 Last-Modified: Mon, 28 Apr 2003 07:50:08 GMT
133 socket << "Content-Type: text/html; charset=iso-8859-1" << endl;
134 response_xml << "<html>" << endl;
135 response_xml << " <head>" << endl;
136 response_xml << " <title>PlaQui v0.4</title>" << endl;
137 response_xml << " </head>" << endl;
138 response_xml << " <body>" << endl;
139 response_xml << " <h1>PlaQui</h1>" << endl;
140 response_xml << " <p>versión 0.4</p>" << endl;
141 response_xml << " <h2>Comando</h2>" << endl;
142 response_xml << " <ul>" << endl;
143 /* response_xml << " <li><b>Versión:</b> " << request.version << endl;
144 response_xml << " <li><b>Método:</b> " << (request.method ? "POST" : "GET") << endl;
145 response_xml << " <li><b>URI:</b> " << request.uri << endl;
146 response_xml << " <li><b>Query:</b> " << request.query << endl;
147 for (HTTPHeaders::const_iterator i = request.headers.begin();
148 i != request.headers.end(); i++) {
149 response_xml << " <li><b>" << i->first << ":</b> "
150 << i->second << endl;
152 response_xml << " <li><b>Target:</b> " << command.get_target() << endl;
153 response_xml << " <li><b>Command:</b> " << command.get_command() << endl;
154 response_xml << " <li><b>Argumentos:</b>" << endl;
155 response_xml << " <ol>" << endl;
156 for (Command::Arguments::const_iterator i = command.get_args().begin();
157 i != command.get_args().end(); i++) {
158 response_xml << " <li>" << *i << "</li>" << endl;
160 response_xml << " </ol>" << endl;
161 response_xml << " </ul>" << endl;
162 response_xml << " <h2>Desarrollado por</h2>" << endl;
163 response_xml << " <ul>" << endl;
164 response_xml << " <li>Nicolás Dimov.</li>" << endl;
165 response_xml << " <li>Leandro Lucarella.</li>" << endl;
166 response_xml << " <li>Ricardo Markiewicz.</li>" << endl;
167 response_xml << " </ul>" << endl;
168 response_xml << " <address>" << endl;
169 response_xml << " Copyleft 2003 - bajo los " << endl;
170 response_xml << " términos de la licencia GPL" << endl;
171 response_xml << " </address>" << endl;
172 response_xml << " </body>" << endl;
173 response_xml << "</html>" << endl;
174 socket << "Content-Length: " << response_xml.str().length() << endl;
176 socket << response_xml.str() << flush;
180 ControlServer::SignalCommandReceived& ControlServer::signal_command_received(void) {
181 return command_received;
184 } // namespace Server
186 } // namespace PlaQui