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;
54 HTTPResponse::HTTPResponse(const HTTPError& error):
55 status_code(error.code) {
57 cerr << __FILE__ << ": HTTPError(status_code = " << error.code
58 << ", reason = " << HTTPMessage::reason(error.code) << ", desc = " << error.what()
61 set_body(string("<plaqui><error desc=\"") + error.what() + "\" /></plaqui>");
64 HTTPResponse::HTTPResponse(unsigned status_code, const string& body):
65 HTTPMessage(body), status_code(status_code) {
67 cerr << __FILE__ << ": status_code = " << status_code
68 << " | body.length = " << body.length() << endl;
72 istream& operator>>(istream& is, HTTPResponse& resp)
73 throw (HTTPResponse::Error, ios::failure) {
75 cerr << __FILE__ << ": operator>>()" << endl;
78 // Obtengo primera línea (request)
79 if (!is.getline(buf, BUFSIZ)) {
81 throw ios::failure("socket closed");
84 cerr << __FILE__ << ":\tRecibiendo linea: " << buf << endl;
87 // Si es la primera línea, es el request.
88 if (String(line.substr(0, 5)).to_upper() != "HTTP/") {
89 throw HTTPResponse::INVALID_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 throw HTTPResponse::INVALID_HTTP_VERSION;
99 // Si tiene sólo la versión HTTP, no es válido.
100 line = line.substr(pos + 1);
102 if (!line.length()) {
103 throw HTTPResponse::MISSING_HTTP_RESPONSE_CODE;
105 // Si tiene más espacios, tengo la razón (reason).
106 pos = line.find_first_of(String::SPACE_CHARS);
107 if (pos != string::npos) {
108 //FIXME String r = line.substr(pos + 1);
109 //resp.reason = r.trim();
110 line = line.substr(0, pos);
114 // TODO - chequear el codigo a ver si pertenece a uno definido en la RFC.
115 if (line.length() != 3) {
116 throw HTTPResponse::INVALID_HTTP_RESPONSE_CODE;
119 ss << line; // TODO ver forma mas linda de convertir
120 ss >> resp.status_code;
121 is >> static_cast<HTTPMessage&>(resp);
125 ostream& operator<<(ostream& os, const HTTPResponse& resp) {
127 cerr << __FILE__ << ": operator<<()" << endl;
129 os << "HTTP/" << resp.version << " " << resp.status_code << " "
130 << HTTPMessage::reason(resp.status_code) << "\n\r";
131 // TODO ver que este bien el \r\l
132 os << static_cast<const HTTPMessage&>(resp);
136 } // namespace Server
138 } // namespace PlaQui