-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.cc
309 lines (272 loc) · 8.53 KB
/
response.cc
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "response.h"
namespace MiscString {
// Strings used in forming an HTTP Response
const char field_separator[] = { ':', ' ' };
const char crlf[] = { '\r', '\n' };
} // namespace MiscString
namespace StatusString {
// Status lines for every status code in HTTP/1.0
const std::string ok =
"HTTP/1.0 200 OK\r\n";
const std::string created =
"HTTP/1.0 201 Created\r\n";
const std::string accepted =
"HTTP/1.0 202 Accepted\r\n";
const std::string no_content =
"HTTP/1.0 204 No Content\r\n";
const std::string moved_permanently =
"HTTP/1.0 301 Moved Permanently\r\n";
const std::string moved_temporarily =
"HTTP/1.0 302 Moved Temporarily\r\n";
const std::string not_modified =
"HTTP/1.0 304 Not Modified\r\n";
const std::string bad_request =
"HTTP/1.0 400 Bad Request\r\n";
const std::string unauthorized =
"HTTP/1.0 401 Unauthorized\r\n";
const std::string forbidden =
"HTTP/1.0 403 Forbidden\r\n";
const std::string not_found =
"HTTP/1.0 404 Not Found\r\n";
const std::string internal_server_error =
"HTTP/1.0 500 Internal Server Error\r\n";
const std::string not_implemented =
"HTTP/1.0 501 Not Implemented\r\n";
const std::string bad_gateway =
"HTTP/1.0 502 Bad Gateway\r\n";
const std::string service_unavailable =
"HTTP/1.0 503 Service Unavailable\r\n";
// Gets status line for a given status code
std::string ToString(Response::ResponseCode status) {
switch (status) {
case Response::ok:
return (ok);
case Response::created:
return (created);
case Response::accepted:
return (accepted);
case Response::no_content:
return (no_content);
case Response::moved_permanently:
return (moved_permanently);
case Response::moved_temporarily:
return (moved_temporarily);
case Response::not_modified:
return (not_modified);
case Response::bad_request:
return (bad_request);
case Response::unauthorized:
return (unauthorized);
case Response::forbidden:
return (forbidden);
case Response::not_found:
return (not_found);
case Response::internal_server_error:
return (internal_server_error);
case Response::not_implemented:
return (not_implemented);
case Response::bad_gateway:
return (bad_gateway);
case Response::service_unavailable:
return (service_unavailable);
default:
return (internal_server_error);
}
}
} // namespace StatusString
namespace DefaultResponse {
// Default message bodies for every status code in HTTP/1.0
const char ok[] =
"";
const char created[] =
"<html>"
"<head><title>Created</title></head>"
"<body><h1>201 Created</h1></body>"
"</html>";
const char accepted[] =
"<html>"
"<head><title>Accepted</title></head>"
"<body><h1>202 Accepted</h1></body>"
"</html>";
const char no_content[] =
"<html>"
"<head><title>No Content</title></head>"
"<body><h1>204 Content</h1></body>"
"</html>";
const char moved_permanently[] =
"<html>"
"<head><title>Moved Permanently</title></head>"
"<body><h1>301 Moved Permanently</h1></body>"
"</html>";
const char moved_temporarily[] =
"<html>"
"<head><title>Moved Temporarily</title></head>"
"<body><h1>302 Moved Temporarily</h1></body>"
"</html>";
const char not_modified[] =
"<html>"
"<head><title>Not Modified</title></head>"
"<body><h1>304 Not Modified</h1></body>"
"</html>";
const char bad_request[] =
"<html>"
"<head><title>Bad Request</title></head>"
"<body><h1>400 Bad Request</h1></body>"
"</html>";
const char unauthorized[] =
"<html>"
"<head><title>Unauthorized</title></head>"
"<body><h1>401 Unauthorized</h1></body>"
"</html>";
const char forbidden[] =
"<html>"
"<head><title>Forbidden</title></head>"
"<body><h1>403 Forbidden</h1></body>"
"</html>";
const char not_found[] =
"<html>"
"<head><title>Not Found</title></head>"
"<body><h1>404 Not Found</h1></body>"
"</html>";
const char internal_server_error[] =
"<html>"
"<head><title>Internal Server Error</title></head>"
"<body><h1>500 Internal Server Error</h1></body>"
"</html>";
const char not_implemented[] =
"<html>"
"<head><title>Not Implemented</title></head>"
"<body><h1>501 Not Implemented</h1></body>"
"</html>";
const char bad_gateway[] =
"<html>"
"<head><title>Bad Gateway</title></head>"
"<body><h1>502 Bad Gateway</h1></body>"
"</html>";
const char service_unavailable[] =
"<html>"
"<head><title>Service Unavailable</title></head>"
"<body><h1>503 Service Unavailable</h1></body>"
"</html>";
// Gets default message body for a given status code
std::string ToHtml(Response::ResponseCode status)
{
switch (status)
{
case Response::ok:
return ok;
case Response::created:
return created;
case Response::accepted:
return accepted;
case Response::no_content:
return no_content;
case Response::moved_permanently:
return moved_permanently;
case Response::moved_temporarily:
return moved_temporarily;
case Response::not_modified:
return not_modified;
case Response::bad_request:
return bad_request;
case Response::unauthorized:
return unauthorized;
case Response::forbidden:
return forbidden;
case Response::not_found:
return not_found;
case Response::internal_server_error:
return internal_server_error;
case Response::not_implemented:
return not_implemented;
case Response::bad_gateway:
return bad_gateway;
case Response::service_unavailable:
return service_unavailable;
default:
return internal_server_error;
}
}
} // namespace DefaultResponse
// Creates a default Response for a given status code
Response Response::DefaultResponse(Response::ResponseCode status) {
Response r;
r.status = status;
r.content = DefaultResponse::ToHtml(status);
r.headers.resize(2);
r.headers[0].first = "Content-Length";
r.headers[0].second = std::to_string(r.content.size());
r.headers[1].first = "Content-Type";
r.headers[1].second = "text/html";
return r;
}
// Creates a text/plain Response for the given text or html
Response Response::PlainTextResponse(std::string&& text) {
Response r;
r.status = Response::ok;
r.content = std::move(text);
r.headers.resize(2);
r.headers[0].first = "Content-Length";
r.headers[0].second = std::to_string(r.content.size());
r.headers[1].first = "Content-Type";
r.headers[1].second = "text/plain";
return r;
}
// Creates a text/html Response for the given text or html
Response Response::HtmlResponse(std::string&& html) {
Response r;
r.status = Response::ok;
r.content = std::move(html);
r.headers.resize(2);
r.headers[0].first = "Content-Length";
r.headers[0].second = std::to_string(r.content.size());
r.headers[1].first = "Content-Type";
r.headers[1].second = "text/html";
return r;
}
// Get the headers of the response
Response::Headers Response::GetHeaders() const {
return headers;
}
// Adds a header to the response
void Response::AddHeader(const std::string& name, const std::string& value) {
headers.push_back(std::make_pair(name, value));
}
// Get the body of the response
std::string Response::GetBody() const {
return content;
}
// Sets the body of the response
void Response::SetBody(const std::string& body) {
content = body;
}
// Get the status of the response
Response::ResponseCode Response::GetStatus() const {
return status;
}
// Sets the status of the repsonse
void Response::SetStatus(const ResponseCode code) {
status = code;
}
// Converts response to a string representing data to be sent to the client
std::string Response::ToString() const {
if (full_response != ""){
return full_response;
}
std::string response_string;
response_string += StatusString::ToString(status);
for (std::size_t i = 0; i < headers.size(); ++i) {
const Response::Header& h = headers[i];
response_string += h.first;
response_string += std::string(MiscString::field_separator, sizeof(MiscString::field_separator));
response_string += h.second;
response_string += std::string(MiscString::crlf, sizeof(MiscString::crlf));
}
response_string += MiscString::crlf;
response_string += content;
return response_string;
}
// Sets the whole response, headers and body
void Response::SetFullResponse(const std::string& response) {
full_response = response;
}