]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/controlserver.cpp
64f4cfe1f99c0b69c282a84d678e0193e12f3bf6
[z.facultad/75.42/plaqui.git] / Server / src / controlserver.cpp
1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
3 //                                  PlaQui
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
6 //
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
10 // version.
11 //
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
15 // details.
16 //
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 //----------------------------------------------------------------------------
24 //
25 // $Id$
26 //
27
28 #include "plaqui/server/controlserver.h"
29 #include "plaqui/server/request.h"
30 #include <cstring>
31 #include <sstream>
32 #ifdef DEBUG
33 #       include <iostream>
34 #endif // DEBUG
35
36 PlaQui::Server::ControlServer::~ControlServer(void) {
37 #ifdef DEBUG
38         std::cerr << __FILE__ << ": destructor." << std::endl;
39 #endif // DEBUG
40 }
41
42 PlaQui::Server::ControlServer::ControlServer(const sockbuf::sockdesc& sd):
43                 PlaQui::Server::ServerConnection(sd) {
44 #ifdef DEBUG
45         std::cerr << __FILE__ << ": sd = " << sd.sock << std::endl;
46 #endif // DEBUG
47 }
48
49 void PlaQui::Server::ControlServer::real_run(void) {
50         // FIXME se tiene que ir a la clase para poder frenarlo desde afuera.
51         bool stop = false;
52         char buf[BUFFER_SIZE];
53         while (!stop) {
54                 Request request;
55                 // Primera línea no vacía (que debe ser el request).
56                 bool is_first = true;
57                 while (!stop && socket.getline(buf, BUFFER_SIZE)) {
58 #ifdef DEBUG
59                         std::cerr << "Reciviendo linea: " << buf << std::endl;
60 #endif // DEBUG
61                         int len = strlen(buf);
62                         // Si tiene un retorno de carro, lo elimina.
63                         if (len && (buf[len-1] == '\r')) {
64                                 buf[--len] = '\0';
65                         }
66                         // Si tiene contenido, lo agrego a la información del request.
67                         if (len) {
68                                 // Si es la primera línea, es el request.
69                                 if (is_first) {
70                                         request.set_request(buf, socket->peerhost(),
71                                                         socket->peerport());
72                                         is_first = false;
73                                 } else {
74                                         // TODO request.parse_header(buf);
75                                 }
76                         // Si viene la línea vacía
77                         } else {
78                                 // Si no es la primera, terminan las cabeceras HTTP.
79                                 if (!is_first) {
80                                         // Podría ir un break.
81                                         stop = true;
82                                         continue;
83                                 }
84                                 // Si es la primera, no pasa nada, sigue esperando un request.
85                         }
86                 }
87                 // TODO: Manda mensaje a la planta.
88                 //signal_command_received().emit(request);
89                 //dispatch_command(parse_command(sstr.str()));
90 #ifdef DEBUG
91                 std::cerr << "Request: " << std::endl;
92                 for (Request::const_iterator i = request.begin(); i != request.end();
93                                 i++) {
94                         std::cerr << "   " << i->first << ": " << i->second << std::endl;
95                 }
96 #endif // DEBUG
97                 // FIXME - hacer respuesta XML.
98                 stringstream response_xml;
99                 socket << "HTTP/1.0 200 OK" << std::endl;
100 /*
101 Date: Sun, 19 Oct 2003 15:11:14 GMT
102 Server: Apache/1.3.28 (Debian GNU/Linux)
103 Last-Modified: Mon, 28 Apr 2003 07:50:08 GMT
104 ETag: "110f4043-11a1-3eacdd30"
105 Accept-Ranges: bytes
106 */
107                 socket << "Content-Type: text/html; charset=iso-8859-1" << std::endl;
108                 response_xml << "<html>" << std::endl;
109                 response_xml << "    <head>" << std::endl;
110                 response_xml << "        <title>PlaQui v0.1</title>" << std::endl;
111                 response_xml << "    </head>" << std::endl;
112                 response_xml << "    <body>" << std::endl;
113                 response_xml << "        <h1>PlaQui</h1>" << std::endl;
114                 response_xml << "        <p>versión 0.2</p>" << std::endl;
115                 response_xml << "        <h2>Pedido HTTP</h2>" << std::endl;
116                 response_xml << "        <ul>" << std::endl;
117                 for (Request::const_iterator i = request.begin(); i != request.end();
118                                 i++) {
119                         response_xml << "           <li><b>" << i->first << ":</b> "
120                                 << i->second << std::endl;
121                 }
122                 response_xml << "        </ul>" << std::endl;
123                 response_xml << "        <h2>Desarrollado por</h2>" << std::endl;
124                 response_xml << "        <ul>" << std::endl;
125                 response_xml << "            <li>Nicolás Dimov.</li>" << std::endl;
126                 response_xml << "            <li>Leandro Lucarella.</li>" << std::endl;
127                 response_xml << "            <li>Ricardo Markiewicz.</li>" << std::endl;
128                 response_xml << "        </ul>" << std::endl;
129                 response_xml << "        <address>" << std::endl;
130                 response_xml << "             Copyleft 2003 - bajo los " << std::endl;
131                 response_xml << "             términos de la licencia GPL" << std::endl;
132                 response_xml << "        </address>" << std::endl;
133                 response_xml << "    </body>" << std::endl;
134                 response_xml << "</html>" << std::endl;
135                 socket << "Content-Length: " << response_xml.str().length() << std::endl;
136                 socket << std::endl;
137                 socket << response_xml.str() << std::flush;
138         }
139 }
140