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__ << "(" << __LINE__ << ")"
43 << ": destructor." << endl;
47 HTTPRequest::HTTPRequest(const string& uri, const HTTPMethod& method,
48 const string& query, const string& body, const string& version):
49 HTTPMessage(body, version), method(method), uri(uri), query(query) {
51 cerr << __FILE__ << "(" << __LINE__ << ")"
52 << ": uri = " << uri << " | "
53 << "method = " << method_str() << " | "
54 << "query = " << query << " | "
55 << "body.length = " << body.length() << " | "
56 << "version = " << version << endl;
60 string HTTPRequest::method_str(void) const {
70 istream& operator>>(istream& is, HTTPRequest& req)
71 throw(HTTPError, sockerr, ios::failure) {
73 cerr << __FILE__ << "(" << __LINE__ << ")"
74 << ": operator>>()" << endl;
77 // Obtengo primera línea (request)
78 if (!is.getline(buf, BUFSIZ)) {
80 throw ios::failure("eof");
83 cerr << __FILE__ << "(" << __LINE__ << ")"
84 << ":\tRecibiendo linea: " << buf << endl;
87 // Si es la primera línea, es el request.
88 if (line.length() < 3) {
89 throw HTTPError(HTTPMessage::BAD_REQUEST, line
90 + ": No tiene un método conocido.");
93 string::size_type pos = line.find_first_of(String::SPACE_CHARS);
94 String met = line.substr(0, pos);
97 req.method = HTTPRequest::GET;
98 } else if (met == "POST") {
99 req.method = HTTPRequest::POST;
101 throw HTTPError(HTTPMessage::NOT_IMPLEMENTED, met
102 + ": No es un método soportado.");
104 // Si tiene sólo el método, no es válido.
105 line = line.substr(pos + 1);
107 if (!line.length()) {
108 throw HTTPError(HTTPMessage::BAD_REQUEST, "Falta URI.");
110 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
111 pos = line.find_first_of(String::SPACE_CHARS);
112 if (pos != string::npos) {
113 // Si el resto es un protocolo válido, agrego más variables.
114 String protocol = line.substr(pos + 1);
115 protocol = protocol.trim().to_upper();
116 if (protocol.substr(0, 5) != "HTTP/") {
117 throw HTTPError(HTTPMessage::BAD_REQUEST,
118 protocol + ": Protocolo desconocido");
120 if (protocol.substr(5) == "1.0") {
122 } else if (protocol.substr(5) == "1.1") {
124 // Si no es un error.
126 throw HTTPError(HTTPMessage::HTTP_VERSION_NOT_SUPPORTED,
127 protocol.substr(5) + ": Versión HTTP no soportada.");
129 line = line.substr(0, pos);
131 // Agrego la URI y sus derivados.
132 if (!line.length() || (line[0] != '/')) {
133 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
134 throw HTTPError(HTTPMessage::BAD_REQUEST,
135 line + ": La URI no comienza con /.");
137 // Si tiene un query string.
138 pos = line.find("?");
139 if (pos != string::npos) {
140 req.query = line.substr(pos + 1);
144 req.uri = line.substr(0, pos);
145 // Fin de request, obtengo el mensaje.
146 is >> static_cast<HTTPMessage&>(req);
147 //PlaQui::Server::operator>>(is, static_cast<HTTPMessage>(req));
151 ostream& operator<<(ostream& os, const HTTPRequest& req) throw(sockerr) {
153 cerr << __FILE__ << "(" << __LINE__ << ")"
154 << ": operator<<()" << endl;
156 os << req.method_str() << " " << req.uri;
157 if (req.query.length()) {
158 os << "?" << req.query;
160 os << " HTTP/" << req.version << "\r\n"
161 << static_cast<const HTTPMessage&>(req);
165 } // namespace Server
167 } // namespace PlaQui