]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/httpmessage.cpp
737f6365dd0d94debd33e5be2ffb96ec9948e108
[z.facultad/75.42/plaqui.git] / Server / src / httpmessage.cpp
1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
3 //                                  PlaQui
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
6 //
7 // PlaQui is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the Free Software
9 // Foundation; either version 2 of the License, or (at your option) any later
10 // version.
11 //
12 // PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 // details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA  02111-1307  USA
20 //----------------------------------------------------------------------------
21 // Creado:  dom oct 26 22:02:09 ART 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
24 //
25 // $Id$
26 //
27
28 #include "plaqui/server/httpmessage.h"
29 #include <sstream>
30 #include <cstdlib>
31 #ifdef DEBUG
32 #       include <iostream>
33 #endif // DEBUG
34
35 using namespace std;
36 using namespace PlaQui::Server;
37
38 HTTPMessage::~HTTPMessage(void) {
39 #ifdef DEBUG
40         cerr << __FILE__ << ": destructor." << endl;
41 #endif // DEBUG
42 }
43
44 HTTPMessage::HTTPMessage(const string& version):
45                 version(version) {
46 #ifdef DEBUG
47         cerr << __FILE__ << ": version = " << version << endl;
48 #endif // DEBUG
49 }
50
51 /*
52 HTTPMessage::HTTPMessage(const string& _body,
53                 const string& http_version):
54                 http_version(http_version) {
55 #ifdef DEBUG
56         cerr << __FILE__ << ": http_version = " << http_version
57                 << " | body = " << body << endl;
58 #endif // DEBUG
59         set_body(_body);
60 }
61 */
62
63 void HTTPMessage::set_body(const string& _body) {
64         body = _body;
65         if (body.length()) {
66                 stringstream ss; // TODO ver forma mas linda de convertir
67                 ss << body.length();
68                 headers["Content-Length"] = ss.str();
69         }
70 }
71
72 const string& HTTPMessage::get_body(void) const {
73         return body;
74 }
75
76 istream& operator>>(istream& is, HTTPMessage& m) {
77 #ifdef DEBUG
78         cerr << __FILE__ << ": operator>>()" << endl;
79 #endif // DEBUG
80         char buf[BUFSIZ];
81         bool is_header = true;
82         stringstream body_ss;
83         while (is.getline(buf, BUFSIZ)) {
84                 string sbuf = buf;
85                 if (sbuf.length())
86                         if (is_header) {
87                                 istringstream(buf) >> m.headers;
88                         } else {
89                                 body_ss << buf << endl;
90                         }
91                 } else {
92                         if (is_header) {
93                                 is_header = false;
94                         } else {
95                                 body_ss << buf << endl;
96                         }
97                 }
98         }
99         // TODO si el body es un serializable, deberia auto deserializarse.
100         body = body_ss.str();
101         return is;
102 }
103
104 ostream& operator<<(ostream& os, HTTPMessage) {
105 #ifdef DEBUG
106         cerr << __FILE__ << ": operator<<()" << endl;
107 #endif // DEBUG
108         return os << headers << "\r\n" // Fin de cabeceras
109                 << body;
110 }
111