1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_http_Message_hpp
38 #define anna_http_Message_hpp
40 #include <anna/core/util/Recycler.hpp>
42 #include <anna/comm/Message.hpp>
44 #include <anna/http/Header.hpp>
45 #include <anna/http/Version.hpp>
56 Clase base de los mensajes HTTP segun la RFC 2616.
58 class Message : public comm::Message {
60 typedef Recycler <Header>::iterator header_iterator;
61 typedef Recycler <Header>::const_iterator const_header_iterator;
64 * Para poder acceder a todos los métodos de anna::comm::Message;;setBody cuando
65 * se invocan desde un anna::http::Message (o heredados).
67 using comm::Message::setBody;
70 Tipos de mensaje HTTP.
75 Request, /**< Peticion HTTP */
76 Response /**< Respuesta HTTP */
81 Devuelve el tipo de este mensaje HTTP.
82 \return el tipo de este mensaje HTTP.
84 Type::_v getType() const throw() { return a_type; }
87 Devuelve la version del protocolo HTTP usada en el mensaje.
88 \return la version del protocolo HTTP usada en el mensaje.
90 const std::string& getVersion() const throw() { return a_version; }
93 * Devuelve el enumerado que identifica la versión HTTP usada en el mensaje.
94 * \return el enumerado que identifica la versión HTTP usada en el mensaje.
96 Version::_v getVersionAsEnum() const throw() { return Version::asEnum(a_version); }
99 * Devuelve los parámetros extras que pueden haber estado contenidos en los bloques
100 * de datos, en caso de que el mensaje se haya recibido como un "Transfer-Encoding: chunked".
101 * \return Los parámetros extras que pueden haber estado contenidos en los bloques
103 const std::string& getExtraParameters() const throw() { return a_extraParameters; }
106 Establece la version a codificar en el mensaje.
107 \param version Texto con la version HTTP a usar, HTTP/1.1, por ejemplo.
109 void setVersion(const std::string& version) throw() { a_version = version; }
112 * Establece la versión a codificar en el mensaje.
113 * \param version Identificador de la versión.
115 void setVersion(const Version::_v version) throw() { a_version = Version::asCString(version); }
118 * Establece el cuerpo de este mensaje con el contenido del documento XML correspondiente al
119 * nodo XML recibido como parámetro.
121 * También establece la cabecera \em Content-Type.
123 * \param node Nodo XML que contiene el documento XML.
125 comm::Message* setBody(const xml::Node* node) throw(RuntimeException);
128 Crea una nueva cabecera en este mensaje.
129 \param type Tipo de cabecera a crear.
130 \return La instancia de la nueva cabecera.
132 Header* createHeader(const Header::Type::_v type)
133 throw(RuntimeException) {
134 return a_headers.create()->initialize(type);
138 Crea una nueva cabecera en este mensaje.
139 \param name Nombre de la cabecera a crear.
140 \return La instancia de la nueva cabecera.
142 Header* createHeader(const std::string& name)
143 throw(RuntimeException) {
144 return a_headers.create()->initialize(name);
148 Devuelve la instancia de la primera cabecera que coincide con el tipo
149 recibido como parametro. Puede ser NULL si el mensaje no contiene ninguna
150 cabecera del tipo solicitado.
151 \param type Tipo de cabecera buscado.
152 \return La primera cabecera que coincide con el tipo recibido como parametro.
154 const Header* find(const Header::Type::_v type) const throw() {
155 return const_cast <Message*>(this)->find(type);
159 Devuelve la instancia de la primera cabecera que coincide con el tipo
160 recibido como parametro. Puede ser NULL si el mensaje no contiene ninguna
161 cabecera del tipo solicitado.
162 \param type Tipo de cabecera buscado.
163 \return La primera cabecera que coincide con el tipo recibido como parametro.
165 Header* find(const Header::Type::_v type) throw();
168 Devuelve la instancia de la primera cabecera de tipo Header::Category::Extension cuyo
169 nombre coincide con el recibido como parametro. Puede ser NULL.
170 \param name Nombre de la extension buscada.
171 \return La primera cabecera que coincide con el tipo recibido como parametro.
173 Header* find(const char* name) throw();
176 Inicializa el contenido de este mensaje. Libera las cabeceras y el cuerpo del mensaje.
178 void clear() throw() { a_headers.clear(); clearBody(); a_extraParameters.clear();}
181 Devuelve un iterador al comienzo de la lista de cabeceras.
182 \return un iterador al comienzo de la lista de cabeceras.
184 header_iterator header_begin() throw() { return a_headers.begin(); }
187 Devuelve un iterador al final de la lista de cabeceras.
188 \return un iterador al final de la lista de cabeceras.
190 header_iterator header_end() throw() { return a_headers.end(); }
193 Devuelve un iterador al comienzo de la lista de cabeceras.
194 \return un iterador al comienzo de la lista de cabeceras.
196 const_header_iterator header_begin() const throw() { return a_headers.begin(); }
199 Devuelve un iterador al final de la lista de cabeceras.
200 \return un iterador al final de la lista de cabeceras.
202 const_header_iterator header_end() const throw() { return a_headers.end(); }
205 Codifica este mensaje.
206 \return El bloque de datos que contiene el mensaje codificado.
208 const DataBlock& code() throw(anna::RuntimeException);
211 Devuelve una cadena con toda la informacion relevante de este objeto.
212 \return una cadena con toda la informacion relevante de este objeto.
214 virtual std::string asString() const throw() = 0;
217 Devuelve la instancia de la cabecera a la que referencia el iterator
218 recibido como parametro.
219 \return la instancia de la cabecera a la que referencia el iterator
220 recibido como parametro.
222 static Header* header(header_iterator& ii) throw() { return Recycler<Header>::data(ii); }
225 Devuelve la instancia de la cabecera a la que referencia el iterator
226 recibido como parametro.
227 \return la instancia de la cabecera a la que referencia el iterator
228 recibido como parametro.
230 static const Header* header(const_header_iterator& ii) throw() { return Recycler<Header>::data(ii); }
235 \param type Tipo de mensaje.
237 Message(const Type::_v type) : comm::Message(StatusCodeBuffer::Reserve),
238 a_type(type), a_version("HTTP/1.1")
242 const Type::_v a_type;
243 std::string a_version;
244 Recycler <Header> a_headers;
245 std::string a_extraParameters;
247 void codeLine(const std::string& line) throw(RuntimeException);
249 void appendExtraParameter(const std::string& extraParameter) throw() {
250 a_extraParameters += ' ';
251 a_extraParameters += extraParameter;
254 virtual std::string codeStartLine() const throw(anna::RuntimeException) = 0;
256 friend class parser::Abstract;