-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrequest_handler.cc
More file actions
203 lines (159 loc) · 5.65 KB
/
request_handler.cc
File metadata and controls
203 lines (159 loc) · 5.65 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
#include "iostream"
#include "sstream"
#include "request_handler.h"
// Helper Function(s)
static std::vector<std::string> split_lines(const std::string& str) {
std::stringstream ss(str);
std::string line;
std::vector<std::string> lines;
// tokenize each line by newline
while (std::getline(ss, line, '\n')) {
lines.push_back(line);
}
return lines;
}
// Request Implementations
std::unique_ptr<Request> Request::Parse(const std::string & raw_request) {
std::unique_ptr<Request> new_request(new Request());
new_request->raw_request_ = raw_request;
std::vector<std::string> lines = split_lines(raw_request);
for (size_t i = 0; i < lines.size(); i++) {
// remove carriage return from line
if (lines[i][lines[i].size()-1] == '\r') {
lines[i] = lines[i].substr(0, lines[i].size()-1);
}
if (i == 0) {
// parse first line of the request
auto first_space = lines[i].find(" ");
new_request->method_ = lines[i].substr(0, first_space);
std::string temp = lines[i].substr(first_space + 1);
auto second_space = temp.find(" ");
new_request->uri_ = temp.substr(0, second_space);
new_request->version_ = temp.substr(second_space + 1);
} else if (!lines[i].empty()) {
// parse header fields of the request
auto first_space = lines[i].find(" ");
std::string field = lines[i].substr(0, first_space - 1);
std::string value = lines[i].substr(first_space + 1);
new_request->headers_.push_back(std::make_pair(field, value));
}
}
return new_request;
}
std::string Request::raw_request() const {
return raw_request_;
}
std::string Request::method() const {
return method_;
}
std::string Request::uri() const {
return uri_;
}
std::string Request::version() const {
return version_;
}
std::string Request::body() const {
return body_;
}
using Headers = std::vector<std::pair<std::string, std::string>>;
Headers Request::headers() const {
return headers_;
}
// Response Implementations
std::unique_ptr<Response> Response::Parse(const std::string& raw_response) {
std::unique_ptr<Response> new_response(new Response());
std::vector<std::string> lines = split_lines(raw_response);
// parse first line of the response
if (lines.size() > 0) {
auto first_space = lines[0].find(" ");
std::string temp = lines[0].substr(first_space + 1);
auto second_space = temp.find(" ");
std::string status_code = temp.substr(0, second_space);
try {
new_response->status_code_ = std::stoi(status_code);
} catch (...) {
return std::unique_ptr<Response>();
}
}
// parse header fields of the response
unsigned int i = 1;
while (i < lines.size() && lines[i].size() > 1) {
// remove carriage return from line
if (lines[i][lines[i].size()-1] == '\r') {
lines[i] = lines[i].substr(0, lines[i].size()-1);
}
auto first_space = lines[i].find(" ");
std::string field = lines[i].substr(0, first_space - 1);
std::string value = lines[i].substr(first_space + 1);
new_response->headers_.push_back(std::make_pair(field, value));
i++;
}
// parse body of the response
i++;
while (i < lines.size()) {
new_response->response_body_ += lines[i];
if (i < lines.size()-1) {
new_response->response_body_ += "\n";
}
i++;
}
// the last line may or may not have a newline
if (raw_response[raw_response.size()-1] == '\n') {
new_response->response_body_ += "\n";
}
return new_response;
}
std::string Response::ToString() {
std::string response = build_status_line();
for (auto const& header: headers_)
response += build_header_string(header);
response += "\r\n" + response_body_;
return response;
}
std::string Response::build_status_line() {
std::string status_line = "HTTP/1.1";
if (status_code_ == ResponseCode::OK) {
status_line += " 200 OK";
} else if (status_code_ == ResponseCode::FILE_NOT_FOUND) {
status_line += " 404 Not Found";
} else if (status_code_ == ResponseCode::REDIRECT) {
status_line += " 302 Found";
} else {
status_line += " " + std::to_string(status_code_) + " Proxy";
}
return status_line + "\r\n";
}
std::string Response::build_header_string(const std::pair<std::string, std::string>& field) {
return field.first + ": " + field.second + "\r\n";
}
void Response::SetBody(const std::string& body) {
response_body_ = body;
}
void Response::SetStatus(int response_code) {
status_code_ = response_code;
}
void Response::AddHeader(const std::string& header_name, const std::string& header_value) {
headers_.push_back(std::make_pair(header_name, header_value));
}
int Response::GetStatus() const {
return status_code_;
}
std::string Response::body() const {
return response_body_;
}
using Headers = std::vector<std::pair<std::string, std::string>>;
Headers Response::headers() const {
return headers_;
}
// RequestHandler Implementations
RequestHandler::RequestHandler() {}
RequestHandler::~RequestHandler() {}
// RequestHandler Registerer
std::map<std::string, std::unique_ptr<RequestHandler> (*)(void)>* request_handler_builders = nullptr;
std::unique_ptr<RequestHandler> RequestHandler::CreateByName(const std::string type) {
const auto type_and_builder = request_handler_builders->find(type);
if (type_and_builder == request_handler_builders->end()) {
return nullptr;
}
return (*type_and_builder->second)();
}