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;
57 istream& operator>>(istream& is, HTTPRequest& req)
58 throw(HTTPError, ios::failure) {
60 cerr << __FILE__ << ": operator>>()" << endl;
63 // Obtengo primera línea (request)
64 if (!is.getline(buf, BUFSIZ)) {
66 throw ios::failure("socket closed");
69 cerr << __FILE__ << ":\tRecibiendo linea: " << buf << endl;
72 // Si es la primera línea, es el request.
73 if (line.length() < 3) {
74 throw HTTPError(HTTPMessage::BAD_REQUEST, line
75 + ": No tiene un método conocido.");
78 string::size_type pos = line.find_first_of(String::SPACE_CHARS);
79 String met = line.substr(0, pos);
82 req.method = HTTPRequest::GET;
83 } else if (met == "POST") {
84 req.method = HTTPRequest::POST;
86 throw HTTPError(HTTPMessage::NOT_IMPLEMENTED, met
87 + ": No es un método soportado.");
89 // Si tiene sólo el método, no es válido.
90 line = line.substr(pos + 1);
93 throw HTTPError(HTTPMessage::BAD_REQUEST, "Falta URI.");
95 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
96 pos = line.find_first_of(String::SPACE_CHARS);
97 if (pos != string::npos) {
98 // Si el resto es un protocolo válido, agrego más variables.
99 String protocol = line.substr(pos + 1);
100 protocol = protocol.trim().to_upper();
101 if (protocol.substr(0, 5) != "HTTP/") {
102 throw HTTPError(HTTPMessage::BAD_REQUEST,
103 protocol + ": Protocolo desconocido");
105 if (protocol.substr(5) == "1.0") {
107 } else if (protocol.substr(5) == "1.1") {
109 // Si no es un error.
111 throw HTTPError(HTTPMessage::HTTP_VERSION_NOT_SUPPORTED,
112 protocol.substr(5) + ": Versión HTTP no soportada.");
114 line = line.substr(0, pos);
116 // Agrego la URI y sus derivados.
117 if (!line.length() || (line[0] != '/')) {
118 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
119 throw HTTPError(HTTPMessage::BAD_REQUEST,
120 line + ": La URI no comienza con /.");
122 // Si tiene un query string.
123 pos = line.find("?");
124 if (pos != string::npos) {
125 req.query = line.substr(pos + 1);
129 req.uri = line.substr(0, pos);
130 // Fin de request, obtengo el mensaje.
131 is >> static_cast<HTTPMessage&>(req);
132 //PlaQui::Server::operator>>(is, static_cast<HTTPMessage>(req));
136 ostream& operator<<(ostream& os, const HTTPRequest& req) {
138 cerr << __FILE__ << ": operator<<()" << endl;
140 os << req.method << " " << req.uri;
141 if (req.query.length()) {
142 os << "?" << req.query;
144 // TODO ver que este bien el \n/r
145 os << " HTTP/" << req.version << "\n\r" << static_cast<const HTTPMessage&>(req);
149 } // namespace Server
151 } // namespace PlaQui