Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7fa83c3

Browse files
committed
Latest snapshot to incorporate initial comments from some Boost-devel list members.
1 parent10c661e commit7fa83c3

33 files changed

+678
-375
lines changed

‎_sources/getting_started.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ Downloading an official release
88
===============================
99

1010
All stable versions of :mod:`cpp-netlib` can be downloaded from
11-
Github_. Each release is available as gzipped (Using the command
11+
Github_ from this url:
12+
13+
http://github.com/cpp-netlib/cpp-netlib/downloads
14+
15+
Each release is available as gzipped (Using the command
1216
``tar xzf cpp-netlib.tar.gz``) or bzipped (Using ``tar xjf
1317
cpp-netlib.tar.bz2``) tarball, or as a zipfile (``unzip
1418
cpp-netlib.zip``, or on Windows using a tool such as 7zip_).
@@ -32,6 +36,16 @@ This should be enough information get to started. To do more complex
3236
things with Git, such as pulling changes or checking out a new branch,
3337
refer to the `Git documentation`_.
3438

39+
.. note:: If you look at the Git repository closely, this is the repository of
40+
*mikhailberis* instead of *cpp-netlib*. The reason is that the main developer
41+
and maintainer of the project is Dean Michael Berris, who goes by the alias
42+
*mikhailberis* on the Internet.
43+
44+
Dean does the merging and maintenance of the whole library, similar to how
45+
`Linus Torvalds`_ of the Linux project acts as the gatekeeper of the project.
46+
47+
.. _`Linus Torvalds`: http://en.wikipedia.org/wiki/Linus_Torvalds
48+
3549
Windows users need to use msysGit_, and to invoke the command above
3650
from a shell.
3751

@@ -106,6 +120,9 @@ additional parameters::
106120
> -DCMAKE_CXX_COMPILER=g++ \
107121
> .
108122

123+
Building on Linux
124+
~~~~~~~~~~~~~~~~~
125+
109126
On Linux, this will generate the appropriate Makefiles that will enable you to
110127
build and run the tests and examples that come with :mod:`cpp-netlib`. To build
111128
the tests, you can run ``make`` in the same top-level directory of

‎_sources/hello_world_client.txt

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,96 @@
44
"Hello world" HTTP client
55
***************************
66

7-
.. todo::
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.
810

9-
Take the HTTP client example and server example and describe
10-
what's happening.
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 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
56+
$ g++ -o hello_world_client \
57+
> libs/network/example/http/hello_world_client.cpp \
58+
> -I$BOOST_ROOT \
59+
> -I. \
60+
> -L$BOOST_ROOT/stage/lib \
61+
> -lboost_system \
62+
> -pthread
63+
64+
This example can be run from the command line as follows:
1165

1266
::
1367

14-
./hello_world_client http://127.0.0.1/
68+
$ ./hello_world_client 127.0.0.1 8000
69+
70+
.. note:: This assumes that you have the ``hello_world_server`` running on
71+
localhost port 8000.
72+
73+
Diving into the Code
74+
====================
75+
76+
All this example shows is how easy it is to write an HTTP client that connects
77+
to an HTTP server, and gets the body of the response. The relevant lines are:
78+
79+
.. code-block:: c++
80+
81+
http::client client;
82+
http::client::request request(url.str());
83+
http::client::response response =
84+
client.get(request);
85+
std::cout << body(response) << std::endl;
86+
87+
You can then imagine using this in an XML-RPC client, where you can craft the
88+
XML-RPC request as payload which you can pass as the body to a request, then
89+
perform the request via HTTP:
90+
91+
.. code-block:: c++
92+
93+
http::client client;
94+
http::client::request request("http://my.webservice.com/");
95+
http::client::response =
96+
client.post(request, "application/xml", some_xml_string);
97+
std::data = body(response);
98+
99+

‎_sources/hello_world_server.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ simple response to any HTTP request.
1919
.. code-block:: c++
2020

2121
#include <boost/network/protocol/http/server.hpp>
22+
#include <string>
2223
#include <iostream>
2324

2425
namespace http = boost::network::http;
@@ -29,8 +30,9 @@ simple response to any HTTP request.
2930
struct hello_world {
3031
void operator() (server::request const &request,
3132
server::response &response) {
33+
std::string ip = source(request);
3234
response = server::response::stock_reply(
33-
server::response::ok, "Hello,World!");
35+
server::response::ok,std::string("Hello,") + ip + "!");
3436
}
3537
};
3638

@@ -79,7 +81,7 @@ a command line as follows:
7981

8082
::
8183

82-
shell$ ./hello_world_server 0.0.0.080
84+
shell$ ./hello_world_server 0.0.0.08000
8385

8486
.. note:: If you're going to run the server on port 80, you may have to run it
8587
as an administrator.
@@ -104,15 +106,19 @@ This header contains all the code needed to develop an HTTP server with
104106
struct hello_world {
105107
void operator () (server::request const &request,
106108
server::response &response) {
109+
std::string ip = source(request);
107110
response = server::response::stock_reply(
108-
server::response::ok, "Hello,World!");
111+
server::response::ok,std::string("Hello,") + ip + "!");
109112
}
110113
};
111114

112115
``hello_world`` is a functor class which handles HTTP requests. All
113116
the operator does here is return an HTTP response with HTTP code 200
114-
and the body ``"Hello, World!"``. There are a number of pre-defined stock
115-
replies differentiated by status code with configurable bodies.
117+
and the body ``"Hello, <ip>!"``. The ``<ip>`` in this case would be
118+
the IP address of the client that made the request.
119+
120+
There are a number of pre-defined stock replies differentiated by
121+
status code with configurable bodies.
116122

117123
All the supported enumeration values for the response status codes can be found
118124
in ``boost/network/protocol/http/impl/response.ipp``.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp