5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
17 namespace status_strings
{
19 const std::string ok
=
20 "HTTP/1.0 200 OK\r\n";
21 const std::string created
=
22 "HTTP/1.0 201 Created\r\n";
23 const std::string accepted
=
24 "HTTP/1.0 202 Accepted\r\n";
25 const std::string no_content
=
26 "HTTP/1.0 204 No Content\r\n";
27 const std::string multiple_choices
=
28 "HTTP/1.0 300 Multiple Choices\r\n";
29 const std::string moved_permanently
=
30 "HTTP/1.0 301 Moved Permanently\r\n";
31 const std::string moved_temporarily
=
32 "HTTP/1.0 302 Moved Temporarily\r\n";
33 const std::string not_modified
=
34 "HTTP/1.0 304 Not Modified\r\n";
35 const std::string bad_request
=
36 "HTTP/1.0 400 Bad Request\r\n";
37 const std::string unauthorized
=
38 "HTTP/1.0 401 Unauthorized\r\n";
39 const std::string forbidden
=
40 "HTTP/1.0 403 Forbidden\r\n";
41 const std::string not_found
=
42 "HTTP/1.0 404 Not Found\r\n";
43 const std::string internal_server_error
=
44 "HTTP/1.0 500 Internal Server Error\r\n";
45 const std::string not_implemented
=
46 "HTTP/1.0 501 Not Implemented\r\n";
47 const std::string bad_gateway
=
48 "HTTP/1.0 502 Bad Gateway\r\n";
49 const std::string service_unavailable
=
50 "HTTP/1.0 503 Service Unavailable\r\n";
52 boost::asio::const_buffer
to_buffer(reply::status_type status
)
57 return boost::asio::buffer(ok
);
59 return boost::asio::buffer(created
);
61 return boost::asio::buffer(accepted
);
62 case reply::no_content
:
63 return boost::asio::buffer(no_content
);
64 case reply::multiple_choices
:
65 return boost::asio::buffer(multiple_choices
);
66 case reply::moved_permanently
:
67 return boost::asio::buffer(moved_permanently
);
68 case reply::moved_temporarily
:
69 return boost::asio::buffer(moved_temporarily
);
70 case reply::not_modified
:
71 return boost::asio::buffer(not_modified
);
72 case reply::bad_request
:
73 return boost::asio::buffer(bad_request
);
74 case reply::unauthorized
:
75 return boost::asio::buffer(unauthorized
);
76 case reply::forbidden
:
77 return boost::asio::buffer(forbidden
);
78 case reply::not_found
:
79 return boost::asio::buffer(not_found
);
80 case reply::internal_server_error
:
81 return boost::asio::buffer(internal_server_error
);
82 case reply::not_implemented
:
83 return boost::asio::buffer(not_implemented
);
84 case reply::bad_gateway
:
85 return boost::asio::buffer(bad_gateway
);
86 case reply::service_unavailable
:
87 return boost::asio::buffer(service_unavailable
);
89 return boost::asio::buffer(internal_server_error
);
93 } // namespace status_strings
95 namespace misc_strings
{
97 const char name_value_separator
[] = { ':', ' ' };
98 const char crlf
[] = { '\r', '\n' };
100 } // namespace misc_strings
102 std::vector
<boost::asio::const_buffer
> reply::to_buffers()
104 std::vector
<boost::asio::const_buffer
> buffers
;
105 buffers
.push_back(status_strings::to_buffer(status
));
106 for (std::size_t i
= 0; i
< headers
.size(); ++i
)
108 header
& h
= headers
[i
];
109 buffers
.push_back(boost::asio::buffer(h
.name
));
110 buffers
.push_back(boost::asio::buffer(misc_strings::name_value_separator
));
111 buffers
.push_back(boost::asio::buffer(h
.value
));
112 buffers
.push_back(boost::asio::buffer(misc_strings::crlf
));
114 buffers
.push_back(boost::asio::buffer(misc_strings::crlf
));
115 buffers
.push_back(boost::asio::buffer(content
));
119 namespace stock_replies
{
121 const char ok
[] = "";
122 const char created
[] =
124 "<head><title>Created</title></head>"
125 "<body><h1>201 Created</h1></body>"
127 const char accepted
[] =
129 "<head><title>Accepted</title></head>"
130 "<body><h1>202 Accepted</h1></body>"
132 const char no_content
[] =
134 "<head><title>No Content</title></head>"
135 "<body><h1>204 Content</h1></body>"
137 const char multiple_choices
[] =
139 "<head><title>Multiple Choices</title></head>"
140 "<body><h1>300 Multiple Choices</h1></body>"
142 const char moved_permanently
[] =
144 "<head><title>Moved Permanently</title></head>"
145 "<body><h1>301 Moved Permanently</h1></body>"
147 const char moved_temporarily
[] =
149 "<head><title>Moved Temporarily</title></head>"
150 "<body><h1>302 Moved Temporarily</h1></body>"
152 const char not_modified
[] =
154 "<head><title>Not Modified</title></head>"
155 "<body><h1>304 Not Modified</h1></body>"
157 const char bad_request
[] =
159 "<head><title>Bad Request</title></head>"
160 "<body><h1>400 Bad Request</h1></body>"
162 const char unauthorized
[] =
164 "<head><title>Unauthorized</title></head>"
165 "<body><h1>401 Unauthorized</h1></body>"
167 const char forbidden
[] =
169 "<head><title>Forbidden</title></head>"
170 "<body><h1>403 Forbidden</h1></body>"
172 const char not_found
[] =
174 "<head><title>Not Found</title></head>"
175 "<body><h1>404 Not Found</h1></body>"
177 const char internal_server_error
[] =
179 "<head><title>Internal Server Error</title></head>"
180 "<body><h1>500 Internal Server Error</h1></body>"
182 const char not_implemented
[] =
184 "<head><title>Not Implemented</title></head>"
185 "<body><h1>501 Not Implemented</h1></body>"
187 const char bad_gateway
[] =
189 "<head><title>Bad Gateway</title></head>"
190 "<body><h1>502 Bad Gateway</h1></body>"
192 const char service_unavailable
[] =
194 "<head><title>Service Unavailable</title></head>"
195 "<body><h1>503 Service Unavailable</h1></body>"
198 std::string
to_string(reply::status_type status
)
206 case reply::accepted
:
208 case reply::no_content
:
210 case reply::multiple_choices
:
211 return multiple_choices
;
212 case reply::moved_permanently
:
213 return moved_permanently
;
214 case reply::moved_temporarily
:
215 return moved_temporarily
;
216 case reply::not_modified
:
218 case reply::bad_request
:
220 case reply::unauthorized
:
222 case reply::forbidden
:
224 case reply::not_found
:
226 case reply::internal_server_error
:
227 return internal_server_error
;
228 case reply::not_implemented
:
229 return not_implemented
;
230 case reply::bad_gateway
:
232 case reply::service_unavailable
:
233 return service_unavailable
;
235 return internal_server_error
;
239 } // namespace stock_replies
241 reply
reply::stock_reply(reply::status_type status
)
245 rep
.content
= stock_replies::to_string(status
);
246 rep
.headers
.resize(2);
247 rep
.headers
[0].name
= "Content-Length";
248 rep
.headers
[0].value
= std::to_string(rep
.content
.size());
249 rep
.headers
[1].name
= "Content-Type";
250 rep
.headers
[1].value
= "text/html";
254 } // namespace server