X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/1b53a979e1de9034a9755955ea22cd40ed138f23..f0ca04947cd23aa5f973f184941dc5af13b43744:/Server/src/httpresponse.cpp?ds=inline diff --git a/Server/src/httpresponse.cpp b/Server/src/httpresponse.cpp index fb6ba78..ab26bd3 100644 --- a/Server/src/httpresponse.cpp +++ b/Server/src/httpresponse.cpp @@ -27,80 +27,88 @@ #include "plaqui/server/httpresponse.h" #include "plaqui/server/string.h" +#include #ifdef DEBUG # include #endif // DEBUG -PlaQui::Server::HTTPRequest::~HTTPResponse(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +HTTPResponse::~HTTPResponse(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::HTTPResponse::HTTPResponse(const std::string& version): - PlaQui::Server::HTTPMessage(version) { +HTTPResponse::HTTPResponse(const string& version): + HTTPMessage(version) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version << std::endl; + cerr << __FILE__ << ": version = " << version << endl; #endif // DEBUG } /* -PlaQui::Server::HTTPResponse::HTTPResponse(const Serializable& body, - const std::string& version): - PlaQui::Server::HTTPMessage(body, version) { +HTTPResponse::HTTPResponse(const Serializable& body, + const string& version): + HTTPMessage(body, version) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version - << " | body = " << body.serialize() << std::endl; + cerr << __FILE__ << ": http_version = " << http_version + << " | body = " << body.serialize() << endl; #endif // DEBUG } -PlaQui::Server::HTTPResponse::HTTPResponse(const std::string& uri, - const PlaQui::Server::HTTPResponse::HTTPMethod& method, - std::string& query, std::string& version): - PlaQui::Server::HTTPMessage(body, version) { +HTTPResponse::HTTPResponse(const string& uri, + const HTTPResponse::HTTPMethod& method, + string& query, string& version): + HTTPMessage(body, version) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version - << " | body = " << body.serialize() << std::endl; + cerr << __FILE__ << ": http_version = " << http_version + << " | body = " << body.serialize() << endl; #endif // DEBUG } */ -std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPResponse& req) { +istream& operator>>(istream& is, HTTPResponse& resp) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator>>()" << std::endl; + cerr << __FILE__ << ": operator>>()" << endl; #endif // DEBUG char buf[BUFSIZ]; // Obtengo primera línea (request) is.getline(buf, BUFSIZ); #ifdef DEBUG - std::cerr << "Recibiendo linea: " << buf << std::endl; + cerr << "Recibiendo linea: " << buf << endl; #endif // DEBUG - String line = buf; + 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"; } // Averiguo la versión. - std::string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5); - std::string ver = line.substr(5, pos); + string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5); + string ver = line.substr(5, pos); if ((ver == "1.1") || (ver == "1.0")) { - version = ver; + resp.version = ver; } else { // FIXME - poner excepciones lindas. throw "Invalid HTTP version"; } // Si tiene sólo la versión HTTP, no es válido. - line = line.substr(pos + 1).trim(); + line = line.substr(pos + 1); + line.trim(); if (!line.length()) { // FIXME - poner excepciones lindas. throw "Invalid HTTP response"; } // Si tiene más espacios, tengo la razón (reason). pos = line.find_first_of(String::SPACE_CHARS); - if (pos != std::string::npos) { - // Si el resto es un protocolo válido, agrego más variables. - String reas = line.substr(pos + 1).trim(); + if (pos != string::npos) { + String r = line.substr(pos + 1); + resp.reason = r.trim(); line = line.substr(0, pos); } line = line.trim(); @@ -109,16 +117,24 @@ std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPResponse& req) { // FIXME - poner excepciones lindas. throw "Invalid response code"; } - stringstream ss = line; // TODO ver forma mas linda de convertir - ss >> status_code; + stringstream ss; + ss << line; // TODO ver forma mas linda de convertir + ss >> resp.status_code; + is >> static_cast(resp); + return is; } -std::ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPResponse& req) { +ostream& operator<<(ostream& os, const HTTPResponse& resp) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator<<()" << std::endl; + cerr << __FILE__ << ": operator<<()" << endl; #endif // DEBUG - os << "HTTP/" << version << " " << status_code << " " << reason << "\r\l"; + os << "HTTP/" << resp.version << " " << resp.status_code << " " << resp.reason << "\n\r"; // TODO ver que este bien el \r\l - os << *(static_cast(this)); + os << static_cast(resp); + return os; } +} // namespace Server + +} // namespace PlaQui +