Skip to content

Commit 10c661e

Browse files
committed
Making the latest snapshot from 0762e99fffdaaaaa758f
1 parent 3a49b0d commit 10c661e

File tree

8 files changed

+268
-40
lines changed

8 files changed

+268
-40
lines changed

_sources/hello_world_server.txt

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
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

1315
The :mod:`cpp-netlib` provides the framework to develop embedded HTTP
1416
servers. For this example, the server is configured to return a
@@ -55,11 +57,44 @@ simple response to any HTTP request.
5557

5658
This 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
78113
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``.
80119

81120
.. code-block:: c++
82121

@@ -86,10 +125,13 @@ and the body ``"Hello, World!"``.
86125

87126
The first two arguments to the ``server`` constructor are the host and
88127
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.
95137

_sources/http.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ request looks trivially simple:
4141

4242
using namespace boost::network;
4343
http::client client;
44-
http::request request("http://www.boost.org/");
45-
http::response response = client.get(request);
44+
http::client::request request("http://www.boost.org/");
45+
http::client::response response = client.get(request);
4646

4747
Accessing data from ``http::response`` is also done using directives.
4848
To get the response headers, we use the ``headers`` directive which
@@ -129,7 +129,7 @@ HTTP OK response (200).
129129
HTTP URI
130130
````````
131131

132-
Firstly, cpp-netlib provides a specialization and ``typedef`` for an
132+
:mod:`cpp-netlib` provides a specialization and ``typedef`` for an
133133
HTTP URI:
134134

135135
.. code-block:: c++

_sources/index.txt

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
.. :Authors: Glyn Matthews <glyn.matthews@gmail.com>
1414
.. Dean Michael Berris <mikhailberis@gmail.com>
15-
.. :Date: Sep 19, 2010
15+
.. :Date: Oct 9, 2010
1616
.. :Version: 0.7
1717
.. :Description: Complete user documentation, with examples, for the :mod:`cpp-netlib`.
1818
.. :Copyright: Copyright Glyn Matthews, Dean Michael Berris 2008-2010.
@@ -33,11 +33,62 @@ some point in the future be submitted for review into Boost. A
3333
presentation about :mod:`cpp-netlib` was given at `BoostCon 2010`_,
3434
for which the `slides`_ and the `paper`_ can be found on-line.
3535

36+
Sneak Peak
37+
==========
38+
39+
The :mod:`cpp-netlib` allows you to write semantically consistent code for
40+
making different kinds of higher level network applications.
41+
42+
The library allows for writing simple code like:
43+
44+
.. code-block:: c++
45+
46+
using namespace boost::network;
47+
using namespace boost::network::http;
48+
49+
client::request request_("http://www.boost.org/");
50+
request_ << header("Connection", "close");
51+
client client_;
52+
client::response response_ = client_.get(request);
53+
std::string body = body(response_);
54+
55+
For simple C++ HTTP client applications.
56+
57+
The :mod:`cpp-netlib` is being developed for eventual submission to Boost_.
58+
59+
Download
60+
========
61+
62+
You can download the latest releases of the library at:
63+
64+
http://github.com/mikhailberis/cpp-netlib/downloads
65+
66+
You can also get the latest developments from the Git_ repository at:
67+
68+
git://github.com/mikhailberis/cpp-netlib.git
69+
70+
You can find more information about the progress of the development by checking
71+
the GitHub_ project page at:
72+
73+
http://github.com/mikhailberis/cpp-netlib
74+
75+
Support
76+
=======
77+
78+
You can ask questions, join the discussion, and report issues to the developers
79+
mailing list by joining via:
80+
81+
https://lists.sourceforge.net/lists/listinfo/cpp-netlib-devel
82+
83+
You may also file issues on the Github_ issue tracker at:
84+
85+
http://github.com/mikhailberis/cpp-netlib/issues
86+
3687
Contents
3788
========
3889

3990
.. toctree::
40-
:maxdepth: 1
91+
:maxdepth: 2
4192

4293
whats_new.rst
4394
getting_started.rst
@@ -52,3 +103,6 @@ Contents
52103
.. _`BoostCon 2010`: http://www.boostcon.com/
53104
.. _`slides`: http://www.filetolink.com/b0e89d06
54105
.. _`paper`: http://github.com/downloads/mikhailberis/cpp-netlib-boostcon-paper/cpp-netlib.pdf
106+
.. _Git: http://git-scm.com/
107+
.. _GitHub: http://github.com/
108+

examples_http.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ <h1>HTTP examples<a class="headerlink" href="#http-examples" title="Permalink to
6565
<li class="toctree-l2"><a class="reference internal" href="http_client.html#diving-into-the-code">Diving into the Code</a></li>
6666
</ul>
6767
</li>
68-
<li class="toctree-l1"><a class="reference internal" href="hello_world_server.html">&#8220;Hello world&#8221; HTTP server</a></li>
68+
<li class="toctree-l1"><a class="reference internal" href="hello_world_server.html">&#8220;Hello world&#8221; HTTP server</a><ul>
69+
<li class="toctree-l2"><a class="reference internal" href="hello_world_server.html#the-code">The Code</a></li>
70+
<li class="toctree-l2"><a class="reference internal" href="hello_world_server.html#building-the-server">Building the Server</a></li>
71+
<li class="toctree-l2"><a class="reference internal" href="hello_world_server.html#diving-into-the-code">Diving into the Code</a></li>
72+
</ul>
73+
</li>
6974
<li class="toctree-l1"><a class="reference internal" href="hello_world_client.html">&#8220;Hello world&#8221; HTTP client</a></li>
7075
</ul>
7176
</div>

hello_world_server.html

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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>&#8220;Hello world&#8221; 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&#8217;re building on knowledge gained
60-
from the HTTP client (hello world) example. Make sure there&#8217;s
61-
more description than code.</p>
62-
</div>
57+
<p>Now that we&#8217;ve seen how we can deal with request and response objects from the
58+
client side, we&#8217;ll see how we can then use the same abstractions on the server
59+
side. In this example we&#8217;re going 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
6464
servers. For this example, the server is configured to return a
6565
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>
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+
&gt; libs/network/example/http/hello_world_server.cpp \
113+
&gt; -I$BOOST_ROOT \
114+
&gt; -I. \
115+
&gt; -L$BOOST_ROOT/stage/lib \
116+
&gt; -lboost_system \
117+
&gt; -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&#8217;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&#8217;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 &lt;boost/network/protocol/http/server.hpp&gt;</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">&lt;</span><span class="n">hello_world</span><span class="o">&gt;</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
122152
the operator does here is return an HTTP response with HTTP code 200
123-
and the body <tt class="docutils literal"><span class="pre">&quot;Hello,</span> <span class="pre">World!&quot;</span></tt>.</p>
153+
and the body <tt class="docutils literal"><span class="pre">&quot;Hello,</span> <span class="pre">World!&quot;</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
130163
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>
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="#">&#8220;Hello world&#8221; 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

http.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ <h2>HTTP client<a class="headerlink" href="#http-client" title="Permalink to thi
8585
request looks trivially simple:</p>
8686
<div class="highlight-c++"><div class="highlight"><pre><span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">network</span><span class="p">;</span>
8787
<span class="n">http</span><span class="o">::</span><span class="n">client</span> <span class="n">client</span><span class="p">;</span>
88-
<span class="n">http</span><span class="o">::</span><span class="n">request</span> <span class="n">request</span><span class="p">(</span><span class="s">&quot;http://www.boost.org/&quot;</span><span class="p">);</span>
89-
<span class="n">http</span><span class="o">::</span><span class="n">response</span> <span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">request</span><span class="p">);</span>
88+
<span class="n">http</span><span class="o">::</span><span class="n">client</span><span class="o">::</span><span class="n">request</span> <span class="n">request</span><span class="p">(</span><span class="s">&quot;http://www.boost.org/&quot;</span><span class="p">);</span>
89+
<span class="n">http</span><span class="o">::</span><span class="n">client</span><span class="o">::</span><span class="n">response</span> <span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">request</span><span class="p">);</span>
9090
</pre></div>
9191
</div>
9292
<p>Accessing data from <tt class="docutils literal"><span class="pre">http::response</span></tt> is also done using directives.
@@ -162,7 +162,7 @@ <h2>HTTP server<a class="headerlink" href="#http-server" title="Permalink to thi
162162
</div>
163163
<div class="section" id="http-uri">
164164
<h2>HTTP URI<a class="headerlink" href="#http-uri" title="Permalink to this headline"></a></h2>
165-
<p>Firstly, cpp-netlib provides a specialization and <tt class="docutils literal"><span class="pre">typedef</span></tt> for an
165+
<p><tt class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></tt> provides a specialization and <tt class="docutils literal"><span class="pre">typedef</span></tt> for an
166166
HTTP URI:</p>
167167
<div class="highlight-c++"><div class="highlight"><pre><span class="k">namespace</span> <span class="n">http</span> <span class="p">{</span>
168168
<span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span> <span class="k">class</span> <span class="nc">basic_uri</span><span class="p">;</span>

0 commit comments

Comments
 (0)