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

Updated hello_world server example, fixes #624#630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
glynos merged 1 commit intocpp-netlib:0.12-releasefromsusnux:documentation
Apr 9, 2016
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletionslibs/network/doc/examples/http/hello_world_server.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,15 +27,12 @@ simple response to any HTTP request.
typedef http::server<hello_world> server;

struct hello_world {
void operator()(server::request const &request, server::response &response) {
void operator()(server::request const &request, server::connection_ptr connection) {
server::string_type ip = source(request);
unsigned int port = request.source_port;
std::ostringstream data;
data << "Hello, " << ip << ':' << port << '!';
response = server::response::stock_reply(server::response::ok, data.str());
}
void log(const server::string_type& message) {
std::cerr << "ERROR: " << message << std::endl;
connection->write(data.str());
}
};

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

struct hello_world {
void operator()(server::request const &request, server::response &response) {
void operator()(server::request const &request, server::connection_ptr connection) {
server::string_type ip = source(request);
unsigned int port = request.source_port;
std::ostringstream data;
data << "Hello, " << ip << ':' << port << '!';
response = server::response::stock_reply(server::response::ok, data.str());
}
void log(const server::string_type& message) {
std::cerr << "ERROR: " << message << std::endl;
connection->write(data.str());
}
};

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

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

[8]ページ先頭

©2009-2025 Movatter.jp