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