4
4
"Hello world" HTTP server
5
5
***************************
6
6
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 `.
8
11
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
+ ========
12
14
13
15
The :mod: `cpp-netlib ` provides the framework to develop embedded HTTP
14
16
servers. For this example, the server is configured to return a
@@ -55,11 +57,44 @@ simple response to any HTTP request.
55
57
56
58
This is about a straightforward as server programming will get in C++.
57
59
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
+
58
92
.. code-block :: c++
59
93
60
94
#include <boost/network/protocol/http/server.hpp>
61
95
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 `.
63
98
64
99
.. code-block :: c++
65
100
@@ -76,7 +111,11 @@ This header contains all the code needed to develop an HTTP server.
76
111
77
112
``hello_world `` is a functor class which handles HTTP requests. All
78
113
the 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 ``.
80
119
81
120
.. code-block :: c++
82
121
@@ -86,10 +125,13 @@ and the body ``"Hello, World!"``.
86
125
87
126
The first two arguments to the ``server `` constructor are the host and
88
127
the 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.
95
137
0 commit comments