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"
41 HTTPResponse::~HTTPResponse(void) {
43 cerr << __FILE__ << ": destructor." << endl;
47 HTTPResponse::HTTPResponse(const string& version):
48 HTTPMessage(version) {
50 cerr << __FILE__ << ": version = " << version << endl;
55 HTTPResponse::HTTPResponse(const Serializable& body,
56 const string& version):
57 HTTPMessage(body, version) {
59 cerr << __FILE__ << ": http_version = " << http_version
60 << " | body = " << body.serialize() << endl;
64 HTTPResponse::HTTPResponse(const string& uri,
65 const HTTPResponse::HTTPMethod& method,
66 string& query, string& version):
67 HTTPMessage(body, version) {
69 cerr << __FILE__ << ": http_version = " << http_version
70 << " | body = " << body.serialize() << endl;
75 istream& operator>>(istream& is, HTTPResponse& resp) {
77 cerr << __FILE__ << ": operator>>()" << endl;
80 // Obtengo primera línea (request)
81 is.getline(buf, BUFSIZ);
83 cerr << "Recibiendo linea: " << buf << endl;
86 // Si es la primera línea, es el request.
87 if (line.to_upper().substr(0, 4) != "HTTP/") {
88 // FIXME - poner excepciones lindas.
89 throw "Not a HTTP response";
91 // Averiguo la versión.
92 string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5);
93 string ver = line.substr(5, pos);
94 if ((ver == "1.1") || (ver == "1.0")) {
97 // FIXME - poner excepciones lindas.
98 throw "Invalid HTTP version";
100 // Si tiene sólo la versión HTTP, no es válido.
101 line = line.substr(pos + 1);
103 if (!line.length()) {
104 // FIXME - poner excepciones lindas.
105 throw "Invalid HTTP response";
107 // Si tiene más espacios, tengo la razón (reason).
108 pos = line.find_first_of(String::SPACE_CHARS);
109 if (pos != string::npos) {
110 String r = line.substr(pos + 1);
111 resp.reason = r.trim();
112 line = line.substr(0, pos);
116 if (line.length() != 3) {
117 // FIXME - poner excepciones lindas.
118 throw "Invalid response code";
121 ss << line; // TODO ver forma mas linda de convertir
122 ss >> resp.status_code;
123 is >> static_cast<HTTPMessage&>(resp);
127 ostream& operator<<(ostream& os, const HTTPResponse& resp) {
129 cerr << __FILE__ << ": operator<<()" << endl;
131 os << "HTTP/" << resp.version << " " << resp.status_code << " " << resp.reason << "\r\n";
132 // TODO ver que este bien el \r\l
133 os << static_cast<const HTTPMessage&>(resp);
137 } // namespace Server
139 } // namespace PlaQui