Skip to content

Commit 7e07ef6

Browse files
committed
Adding 0.10.1 documentation.
1 parent ed150e5 commit 7e07ef6

File tree

119 files changed

+13875
-0
lines changed

Some content is hidden

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

119 files changed

+13875
-0
lines changed

‎0.10.1/.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:
4+
tags:

‎0.10.1/.doctrees/contents.doctree

3.73 KB
Binary file not shown.

‎0.10.1/.doctrees/environment.pickle

75.5 KB
Binary file not shown.

‎0.10.1/.doctrees/examples.doctree

6.85 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
46.5 KB
Binary file not shown.

‎0.10.1/.doctrees/history.doctree

12.8 KB
Binary file not shown.

‎0.10.1/.doctrees/in_depth.doctree

5.24 KB
Binary file not shown.
30.5 KB
Binary file not shown.
Binary file not shown.
51.5 KB
Binary file not shown.

‎0.10.1/.doctrees/in_depth/uri.doctree

33.1 KB
Binary file not shown.

‎0.10.1/.doctrees/index.doctree

20.9 KB
Binary file not shown.

‎0.10.1/.doctrees/reference.doctree

4.43 KB
Binary file not shown.
88.8 KB
Binary file not shown.
82.9 KB
Binary file not shown.
Binary file not shown.
107 KB
Binary file not shown.

‎0.10.1/.doctrees/references.doctree

9.45 KB
Binary file not shown.

‎0.10.1/.doctrees/techniques.doctree

3.79 KB
Binary file not shown.
16.7 KB
Binary file not shown.
10.9 KB
Binary file not shown.
Binary file not shown.

‎0.10.1/.doctrees/whats_new.doctree

46.2 KB
Binary file not shown.

‎0.10.1/_images/ftp_uri.png

25 KB
Loading

‎0.10.1/_images/http_uri.png

20.2 KB
Loading

‎0.10.1/_images/mailto_uri.png

9.98 KB
Loading

‎0.10.1/_sources/contents.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
in_depth.rst
14+
techniques.rst
15+
history.rst
16+
reference.rst
17+
references.rst

‎0.10.1/_sources/examples.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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
27+
examples/http/twitter_search
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, "application/xml", some_xml_string);
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,139 @@
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 <string>
23+
#include <iostream>
24+
25+
namespace http = boost::network::http;
26+
27+
struct hello_world;
28+
typedef http::server<hello_world> server;
29+
30+
struct hello_world {
31+
void operator() (server::request const &request,
32+
server::response &response) {
33+
std::string ip = source(request);
34+
response = server::response::stock_reply(
35+
server::response::ok, std::string("Hello, ") + ip + "!");
36+
}
37+
};
38+
39+
int
40+
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 server_(argv[1], argv[2], handler);
50+
server_.run();
51+
}
52+
catch (std::exception &e) {
53+
std::cerr << e.what() << std::endl;
54+
return 1;
55+
}
56+
57+
return 0;
58+
}
59+
60+
This is about a straightforward as server programming will get in C++.
61+
62+
Building and running the server
63+
===============================
64+
65+
Just like with the HTTP client, we can build this example by doing the following
66+
on the shell:
67+
68+
.. code-block:: bash
69+
70+
$ cd ~/cpp-netlib-build
71+
$ make hello_world_server
72+
73+
The first two arguments to the ``server`` constructor are the host and
74+
the port on which the server will listen. The third argument is the
75+
the handler object defined previously. This example can be run from
76+
a command line as follows:
77+
78+
.. code-block:: bash
79+
80+
$ ./example/hello_world_server 0.0.0.0 8000
81+
82+
.. note:: If you're going to run the server on port 80, you may have to run it
83+
as an administrator.
84+
85+
Diving into the code
86+
====================
87+
88+
Let's take a look at the code listing above in greater detail.
89+
90+
.. code-block:: c++
91+
92+
#include <boost/network/protocol/http/server.hpp>
93+
94+
This header contains all the code needed to develop an HTTP server with
95+
:mod:`cpp-netlib`.
96+
97+
.. code-block:: c++
98+
99+
struct hello_world;
100+
typedef http::server<hello_world> server;
101+
102+
struct hello_world {
103+
void operator () (server::request const &request,
104+
server::response &response) {
105+
std::string ip = source(request);
106+
response = server::response::stock_reply(
107+
server::response::ok, std::string("Hello, ") + ip + "!");
108+
}
109+
};
110+
111+
``hello_world`` is a functor class which handles HTTP requests. All
112+
the operator does here is return an HTTP response with HTTP code 200
113+
and the body ``"Hello, <ip>!"``. The ``<ip>`` in this case would be
114+
the IP address of the client that made the request.
115+
116+
There are a number of pre-defined stock replies differentiated by
117+
status code with configurable bodies.
118+
119+
All the supported enumeration values for the response status codes can be found
120+
in ``boost/network/protocol/http/impl/response.ipp``.
121+
122+
.. code-block:: c++
123+
124+
hello_world handler;
125+
server server_(argv[1], argv[2], handler);
126+
server_.run();
127+
128+
The first two arguments to the ``server`` constructor are the host and
129+
the port on which the server will listen. The third argument is the
130+
the handler object defined previously.
131+
132+
.. note:: In this example, the server is specifically made to be single-threaded.
133+
In a multi-threaded server, you would invoke the ``hello_world::run`` member
134+
method in a set of threads. In a multi-threaded environment you would also
135+
make sure that the handler does all the necessary synchronization for shared
136+
resources across threads. The handler is passed by reference to the server
137+
constructor and you should ensure that any calls to the ``operator()`` overload
138+
are thread-safe.
139+

0 commit comments

Comments
 (0)