@@ -54,12 +54,12 @@ <h1><a href="index.html">The C++ Network Library v0.7 documentation</a></h1>
54
54
55
55
< div class ="section "id ="hello-world-http-server ">
56
56
< span id ="id1 "> </ span > < h1 > “Hello world” HTTP server< a class ="headerlink "href ="#hello-world-http-server "title ="Permalink to this headline "> ¶</ a > </ h1 >
57
- < div class =" admonition-todo admonition " id =" index-0 " >
58
- < p class =" first admonition-title " > Todo </ p >
59
- < p class =" last " > The text needs to show that we’rebuilding on knowledge gained
60
- from the HTTP client (hello world) example. Make sure there’s
61
- more description than code. </ p >
62
- </ div >
57
+ < p > Now that we’ve seen how we can deal with request and response objects from the
58
+ client side, we’ll see how we can then use the same abstractions on the server
59
+ side. In this example we’regoing to create a simple HTTP Server in C++ using
60
+ < tt class =" xref py py-mod docutils literal " > < span class =" pre " > cpp-netlib </ span > </ tt > . </ p >
61
+ < div class =" section " id =" the- code" >
62
+ < h2 > The Code < a class =" headerlink " href =" #the-code " title =" Permalink to this headline " > ¶ </ a > </ h2 >
63
63
< p > The< tt class ="xref py py-mod docutils literal "> < span class ="pre "> cpp-netlib</ span > </ tt > provides the framework to develop embedded HTTP
64
64
servers. For this example, the server is configured to return a
65
65
simple response to any HTTP request.</ p >
@@ -102,10 +102,40 @@ <h1><a href="index.html">The C++ Network Library v0.7 documentation</a></h1>
102
102
</ pre > </ div >
103
103
</ div >
104
104
< p > This is about a straightforward as server programming will get in C++.</ p >
105
+ </ div >
106
+ < div class ="section "id ="building-the-server ">
107
+ < h2 > Building the Server< a class ="headerlink "href ="#building-the-server "title ="Permalink to this headline "> ¶</ a > </ h2 >
108
+ < p > Just like with the HTTP client, we can build this example by doing the following
109
+ on the shell:</ p >
110
+ < div class ="highlight-python "> < pre > $ cd ~/cpp-netlib
111
+ $ g++ -o hello_world_server \
112
+ > libs/network/example/http/hello_world_server.cpp \
113
+ > -I$BOOST_ROOT \
114
+ > -I. \
115
+ > -L$BOOST_ROOT/stage/lib \
116
+ > -lboost_system \
117
+ > -pthread</ pre >
118
+ </ div >
119
+ < p > The first two arguments to the< tt class ="docutils literal "> < span class ="pre "> server</ span > </ tt > constructor are the host and
120
+ the port on which the server will listen. The third argument is the
121
+ the handler object defined previously. This example can be run from
122
+ a command line as follows:</ p >
123
+ < div class ="highlight-python "> < pre > shell$ ./hello_world_server 0.0.0.0 80</ pre >
124
+ </ div >
125
+ < div class ="admonition note ">
126
+ < p class ="first admonition-title "> Note</ p >
127
+ < p class ="last "> If you’re going to run the server on port 80, you may have to run it
128
+ as an administrator.</ p >
129
+ </ div >
130
+ </ div >
131
+ < div class ="section "id ="diving-into-the-code ">
132
+ < h2 > Diving into the Code< a class ="headerlink "href ="#diving-into-the-code "title ="Permalink to this headline "> ¶</ a > </ h2 >
133
+ < p > Let’s take a look at the code listing above in greater detail.</ p >
105
134
< div class ="highlight-c++ "> < div class ="highlight "> < pre > < span class ="cp "> #include <boost/network/protocol/http/server.hpp></ span >
106
135
</ pre > </ div >
107
136
</ div >
108
- < p > This header contains all the code needed to develop an HTTP server.</ p >
137
+ < p > This header contains all the code needed to develop an HTTP server with
138
+ < tt class ="xref py py-mod docutils literal "> < span class ="pre "> cpp-netlib</ span > </ tt > .</ p >
109
139
< div class ="highlight-c++ "> < div class ="highlight "> < pre > < span class ="k "> struct</ span > < span class ="n "> hello_world</ span > < span class ="p "> ;</ span >
110
140
< span class ="k "> typedef</ span > < span class ="n "> http</ span > < span class ="o "> ::</ span > < span class ="n "> server</ span > < span class ="o "> <</ span > < span class ="n "> hello_world</ span > < span class ="o "> ></ span > < span class ="n "> server</ span > < span class ="p "> ;</ span >
111
141
@@ -120,17 +150,28 @@ <h1><a href="index.html">The C++ Network Library v0.7 documentation</a></h1>
120
150
</ div >
121
151
< p > < tt class ="docutils literal "> < span class ="pre "> hello_world</ span > </ tt > is a functor class which handles HTTP requests. All
122
152
the operator does here is return an HTTP response with HTTP code 200
123
- and the body< tt class ="docutils literal "> < span class ="pre "> "Hello,</ span > < span class ="pre "> World!"</ span > </ tt > .</ p >
153
+ and the body< tt class ="docutils literal "> < span class ="pre "> "Hello,</ span > < span class ="pre "> World!"</ span > </ tt > . There are a number of pre-defined stock
154
+ replies differentiated by status code with configurable bodies.</ p >
155
+ < p > All the supported enumeration values for the response status codes can be found
156
+ in< tt class ="docutils literal "> < span class ="pre "> boost/network/protocol/http/impl/response.ipp</ span > </ tt > .</ p >
124
157
< div class ="highlight-c++ "> < div class ="highlight "> < pre > < span class ="n "> hello_world</ span > < span class ="n "> handler</ span > < span class ="p "> ;</ span >
125
158
< span class ="n "> server</ span > < span class ="n "> server_</ span > < span class ="p "> (</ span > < span class ="n "> argv</ span > < span class ="p "> [</ span > < span class ="mi "> 1</ span > < span class ="p "> ],</ span > < span class ="n "> argv</ span > < span class ="p "> [</ span > < span class ="mi "> 2</ span > < span class ="p "> ],</ span > < span class ="n "> handler</ span > < span class ="p "> );</ span >
126
159
< span class ="n "> server_</ span > < span class ="p "> .</ span > < span class ="n "> run</ span > < span class ="p "> ();</ span >
127
160
</ pre > </ div >
128
161
</ div >
129
162
< p > The first two arguments to the< tt class ="docutils literal "> < span class ="pre "> server</ span > </ tt > constructor are the host and
130
163
the port on which the server will listen. The third argument is the
131
- the handler object defined previously. This example can be run from
132
- a command line as follows:</ p >
133
- < div class ="highlight-python "> < pre > shell$ ./hello_world_server 0.0.0.0 80</ pre >
164
+ the handler object defined previously.</ p >
165
+ < div class ="admonition note ">
166
+ < p class ="first admonition-title "> Note</ p >
167
+ < p class ="last "> In this example, the server is specifically made to be single-threaded.
168
+ In a multi-threaded server, you would invoke the< tt class ="docutils literal "> < span class ="pre "> hello_world::run</ span > </ tt > member
169
+ method in a set of threads. In a multi-threaded environment you would also
170
+ make sure that the handler does all the necessary synchronization for shared
171
+ resources across threads. The handler is passed by reference to the server
172
+ constructor and you should ensure that any calls to the< tt class ="docutils literal "> < span class ="pre "> operator()</ span > </ tt > overload
173
+ are thread-safe.</ p >
174
+ </ div >
134
175
</ div >
135
176
</ div >
136
177
@@ -144,6 +185,16 @@ <h1><a href="index.html">The C++ Network Library v0.7 documentation</a></h1>
144
185
145
186
< div class ="sphinxsidebar ">
146
187
< div class ="sphinxsidebarwrapper ">
188
+ < h3 > < a href ="index.html "> Table Of Contents</ a > </ h3 >
189
+ < ul >
190
+ < li > < a class ="reference internal "href ="# "> “Hello world” HTTP server</ a > < ul >
191
+ < li > < a class ="reference internal "href ="#the-code "> The Code</ a > </ li >
192
+ < li > < a class ="reference internal "href ="#building-the-server "> Building the Server</ a > </ li >
193
+ < li > < a class ="reference internal "href ="#diving-into-the-code "> Diving into the Code</ a > </ li >
194
+ </ ul >
195
+ </ li >
196
+ </ ul >
197
+
147
198
< h3 > Browse</ h3 >
148
199
< ul >
149
200