Updated license
[anna.git] / source / http / Response.cpp
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 #include <anna/core/functions.hpp>
38
39 #include <anna/http/Response.hpp>
40
41 using namespace std;
42 using namespace anna;
43
44 void http::Response::setStatusCode(const int statusCode)
45 throw() {
46   static struct { int statusCode; const char* reasonPhrase; } reasons [] = {
47     100, "Continue",
48     101, "Switching Protocols",
49     200, "OK",
50     201, "Created",
51     202, "Accepted",
52     203, "Non-Authoritative Information",
53     204, "No Content",
54     205, "Reset Content",
55     206, "Partial Content",
56     300, "Multiple Choise",
57     301, "Moved Permanently",
58     302, "Found",
59     303, "See Other",
60     304, "Not Modified",
61     305, "Use proxy",
62     307, "Temporary Redirect",
63     400, "Bad Request",
64     401, "Unautorized",
65     402, "Payment Required",
66     403, "Forbidden",
67     404, "Not found",
68     405, "Method Not Allowed",
69     406, "Not Acceptable",
70     407, "Proxy Authentication Required",
71     408, "Request Time-out",
72     409, "Conflict",
73     410, "Gone",
74     411, "Length Required",
75     412, "Precondition Failed",
76     413, "Request Entity Too Large",
77     414, "Request-URI Too Large",
78     415, "Unsupported Media Type",
79     416, "Requested range not satisfiable",
80     417, "Expectation Failed",
81     500, "Internal Server Error",
82     501, "Not Implemented",
83     502, "Bad Gateway",
84     503, "Service Unavailable",
85     504, "Gateway Time-out",
86     505, "HTTP Version not supported",
87     0, NULL
88   };
89
90   if(a_statusCode == statusCode)
91     return;
92
93   a_statusCode = statusCode;
94
95   for(int i = 0; reasons [i].statusCode != 0; i ++) {
96     if(statusCode < reasons [i].statusCode) {
97       a_reasonPhrase.clear();
98       break;
99     }
100
101     if(statusCode == reasons [i].statusCode) {
102       a_reasonPhrase = reasons [i].reasonPhrase;
103       break;
104     }
105   }
106 }
107
108 string http::Response::codeStartLine() const
109 throw(RuntimeException) {
110   string result(getVersion());
111   result += ' ';
112   result += functions::asString(a_statusCode);
113   result += ' ';
114   return result += a_reasonPhrase;
115 }
116
117 string http::Response::asString() const
118 throw() {
119   string result("http::Response { Version: ");
120   result += getVersion();
121   result += " | StatusCode: ";
122   result += functions::asString(a_statusCode);
123   result += " | ReasonPhrase: ";
124   result += (a_reasonPhrase.empty()) ? "<null>" : a_reasonPhrase.c_str();
125   return result += " }";
126 }