@@ -54,12 +54,12 @@ <h1><a href="index.html">The C++ Network Library v0.7 documentation</a></h1>
5454
5555< div class ="section "id ="hello-world-http-server ">
5656< 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 >
6363< p > The< tt class ="xref py py-mod docutils literal "> < span class ="pre "> cpp-netlib</ span > </ tt > provides the framework to develop embedded HTTP
6464servers. For this example, the server is configured to return a
6565simple response to any HTTP request.</ p >
@@ -102,10 +102,40 @@ <h1><a href="index.html">The C++ Network Library v0.7 documentation</a></h1>
102102</ pre > </ div >
103103</ div >
104104< 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 >
105134< div class ="highlight-c++ "> < div class ="highlight "> < pre > < span class ="cp "> #include <boost/network/protocol/http/server.hpp></ span >
106135</ pre > </ div >
107136</ 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 >
109139< div class ="highlight-c++ "> < div class ="highlight "> < pre > < span class ="k "> struct</ span > < span class ="n "> hello_world</ span > < span class ="p "> ;</ span >
110140< 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 >
111141
@@ -120,17 +150,28 @@ <h1><a href="index.html">The C++ Network Library v0.7 documentation</a></h1>
120150</ div >
121151< p > < tt class ="docutils literal "> < span class ="pre "> hello_world</ span > </ tt > is a functor class which handles HTTP requests. All
122152the 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 >
124157< div class ="highlight-c++ "> < div class ="highlight "> < pre > < span class ="n "> hello_world</ span > < span class ="n "> handler</ span > < span class ="p "> ;</ span >
125158< 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 >
126159< span class ="n "> server_</ span > < span class ="p "> .</ span > < span class ="n "> run</ span > < span class ="p "> ();</ span >
127160</ pre > </ div >
128161</ div >
129162< p > The first two arguments to the< tt class ="docutils literal "> < span class ="pre "> server</ span > </ tt > constructor are the host and
130163the 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 >
134175</ div >
135176</ div >
136177
@@ -144,6 +185,16 @@ <h1><a href="index.html">The C++ Network Library v0.7 documentation</a></h1>
144185
145186< div class ="sphinxsidebar ">
146187< 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+
147198< h3 > Browse</ h3 >
148199< ul >
149200