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: dom oct 26 22:11:03 ART 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
28 #include "plaqui/server/httprequest.h"
29 #include "plaqui/server/string.h"
34 PlaQui::Server::HTTPRequest::~HTTPRequest(void) {
36 std::cerr << __FILE__ << ": destructor." << std::endl;
40 PlaQui::Server::HTTPRequest::HTTPRequest(const std::string& version):
41 PlaQui::Server::HTTPMessage(version) {
43 std::cerr << __FILE__ << ": http_version = " << http_version << std::endl;
48 PlaQui::Server::HTTPRequest::HTTPRequest(const Serializable& body,
49 const std::string& version):
50 PlaQui::Server::HTTPMessage(body, version) {
52 std::cerr << __FILE__ << ": http_version = " << http_version
53 << " | body = " << body.serialize() << std::endl;
57 PlaQui::Server::HTTPRequest::HTTPRequest(const std::string& uri,
58 const PlaQui::Server::HTTPRequest::HTTPMethod& method,
59 std::string& query, std::string& version):
60 PlaQui::Server::HTTPMessage(body, version) {
62 std::cerr << __FILE__ << ": http_version = " << http_version
63 << " | body = " << body.serialize() << std::endl;
68 std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPRequest& req) {
70 std::cerr << __FILE__ << ": operator>>()" << std::endl;
73 // Obtengo primera línea (request)
74 is.getline(buf, BUFSIZ);
76 std::cerr << "Recibiendo linea: " << buf << std::endl;
79 // Si es la primera línea, es el request.
80 if (line.length() < 3) {
81 // FIXME - poner excepciones lindas.
82 throw "HTTP/1.1 501 Method Not Implemented";
85 std::string::size_type pos = line.find_first_of(String::SPACE_CHARS);
86 String met = line.substr(0, pos);
87 if (met.to_upper() == "GET") {
89 } else if (met.to_upper() == "POST") {
92 // FIXME - poner excepciones lindas.
93 throw "HTTP/1.1 501 Method Not Implemented";
95 // Si tiene sólo el método, no es válido.
96 line = line.substr(pos + 1);
99 // FIXME - poner excepciones lindas.
100 throw "HTTP/1.1 400 Bad Request";
102 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
103 pos = line.find_first_of(String::SPACE_CHARS);
104 if (pos != std::string::npos) {
105 // Si el resto es un protocolo válido, agrego más variables.
106 String protocol = line.substr(pos + 1);
107 protocol = protocol.trim();
108 if (protocol.to_upper() == "HTTP/1.0") {
110 } else if (protocol.to_upper() == "HTTP/1.1") {
112 // Si no es un error.
114 // FIXME - poner excepciones lindas.
115 throw "HTTP/1.1 400 Bad Request";
117 line = line.substr(0, pos);
119 // Agrego la URI y sus derivados.
120 if (!line.length() || (line[0] != '/')) {
121 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
122 // FIXME - poner excepciones lindas.
123 throw "HTTP/1.1 400 Bad Request";
125 // Si tiene un query string.
126 pos = request.find("?");
127 if (pos != std::string::npos) {
128 query = line.substr(pos + 1);
132 uri = line.substr(0, pos);
133 // Fin de request, obtengo el mensaje.
134 is >> *(static_cast<HTTPMessage*>(this));
137 std::ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPRequest& req) {
139 std::cerr << __FILE__ << ": operator<<()" << std::endl;
141 os << method << " " << uri;
142 if (query_string.length()) {
145 // TODO ver que este bien el \r\l
146 os << " HTTP/" << version << "\r\l" << *(static_cast<HTTPMessage*>(this));