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__ << "(" << __LINE__ << ")"
44 << ": destructor." << endl;
48 HTTPResponse::HTTPResponse(const string& body, const string& version):
49 HTTPMessage(body, version) {
51 cerr << __FILE__ << "(" << __LINE__ << ")"
52 << ": body.length = " << body.length()
53 << " | version = " << version << endl;
57 HTTPResponse::HTTPResponse(const HTTPError& error):
58 status_code(error.code) {
60 cerr << __FILE__ << "(" << __LINE__ << ")"
61 << ": HTTPError(status_code = " << error.code
62 << ", reason = " << HTTPMessage::reason(error.code) << ", desc = " << error.what()
65 set_body(string("<response desc=\"") + error.what() + "\" />");
68 HTTPResponse::HTTPResponse(unsigned status_code, const string& body):
69 HTTPMessage(body), status_code(status_code) {
71 cerr << __FILE__ << "(" << __LINE__ << ")"
72 << ": status_code = " << status_code
73 << " | body.length = " << body.length() << endl;
77 istream& operator>>(istream& is, HTTPResponse& resp)
78 throw (HTTPResponse::Error, sockerr, ios::failure) {
80 cerr << __FILE__ << "(" << __LINE__ << ")"
81 << ": operator>>()" << endl;
84 // Obtengo primera línea (request)
85 if (!is.getline(buf, BUFSIZ)) {
87 throw ios::failure("eof");
90 cerr << __FILE__ << "(" << __LINE__ << ")"
91 << ":\tRecibiendo linea: " << buf << endl;
94 // Si es la primera línea, es el request.
95 if (String(line.substr(0, 5)).to_upper() != "HTTP/") {
96 throw HTTPResponse::INVALID_HTTP_RESPONSE;
98 // Averiguo la versión.
99 string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5);
100 string ver = line.substr(5, pos - 5);
101 if ((ver == "1.1") || (ver == "1.0")) {
104 throw HTTPResponse::INVALID_HTTP_VERSION;
106 // Si tiene sólo la versión HTTP, no es válido.
107 line = line.substr(pos + 1);
109 if (!line.length()) {
110 throw HTTPResponse::MISSING_HTTP_RESPONSE_CODE;
112 // Si tiene más espacios, tengo la razón (reason).
113 pos = line.find_first_of(String::SPACE_CHARS);
114 if (pos != string::npos) {
115 //FIXME String r = line.substr(pos + 1);
116 //resp.reason = r.trim();
117 line = line.substr(0, pos);
121 // TODO - chequear el codigo a ver si pertenece a uno definido en la RFC.
122 if (line.length() != 3) {
123 throw HTTPResponse::INVALID_HTTP_RESPONSE_CODE;
126 ss << line; // TODO ver forma mas linda de convertir
127 ss >> resp.status_code;
128 is >> static_cast<HTTPMessage&>(resp);
132 ostream& operator<<(ostream& os, const HTTPResponse& resp) throw(sockerr) {
134 cerr << __FILE__ << "(" << __LINE__ << ")"
135 << ": operator<<()" << endl;
137 os << "HTTP/" << resp.version << " " << resp.status_code << " "
138 << HTTPMessage::reason(resp.status_code) << "\r\n";
139 os << static_cast<const HTTPMessage&>(resp);
143 } // namespace Server
145 } // namespace PlaQui