Skip to content

Commit 7f11e67

Browse files
committed
Add the 0.13.0 docs online
1 parent 93366f9 commit 7f11e67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+22532
-0
lines changed

‎0.13.0/.buildinfo

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Sphinx build info version 1
2+
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3+
config: 2a727c93c7f2495ee4d62797d88de6a1
4+
tags: 645f666f9bcd5a90fca523b33c5a78b7

‎0.13.0/_sources/contents.rst.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.. _contents:
2+
3+
Contents
4+
--------
5+
6+
.. toctree::
7+
:maxdepth: 4
8+
9+
index.rst
10+
whats_new.rst
11+
getting_started.rst
12+
examples.rst
13+
reference.rst
14+
references.rst

‎0.13.0/_sources/examples.rst.txt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.. _examples:
2+
3+
Examples
4+
========
5+
6+
The :mod:`cpp-netlib` is a practical library that is designed to aid
7+
the development of applications for that need to communicate using
8+
common networking protocols. The following set of examples describe a
9+
series of realistic examples that use the :mod:`cpp-netlib` for these
10+
kinds of application. All examples are built using CMake.
11+
12+
HTTP examples
13+
`````````````
14+
15+
The HTTP component of the :mod:`cpp-netlib` contains a client and server.
16+
The examples that follow show how to use both for programs that can be
17+
embedded into larger applications.
18+
19+
.. toctree::
20+
:maxdepth: 1
21+
22+
examples/http/http_client
23+
examples/http/simple_wget
24+
examples/http/hello_world_server
25+
examples/http/hello_world_client
26+
examples/http/atom_reader
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.. _atom_reader:
2+
3+
******************
4+
Atom feed reader
5+
******************
6+
7+
The next examples show some simple, more practical applications using
8+
the HTTP client. The first one reads a simple Atom_ feed and prints
9+
the titles of each entry to the console.
10+
11+
.. _Atom: http://en.wikipedia.org/wiki/Atom_(standard)
12+
13+
The code
14+
========
15+
16+
.. code-block:: c++
17+
18+
#include "atom.hpp"
19+
#include <boost/network/protocol/http/client.hpp>
20+
#include <boost/foreach.hpp>
21+
#include <iostream>
22+
23+
int main(int argc, char * argv[]) {
24+
using namespace boost::network;
25+
26+
if (argc != 2) {
27+
std::cout << "Usage: " << argv[0] << " <url>" << std::endl;
28+
return 1;
29+
}
30+
31+
try {
32+
http::client client;
33+
http::client::request request(argv[1]);
34+
request << header("Connection", "close");
35+
http::client::response response = client.get(request);
36+
atom::feed feed(response);
37+
38+
std::cout << "Feed: " << feed.title()
39+
<< " (" << feed.subtitle() << ")" << std::endl;
40+
BOOST_FOREACH(const atom::entry &entry, feed) {
41+
std::cout << entry.title()
42+
<< " (" << entry.published() << ")" << std::endl;
43+
}
44+
}
45+
catch (std::exception &e) {
46+
std::cerr << e.what() << std::endl;
47+
}
48+
49+
return 0;
50+
}
51+
52+
Building and running ``atom_reader``
53+
====================================
54+
55+
.. code-block:: bash
56+
57+
$ cd ~/cpp-netlib-build
58+
$ make atom_reader
59+
60+
And to run the example from the command line to access the feed that
61+
lists of all the commits on cpp-netlib's master branch:
62+
63+
.. code-block:: bash
64+
65+
$ ./example/atom_reader https://github.com/cpp-netlib/cpp-netlib/commits/master.atom
66+
67+
Diving into the code
68+
====================
69+
70+
Most of this will now be familiar. The response is passed to the
71+
constructor to the ``atom::feed`` class, which parses the resultant
72+
XML. To keep this example as simple as possible, `rapidxml`_, a
73+
header-only XML parser library, was used to parse the response.
74+
75+
.. _`rapidxml`: http://rapidxml.sourceforge.net/
76+
77+
A similar example using RSS feeds exists in
78+
``libs/network/example/rss``.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
.. _hello_world_http_client:
2+
3+
***************************
4+
"Hello world" HTTP client
5+
***************************
6+
7+
Since we have a "Hello World" HTTP server, let's then create an HTTP client to
8+
access that server. This client will be similar to the HTTP client we made
9+
earlier in the documentation.
10+
11+
The code
12+
========
13+
14+
We want to create a simple HTTP client that just makes a request to the HTTP
15+
server that we created earlier. This really simple client will look like this:
16+
17+
.. code-block:: c++
18+
19+
#include <boost/network/protocol/http/client.hpp>
20+
#include <string>
21+
#include <sstream>
22+
#include <iostream>
23+
24+
namespace http = boost::network::http;
25+
26+
int main(int argc, char * argv[]) {
27+
if (argc != 3) {
28+
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
29+
return 1;
30+
}
31+
32+
try {
33+
http::client client;
34+
std::ostringstream url;
35+
url << "http://" << argv[1] << ":" << argv[2] << "/";
36+
http::client::request request(url.str());
37+
http::client::response response =
38+
client.get(request);
39+
std::cout << body(response) << std::endl;
40+
} catch (std::exception & e) {
41+
std::cerr << e.what() << std::endl;
42+
return 1;
43+
}
44+
return 0;
45+
}
46+
47+
Building and running the client
48+
===============================
49+
50+
Just like with the HTTP Server and HTTP client example before, we can build this
51+
example by doing the following on the shell:
52+
53+
.. code-block:: bash
54+
55+
$ cd ~/cpp-netlib-build
56+
$ make hello_world_client
57+
58+
This example can be run from the command line as follows:
59+
60+
.. code-block:: bash
61+
62+
$ ./example/hello_world_client http://127.0.0.1:8000
63+
64+
.. note:: This assumes that you have the ``hello_world_server`` running on
65+
localhost port 8000.
66+
67+
Diving into the code
68+
====================
69+
70+
All this example shows is how easy it is to write an HTTP client that connects
71+
to an HTTP server, and gets the body of the response. The relevant lines are:
72+
73+
.. code-block:: c++
74+
75+
http::client client;
76+
http::client::request request(url.str());
77+
http::client::response response =
78+
client.get(request);
79+
std::cout << body(response) << std::endl;
80+
81+
You can then imagine using this in an XML-RPC client, where you can craft the
82+
XML-RPC request as payload which you can pass as the body to a request, then
83+
perform the request via HTTP:
84+
85+
.. code-block:: c++
86+
87+
http::client client;
88+
http::client::request request("http://my.webservice.com/");
89+
http::client::response =
90+
client.post(request, some_xml_string, "application/xml");
91+
std::data = body(response);
92+
93+
The next set of examples show some more practical applications using
94+
the :mod:`cpp-netlib` HTTP client.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
.. _hello_world_http_server:
2+
3+
***************************
4+
"Hello world" HTTP server
5+
***************************
6+
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`.
11+
12+
The code
13+
========
14+
15+
The :mod:`cpp-netlib` provides the framework to develop embedded HTTP
16+
servers. For this example, the server is configured to return a
17+
simple response to any HTTP request.
18+
19+
.. code-block:: c++
20+
21+
#include <boost/network/protocol/http/server.hpp>
22+
#include <iostream>
23+
24+
namespace http = boost::network::http;
25+
26+
struct hello_world;
27+
typedef http::server<hello_world> server;
28+
29+
struct hello_world {
30+
void operator()(server::request const &request, server::connection_ptr connection) {
31+
server::string_type ip = source(request);
32+
unsigned int port = request.source_port;
33+
std::ostringstream data;
34+
data << "Hello, " << ip << ':' << port << '!';
35+
connection->set_status(server::connection::ok);
36+
connection->write(data.str());
37+
}
38+
};
39+
40+
int main(int argc, char *argv[]) {
41+
42+
if (argc != 3) {
43+
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
44+
return 1;
45+
}
46+
47+
try {
48+
hello_world handler;
49+
server::options options(handler);
50+
server server_(options.address(argv[1]).port(argv[2]));
51+
server_.run();
52+
}
53+
catch (std::exception &e) {
54+
std::cerr << e.what() << std::endl;
55+
return 1;
56+
}
57+
58+
return 0;
59+
}
60+
61+
This is about a straightforward as server programming will get in C++.
62+
63+
Building and running the server
64+
===============================
65+
66+
Just like with the HTTP client, we can build this example by doing the following
67+
on the shell:
68+
69+
.. code-block:: bash
70+
71+
$ cd ~/cpp-netlib-build
72+
$ make hello_world_server
73+
74+
The first two arguments to the ``server`` constructor are the host and
75+
the port on which the server will listen. The third argument is the
76+
the handler object defined previously. This example can be run from
77+
a command line as follows:
78+
79+
.. code-block:: bash
80+
81+
$ ./example/hello_world_server 0.0.0.0 8000
82+
83+
.. note:: If you're going to run the server on port 80, you may have to run it
84+
as an administrator.
85+
86+
Diving into the code
87+
====================
88+
89+
Let's take a look at the code listing above in greater detail.
90+
91+
.. code-block:: c++
92+
93+
#include <boost/network/protocol/http/server.hpp>
94+
95+
This header contains all the code needed to develop an HTTP server with
96+
:mod:`cpp-netlib`.
97+
98+
.. code-block:: c++
99+
100+
struct hello_world;
101+
typedef http::server<hello_world> server;
102+
103+
struct hello_world {
104+
void operator()(server::request const &request, server::connection_ptr connection) {
105+
server::string_type ip = source(request);
106+
unsigned int port = request.source_port;
107+
std::ostringstream data;
108+
data << "Hello, " << ip << ':' << port << '!';
109+
connection->write(data.str());
110+
}
111+
};
112+
113+
``hello_world`` is a functor class which handles HTTP requests.
114+
All the operator does here is return an HTTP response with HTTP code 200
115+
and the body ``"Hello, <ip>:<port>!"``. The ``<ip>`` in this case would be
116+
the IP address of the client that made the request and ``<port>`` the clients port.
117+
If you like to send an other status have a look at the function
118+
``set_status(status_t status)`` from connection.
119+
120+
There are a number of pre-defined stock replies differentiated by
121+
status code with configurable bodies.
122+
All the supported enumeration values for the response status codes can be found
123+
in ``boost/network/protocol/http/impl/response.ipp``.
124+
125+
.. code-block:: c++
126+
127+
hello_world handler;
128+
server::options options(handler);
129+
server server_(options.address(argv[1]).port(argv[2]));
130+
server_.run();
131+
132+
The ``server`` constructor requires an object of the ``options`` class,
133+
this object stores all needed options, especially the host and
134+
the port on which the server will listen.
135+
The ``options`` constructor's single argument is the handler object defined previously.
136+
137+
.. note:: In this example, the server is specifically made to be single-threaded.
138+
In a multi-threaded server, you would invoke the ``hello_world::run`` member
139+
method in a set of threads. In a multi-threaded environment you would also
140+
make sure that the handler does all the necessary synchronization for shared
141+
resources across threads. The handler is passed by reference to the server
142+
constructor and you should ensure that any calls to the ``operator()`` overload
143+
are thread-safe.

0 commit comments

Comments
 (0)