X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/cd6078479123cd2cfe62df2d838633c143649edf..HEAD:/Server/src/httpmessage.cpp?ds=inline diff --git a/Server/src/httpmessage.cpp b/Server/src/httpmessage.cpp index 625d82e..c110808 100644 --- a/Server/src/httpmessage.cpp +++ b/Server/src/httpmessage.cpp @@ -25,87 +25,126 @@ // $Id$ // +#include "plaqui/server/string.h" #include "plaqui/server/httpmessage.h" +#include #include #ifdef DEBUG # include #endif // DEBUG -PlaQui::Server::HTTPMessage::~HTTPMessage(void) { -#ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; -#endif // DEBUG -} +using namespace std; + +namespace PlaQui { + +namespace Server { -PlaQui::Server::HTTPMessage::HTTPMessage(const std::string& http_version): - http_version(http_version) { +HTTPMessage::~HTTPMessage(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::HTTPMessage::HTTPMessage(const std::string& _body, - const std::string& http_version): - http_version(http_version) { +HTTPMessage::HTTPMessage(const string& _body, const string& _version): + version(_version) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version - << " | body = " << body << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": version = " << version << " | body (" + << _body.length() << ") = " << _body << endl; #endif // DEBUG + headers["Accept-Ranges"] = "bytes"; set_body(_body); } -void PlaQui::Server::HTTPMessage::set_body(const std::string& _body) { +void HTTPMessage::set_body(const string& _body) { body = _body; - if (body.length()) { - stringstream ss; // TODO ver forma mas linda de convertir - ss << body.length(); - headers["Content-Length"] = ss.str(); - } + headers["Content-Length"] = String().from(body.length()); } -const std::string& PlaQui::Server::HTTPMessage::get_body(void) { +const string& HTTPMessage::get_body(void) const { return body; } -ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPMessage) { +istream& operator>>(istream& is, HTTPMessage& m) + throw(HTTPError, sockerr, ios::failure) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator<<()" << std::endl; -#endif // DEBUG - return os << headers << "\r\l" // Fin de cabeceras - << body << std::flush; -} - -istream& operator>>(std::istream& is, PlaQui::Server::HTTPMessage) { -#ifdef DEBUG - std::cerr << __FILE__ << ": operator>>()" << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": operator>>()" << endl; #endif // DEBUG char buf[BUFSIZ]; - bool is_header = true; - stringstream body_ss; while (is.getline(buf, BUFSIZ)) { - std::string sbuf = buf; - if (sbuf.length()) - if (is_header) { - // TODO esto va al operator>> de HTTPHeaders. - std::string::size_type pos = sbuf.find(":"); - if (pos == std::string::npos) { - // FIXME poner mejores excepciones. - throw "Wrong header"; - } - headers[sbuf.substr(0, pos)] = sbuf.substr(pos + 1); - } else { - body_ss << buf << std::endl; - } + String sbuf(buf); + sbuf.trim(); + if (sbuf.length()) { + stringstream ss(sbuf); + ss >> m.headers; + // Fin de las cabeceras. } else { - if (is_header) { - is_header = false; - } else { - body_ss << buf << std::endl; + // Hay Content-Length, entonces hay body (no respeta RFC AFAIK). + if (m.headers.find("Content-Length") != m.headers.end()) { + streamsize size, readed; + to(m.headers["Content-Length"], size); + char* const buf2 = new char[size+1]; + is.read(buf2, size); + if (is.gcount() == size) { + // Agrego fin de string porque el readsome no lo hace. + buf2[size] = '\0'; + m.set_body(buf2); +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": operator>>() raaaaw body: " << buf2 << endl; +#endif // DEBUG + } else { + // TODO else dar error? +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": operator>>() ERROR: No se pudo leer el mensaje completo. Se leyeron sóolo " + << is.gcount() << " bytes de " << size << " que deberían haberse leído." << endl; +#endif // DEBUG + } + delete []buf2; } + // Después de una línea vacía, haya obtenido el body o no, sale del + // while. + break; } } - // TODO si el body es un serializable, deberia auto deserializarse. - body = body_ss.str(); return is; } +ostream& operator<<(ostream& os, const HTTPMessage& m) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": operator<<()" << endl; +#endif // DEBUG + return os << m.headers << "\r\n" // Fin de cabeceras + << m.body; +} + +string HTTPMessage::reason(unsigned code) { + switch (code) { + // TODO completar los códigos. + case OK: + return "OK"; + case BAD_REQUEST: + return "Bad Request"; + case NOT_FOUND: + return "Not Found"; + case LENGTH_REQUIRED: + return "Length Required"; + case INTERNAL_SERVER_ERROR: + return "Internal Server Error"; + case NOT_IMPLEMENTED: + return "Not Implemented"; + case HTTP_VERSION_NOT_SUPPORTED: + return "HTTP Version Not Supported"; + default: + return "No Reason"; + } +} + +} // namespace Server + +} // namespace PlaQui +