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

Fixed documentation for 0.11.2, this fixes cpp-netlib/cpp-netlib#581#9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
deanberris merged 1 commit intocpp-netlib:masterfromsusnux:fix-doc-0.11.2
Jan 10, 2016
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions0.11.2/_sources/examples/http/hello_world_server.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,6 @@ simple response to any HTTP request.
.. code-block:: c++

#include <boost/network/protocol/http/server.hpp>
#include <string>
#include <iostream>

namespace http = boost::network::http;
Expand All@@ -28,16 +27,19 @@ simple response to any HTTP request.
typedef http::server<hello_world> server;

struct hello_world {
void operator() (server::request const &request,
server::response &response) {
std::string ip = source(request);
response = server::response::stock_reply(
server::response::ok, std::string("Hello, ") + ip + "!");
void operator()(server::request const &request, server::response &response) {
server::string_type ip = source(request);
unsigned int port = request.source_port;
std::ostringstream data;
data << "Hello, " << ip << ':' << port << '!';
response = server::response::stock_reply(server::response::ok, data.str());
}
void log(const server::string_type& message) {
std::cerr << "ERROR: " << message << std::endl;
}
};

int
main(int argc, char * argv[]) {
int main(int argc, char *argv[]) {

if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
Expand All@@ -46,7 +48,8 @@ simple response to any HTTP request.

try {
hello_world handler;
server server_(argv[1], argv[2], handler);
server::options options(handler);
server server_(options.address(argv[1]).port(argv[2]));
server_.run();
}
catch (std::exception &e) {
Expand DownExpand Up@@ -100,34 +103,39 @@ This header contains all the code needed to develop an HTTP server with
typedef http::server<hello_world> server;

struct hello_world {
void operator () (server::request const &request,
server::response &response) {
std::string ip = source(request);
response = server::response::stock_reply(
server::response::ok, std::string("Hello, ") + ip + "!");
void operator()(server::request const &request, server::response &response) {
server::string_type ip = source(request);
unsigned int port = request.source_port;
std::ostringstream data;
data << "Hello, " << ip << ':' << port << '!';
response = server::response::stock_reply(server::response::ok, data.str());
}
void log(const server::string_type& message) {
std::cerr << "ERROR: " << message << std::endl;
}
};

``hello_world`` is a functor class which handles HTTP requests. All
the operator does here is return an HTTP response with HTTP code 200
and the body ``"Hello, <ip>!"``. The ``<ip>`` in this case would be
the IP address of the client that made the request.
``hello_world`` is a functor class which handles HTTP requests.
Allthe operator does here is return an HTTP response with HTTP code 200
and the body ``"Hello, <ip>:<port>!"``. The ``<ip>`` in this case would be
the IP address of the client that made the request and ``<port>`` the clients port.

There are a number of pre-defined stock replies differentiated by
status code with configurable bodies.

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

.. code-block:: c++

hello_world handler;
server server_(argv[1], argv[2], handler);
server::options options(handler);
server server_(options.address(argv[1]).port(argv[2]));
server_.run();

The first two arguments to the ``server`` constructor are the host and
the port on which the server will listen. The third argument is the
the handler object defined previously.
The ``server`` constructor requires an object of the ``options`` class,
this object stores all needed options, especially the host and
the port on which the server will listen.
The ``options`` constructor's single argument is the handler object defined previously.

.. note:: In this example, the server is specifically made to be single-threaded.
In a multi-threaded server, you would invoke the ``hello_world::run`` member
Expand Down
2 changes: 1 addition & 1 deletion0.11.2/_sources/examples/http/twitter_search.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,7 +64,7 @@ The code
`rapidjson`_, a header-only library that is released under
the `MIT License`_.

.. _`rapidjson`:http://code.google.com/p/rapidjson/
.. _`rapidjson`:https://github.com/miloyip/rapidjson
.. _`MIT License`: http://www.opensource.org/licenses/mit-license.php

Building and running ``twitter_search``
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp