|
| 1 | +HTTP implementation |
| 2 | +=================== |
| 3 | + |
| 4 | +HTTP client |
| 5 | +``````````` |
| 6 | + |
| 7 | +The cpp-netlib HTTP client is composed of three template classes: |
| 8 | + |
| 9 | +.. code-block:: c++ |
| 10 | + |
| 11 | + namespace http { |
| 12 | + template <class Tag> basic_request; |
| 13 | + template <class Tag> basic_response; |
| 14 | + template <class Tag> basic_client; |
| 15 | + typedef basic_request<default_> request; |
| 16 | + typedef basic_response<default_> response; |
| 17 | + typedef basic_client<default_> client; |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | +Each of these use again the tag-based static polymorphism that was described in |
| 22 | +previous sections. A default, human-readable typedef is provided for each one |
| 23 | +of the ``basic_request``, ``basic_response`` and ``basic_client``. |
| 24 | +``basic_request`` and ``basic_response`` each model the message concept. They |
| 25 | +make use of directives to set and get HTTP headers, body etc. The code snippet |
| 26 | +below shows how to set the HTTP header field "Connection" with the option |
| 27 | +"close" using the DSEL described in the directives section: |
| 28 | + |
| 29 | +.. code-block:: c++ |
| 30 | + |
| 31 | + using namespace boost::network; |
| 32 | + http::request request("http://www.boost.org/"); |
| 33 | + request << header("Connection", "close"); |
| 34 | + |
| 35 | +The ``basic_client`` implements all HTTP methods as member functions (HEAD, |
| 36 | +GET, POST, PUT, DELETE). Therefore, the code to make an HTTP request looks |
| 37 | +trivially simple: |
| 38 | + |
| 39 | +.. code-block:: c++ |
| 40 | + |
| 41 | + using namespace boost::network; |
| 42 | + http::client client; |
| 43 | + http::request request("http://www.boost.org/"); |
| 44 | + http::response response = client.get(request); |
| 45 | + |
| 46 | +Accessing data from ``http::response`` is also done using directives. To |
| 47 | +get the response headers, we use the ``headers`` directive which returns, |
| 48 | +in the default case, a map of strings to strings: |
| 49 | + |
| 50 | +.. code-block:: c++ |
| 51 | + |
| 52 | + using namespace boost::network; |
| 53 | + typedef headers_range<http_client::response>::type response_headers; |
| 54 | + boost::range_iterator<response_headers>::type iterator; |
| 55 | + |
| 56 | + response_headers headers_ = headers(response); |
| 57 | + for (iterator it = headers_.begin(); it != headers_.end(); ++it) { |
| 58 | + std::cout << it->first << ": " << it->second << std::endl; |
| 59 | + } |
| 60 | + std::cout << std::endl; |
| 61 | + |
| 62 | +HTTP server |
| 63 | +``````````` |
| 64 | + |
| 65 | +As with the HTTP client, the HTTP server that is provided with cpp-netlib is |
| 66 | +extensible through the tag mechanism and is embeddable. The template class |
| 67 | +declaration of ``basic_server`` is given below: |
| 68 | + |
| 69 | +.. code-block:: c++ |
| 70 | + |
| 71 | + namespace http { |
| 72 | + template <class Tag, class RequestHandler> basic_server; |
| 73 | + } |
| 74 | + |
| 75 | +The second template argument is used to specify the request handler type. The |
| 76 | +request handler type is a functor type which should overload the function call |
| 77 | +operator (``RequestHandler::operator()`` should be overloaded) that takes two |
| 78 | +parameters: the first one being a reference to a ``const basic_request<Tag>`` |
| 79 | +and the second being a reference to a ``basic_response<Tag>`` instance. |
| 80 | + |
| 81 | +All the logic for parsing the HTTP request and building the ``const |
| 82 | +basic_request<Tag>`` object resides internally in the ``basic_server`` template. |
| 83 | +Processing the request is delegated to the ``RequestHandler`` type, and the |
| 84 | +assumption of which would be that the response is formed inside the |
| 85 | +``RequestHandler`` function call operator overload. |
| 86 | + |
| 87 | +The ``basic_server`` template however is only an underlying implementation while |
| 88 | +the user-visible implementation is the ``http::server`` template. This simply |
| 89 | +specializes the ``basic_server`` template to use the ``default_`` tag and |
| 90 | +forwards the ``RequestHandler`` parameter: |
| 91 | + |
| 92 | +.. code-block:: c++ |
| 93 | + |
| 94 | + namespace http { |
| 95 | + template <class RequestHandler> server |
| 96 | + : public basic_server<default_, RequestHandler> |
| 97 | + {}; |
| 98 | + } |
| 99 | + |
| 100 | +To use the forwarding server type you just supply the request handler |
| 101 | +implementation as the parameter. For example, an "echo" server example might |
| 102 | +look something like this: |
| 103 | + |
| 104 | +.. code-block:: c++ |
| 105 | + |
| 106 | + using namespace boost::network; |
| 107 | + struct echo; |
| 108 | + typedef http::server<echo> echo_server; |
| 109 | + |
| 110 | + struct echo { |
| 111 | + void operator () (const echo_server::request &request, |
| 112 | + echo_server::response &response) const { |
| 113 | + response = echo_server::response::stock_reply( |
| 114 | + echo_server::response::ok, |
| 115 | + body(request)); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + |
| 120 | +Here, all we're doing is returning the original request body with an HTTP OK |
| 121 | +response (200). |
| 122 | + |
| 123 | +HTTP URI |
| 124 | +```````` |
| 125 | + |
| 126 | +Firstly, cpp-netlib provides a specialization and ``typedef`` for an HTTP URI: |
| 127 | + |
| 128 | +.. code-block:: c++ |
| 129 | + |
| 130 | + namespace http { |
| 131 | + template <class T> class basic_uri; |
| 132 | + typedef basic_uri<default_> uri; |
| 133 | + } |
| 134 | + |
| 135 | +``basic_uri`` provides a parser which breaks down a URI string passed to it's |
| 136 | +constructor into different parts. |
| 137 | + |
| 138 | +.. code-block:: c++ |
| 139 | + |
| 140 | + using namespace boost::network::uri; |
| 141 | + http::uri uri_("http://www.boost.org/"); |
| 142 | + assert(valid(uri_)); |
| 143 | + assert(scheme(uri_) == "http"); |
| 144 | + assert(host(uri_) == "www.boost.org") |
| 145 | + assert(port(uri_) == 80) |
| 146 | + assert(path(uri_) == "/"); |
| 147 | + |
| 148 | +The syntax of the HTTP URI are defined in RFC 1738 section 3.3 [#]_ and the |
| 149 | +default URI provided with cpp-netlib conforms to this. In such a way, |
| 150 | +specializations that conform to any URI scheme can be provided. |
| 151 | + |
| 152 | +.. [#] http://tools.ietf.org/html/rfc1738 |
0 commit comments