-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_request.cpp
More file actions
223 lines (184 loc) · 6.38 KB
/
http_request.cpp
File metadata and controls
223 lines (184 loc) · 6.38 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "http_request.h"
#include <sstream>
#include <algorithm>
#include <cctype>
HttpRequest HttpRequest::parse(const std::string& raw_request) {
HttpRequest request;
if (raw_request.empty()) {
return request;
}
std::istringstream stream(raw_request);
std::string line;
bool first_line = true;
bool headers_done = false;
std::string body;
while (std::getline(stream, line)) {
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
if (first_line) {
request.parse_request_line(line);
first_line = false;
} else if (!headers_done) {
if (line.empty()) {
headers_done = true;
continue;
}
request.parse_header_line(line);
} else {
if (!body.empty()) {
body += "\n";
}
body += line;
}
}
request.body_ = body;
request.parse_query_string();
request.valid_ = request.is_request_valid();
return request;
}
void HttpRequest::parse_request_line(const std::string& line) {
std::istringstream stream(line);
std::string method_str, path_with_query, version;
if (!(stream >> method_str >> path_with_query >> version)) {
return;
}
method_ = string_to_method(method_str);
version_ = version;
//Spllit path and query string
size_t query_pos = path_with_query.find('?');
if (query_pos != std::string::npos) {
path_ = path_with_query.substr(0, query_pos);
query_string_ = path_with_query.substr(query_pos + 1);
} else {
path_ = path_with_query;
}
// URL decode the path
path_ = url_decode(path_);
}
void HttpRequest::parse_header_line(const std::string& line) {
size_t colon_pos = line.find(':');
if (colon_pos == std::string::npos) {
return;
}
std::string name = line.substr(0, colon_pos);
std::string value = line.substr(colon_pos + 1);
//Trim whitespace
name.erase(name.find_last_not_of(" \t") + 1);
value.erase(0, value.find_first_not_of(" \t"));
value.erase(value.find_last_not_of(" \t") + 1);
//Convert header name to lowercase for case-insensitive lookup
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
headers_[name] = value;
}
void HttpRequest::parse_query_string() {
if (query_string_.empty()) {
return;
}
std::istringstream stream(query_string_);
std::string pair;
while (std::getline(stream, pair, '&')) {
size_t eq_pos = pair.find('=');
if (eq_pos != std::string::npos) {
std::string key = url_decode(pair.substr(0, eq_pos));
std::string value = url_decode(pair.substr(eq_pos + 1));
query_params_[key] = value;
} else {
query_params_[url_decode(pair)] = "";
}
}
}
std::string HttpRequest::url_decode(const std::string& str) {
std::string result;
result.reserve(str.length());
for (size_t i = 0; i < str.length(); ++i) {
if (str[i] == '%' && i + 2 < str.length()) {
unsigned int hex_value;
if (std::sscanf(str.substr(i + 1, 2).c_str(), "%x", &hex_value) == 1) {
result += static_cast<char>(hex_value);
i += 2;
} else {
result += str[i];
}
} else if (str[i] == '+') {
result += ' ';
} else {
result += str[i];
}
}
return result;
}
std::string HttpRequest::get_header(const std::string& name) const {
std::string lower_name = name;
std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower);
auto it = headers_.find(lower_name);
return (it != headers_.end()) ? it->second : "";
}
bool HttpRequest::has_header(const std::string& name) const {
std::string lower_name = name;
std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower);
return headers_.find(lower_name) != headers_.end();
}
std::string HttpRequest::get_query_param(const std::string& name) const {
auto it = query_params_.find(name);
return (it != query_params_.end()) ? it->second : "";
}
bool HttpRequest::is_keep_alive() const {
std::string connection = get_header("connection");
std::transform(connection.begin(), connection.end(), connection.begin(), ::tolower);
if (version_ == "HTTP/1.1") {
return connection != "close";
} else {
return connection == "keep-alive";
}
}
std::string HttpRequest::method_to_string(HttpMethod method) {
switch (method) {
case HttpMethod::GET: return "GET";
case HttpMethod::POST: return "POST";
case HttpMethod::PUT: return "PUT";
case HttpMethod::DELETE: return "DELETE";
case HttpMethod::HEAD: return "HEAD";
case HttpMethod::OPTIONS: return "OPTIONS";
default: return "UNKNOWN";
}
}
HttpMethod HttpRequest::string_to_method(const std::string& method_str) {
if (method_str == "GET") return HttpMethod::GET;
if (method_str == "POST") return HttpMethod::POST;
if (method_str == "PUT") return HttpMethod::PUT;
if (method_str == "DELETE") return HttpMethod::DELETE;
if (method_str == "HEAD") return HttpMethod::HEAD;
if (method_str == "OPTIONS") return HttpMethod::OPTIONS;
return HttpMethod::UNKNOWN;
}
bool HttpRequest::is_request_valid() const {
if (method_ == HttpMethod::UNKNOWN) {
return false;
}
if (path_.empty() || path_[0] != '/') {
return false;
}
if (version_ != "HTTP/1.0" && version_ != "HTTP/1.1") {
return false;
}
for (char c : path_) {
if (c < 0x20 || c == 0x7F) {
return false;
}
}
if (method_ == HttpMethod::POST || method_ == HttpMethod::PUT) {
auto content_length_header = get_header("content-length");
if (!content_length_header.empty()) {
try {
size_t content_length = std::stoull(content_length_header);
if (body_.size() != content_length) {
return body_.size() >= content_length;
}
} catch (const std::exception&) {
return false;
}
}
}
return true;
}