Avoid service overhead about checking netstat ports
[anna.git] / source / http / Response.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <anna/core/functions.hpp>
10
11 #include <anna/http/Response.hpp>
12
13 using namespace std;
14 using namespace anna;
15
16 void http::Response::setStatusCode(const int statusCode)
17 throw() {
18   static struct { int statusCode; const char* reasonPhrase; } reasons [] = {
19     100, "Continue",
20     101, "Switching Protocols",
21     200, "OK",
22     201, "Created",
23     202, "Accepted",
24     203, "Non-Authoritative Information",
25     204, "No Content",
26     205, "Reset Content",
27     206, "Partial Content",
28     300, "Multiple Choise",
29     301, "Moved Permanently",
30     302, "Found",
31     303, "See Other",
32     304, "Not Modified",
33     305, "Use proxy",
34     307, "Temporary Redirect",
35     400, "Bad Request",
36     401, "Unautorized",
37     402, "Payment Required",
38     403, "Forbidden",
39     404, "Not found",
40     405, "Method Not Allowed",
41     406, "Not Acceptable",
42     407, "Proxy Authentication Required",
43     408, "Request Time-out",
44     409, "Conflict",
45     410, "Gone",
46     411, "Length Required",
47     412, "Precondition Failed",
48     413, "Request Entity Too Large",
49     414, "Request-URI Too Large",
50     415, "Unsupported Media Type",
51     416, "Requested range not satisfiable",
52     417, "Expectation Failed",
53     500, "Internal Server Error",
54     501, "Not Implemented",
55     502, "Bad Gateway",
56     503, "Service Unavailable",
57     504, "Gateway Time-out",
58     505, "HTTP Version not supported",
59     0, NULL
60   };
61
62   if(a_statusCode == statusCode)
63     return;
64
65   a_statusCode = statusCode;
66
67   for(int i = 0; reasons [i].statusCode != 0; i ++) {
68     if(statusCode < reasons [i].statusCode) {
69       a_reasonPhrase.clear();
70       break;
71     }
72
73     if(statusCode == reasons [i].statusCode) {
74       a_reasonPhrase = reasons [i].reasonPhrase;
75       break;
76     }
77   }
78 }
79
80 string http::Response::codeStartLine() const
81 throw(RuntimeException) {
82   string result(getVersion());
83   result += ' ';
84   result += functions::asString(a_statusCode);
85   result += ' ';
86   return result += a_reasonPhrase;
87 }
88
89 string http::Response::asString() const
90 throw() {
91   string result("http::Response { Version: ");
92   result += getVersion();
93   result += " | StatusCode: ";
94   result += functions::asString(a_statusCode);
95   result += " | ReasonPhrase: ";
96   result += (a_reasonPhrase.empty()) ? "<null>" : a_reasonPhrase.c_str();
97   return result += " }";
98 }