Skip to content

Commit c3eeb92

Browse files
committed
Using correct traits for defining the string type and the vector type, making stock_reply creation easier.
1 parent e1e11a8 commit c3eeb92

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

boost/network/protocol/http/impl/response.ipp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_IMPL_RESPONSE_RESPONSE_IPP
1818
#define BOOST_NETWORK_PROTOCOL_HTTP_IMPL_RESPONSE_RESPONSE_IPP
1919

20-
#include <string>
21-
#include <vector>
2220
#include <boost/asio.hpp>
2321
#include <boost/lexical_cast.hpp>
22+
#include <boost/network/traits/string.hpp>
23+
#include <boost/network/traits/vector.hpp>
2424
#include <boost/network/protocol/http/header.hpp>
2525

2626
namespace boost { namespace network { namespace http {
@@ -53,10 +53,12 @@ namespace boost { namespace network { namespace http {
5353
} status;
5454

5555
/// The headers to be included in the reply.
56-
std::vector<request_header> headers;
56+
typedef vector<tags::http_server>::apply<request_header>::type headers_vector;
57+
headers_vector headers;
5758

5859
/// The content to be sent in the reply.
59-
std::string content;
60+
typedef string<tags::http_server>::type string_type;
61+
string_type content;
6062

6163
/// Convert the reply into a vector of buffers. The buffers do not own the
6264
/// underlying memory blocks, therefore the reply object must remain valid and
@@ -83,21 +85,26 @@ namespace boost { namespace network { namespace http {
8385

8486
/// Get a stock reply.
8587
static basic_response<tags::http_server> stock_reply(status_type status) {
88+
return stock_reply(status, to_string(status));
89+
}
90+
91+
/// Get a stock reply with custom plain text data.
92+
static basic_response<tags::http_server> stock_reply(status_type status, string_type content) {
8693
using boost::lexical_cast;
8794
basic_response<tags::http_server> rep;
8895
rep.status = status;
89-
rep.content = to_string(status);
96+
rep.content = content;
9097
rep.headers.resize(2);
9198
rep.headers[0].name = "Content-Length";
92-
rep.headers[0].value = lexical_cast<std::string>(rep.content.size());
99+
rep.headers[0].value = lexical_cast<string_type>(rep.content.size());
93100
rep.headers[1].name = "Content-Type";
94101
rep.headers[1].value = "text/html";
95102
return rep;
96103
}
97104

98105
private:
99106

100-
static std::string to_string(status_type status) {
107+
static string_type to_string(status_type status) {
101108
static const char ok[] = "";
102109
static const char created[] =
103110
"<html>"
@@ -230,41 +237,41 @@ namespace boost { namespace network { namespace http {
230237

231238
boost::asio::const_buffer to_buffer(status_type status) {
232239
using boost::asio::buffer;
233-
static const std::string ok =
240+
static const string_type ok =
234241
"HTTP/1.0 200 OK\r\n";
235-
static const std::string created =
242+
static const string_type created =
236243
"HTTP/1.0 201 Created\r\n";
237-
static const std::string accepted =
244+
static const string_type accepted =
238245
"HTTP/1.0 202 Accepted\r\n";
239-
static const std::string no_content =
246+
static const string_type no_content =
240247
"HTTP/1.0 204 No Content\r\n";
241-
static const std::string multiple_choices =
248+
static const string_type multiple_choices =
242249
"HTTP/1.0 300 Multiple Choices\r\n";
243-
static const std::string moved_permanently =
250+
static const string_type moved_permanently =
244251
"HTTP/1.0 301 Moved Permanently\r\n";
245-
static const std::string moved_temporarily =
252+
static const string_type moved_temporarily =
246253
"HTTP/1.0 302 Moved Temporarily\r\n";
247-
static const std::string not_modified =
254+
static const string_type not_modified =
248255
"HTTP/1.0 304 Not Modified\r\n";
249-
static const std::string bad_request =
256+
static const string_type bad_request =
250257
"HTTP/1.0 400 Bad Request\r\n";
251-
static const std::string unauthorized =
258+
static const string_type unauthorized =
252259
"HTTP/1.0 401 Unauthorized\r\n";
253-
static const std::string forbidden =
260+
static const string_type forbidden =
254261
"HTTP/1.0 403 Forbidden\r\n";
255-
static const std::string not_found =
262+
static const string_type not_found =
256263
"HTTP/1.0 404 Not Found\r\n";
257-
static const std::string not_supported =
264+
static const string_type not_supported =
258265
"HTTP/1.0 405 Method Not Supported\r\n";
259-
static const std::string not_acceptable =
266+
static const string_type not_acceptable =
260267
"HTTP/1.0 406 Method Not Acceptable\r\n";
261-
static const std::string internal_server_error =
268+
static const string_type internal_server_error =
262269
"HTTP/1.0 500 Internal Server Error\r\n";
263-
static const std::string not_implemented =
270+
static const string_type not_implemented =
264271
"HTTP/1.0 501 Not Implemented\r\n";
265-
static const std::string bad_gateway =
272+
static const string_type bad_gateway =
266273
"HTTP/1.0 502 Bad Gateway\r\n";
267-
static const std::string service_unavailable =
274+
static const string_type service_unavailable =
268275
"HTTP/1.0 503 Service Unavailable\r\n";
269276

270277
switch (status) {

libs/network/test/hello_world.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ typedef http::server<hello_world> server;
2121

2222
struct hello_world {
2323
void operator()(server::request const & request, server::response & response) {
24-
response.content = "Hello, World!";
25-
http::request_header content_length = { "Content-Length", lexical_cast<string>(response.content.size()) };
26-
http::request_header content_type = { "Content-Type", "text/plain" };
27-
response.headers = list_of(content_length)(content_type);
28-
response.status = server::response::ok;
24+
response = server::response::stock_reply(server::response::ok, "Hello, World!");
2925
assert(response.status == server::response::ok);
3026
assert(response.headers.size() == 2);
3127
assert(response.content == "Hello, World!");

0 commit comments

Comments
 (0)