1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
11 #include <anna/core/functions.hpp>
13 #include <anna/http/Header.hpp>
14 #include <anna/http/internal/Token.hpp>
19 const char* http::Header::st_names [http::Header::Type::End] = {
21 "Cache-Control", "Connection", "Date", "Pragma", "Trailer", "Transfer-Encoding", "Upgrade", "Via",
23 "Accept", "Accept-Charset", "Accept-Encoding", "Accept-Language", "Authorization", "Expect", "From",
24 "Host", "If-Match", "If-Modified-Since", "If-None-Match", "If-Range", "If-Unmodified-Since", "Max-Forwards",
25 "Proxy-Authorization", "Range", "Referer", "TE", "User-Agent",
26 "Allow", "Content-Encoding", "Content-Language", "Content-Length", "Content-Location", "Content-MD5",
27 "Content-Range", "Content-Type", "Expires", "Last-Modified",
28 "Accept-Ranges", "Age", "ETAG", "Location", "Proxy-Authenticate", "Retry-After", "Server", "Vary",
29 "WWW-Authenticate", "Unknown"
32 http::Header* http::Header::initialize(const Type::_v type)
33 throw(RuntimeException) {
34 if(type == Type::None || type >= Type::Unknown) {
35 string msg(asString());
37 msg += functions::asString((int) type);
38 msg += " | Invalid header initializer";
39 throw RuntimeException(msg, ANNA_FILE_LOCATION);
44 if(type >= Type::CacheControl && type <= Type::Warning)
45 a_category = Category::General;
46 else if(type >= Type::Accept && type <= Type::UserAgent)
47 a_category = Category::Request;
48 else if(type >= Type::Allow && type <= Type::LastModified)
49 a_category = Category::Entity;
50 else if(type >= Type::AcceptRanges && type <= Type::WWWAuthenticate)
51 a_category = Category::Response;
53 a_category = Category::Extension;
55 delete a_extensionName;
56 a_extensionName = NULL;
60 http::Header* http::Header::initialize(const string& name)
61 throw(RuntimeException) {
62 if(a_extensionName == NULL)
63 a_extensionName = new string(name);
65 *a_extensionName = name;
67 a_type = Type::Unknown;
68 a_category = Category::Extension;
72 const int http::Header::getIntegerValue() const
74 return atoi(a_value.c_str());
77 void http::Header::setValue(const http::Token* token)
82 a_value = token->getStringValue();
85 void http::Header::setValue(const int value)
87 a_value = anna::functions::asString(value);
90 http::Header& http::Header::operator = (const Header & other)
95 a_type = other.a_type;
96 a_value = other.a_value;
98 if(other.a_extensionName == NULL) {
99 delete a_extensionName;
100 a_extensionName = NULL;
102 if(a_extensionName == NULL)
103 a_extensionName = new string(*other.a_extensionName);
105 *a_extensionName = *other.a_extensionName;
111 int http::Header::compare(const char* str, const int flags) const
113 const char* p = a_value.c_str();
117 if(flags & Compare::LeftTrim) {
118 while(*p && *p == ' ')
122 if((flags & Compare::RightTrim) && *p != 0 && anna_strchr(p, ' ') != NULL) {
124 ww = dup = strdup(p);
125 p = const_cast <const char*>(dup);
126 ww += anna_strlen(ww) - 1;
128 while(ww >= dup && *ww == ' ')
134 result = (flags & Compare::NoCase) ? strcasecmp(p, str) : anna_strcmp(p, str);
142 std::string http::Header::asString() const
144 string result("http::Header { Type: ");
145 result += asLiteral(a_type);
147 if(a_extensionName != NULL) {
148 result += " | Name: ";
149 result += *a_extensionName;
152 result += " | Value: ";
159 return result += " }";
162 string http::Header::code() const
166 if(a_category == Category::Extension)
167 result = (a_extensionName == NULL) ? "None" : a_extensionName->c_str();
169 result = asLiteral(a_type);
171 if(a_value.length() > 0) {
179 http::Header::Type::_v http::Header::asType(const http::Token* token)
181 for(int i = Type::Begin; token != NULL && i != Type::End; i ++) {
182 if(token->match(st_names [i]) == true)
189 const char* http::Header::asLiteral(const http::Header::Type::_v type)
191 return (type < Type::End) ? st_names [type] : "Extension";