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

Commit3f35a1e

Browse files
committed
Merge pull request#630 from susnux/documentation
Updated hello_world server example,fixes#624
2 parents89f9768 +5fbb5bb commit3f35a1e

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

‎libs/network/doc/examples/http/hello_world_server.rst

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ simple response to any HTTP request.
2727
typedef http::server<hello_world> server;
2828

2929
struct hello_world {
30-
void operator()(server::request const &request, server::response &response) {
30+
void operator()(server::request const &request, server::connection_ptr connection) {
3131
server::string_type ip = source(request);
3232
unsigned int port = request.source_port;
3333
std::ostringstream data;
3434
data << "Hello, " << ip << ':' << port << '!';
35-
response = server::response::stock_reply(server::response::ok, data.str());
36-
}
37-
void log(const server::string_type& message) {
38-
std::cerr << "ERROR: " << message << std::endl;
35+
connection->write(data.str());
3936
}
4037
};
4138

@@ -103,22 +100,21 @@ This header contains all the code needed to develop an HTTP server with
103100
typedef http::server<hello_world> server;
104101

105102
struct hello_world {
106-
void operator()(server::request const &request, server::response &response) {
103+
void operator()(server::request const &request, server::connection_ptr connection) {
107104
server::string_type ip = source(request);
108105
unsigned int port = request.source_port;
109106
std::ostringstream data;
110107
data << "Hello, " << ip << ':' << port << '!';
111-
response = server::response::stock_reply(server::response::ok, data.str());
112-
}
113-
void log(const server::string_type& message) {
114-
std::cerr << "ERROR: " << message << std::endl;
108+
connection->write(data.str());
115109
}
116110
};
117111

118112
``hello_world`` is a functor class which handles HTTP requests.
119113
All the operator does here is return an HTTP response with HTTP code 200
120114
and the body ``"Hello, <ip>:<port>!"``. The ``<ip>`` in this case would be
121115
the IP address of the client that made the request and ``<port>`` the clients port.
116+
If you like to send an other status have a look at the function
117+
``set_status(status_t status)`` from connection.
122118

123119
There are a number of pre-defined stock replies differentiated by
124120
status code with configurable bodies.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp