X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/1b53a979e1de9034a9755955ea22cd40ed138f23..0ed3ebf8af491d51f1d7e5226fce6ea7cf56bf2d:/Server/src/httpheaders.cpp diff --git a/Server/src/httpheaders.cpp b/Server/src/httpheaders.cpp index 67ec270..b54201e 100644 --- a/Server/src/httpheaders.cpp +++ b/Server/src/httpheaders.cpp @@ -25,41 +25,67 @@ // $Id$ // +#include "plaqui/server/httperror.h" +#include "plaqui/server/httpmessage.h" #include "plaqui/server/httpheaders.h" +#include "plaqui/server/string.h" //#include #ifdef DEBUG # include #endif // DEBUG -PlaQui::Server::HTTPHeaders::~HTTPHeaders(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +HTTPHeaders::~HTTPHeaders(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": destructor." << endl; #endif // DEBUG } -istream& operator>>(std::istream& is, PlaQui::Server::httpheaders& h) { +istream& operator>>(istream& is, HTTPHeaders& h) + throw(HTTPError, sockerr, ios::failure) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator>>()" << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": operator>>()" << endl; #endif // DEBUG char buf[BUFSIZ]; - is.getline(buf, BUFSIZ); - std::string sbuf = buf; - std::string::size_type pos = sbuf.find(":"); - if (pos == std::string::npos) { + // Fin del stream, no había cabeceras. + if (!is.getline(buf, BUFSIZ)) { + throw ios::failure("eof"); + } + string sbuf = buf; + string::size_type pos = sbuf.find(":"); + if (pos == string::npos) { // FIXME poner mejores excepciones. - throw "Wrong header"; + throw HTTPError(HTTPMessage::BAD_REQUEST, sbuf + ": No es una cabecera válida."); } - (*this)[sbuf.substr(0, pos)] = sbuf.substr(pos + 1); + h[sbuf.substr(0, pos)] = String(sbuf.substr(pos + 1)).trim(); +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << " " << sbuf.substr(0, pos) << " = " + << h[sbuf.substr(0, pos)] << endl; +#endif // DEBUG return is; } -ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPMessage) { +ostream& operator<<(ostream& os, const HTTPHeaders& h) throw(sockerr) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator<<()" << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": operator<<()" << endl; #endif // DEBUG - for (HTTPMessage::const_iterator i = begin(); i != end(); ++i) { - os << i->first << ": " << i->second << "\r\l"; + for (HTTPHeaders::const_iterator i = h.begin(); + i != h.end(); ++i) { + os << i->first << ": " << i->second << "\r\n"; } return os; } +} // namespace Server + +} // namespace PlaQui +