+// vim: set noexpandtab tabstop=4 shiftwidth=4:
+//----------------------------------------------------------------------------
+// PlaQui
+//----------------------------------------------------------------------------
+// This file is part of PlaQui.
+//
+// PlaQui is free software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the Free Software
+// Foundation; either version 2 of the License, or (at your option) any later
+// version.
+//
+// PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+// details.
+//
+// You should have received a copy of the GNU General Public License along
+// with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
+// Place, Suite 330, Boston, MA 02111-1307 USA
+//----------------------------------------------------------------------------
+// Creado: dom oct 26 22:11:03 ART 2003
+// Autores: Leandro Lucarella <llucare@fi.uba.ar>
+//----------------------------------------------------------------------------
+//
+// $Id$
+//
+
+#include "plaqui/server/httprequest.h"
+#include "plaqui/server/string.h"
+#ifdef DEBUG
+# include <iostream>
+#endif // DEBUG
+
+PlaQui::Server::HTTPRequest::~HTTPRequest(void) {
+#ifdef DEBUG
+ std::cerr << __FILE__ << ": destructor." << std::endl;
+#endif // DEBUG
+}
+
+PlaQui::Server::HTTPRequest::HTTPRequest(const std::string& version):
+ PlaQui::Server::HTTPMessage(version) {
+#ifdef DEBUG
+ std::cerr << __FILE__ << ": http_version = " << http_version << std::endl;
+#endif // DEBUG
+}
+
+PlaQui::Server::HTTPRequest::HTTPRequest(const Serializable& body,
+ const std::string& version):
+ PlaQui::Server::HTTPMessage(body, version) {
+#ifdef DEBUG
+ std::cerr << __FILE__ << ": http_version = " << http_version
+ << " | body = " << body.serialize() << std::endl;
+#endif // DEBUG
+}
+
+PlaQui::Server::HTTPRequest::HTTPRequest(const std::string& uri,
+ const PlaQui::Server::HTTPRequest::HTTPMethod& method,
+ std::string& query, std::string& version):
+ PlaQui::Server::HTTPMessage(body, version) {
+#ifdef DEBUG
+ std::cerr << __FILE__ << ": http_version = " << http_version
+ << " | body = " << body.serialize() << std::endl;
+#endif // DEBUG
+}
+
+void PlaQui::Server::HTTPRequest::serialize(std::ostream& os) {
+#ifdef DEBUG
+ std::cerr << __FILE__ << ": serialize()" << std::endl;
+#endif // DEBUG
+ os << method << " " << uri;
+ if (query_string.length()) {
+ os << "?" << query;
+ }
+ os << "HTTP/" << version << "\r\l" << std::flush; // TODO ver que este bien el \r\l
+ HTTPMessage::serilize(os);
+ if (headers.size()) {
+ headers.serilize(os);
+ }
+ os << "\r\l" << std::flush;
+ if (body.size()) {
+ body.serilize(os);
+ }
+}
+
+void PlaQui::Server::HTTPRequest::unserialize(std::istream& is) {
+ const unsigned BUFFER_SIZE = 4096;
+ char buf[BUFFER_SIZE];
+ // Obtengo primera línea (request)
+ is.getline(buf, BUFFER_SIZE);
+#ifdef DEBUG
+ std::cerr << "Recibiendo linea: " << buf << std::endl;
+#endif // DEBUG
+ String line = buf;
+ // Si es la primera línea, es el request.
+ if (line.length() < 3) {
+ // FIXME - poner excepciones lindas.
+ throw "HTTP/1.1 501 Method Not Implemented";
+ }
+ // Averiguo método.
+ std::string::size_type pos = line.find_first_of(String::SPACE_CHARS);
+ String met = line.substr(0, pos);
+ if (met.to_upper() == "GET") {
+ method = GET;
+ } else if (met.to_upper() == "POST") {
+ method = POST;
+ } else {
+ // FIXME - poner excepciones lindas.
+ throw "HTTP/1.1 501 Method Not Implemented";
+ }
+ // Si tiene sólo el método, no es válido.
+ line = line.substr(pos + 1);
+ iine.trim();
+ if (!line.length()) {
+ // FIXME - poner excepciones lindas.
+ throw "HTTP/1.1 400 Bad Request";
+ }
+ // Si tiene más espacios, tengo la URI y el protocolo (o un error).
+ pos = line.find_first_of(String::SPACE_CHARS);
+ if (pos != std::string::npos) {
+ // Si el resto es un protocolo válido, agrego más variables.
+ String protocol = line.substr(pos + 1);
+ protocol = protocol.trim();
+ if (protocol.to_upper() == "HTTP/1.0") {
+ version = "1.0";
+ } else if (protocol.to_upper() == "HTTP/1.1") {
+ version = "1.1";
+ // Si no es un error.
+ } else {
+ // FIXME - poner excepciones lindas.
+ throw "HTTP/1.1 400 Bad Request";
+ }
+ line = line.substr(0, pos);
+ }
+ // Agrego la URI y sus derivados.
+ if (!line.length() || (line[0] != '/')) {
+ // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
+ // FIXME - poner excepciones lindas.
+ throw "HTTP/1.1 400 Bad Request";
+ }
+ // Si tiene un query string.
+ pos = request.find("?");
+ if (pos != std::string::npos) {
+ query = line.substr(pos + 1);
+ } else {
+ query = "";
+ }
+ uri = line.substr(0, pos);
+ // Fin de request, obtengo cabeceras y cuerpo.
+ HTTPMessage::unserialize(is);
+}
+