X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/1b53a979e1de9034a9755955ea22cd40ed138f23..d3ea2ff01c51df0df95cd23157267048e2c2fdcd:/Server/src/httprequest.cpp diff --git a/Server/src/httprequest.cpp b/Server/src/httprequest.cpp index 3614610..ce9c5ab 100644 --- a/Server/src/httprequest.cpp +++ b/Server/src/httprequest.cpp @@ -31,84 +31,94 @@ # include #endif // DEBUG -PlaQui::Server::HTTPRequest::~HTTPRequest(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +HTTPRequest::~HTTPRequest(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::HTTPRequest::HTTPRequest(const std::string& version): - PlaQui::Server::HTTPMessage(version) { +HTTPRequest::HTTPRequest(const string& uri, const HTTPMethod& method, + const string& query, const string& version): + HTTPMessage(version), method(method), uri(uri), query(query) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version << std::endl; + cerr << __FILE__ << ": uri = " << uri << " | " + << "method = " << ((method == GET) ? "GET" : "POST") << " | " + << "query = " << query << " | " + << "version = " << version << endl; #endif // DEBUG } /* -PlaQui::Server::HTTPRequest::HTTPRequest(const Serializable& body, - const std::string& version): - PlaQui::Server::HTTPMessage(body, version) { +HTTPRequest::HTTPRequest(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::HTTPRequest::HTTPRequest(const std::string& uri, - const PlaQui::Server::HTTPRequest::HTTPMethod& method, - std::string& query, std::string& version): - PlaQui::Server::HTTPMessage(body, version) { +HTTPRequest::HTTPRequest(const string& uri, + const HTTPRequest::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::HTTPRequest& req) { +istream& operator>>(istream& is, HTTPRequest& req) { #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.length() < 3) { // FIXME - poner excepciones lindas. throw "HTTP/1.1 501 Method Not Implemented"; } // Averiguo método. - std::string::size_type pos = line.find_first_of(String::SPACE_CHARS); + string::size_type pos = line.find_first_of(String::SPACE_CHARS); String met = line.substr(0, pos); if (met.to_upper() == "GET") { - method = GET; + req.method = HTTPRequest::GET; } else if (met.to_upper() == "POST") { - method = POST; + req.method = HTTPRequest::POST; } else { // FIXME - poner excepciones lindas. throw "HTTP/1.1 501 Method Not Implemented"; } // Si tiene sólo el método, no es válido. line = line.substr(pos + 1); - iine.trim(); + line.trim(); if (!line.length()) { // FIXME - poner excepciones lindas. throw "HTTP/1.1 400 Bad Request"; } // Si tiene más espacios, tengo la URI y el protocolo (o un error). pos = line.find_first_of(String::SPACE_CHARS); - if (pos != std::string::npos) { + if (pos != string::npos) { // Si el resto es un protocolo válido, agrego más variables. String protocol = line.substr(pos + 1); protocol = protocol.trim(); if (protocol.to_upper() == "HTTP/1.0") { - version = "1.0"; + req.version = "1.0"; } else if (protocol.to_upper() == "HTTP/1.1") { - version = "1.1"; + req.version = "1.1"; // Si no es un error. } else { // FIXME - poner excepciones lindas. @@ -123,26 +133,33 @@ std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPRequest& req) { throw "HTTP/1.1 400 Bad Request"; } // Si tiene un query string. - pos = request.find("?"); - if (pos != std::string::npos) { - query = line.substr(pos + 1); + pos = line.find("?"); + if (pos != string::npos) { + req.query = line.substr(pos + 1); } else { - query = ""; + req.query = ""; } - uri = line.substr(0, pos); + req.uri = line.substr(0, pos); // Fin de request, obtengo el mensaje. - is >> *(static_cast(this)); + is >> static_cast(req); + //PlaQui::Server::operator>>(is, static_cast(req)); + return is; } -std::ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPRequest& req) { +ostream& operator<<(ostream& os, const HTTPRequest& req) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator<<()" << std::endl; + cerr << __FILE__ << ": operator<<()" << endl; #endif // DEBUG - os << method << " " << uri; - if (query_string.length()) { - os << "?" << query; + os << req.method << " " << req.uri; + if (req.query.length()) { + os << "?" << req.query; } // TODO ver que este bien el \r\l - os << " HTTP/" << version << "\r\l" << *(static_cast(this)); + os << " HTTP/" << req.version << "\n\r" << static_cast(req); + return os; } +} // namespace Server + +} // namespace PlaQui +