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& body, const string& version):
48 HTTPMessage(body, version) {
50 cerr << __FILE__ << ": body.length = " << body.length()
51 << " | version = " << version << endl;
55 HTTPResponse::HTTPResponse(const HTTPError& error):
56 status_code(error.code) {
58 cerr << __FILE__ << ": HTTPError(status_code = " << error.code
59 << ", reason = " << HTTPMessage::reason(error.code) << ", desc = " << error.what()
62 set_body(string("<plaqui><error desc=\"") + error.what() + "\" /></plaqui>");
65 HTTPResponse::HTTPResponse(unsigned status_code, const string& body):
66 HTTPMessage(body), status_code(status_code) {
68 cerr << __FILE__ << ": status_code = " << status_code
69 << " | body.length = " << body.length() << endl;
73 istream& operator>>(istream& is, HTTPResponse& resp)
74 throw (HTTPResponse::Error, ios::failure) {
76 cerr << __FILE__ << ": operator>>()" << endl;
79 // Obtengo primera línea (request)
80 if (!is.getline(buf, BUFSIZ)) {
82 throw ios::failure("socket closed");
85 cerr << __FILE__ << ":\tRecibiendo linea: " << buf << endl;
88 // Si es la primera línea, es el request.
89 if (String(line.substr(0, 5)).to_upper() != "HTTP/") {
90 throw HTTPResponse::INVALID_HTTP_RESPONSE;
92 // Averiguo la versión.
93 string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5);
94 string ver = line.substr(5, pos - 5);
95 if ((ver == "1.1") || (ver == "1.0")) {
98 throw HTTPResponse::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 throw HTTPResponse::MISSING_HTTP_RESPONSE_CODE;
106 // Si tiene más espacios, tengo la razón (reason).
107 pos = line.find_first_of(String::SPACE_CHARS);
108 if (pos != string::npos) {
109 //FIXME String r = line.substr(pos + 1);
110 //resp.reason = r.trim();
111 line = line.substr(0, pos);
115 // TODO - chequear el codigo a ver si pertenece a uno definido en la RFC.
116 if (line.length() != 3) {
117 throw HTTPResponse::INVALID_HTTP_RESPONSE_CODE;
120 ss << line; // TODO ver forma mas linda de convertir
121 ss >> resp.status_code;
122 is >> static_cast<HTTPMessage&>(resp);
126 ostream& operator<<(ostream& os, const HTTPResponse& resp) {
128 cerr << __FILE__ << ": operator<<()" << endl;
130 os << "HTTP/" << resp.version << " " << resp.status_code << " "
131 << HTTPMessage::reason(resp.status_code) << "\r\n";
132 os << static_cast<const HTTPMessage&>(resp);
136 } // namespace Server
138 } // namespace PlaQui