]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/httpmessage.cpp
Se corrige el XML a mano para probarlo.
[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/string.h"
29 #include "plaqui/server/httpmessage.h"
30 #include <sstream>
31 #include <cstdlib>
32 #ifdef DEBUG
33 #       include <iostream>
34 #endif // DEBUG
35
36 using namespace std;
37
38 namespace PlaQui {
39         
40 namespace Server {
41
42 HTTPMessage::~HTTPMessage(void) {
43 #ifdef DEBUG
44         cerr << __FILE__ << "(" << __LINE__ << ")"
45                 << ": destructor." << endl;
46 #endif // DEBUG
47 }
48
49 HTTPMessage::HTTPMessage(const string& _body, const string& _version):
50                 version(_version) {
51 #ifdef DEBUG
52         cerr << __FILE__ << "(" << __LINE__ << ")"
53                 << ": version = " << version << " | body ("
54                 << _body.length() << ") = " << _body << endl;
55 #endif // DEBUG
56         headers["Accept-Ranges"] = "bytes";
57         set_body(_body);
58 }
59
60 void HTTPMessage::set_body(const string& _body) {
61         body = _body;
62         headers["Content-Length"] = String().from(body.length());
63 }
64
65 const string& HTTPMessage::get_body(void) const {
66         return body;
67 }
68
69 istream& operator>>(istream& is, HTTPMessage& m) {
70 #ifdef DEBUG
71         cerr << __FILE__ << "(" << __LINE__ << ")"
72                 << ": operator>>()" << endl;
73 #endif // DEBUG
74         char buf[BUFSIZ];
75         while (is.getline(buf, BUFSIZ)) {
76                 String sbuf(buf);
77                 sbuf.trim();
78                 if (sbuf.length()) {
79                         stringstream ss(sbuf);
80                         ss >> m.headers;
81                 // Fin de las cabeceras.
82                 } else {
83                         // Hay Content-Length, entonces hay body (no respeta RFC AFAIK).
84                         if (m.headers.find("Content-Length") != m.headers.end()) {
85                                 streamsize size, readed;
86                                 to(m.headers["Content-Length"], size);
87                                 char* const buf2 = new char[size+1];
88                                 if ((readed = is.readsome(buf2, size)) == size) {
89                                         // Agrego fin de string porque el readsome no lo hace.
90                                         buf2[size] = '\0';
91                                         m.set_body(buf2);
92 #ifdef DEBUG
93                                         cerr << __FILE__ << "(" << __LINE__ << ")"
94                                                 << ": operator>>() raaaaw body: " << buf2 << endl;
95 #endif // DEBUG
96                                 } else {
97                                         // TODO else dar error?
98 #ifdef DEBUG
99                                         cerr << __FILE__ << "(" << __LINE__ << ")"
100                                                 << ": operator>>() ERROR: No se pudo leer el mensaje completo. Se leyeron sóolo "
101                                                 << readed << " bytes de " << size << " que deberían haberse leído." << endl;
102 #endif // DEBUG
103                                 }
104                                 delete []buf2;
105                         }
106                         // Después de una línea vacía, haya obtenido el body o no, sale del
107                         // while.
108                         break;
109                 }
110         }
111         return is;
112 }
113
114 ostream& operator<<(ostream& os, const HTTPMessage& m) {
115 #ifdef DEBUG
116         cerr << __FILE__ << "(" << __LINE__ << ")"
117                 << ": operator<<()" << endl;
118 #endif // DEBUG
119         return os << m.headers << "\r\n" // Fin de cabeceras
120                 << m.body;
121 }
122
123 string HTTPMessage::reason(unsigned code) {
124         switch (code) {
125                 // TODO completar los códigos.
126                 case OK:
127                         return "OK";
128                 case BAD_REQUEST:
129                         return "Bad Request";
130                 case NOT_FOUND:
131                         return "Not Found";
132                 case LENGTH_REQUIRED:
133                         return "Length Required";
134                 case INTERNAL_SERVER_ERROR:
135                         return "Internal Server Error";
136                 case NOT_IMPLEMENTED:
137                         return "Not Implemented";
138                 case HTTP_VERSION_NOT_SUPPORTED:
139                         return "HTTP Version Not Supported";
140                 default:
141                         return "No Reason";
142         }
143 }
144
145 } // namespace Server
146
147 } // namespace PlaQui
148