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"
40 HTTPRequest::~HTTPRequest(void) {
42 cerr << __FILE__ << ": destructor." << endl;
46 HTTPRequest::HTTPRequest(const string& uri, const HTTPMethod& method,
47 const string& query, const string& version):
48 HTTPMessage(version), method(method), uri(uri), query(query) {
50 cerr << __FILE__ << ": uri = " << uri << " | "
51 << "method = " << ((method == GET) ? "GET" : "POST") << " | "
52 << "query = " << query << " | "
53 << "version = " << version << endl;
58 HTTPRequest::HTTPRequest(const Serializable& body,
59 const string& version):
60 HTTPMessage(body, version) {
62 cerr << __FILE__ << ": http_version = " << http_version
63 << " | body = " << body.serialize() << endl;
67 HTTPRequest::HTTPRequest(const string& uri,
68 const HTTPRequest::HTTPMethod& method,
69 string& query, string& version):
70 HTTPMessage(body, version) {
72 cerr << __FILE__ << ": http_version = " << http_version
73 << " | body = " << body.serialize() << endl;
78 istream& operator>>(istream& is, HTTPRequest& req)
79 throw(HTTPError, ios::failure) {
81 cerr << __FILE__ << ": operator>>()" << endl;
84 // Obtengo primera línea (request)
85 if (!is.getline(buf, BUFSIZ)) {
87 throw ios::failure("socket closed");
90 cerr << __FILE__ << ":\tRecibiendo linea: " << buf << endl;
93 // Si es la primera línea, es el request.
94 if (line.length() < 3) {
95 throw HTTPError(HTTPMessage::BAD_REQUEST, line
96 + ": No tiene un método conocido.");
99 string::size_type pos = line.find_first_of(String::SPACE_CHARS);
100 String met = line.substr(0, pos);
103 req.method = HTTPRequest::GET;
104 } else if (met == "POST") {
105 req.method = HTTPRequest::POST;
107 throw HTTPError(HTTPMessage::NOT_IMPLEMENTED, met
108 + ": No es un método soportado.");
110 // Si tiene sólo el método, no es válido.
111 line = line.substr(pos + 1);
113 if (!line.length()) {
114 throw HTTPError(HTTPMessage::BAD_REQUEST, "Falta URI.");
116 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
117 pos = line.find_first_of(String::SPACE_CHARS);
118 if (pos != string::npos) {
119 // Si el resto es un protocolo válido, agrego más variables.
120 String protocol = line.substr(pos + 1);
121 protocol = protocol.trim().to_upper();
122 if (protocol.substr(0, 5) != "HTTP/") {
123 throw HTTPError(HTTPMessage::BAD_REQUEST,
124 protocol + ": Protocolo desconocido");
126 if (protocol.substr(5) == "1.0") {
128 } else if (protocol.substr(5) == "1.1") {
130 // Si no es un error.
132 throw HTTPError(HTTPMessage::HTTP_VERSION_NOT_SUPPORTED,
133 protocol.substr(5) + ": Versión HTTP no soportada.");
135 line = line.substr(0, pos);
137 // Agrego la URI y sus derivados.
138 if (!line.length() || (line[0] != '/')) {
139 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
140 throw HTTPError(HTTPMessage::BAD_REQUEST,
141 line + ": La URI no comienza con /.");
143 // Si tiene un query string.
144 pos = line.find("?");
145 if (pos != string::npos) {
146 req.query = line.substr(pos + 1);
150 req.uri = line.substr(0, pos);
151 // Fin de request, obtengo el mensaje.
152 is >> static_cast<HTTPMessage&>(req);
153 //PlaQui::Server::operator>>(is, static_cast<HTTPMessage>(req));
157 ostream& operator<<(ostream& os, const HTTPRequest& req) {
159 cerr << __FILE__ << ": operator<<()" << endl;
161 os << req.method << " " << req.uri;
162 if (req.query.length()) {
163 os << "?" << req.query;
165 // TODO ver que este bien el \n/r
166 os << " HTTP/" << req.version << "\n\r" << static_cast<const HTTPMessage&>(req);
170 } // namespace Server
172 } // namespace PlaQui