+// 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/httpresponse.h"
+#include "plaqui/server/string.h"
+#ifdef DEBUG
+# include <iostream>
+#endif // DEBUG
+
+PlaQui::Server::HTTPRequest::~HTTPResponse(void) {
+#ifdef DEBUG
+ std::cerr << __FILE__ << ": destructor." << std::endl;
+#endif // DEBUG
+}
+
+PlaQui::Server::HTTPResponse::HTTPResponse(const std::string& version):
+ PlaQui::Server::HTTPMessage(version) {
+#ifdef DEBUG
+ std::cerr << __FILE__ << ": http_version = " << http_version << std::endl;
+#endif // DEBUG
+}
+
+/*
+PlaQui::Server::HTTPResponse::HTTPResponse(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::HTTPResponse::HTTPResponse(const std::string& uri,
+ const PlaQui::Server::HTTPResponse::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
+}
+*/
+
+std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPResponse& req) {
+#ifdef DEBUG
+ std::cerr << __FILE__ << ": operator>>()" << std::endl;
+#endif // DEBUG
+ char buf[BUFSIZ];
+ // Obtengo primera línea (request)
+ is.getline(buf, BUFSIZ);
+#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.to_upper().substr(0, 4) != "HTTP/") {
+ // FIXME - poner excepciones lindas.
+ throw "Not a HTTP response";
+ }
+ // Averiguo la versión.
+ std::string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5);
+ std::string ver = line.substr(5, pos);
+ if ((ver == "1.1") || (ver == "1.0")) {
+ version = ver;
+ } else {
+ // FIXME - poner excepciones lindas.
+ throw "Invalid HTTP version";
+ }
+ // Si tiene sólo la versión HTTP, no es válido.
+ line = line.substr(pos + 1).trim();
+ if (!line.length()) {
+ // FIXME - poner excepciones lindas.
+ throw "Invalid HTTP response";
+ }
+ // Si tiene más espacios, tengo la razón (reason).
+ 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 reas = line.substr(pos + 1).trim();
+ line = line.substr(0, pos);
+ }
+ line = line.trim();
+ // Seteo el código.
+ if (line.length() != 3) {
+ // FIXME - poner excepciones lindas.
+ throw "Invalid response code";
+ }
+ stringstream ss = line; // TODO ver forma mas linda de convertir
+ ss >> status_code;
+}
+
+std::ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPResponse& req) {
+#ifdef DEBUG
+ std::cerr << __FILE__ << ": operator<<()" << std::endl;
+#endif // DEBUG
+ os << "HTTP/" << version << " " << status_code << " " << reason << "\r\l";
+ // TODO ver que este bien el \r\l
+ os << *(static_cast<HTTPMessage*>(this));
+}
+