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) {
80 cerr << __FILE__ << ": operator>>()" << endl;
83 // Obtengo primera línea (request)
84 is.getline(buf, BUFSIZ);
86 cerr << "Recibiendo linea: " << buf << endl;
89 // Si es la primera línea, es el request.
90 if (line.length() < 3) {
91 // FIXME - poner excepciones lindas.
92 throw "HTTP/1.1 501 Method Not Implemented";
95 string::size_type pos = line.find_first_of(String::SPACE_CHARS);
96 String met = line.substr(0, pos);
97 if (met.to_upper() == "GET") {
98 req.method = HTTPRequest::GET;
99 } else if (met.to_upper() == "POST") {
100 req.method = HTTPRequest::POST;
102 // FIXME - poner excepciones lindas.
103 throw "HTTP/1.1 501 Method Not Implemented";
105 // Si tiene sólo el método, no es válido.
106 line = line.substr(pos + 1);
108 if (!line.length()) {
109 // FIXME - poner excepciones lindas.
110 throw "HTTP/1.1 400 Bad Request";
112 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
113 pos = line.find_first_of(String::SPACE_CHARS);
114 if (pos != string::npos) {
115 // Si el resto es un protocolo válido, agrego más variables.
116 String protocol = line.substr(pos + 1);
117 protocol = protocol.trim();
118 if (protocol.to_upper() == "HTTP/1.0") {
120 } else if (protocol.to_upper() == "HTTP/1.1") {
122 // Si no es un error.
124 // FIXME - poner excepciones lindas.
125 throw "HTTP/1.1 400 Bad Request";
127 line = line.substr(0, pos);
129 // Agrego la URI y sus derivados.
130 if (!line.length() || (line[0] != '/')) {
131 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
132 // FIXME - poner excepciones lindas.
133 throw "HTTP/1.1 400 Bad Request";
135 // Si tiene un query string.
136 pos = line.find("?");
137 if (pos != string::npos) {
138 req.query = line.substr(pos + 1);
142 req.uri = line.substr(0, pos);
143 // Fin de request, obtengo el mensaje.
144 is >> static_cast<HTTPMessage&>(req);
145 //PlaQui::Server::operator>>(is, static_cast<HTTPMessage>(req));
149 ostream& operator<<(ostream& os, const HTTPRequest& req) {
151 cerr << __FILE__ << ": operator<<()" << endl;
153 os << req.method << " " << req.uri;
154 if (req.query.length()) {
155 os << "?" << req.query;
157 // TODO ver que este bien el \r\l
158 os << " HTTP/" << req.version << "\r\n" << static_cast<const HTTPMessage&>(req);
162 } // namespace Server
164 } // namespace PlaQui