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& body, const string& version):
48 HTTPMessage(body, version), method(method), uri(uri), query(query) {
50 cerr << __FILE__ << ": uri = " << uri << " | "
51 << "method = " << method_str() << " | "
52 << "query = " << query << " | "
53 << "body.length = " << body.length() << " | "
54 << "version = " << version << endl;
58 string HTTPRequest::method_str(void) const {
68 istream& operator>>(istream& is, HTTPRequest& req)
69 throw(HTTPError, ios::failure) {
71 cerr << __FILE__ << ": operator>>()" << endl;
74 // Obtengo primera línea (request)
75 if (!is.getline(buf, BUFSIZ)) {
77 throw ios::failure("socket closed");
80 cerr << __FILE__ << ":\tRecibiendo linea: " << buf << endl;
83 // Si es la primera línea, es el request.
84 if (line.length() < 3) {
85 throw HTTPError(HTTPMessage::BAD_REQUEST, line
86 + ": No tiene un método conocido.");
89 string::size_type pos = line.find_first_of(String::SPACE_CHARS);
90 String met = line.substr(0, pos);
93 req.method = HTTPRequest::GET;
94 } else if (met == "POST") {
95 req.method = HTTPRequest::POST;
97 throw HTTPError(HTTPMessage::NOT_IMPLEMENTED, met
98 + ": No es un método soportado.");
100 // Si tiene sólo el método, no es válido.
101 line = line.substr(pos + 1);
103 if (!line.length()) {
104 throw HTTPError(HTTPMessage::BAD_REQUEST, "Falta URI.");
106 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
107 pos = line.find_first_of(String::SPACE_CHARS);
108 if (pos != string::npos) {
109 // Si el resto es un protocolo válido, agrego más variables.
110 String protocol = line.substr(pos + 1);
111 protocol = protocol.trim().to_upper();
112 if (protocol.substr(0, 5) != "HTTP/") {
113 throw HTTPError(HTTPMessage::BAD_REQUEST,
114 protocol + ": Protocolo desconocido");
116 if (protocol.substr(5) == "1.0") {
118 } else if (protocol.substr(5) == "1.1") {
120 // Si no es un error.
122 throw HTTPError(HTTPMessage::HTTP_VERSION_NOT_SUPPORTED,
123 protocol.substr(5) + ": Versión HTTP no soportada.");
125 line = line.substr(0, pos);
127 // Agrego la URI y sus derivados.
128 if (!line.length() || (line[0] != '/')) {
129 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
130 throw HTTPError(HTTPMessage::BAD_REQUEST,
131 line + ": La URI no comienza con /.");
133 // Si tiene un query string.
134 pos = line.find("?");
135 if (pos != string::npos) {
136 req.query = line.substr(pos + 1);
140 req.uri = line.substr(0, pos);
141 // Fin de request, obtengo el mensaje.
142 is >> static_cast<HTTPMessage&>(req);
143 //PlaQui::Server::operator>>(is, static_cast<HTTPMessage>(req));
147 ostream& operator<<(ostream& os, const HTTPRequest& req) {
149 cerr << __FILE__ << ": operator<<()" << endl;
151 os << req.method_str() << " " << req.uri;
152 if (req.query.length()) {
153 os << "?" << req.query;
155 os << " HTTP/" << req.version << "\r\n"
156 << static_cast<const HTTPMessage&>(req);
160 } // namespace Server
162 } // namespace PlaQui