X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/d3ea2ff01c51df0df95cd23157267048e2c2fdcd..ea3d0f30b8e44952b1b9ac3e2f5b2a08cd65ca48:/Server/src/httpresponse.cpp?ds=inline diff --git a/Server/src/httpresponse.cpp b/Server/src/httpresponse.cpp index ab26bd3..2ae86a9 100644 --- a/Server/src/httpresponse.cpp +++ b/Server/src/httpresponse.cpp @@ -40,96 +40,100 @@ namespace Server { HTTPResponse::~HTTPResponse(void) { #ifdef DEBUG - cerr << __FILE__ << ": destructor." << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": destructor." << endl; #endif // DEBUG } -HTTPResponse::HTTPResponse(const string& version): - HTTPMessage(version) { +HTTPResponse::HTTPResponse(const string& body, const string& version): + HTTPMessage(body, version) { #ifdef DEBUG - cerr << __FILE__ << ": version = " << version << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": body.length = " << body.length() + << " | version = " << version << endl; #endif // DEBUG } -/* -HTTPResponse::HTTPResponse(const Serializable& body, - const string& version): - HTTPMessage(body, version) { +HTTPResponse::HTTPResponse(const HTTPError& error): + status_code(error.code) { #ifdef DEBUG - cerr << __FILE__ << ": http_version = " << http_version - << " | body = " << body.serialize() << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": HTTPError(status_code = " << error.code + << ", reason = " << HTTPMessage::reason(error.code) << ", desc = " << error.what() + << ")" << endl; #endif // DEBUG + set_body(string(""); } -HTTPResponse::HTTPResponse(const string& uri, - const HTTPResponse::HTTPMethod& method, - string& query, string& version): - HTTPMessage(body, version) { +HTTPResponse::HTTPResponse(unsigned status_code, const string& body): + HTTPMessage(body), status_code(status_code) { #ifdef DEBUG - cerr << __FILE__ << ": http_version = " << http_version - << " | body = " << body.serialize() << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": status_code = " << status_code + << " | body.length = " << body.length() << endl; #endif // DEBUG } -*/ -istream& operator>>(istream& is, HTTPResponse& resp) { +istream& operator>>(istream& is, HTTPResponse& resp) + throw (HTTPResponse::Error, sockerr, ios::failure) { #ifdef DEBUG - cerr << __FILE__ << ": operator>>()" << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": operator>>()" << endl; #endif // DEBUG char buf[BUFSIZ]; // Obtengo primera línea (request) - is.getline(buf, BUFSIZ); + if (!is.getline(buf, BUFSIZ)) { + // Fin de archivo. + throw ios::failure("eof"); + } #ifdef DEBUG - cerr << "Recibiendo linea: " << buf << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ":\tRecibiendo linea: " << buf << endl; #endif // DEBUG String line(buf); // Si es la primera línea, es el request. - if (line.to_upper().substr(0, 4) != "HTTP/") { - // FIXME - poner excepciones lindas. - throw "Not a HTTP response"; + if (String(line.substr(0, 5)).to_upper() != "HTTP/") { + throw HTTPResponse::INVALID_HTTP_RESPONSE; } // Averiguo la versión. string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5); - string ver = line.substr(5, pos); + string ver = line.substr(5, pos - 5); if ((ver == "1.1") || (ver == "1.0")) { resp.version = ver; } else { - // FIXME - poner excepciones lindas. - throw "Invalid HTTP version"; + throw HTTPResponse::INVALID_HTTP_VERSION; } // Si tiene sólo la versión HTTP, no es válido. line = line.substr(pos + 1); line.trim(); if (!line.length()) { - // FIXME - poner excepciones lindas. - throw "Invalid HTTP response"; + throw HTTPResponse::MISSING_HTTP_RESPONSE_CODE; } // Si tiene más espacios, tengo la razón (reason). pos = line.find_first_of(String::SPACE_CHARS); if (pos != string::npos) { - String r = line.substr(pos + 1); - resp.reason = r.trim(); + //FIXME String r = line.substr(pos + 1); + //resp.reason = r.trim(); line = line.substr(0, pos); } line = line.trim(); // Seteo el código. + // TODO - chequear el codigo a ver si pertenece a uno definido en la RFC. if (line.length() != 3) { - // FIXME - poner excepciones lindas. - throw "Invalid response code"; + throw HTTPResponse::INVALID_HTTP_RESPONSE_CODE; } - stringstream ss; - ss << line; // TODO ver forma mas linda de convertir - ss >> resp.status_code; + to(line, resp.status_code); is >> static_cast(resp); return is; } -ostream& operator<<(ostream& os, const HTTPResponse& resp) { +ostream& operator<<(ostream& os, const HTTPResponse& resp) throw(sockerr) { #ifdef DEBUG - cerr << __FILE__ << ": operator<<()" << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": operator<<()" << endl; #endif // DEBUG - os << "HTTP/" << resp.version << " " << resp.status_code << " " << resp.reason << "\n\r"; - // TODO ver que este bien el \r\l + os << "HTTP/" << resp.version << " " << resp.status_code << " " + << HTTPMessage::reason(resp.status_code) << "\r\n"; os << static_cast(resp); return os; }