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/httpresponse.h"
29 #include "plaqui/server/string.h"
34 PlaQui::Server::HTTPRequest::~HTTPResponse(void) {
36 std::cerr << __FILE__ << ": destructor." << std::endl;
40 PlaQui::Server::HTTPResponse::HTTPResponse(const std::string& version):
41 PlaQui::Server::HTTPMessage(version) {
43 std::cerr << __FILE__ << ": http_version = " << http_version << std::endl;
48 PlaQui::Server::HTTPResponse::HTTPResponse(const Serializable& body,
49 const std::string& version):
50 PlaQui::Server::HTTPMessage(body, version) {
52 std::cerr << __FILE__ << ": http_version = " << http_version
53 << " | body = " << body.serialize() << std::endl;
57 PlaQui::Server::HTTPResponse::HTTPResponse(const std::string& uri,
58 const PlaQui::Server::HTTPResponse::HTTPMethod& method,
59 std::string& query, std::string& version):
60 PlaQui::Server::HTTPMessage(body, version) {
62 std::cerr << __FILE__ << ": http_version = " << http_version
63 << " | body = " << body.serialize() << std::endl;
68 std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPResponse& req) {
70 std::cerr << __FILE__ << ": operator>>()" << std::endl;
73 // Obtengo primera línea (request)
74 is.getline(buf, BUFSIZ);
76 std::cerr << "Recibiendo linea: " << buf << std::endl;
79 // Si es la primera línea, es el request.
80 if (line.to_upper().substr(0, 4) != "HTTP/") {
81 // FIXME - poner excepciones lindas.
82 throw "Not a HTTP response";
84 // Averiguo la versión.
85 std::string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5);
86 std::string ver = line.substr(5, pos);
87 if ((ver == "1.1") || (ver == "1.0")) {
90 // FIXME - poner excepciones lindas.
91 throw "Invalid HTTP version";
93 // Si tiene sólo la versión HTTP, no es válido.
94 line = line.substr(pos + 1).trim();
96 // FIXME - poner excepciones lindas.
97 throw "Invalid HTTP response";
99 // Si tiene más espacios, tengo la razón (reason).
100 pos = line.find_first_of(String::SPACE_CHARS);
101 if (pos != std::string::npos) {
102 // Si el resto es un protocolo válido, agrego más variables.
103 String reas = line.substr(pos + 1).trim();
104 line = line.substr(0, pos);
108 if (line.length() != 3) {
109 // FIXME - poner excepciones lindas.
110 throw "Invalid response code";
112 stringstream ss = line; // TODO ver forma mas linda de convertir
116 std::ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPResponse& req) {
118 std::cerr << __FILE__ << ": operator<<()" << std::endl;
120 os << "HTTP/" << version << " " << status_code << " " << reason << "\r\l";
121 // TODO ver que este bien el \r\l
122 os << *(static_cast<HTTPMessage*>(this));