Skip to content

Commit a606b5e

Browse files
committed
Add 0.12.0 docs
1 parent 57e59f9 commit a606b5e

Some content is hidden

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

70 files changed

+22368
-0
lines changed

‎0.12.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: adbe6ea7ac2e69d5b593dfb1e312603d
4+
tags: 645f666f9bcd5a90fca523b33c5a78b7

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

0 commit comments

Comments
 (0)