@@ -19,7 +19,6 @@ simple response to any HTTP request.
19
19
.. code-block:: c++
20
20
21
21
#include <boost/network/protocol/http/server.hpp>
22
- #include <string>
23
22
#include <iostream>
24
23
25
24
namespace http = boost::network::http;
@@ -28,16 +27,19 @@ simple response to any HTTP request.
28
27
typedef http::server<hello_world> server;
29
28
30
29
struct hello_world {
31
- void operator() (server::request const &request,
32
- server::response &response) {
33
- std::string ip = source(request);
34
- response = server::response::stock_reply(
35
- server::response::ok, std::string("Hello, ") + ip + "!");
30
+ void operator()(server::request const &request, server::response &response) {
31
+ server::string_type ip = source(request);
32
+ unsigned int port = request.source_port;
33
+ std::ostringstream data;
34
+ 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;
36
39
}
37
40
};
38
41
39
- int
40
- main(int argc, char * argv[]) {
42
+ int main(int argc, char *argv[]) {
41
43
42
44
if (argc != 3) {
43
45
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
@@ -46,7 +48,8 @@ simple response to any HTTP request.
46
48
47
49
try {
48
50
hello_world handler;
49
- server server_(argv[1], argv[2], handler);
51
+ server::options options(handler);
52
+ server server_(options.address(argv[1]).port(argv[2]));
50
53
server_.run();
51
54
}
52
55
catch (std::exception &e) {
@@ -100,34 +103,39 @@ This header contains all the code needed to develop an HTTP server with
100
103
typedef http::server<hello_world> server;
101
104
102
105
struct hello_world {
103
- void operator () (server::request const &request,
104
- server::response &response) {
105
- std::string ip = source(request);
106
- response = server::response::stock_reply(
107
- server::response::ok, std::string("Hello, ") + ip + "!");
106
+ void operator()(server::request const &request, server::response &response) {
107
+ server::string_type ip = source(request);
108
+ unsigned int port = request.source_port;
109
+ std::ostringstream data;
110
+ 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
115
}
109
116
};
110
117
111
- ``hello_world`` is a functor class which handles HTTP requests. All
112
- the operator does here is return an HTTP response with HTTP code 200
113
- and the body ``"Hello, <ip>!"``. The ``<ip>`` in this case would be
114
- the IP address of the client that made the request.
118
+ ``hello_world`` is a functor class which handles HTTP requests.
119
+ All the operator does here is return an HTTP response with HTTP code 200
120
+ and the body ``"Hello, <ip>:<port> !"``. The ``<ip>`` in this case would be
121
+ the IP address of the client that made the request and ``<port>`` the clients port .
115
122
116
123
There are a number of pre-defined stock replies differentiated by
117
124
status code with configurable bodies.
118
-
119
125
All the supported enumeration values for the response status codes can be found
120
126
in ``boost/network/protocol/http/impl/response.ipp``.
121
127
122
128
.. code-block:: c++
123
129
124
130
hello_world handler;
125
- server server_(argv[1], argv[2], handler);
131
+ server::options options(handler);
132
+ server server_(options.address(argv[1]).port(argv[2]));
126
133
server_.run();
127
134
128
- The first two arguments to the ``server`` constructor are the host and
129
- the port on which the server will listen. The third argument is the
130
- the handler object defined previously.
135
+ The ``server`` constructor requires an object of the ``options`` class,
136
+ this object stores all needed options, especially the host and
137
+ the port on which the server will listen.
138
+ The ``options`` constructor's single argument is the handler object defined previously.
131
139
132
140
.. note:: In this example, the server is specifically made to be single-threaded.
133
141
In a multi-threaded server, you would invoke the ``hello_world::run`` member
0 commit comments