-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpResponse.cpp
273 lines (226 loc) · 7.15 KB
/
HttpResponse.cpp
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
#include "HttpResponse.h"
#include <string>
#include <boost/lexical_cast.hpp>
namespace http {
namespace server {
namespace HttpStatusStrings {
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 multiple_choices =
"HTTP/1.0 300 Multiple Choices\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";
boost::asio::const_buffer toBuffer(HttpResponse::HttpStatus status)
{
switch (status) {
case HttpResponse::ok:
return boost::asio::buffer(ok);
case HttpResponse::created:
return boost::asio::buffer(created);
case HttpResponse::accepted:
return boost::asio::buffer(accepted);
case HttpResponse::no_content:
return boost::asio::buffer(no_content);
case HttpResponse::multiple_choices:
return boost::asio::buffer(multiple_choices);
case HttpResponse::moved_permanently:
return boost::asio::buffer(moved_permanently);
case HttpResponse::moved_temporarily:
return boost::asio::buffer(moved_temporarily);
case HttpResponse::not_modified:
return boost::asio::buffer(not_modified);
case HttpResponse::bad_request:
return boost::asio::buffer(bad_request);
case HttpResponse::unauthorized:
return boost::asio::buffer(unauthorized);
case HttpResponse::forbidden:
return boost::asio::buffer(forbidden);
case HttpResponse::not_found:
return boost::asio::buffer(not_found);
case HttpResponse::internal_server_error:
return boost::asio::buffer(internal_server_error);
case HttpResponse::not_implemented:
return boost::asio::buffer(not_implemented);
case HttpResponse::bad_gateway:
return boost::asio::buffer(bad_gateway);
case HttpResponse::service_unavailable:
return boost::asio::buffer(service_unavailable);
default:
return boost::asio::buffer(internal_server_error);
}
}
}
namespace HttpResponseMisc {
const char nameValueSeparator[] = { ':', ' ' };
const char crlf[] = { '\r', '\n' };
}
std::vector<boost::asio::const_buffer> HttpResponse::toBuffers()
{
std::vector<boost::asio::const_buffer> buffers;
buffers.push_back(HttpStatusStrings::toBuffer(status));
for (std::size_t i = 0; i < headers.size(); ++i) {
HttpHeader& h = headers[i];
buffers.push_back(boost::asio::buffer(h.name));
buffers.push_back(boost::asio::buffer(HttpResponseMisc::nameValueSeparator));
buffers.push_back(boost::asio::buffer(h.value));
buffers.push_back(boost::asio::buffer(HttpResponseMisc::crlf));
}
buffers.push_back(boost::asio::buffer(HttpResponseMisc::crlf));
buffers.push_back(boost::asio::buffer(content));
return buffers;
}
namespace defaultResponses {
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 multiple_choices[] =
"<html>"
"<head><title>Multiple Choices</title></head>"
"<body><h1>300 Multiple Choices</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>";
std::string toString(HttpResponse::HttpStatus status)
{
switch (status) {
case HttpResponse::ok:
return ok;
case HttpResponse::created:
return created;
case HttpResponse::accepted:
return accepted;
case HttpResponse::no_content:
return no_content;
case HttpResponse::multiple_choices:
return multiple_choices;
case HttpResponse::moved_permanently:
return moved_permanently;
case HttpResponse::moved_temporarily:
return moved_temporarily;
case HttpResponse::not_modified:
return not_modified;
case HttpResponse::bad_request:
return bad_request;
case HttpResponse::unauthorized:
return unauthorized;
case HttpResponse::forbidden:
return forbidden;
case HttpResponse::not_found:
return not_found;
case HttpResponse::internal_server_error:
return internal_server_error;
case HttpResponse::not_implemented:
return not_implemented;
case HttpResponse::bad_gateway:
return bad_gateway;
case HttpResponse::service_unavailable:
return service_unavailable;
default:
return internal_server_error;
}
}
}
HttpResponse HttpResponse::defaultResponse(HttpResponse::HttpStatus status)
{
HttpResponse response;
response.status = status;
response.content = boost::lexical_cast<std::string>(status);
response.headers.resize(1);
response.headers[0].name = "Content-Type";
response.headers[0].value = "text/plain";
return response;
}
}
}