]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Server/src/httpheaders.cpp
Mini bugfix.
[z.facultad/75.42/plaqui.git] / Server / src / httpheaders.cpp
index d0736b2a39aa5cf6a01ee4619873c54cbe504de3..b54201e6dc7daf862c2e16feba675974a103ae81 100644 (file)
 // $Id$
 //
 
+#include "plaqui/server/httperror.h"
+#include "plaqui/server/httpmessage.h"
 #include "plaqui/server/httpheaders.h"
+#include "plaqui/server/string.h"
 //#include <cstdlib>
 #ifdef DEBUG
 #      include <iostream>
 #endif // DEBUG
 
-PlaQui::Server::HTTPHeaders::~HTTPHeaders(void) {
-#ifdef DEBUG
-       std::cerr << __FILE__ << ": destructor." << std::endl;
-#endif // DEBUG
-}
-/*
-PlaQui::Server::HTTPMessage::HTTPMessage(const std::string& http_version):
-               http_version(http_version) {
+using namespace std;
+
+namespace PlaQui {
+
+namespace Server {
+
+HTTPHeaders::~HTTPHeaders(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) {
+istream& operator>>(istream& is, HTTPHeaders& h)
+               throw(HTTPError, sockerr, ios::failure) {
 #ifdef DEBUG
-       std::cerr << __FILE__ << ": http_version = " << http_version
-               << " | body = " << body << std::endl;
+       cerr << __FILE__ << "(" << __LINE__ << ")"
+               << ": operator>>()" << endl;
 #endif // DEBUG
-       set_body(_body);
-}
-*/
-ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPMessage) {
+       char buf[BUFSIZ];
+       // 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 HTTPError(HTTPMessage::BAD_REQUEST, sbuf + ": No es una cabecera válida.");
+       }
+       h[sbuf.substr(0, pos)] = String(sbuf.substr(pos + 1)).trim();
 #ifdef DEBUG
-       std::cerr << __FILE__ << ": operator<<()" << std::endl;
+       cerr << __FILE__ << "(" << __LINE__ << ")"
+               << "    " << sbuf.substr(0, pos) << " = "
+               << h[sbuf.substr(0, pos)] << endl;
 #endif // DEBUG
-       for (HTTPMessage::const_iterator i = begin(); i != end(); ++i) {
-               os << i->first << ": " << i->second << std::flush;
-       return os << headers << "\r\l" // Fin de cabeceras
+       return is;
 }
 
-istream& operator>>(std::istream& is, 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
-       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;
-                       }
-               } else {
-                       if (is_header) {
-                               is_header = false;
-                       } else {
-                               body_ss << buf << std::endl;
-                       }
-               }
+       for (HTTPHeaders::const_iterator i = h.begin();
+                       i != h.end(); ++i) {
+               os << i->first << ": " << i->second << "\r\n";
        }
-       // TODO El body debería leer solo el content-length.
-       body = body_ss.str();
-       return is;
+       return os;
 }
 
+} // namespace Server
+
+} // namespace PlaQui
+