Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2016
Merged
Changes from all 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 deletions libs/network/doc/examples/http/hello_world_server.rst
Original file line number Diff line number Diff 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 Down Expand 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