Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc3eeb92

Browse files
committed
Using correct traits for defining the string type and the vector type, making stock_reply creation easier.
1 parente1e11a8 commitc3eeb92

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
#defineBOOST_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
namespaceboost {namespacenetwork {namespacehttp {
@@ -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+
returnstock_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-
staticstd::stringto_string(status_type status) {
107+
staticstring_typeto_string(status_type status) {
101108
staticconstchar ok[] ="";
102109
staticconstchar created[] =
103110
"<html>"
@@ -230,41 +237,41 @@ namespace boost { namespace network { namespace http {
230237

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

[8]ページ先頭

©2009-2025 Movatter.jp