-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHeaders.cpp
More file actions
183 lines (155 loc) · 6.48 KB
/
Copy pathHttpHeaders.cpp
File metadata and controls
183 lines (155 loc) · 6.48 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "HttpHeaders.hpp"
#include "Utils.hpp"
std::string HttpHeaders::normalizeHeaderName(const std::string& name) const {
std::string normalized = name;
std::transform(normalized.begin(), normalized.end(), normalized.begin(), ::tolower);
return normalized;
}
std::string HttpHeaders::getCanonicalHeaderName(const std::string& name) const {
// Common HTTP headers with proper capitalization
static std::map<std::string, std::string> canonicalHeaders;
// Initialize the map on first call (C++98 compatible)
if (canonicalHeaders.empty()) {
canonicalHeaders["content-type"] = "Content-Type";
canonicalHeaders["content-length"] = "Content-Length";
canonicalHeaders["connection"] = "Connection";
canonicalHeaders["host"] = "Host";
canonicalHeaders["user-agent"] = "User-Agent";
canonicalHeaders["accept"] = "Accept";
canonicalHeaders["accept-encoding"] = "Accept-Encoding";
canonicalHeaders["accept-language"] = "Accept-Language";
canonicalHeaders["referer"] = "Referer";
canonicalHeaders["cookie"] = "Cookie";
canonicalHeaders["server"] = "Server";
canonicalHeaders["date"] = "Date";
canonicalHeaders["last-modified"] = "Last-Modified";
canonicalHeaders["etag"] = "ETag";
canonicalHeaders["cache-control"] = "Cache-Control";
canonicalHeaders["location"] = "Location";
}
// Convert input to lowercase for lookup
std::string lowercaseName = name;
std::transform(lowercaseName.begin(), lowercaseName.end(), lowercaseName.begin(), ::tolower);
// Look up canonical form
std::map<std::string, std::string>::const_iterator it = canonicalHeaders.find(lowercaseName);
if (it != canonicalHeaders.end()) {
return it->second;
}
// If not found in the map, capitalize the first letter of each word
std::string result = lowercaseName;
bool capitalize = true;
for (size_t i = 0; i < result.length(); ++i) {
if (capitalize && std::isalpha(result[i])) {
result[i] = std::toupper(result[i]);
capitalize = false;
} else if (result[i] == '-') {
capitalize = true;
}
}
return result;
}
// std::string& HttpHeaders::operator[](const std::string& key) {
// return headers[key];
// }
// const std::string& HttpHeaders::operator[](const std::string& key) const {
// return headers.at(key);
// }
// Add or update a header
void HttpHeaders::set(const std::string& name, const std::string& value) {
// Verify header name length
if (value.length() > MAX_HEADER_VALUE_LENGTH) {
return; // Just ignore the header if it's too long
}
std::string normalizedName = normalizeHeaderName(name);
for (std::vector<std::pair<std::string, std::string> >::iterator it = headers.begin(); it != headers.end(); ++it) {
if (normalizeHeaderName(it->first) == normalizedName) {
it->second = value;
return;
}
}
headers.push_back(std::make_pair(name, value));
}
// Check if a header exists
bool HttpHeaders::has(const std::string& name) const {
std::string normalizedName = normalizeHeaderName(name);
for (std::vector<std::pair<std::string, std::string> >::const_iterator it = headers.begin(); it != headers.end(); ++it) {
if (normalizeHeaderName(it->first) == normalizedName)
return true;
}
return false;
}
// Get a header value (returns empty string if not found)
std::string HttpHeaders::get(const std::string& name) const {
std::string normalizedName = normalizeHeaderName(name);
for (std::vector<std::pair<std::string, std::string> >::const_iterator it = headers.begin(); it != headers.end(); ++it) {
if (normalizeHeaderName(it->first) == normalizedName)
return it->second;
}
return "";
}
// Remove a header
void HttpHeaders::remove(const std::string& name) {
std::string normalizedName = normalizeHeaderName(name);
for (std::vector<std::pair<std::string, std::string> >::iterator it = headers.begin(); it != headers.end(); ++it) {
if (normalizeHeaderName(it->first) == normalizedName) {
headers.erase(it);
return;
}
}
}
// Get all headers
const std::vector<std::pair<std::string, std::string> >& HttpHeaders::getAll() const {
return headers;
}
// Clear all headers
void HttpHeaders::clear() {
headers.clear();
}
// Parse raw header lines into this object
// Format: "Header-Name: Header-Value\r\n"
bool HttpHeaders::parseHeaderLines(const std::string& headerBlock) {
std::string line;
std::istringstream stream(headerBlock);
size_t headerCount = 0;
while (std::getline(stream, line) && !line.empty() && line != "\r") {
if (headerCount++ > MAX_HEADERS) {
debug("[HttpHeaders]: Too many headers, limit exceeded");
return false;
}
if (line.length() > MAX_HEADER_LINE_LENGTH) {
debug("[HttpHeaders]: Header line too long: " + Utils::to_string(line.length()) + " bytes (max " + Utils::to_string(MAX_HEADER_LINE_LENGTH) + ")");
return false;
}
// Remove trailing \r if present
if (!line.empty() && line[line.length() - 1] == '\r') {
line.erase(line.length() - 1);
}
size_t colonPos = line.find(':');
if (colonPos == std::string::npos) {
debug("[HttpHeaders]: Invalid header format - missing colon");
return false;
}
std::string name = line.substr(0, colonPos);
std::string value = line.substr(colonPos + 1);
for (size_t i = 0; i < name.length(); i++) {
if (!isprint(name[i]) || name[i] == ':') {
debug("[HttpHeaders]: Invalid character in header name");
return false;
}
}
// Verify header value length
if (value.length() > MAX_HEADER_VALUE_LENGTH) {
debug("[HttpHeaders]: Header value too long: " + Utils::to_string(value.length()) + " bytes (max " + Utils::to_string(MAX_HEADER_VALUE_LENGTH) + ")");
return false;
}
// Trim leading/trailing whitespace from value
size_t firstNonSpace = value.find_first_not_of(" \t");
if (firstNonSpace != std::string::npos) {
value = value.substr(firstNonSpace);
} else {
value = "";
}
set(name, value);
}
return true;
}