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

Commit0762e99

Browse files
committed
Adding more information on the server example and the HTTP implementation.
1 parent4120a86 commit0762e99

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

‎libs/network/doc/hello_world_server.rst

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

‎libs/network/doc/http.rst

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++

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp