Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "http_header.hpp"
const std::string HttpHeader::Authorization = "Authorization";
const std::string HttpHeader::Connection = "Connection";
const std::string HttpHeader::KeepAlive = "Keep-Alive";
const std::string HttpHeader::Accept = "Accept";
const std::string HttpHeader::AcceptCharset = "Accept-Charset";
const std::string HttpHeader::AcceptEncoding = "Accept-Encoding";
const std::string HttpHeader::Cookie = "Cookie";
const std::string HttpHeader::SetCookie = "Set-Cookie";
const std::string HttpHeader::ContentDisposition = "Content-Disposition";
const std::string HttpHeader::ContentLength = "Content-Length";
const std::string HttpHeader::ContentType = "Content-Type";
const std::string HttpHeader::ContentEncoding = "Content-Encoding";
const std::string HttpHeader::ContentLanguage = "Content-Language";
const std::string HttpHeader::ContentLocation = "Content-Location";
const std::string HttpHeader::Location = "Location";
const std::string HttpHeader::Host = "Host";
const std::string HttpHeader::Referer = "Referer";
const std::string HttpHeader::UserAgent = "User-Agent";
const std::string HttpHeader::Allow = "Allow";
const std::string HttpHeader::Server = "Server";
const std::string HttpHeader::AcceptRanges = "Accept-Ranges";
const std::string HttpHeader::Range = "Range";
const std::string HttpHeader::ContentRange = "Content-Range";
const std::string HttpHeader::TransferEncoding = "Transfer-Encoding";
const std::string HttpHeader::Date = "Date";
HttpHeader::HttpHeader()
: _isSet{false}
{ }
HttpHeader::HttpHeader(const std::string & key, const std::string & value)
: _isSet{true}, key{key}, value{value}
{ }
const bool HttpHeader::isSet() const
{
return _isSet;
}
const std::string & HttpHeader::getKey() const
{
return key;
}
const std::string & HttpHeader::getValue() const
{
return value;
}
void HttpHeader::setKey(const std::string & _key)
{
key = _key;
// Convert key to lowercase to ensure case insensitivity
for (auto &c : key) c = tolower(c);
}
void HttpHeader::setValue(const std::string & _value)
{
value = _value;
}
///////////////////////
// Class HttpHeaders //
///////////////////////
const HttpHeader HttpHeaders::emptyHeader{};
const std::string HttpHeaders::emptyString{};
const HttpHeader & HttpHeaders::getHeader(const std::string & key) const
{
std::string key_lower = key;
for (auto &c : key_lower) c = std::tolower(c);
auto h = _headers.find(key_lower);
if (h == _headers.end())
{
return HttpHeaders::emptyHeader;
}
return (*h).second;
}
const std::string & HttpHeaders::getValueOrEmpty(const std::string & key) const
{
const HttpHeader & h = getHeader(key);
if (!h._isSet) return HttpHeaders::emptyString;
return h.getValue();
}
const std::unordered_map<std::string, HttpHeader> & HttpHeaders::getRawHeaders() const
{
return _headers;
}
const bool HttpHeaders::headerExists(const std::string & key) const
{
std::string key_lower = key;
for (auto &c : key_lower) c = std::tolower(c);
auto h = _headers.find(key_lower);
if (h == _headers.end())
{
return false;
}
return true;
}
void HttpHeaders::setHeader(const HttpHeader & header)
{
_headers[header.key] = header;
}
void HttpHeaders::setHeader(const std::string & key, const std::string & value)
{
std::string key_lower = key;
for (auto &c : key_lower) c = std::tolower(c);
_headers[key_lower].key = key;
_headers[key_lower].value = value;
}
void HttpHeaders::unsetHeader(const std::string & key)
{
std::string key_lower = key;
for (auto &c : key_lower) c = std::tolower(c);
auto h = _headers.find(key_lower);
if (h != _headers.end())
{
_headers.erase(h);
}
}