Updated license
[anna.git] / include / anna / http / Response.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_Response_hpp
38 #define anna_http_Response_hpp
39
40 #include <anna/http/Message.hpp>
41
42 namespace anna {
43
44 namespace http {
45
46 /**
47    Clase que modela las respuestas HTTP segun la RFC 2616.
48 */
49 class Response : public Message {
50 public:
51   /**
52      Constructor.
53   */
54   explicit Response() :
55     Message(Message::Type::Response) { a_statusCode = 0; setStatusCode(200); }
56
57   /**
58      Devuelve el codigo de estado contenido en la respuesta.
59      \return El codigo de estado contenido en la respuesta
60   */
61   int getStatusCode() const throw() { return a_statusCode; }
62
63   /**
64      Devuelve el campo ReasePhrase contenido en la respuesta.
65      \return el campo ReasePhrase contenido en la respuesta.
66   */
67   const std::string& getReasonPhrase() const throw() { return a_reasonPhrase; }
68
69   /**
70      Establece el codigo de retorno de la respuesta HTTP. Si es un codigo reconocido por la RFC 2616
71      tambien establece el ReasonPhrase por defecto.
72
73      Los codigos de respuesta reconocidos por la RFC 2616 son:
74      \code
75         100: Continue
76         101: Switching Protocols
77         200: OK
78         201: Created
79         202: Accepted
80         203: Non-Authoritative Information
81         204: No Content
82         205: Reset Content
83         206: Partial Content
84         300: Multiple Choise
85         301: Moved Permanently
86         302: Found
87         303: See Other
88         304: Not Modified
89         305: Use proxy
90         307: Temporary Redirect
91         400: Bad Request
92         401: Unautorized
93         402: Payment Required
94         403: Forbidden
95         404: Not found
96         405: Method Not Allowed
97         406: Not Acceptable
98         407: Proxy Authentication Required
99         408: Request Time-out
100         409: Conflict
101         410: Gone
102         411: Length Required
103         412: Precondition Failed
104         413: Request Entity Too Large
105         414: Request-URI Too Large
106         415: Unsupported Media Type
107         416: Requested range not satisfiable
108         417: Expectation Failed
109         500: Internal Server Error
110         501: Not Implemented
111         502: Bad Gateway
112         503: Service Unavailable
113         504: Gateway Time-out
114         505: HTTP Version not supported
115      \endcode
116   */
117   void setStatusCode(const int statusCode) throw();
118
119   /**
120      Establece la frase asociada a la respuesta.
121      \param reasonPhrase Frase asociada a la respuesta.
122   */
123   void setReasonPhrase(const std::string& reasonPhrase) throw() { a_reasonPhrase = reasonPhrase; }
124
125   /**
126      Establece la frase asociada a la respuesta.
127      \param reasonPhrase Frase asociada a la respuesta.
128   */
129   void setReasonPhrase(const char* reasonPhrase) throw() { a_reasonPhrase = reasonPhrase; }
130
131   /**
132      Devuelve una cadena con toda la informacion relevante de este objeto.
133      \return una cadena con toda la informacion relevante de este objeto.
134   */
135   std::string asString() const throw();
136
137 private:
138   int a_statusCode;
139   std::string a_reasonPhrase;
140
141   std::string codeStartLine() const throw(anna::RuntimeException);
142 };
143
144 }
145 }
146
147 #endif
148