Updated license
[anna.git] / include / anna / http / Header.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_http_Header_hpp
38 #define anna_http_Header_hpp
39
40 #include <anna/core/RuntimeException.hpp>
41 #include <anna/core/Allocator.hpp>
42
43 namespace anna {
44
45 namespace http {
46
47 class Token;
48 class Message;
49
50 /**
51    Representacion de las cabeceras HTTP.
52 */
53 class Header {
54 public:
55   /**
56      Tipos de cabeceras definidos en la RFC 2616.
57      \see Header.
58   */
59   struct Type {
60     enum _v {
61       None,
62       /* Generales */
63       CacheControl, Connection, Date, Pragma, Trailer, TransferEncoding, Upgrade, Via,
64       Warning,
65       /* Request  */
66       Accept, AcceptCharset, AcceptEncoding, AcceptLanguage, Authorization, Expect, From,
67       Host, IfMatch, IfModifiedSince, IfNoneMatch, IfRange, IfUnmodifiedSince, MaxForwards,
68       ProxyAuthorization, Range, Referer, TE, UserAgent,
69       /* Entity Header */
70       Allow, ContentEncoding, ContentLanguage, ContentLength, ContentLocation, ContentMD5,
71       ContentRange, ContentType, Expires, LastModified,
72       /* Response Header */
73       AcceptRanges, Age, ETAG, Location, ProxyAuthenticate, RetryAfter, Server, Vary,
74       WWWAuthenticate,
75       /* Uso interno */
76       Unknown, End, Begin = 1
77     };
78   };
79
80   /**
81      Categorias de cabeceras definidos en la RFC 2616.
82      \see Header.
83   */
84   struct Category { enum _v { General, Response, Request, Entity, Extension }; };
85
86   /**
87      Modos de comparacion aplicados en el metodo #compare.
88      \see Header.
89   */
90   struct Compare { enum _v { Exact = 0, RightTrim = 1, LeftTrim = 2, NoCase = 4, FullMode = 7 }; };
91
92   /**
93      Destructor.
94   */
95   ~Header() { delete a_extensionName; }
96
97   /**
98      Devuelve el tipo de la cabecera.
99      \return el tipo de la cabecera.
100   */
101   Type::_v getType() const throw() { return a_type; }
102
103   /**
104      Devuelve la categoria de la cabecera.
105      \return la categoria de la cabecera.
106   */
107   Category::_v getCategory() const throw() { return a_category; }
108
109   /**
110      Devuelve el puntero nombre de la extension, puede ser NULL.
111      \return el puntero nombre de la extension, puede ser NULL.
112   */
113   const std::string* getExtensionName() const throw() { return a_extensionName; }
114
115   /**
116      Devuelve el valor asociado a esta cabecera.
117      \return el valor asociado a esta cabecera.
118   */
119   const std::string& getStringValue() const throw() { return a_value; }
120
121   /**
122      Devuelve el valor numerico asociado a esta cabecera.
123      \return el valor numerico asociado a esta cabecera.
124   */
125   const int getIntegerValue() const throw();
126
127   /**
128      Establece el valor de esta cabecera.
129      \param token Token del que obtendremos el valor.
130      \warning Exclusivamente uso interno.
131   */
132   void setValue(const Token* token) throw();
133
134   /**
135      Establece el valor asociado a esta cabecera.
136      \param value Valor a establecer.
137   */
138   void setValue(const std::string& value) throw() { a_value = value; }
139
140   /**
141      Establece el valor asociado a esta cabecera.
142      \param value Valor a establecer.
143   */
144   void setValue(const int value) throw();
145
146   /**
147      Operador de copia.
148      \param other Cabecera de la que copiar.
149      \return La instancia de si misma.
150   */
151   Header& operator = (const Header& other) throw();
152
153   /**
154      Compara el contenido actual de esta cabecera con el literal recibido como parametro y
155      devuelve  un entero menor, igual o mayor que cero si se encuentra que el contenido
156      es,  respectivamente, menor que, igual a (concordante), o mayor que \em str.
157
158      \param str La cadena con la que comparar.
159      \param flags Modo de comparacion aplicado.
160
161      \return Un entero menor, igual o mayor que cero si se encuentra que el contenido
162      es, respectivamente, menor que, igual a (concordante), o mayor que \em str.
163   */
164   int compare(const char* str, const int flags = Compare::LeftTrim | Compare::NoCase) const throw();
165
166   /**
167      Compara el contenido actual de esta cabecera con el literal recibido como parametro y
168      devuelve  \em true o \em false dependiendo de si el contenido coincide con \em str.
169
170      \param str La cadena con la que comparar.
171      \param flags Modo de comparacion aplicado.
172
173      \return  \em true o \em false dependiendo de si el contenido coincide con \em str.
174   */
175   bool match(const char* str, const int flags = Compare::LeftTrim | Compare::NoCase) const throw() {
176     return compare(str, flags) == 0;
177   }
178
179   /**
180      Devuelve una cadena con toda la informacion relevante de este objeto.
181      \return una cadena con toda la informacion relevante de este objeto.
182   */
183   std::string asString() const throw();
184
185   /**
186      Interpreta el token recibido como parametro y devuelve el valor de Type con el
187      que esta asociado.
188      \param token Token obtenido en la fase de analisis.
189      \warning Exclusivamente uso interno.
190      \return El tipo de cabecera.
191   */
192   static Type::_v asType(const Token* token) throw();
193
194   /**
195      Interpreta el token recibido como parametro y devuelve el nombre de la cabecera
196      con la que esta asociado.
197      \param type Tipo de cabecera.
198      \warning Exclusivamente uso interno.
199      \return El literal con el nombre de la cabecera correspondiente al tipo recibido.
200   */
201   static const char* asLiteral(const Type::_v type) throw();
202
203 private:
204   Type::_v a_type;
205   Category::_v a_category;
206   std::string* a_extensionName;
207   std::string a_value;
208   static const char* st_names [Type::End];
209
210   Header() : a_extensionName(NULL), a_category(Category::Extension) {;}
211   Header* initialize(const Type::_v type) throw(RuntimeException);
212   Header* initialize(const std::string& name) throw(RuntimeException);
213   std::string code() const throw();
214
215   friend class Message;
216   friend class Allocator<Header>;
217 };
218
219
220 }
221 }
222
223 #endif