44 "Hello world" HTTP server
55***************************
66
7- ..todo ::
7+ Now that we've seen how we can deal with request and response objects from the
8+ client side, we'll see how we can then use the same abstractions on the server
9+ side. In this example we're going to create a simple HTTP Server in C++ using
10+ :mod: `cpp-netlib `.
811
9- The text needs to show that we're building on knowledge gained
10- from the HTTP client (hello world) example. Make sure there's
11- more description than code.
12+ The Code
13+ ========
1214
1315The:mod: `cpp-netlib ` provides the framework to develop embedded HTTP
1416servers. For this example, the server is configured to return a
@@ -55,11 +57,44 @@ simple response to any HTTP request.
5557
5658This is about a straightforward as server programming will get in C++.
5759
60+ Building the Server
61+ ===================
62+
63+ Just like with the HTTP client, we can build this example by doing the following
64+ on the shell::
65+
66+ $ cd ~/cpp-netlib
67+ $ g++ -o hello_world_server \
68+ > libs/network/example/http/hello_world_server.cpp \
69+ > -I$BOOST_ROOT \
70+ > -I. \
71+ > -L$BOOST_ROOT/stage/lib \
72+ > -lboost_system \
73+ > -pthread
74+
75+ The first two arguments to the ``server `` constructor are the host and
76+ the port on which the server will listen. The third argument is the
77+ the handler object defined previously. This example can be run from
78+ a command line as follows:
79+
80+ ::
81+
82+ shell$ ./hello_world_server 0.0.0.0 80
83+
84+ ..note ::If you're going to run the server on port 80, you may have to run it
85+ as an administrator.
86+
87+ Diving into the Code
88+ ====================
89+
90+ Let's take a look at the code listing above in greater detail.
91+
5892..code-block ::c++
5993
6094 #include <boost/network/protocol/http/server.hpp>
6195
62- This header contains all the code needed to develop an HTTP server.
96+ This header contains all the code needed to develop an HTTP server with
97+ :mod: `cpp-netlib `.
6398
6499..code-block ::c++
65100
@@ -76,7 +111,11 @@ This header contains all the code needed to develop an HTTP server.
76111
77112``hello_world `` is a functor class which handles HTTP requests. All
78113the operator does here is return an HTTP response with HTTP code 200
79- and the body ``"Hello, World!" ``.
114+ and the body ``"Hello, World!" ``. There are a number of pre-defined stock
115+ replies differentiated by status code with configurable bodies.
116+
117+ All the supported enumeration values for the response status codes can be found
118+ in ``boost/network/protocol/http/impl/response.ipp ``.
80119
81120..code-block ::c++
82121
@@ -86,10 +125,13 @@ and the body ``"Hello, World!"``.
86125
87126The first two arguments to the ``server `` constructor are the host and
88127the port on which the server will listen. The third argument is the
89- the handler object defined previously. This example can be run from
90- a command line as follows:
91-
92- ::
93-
94- shell$ ./hello_world_server 0.0.0.0 80
128+ the handler object defined previously.
129+
130+ ..note ::In this example, the server is specifically made to be single-threaded.
131+ In a multi-threaded server, you would invoke the ``hello_world::run `` member
132+ method in a set of threads. In a multi-threaded environment you would also
133+ make sure that the handler does all the necessary synchronization for shared
134+ resources across threads. The handler is passed by reference to the server
135+ constructor and you should ensure that any calls to the ``operator() `` overload
136+ are thread-safe.
95137