Menu

[r222]: / docs / html / c___network_library / protocol / http.html  Maximize  Restore  History

Download this file

210 lines (204 with data), 20.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>HTTP</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
<link rel="home" href="../../index.html" title="C++ Network Library 0.4">
<link rel="up" href="../protocol.html" title="Application Layer Protocols">
<link rel="prev" href="../protocol.html" title="Application Layer Protocols">
<link rel="next" href="../examples.html" title="Examples">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="http://www.boost.org/boost.png"></td>
<td align="center"><a href="http://www.boost.org/index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="http://www.boost.org/more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../protocol.html"><img src="http://www.boost.org/doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../protocol.html"><img src="http://www.boost.org/doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="http://www.boost.org/doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="../examples.html"><img src="http://www.boost.org/doc/html/images/next.png" alt="Next"></a>
</div>
<div class="section" title="HTTP">
<div class="titlepage"><div><div><h3 class="title">
<a name="c___network_library.protocol.http"></a><a class="link" href="http.html" title="HTTP"> HTTP</a>
</h3></div></div></div>
<p>
The C++ Network Library provides direct support for HTTP. As a motivating
example, here is the code again from the quick start.
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">network</span><span class="special">/</span><span class="identifier">protocol</span><span class="special">/</span><span class="identifier">http</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="keyword">int</span>
<span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">argv</span><span class="special">[])</span> <span class="special">{</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">network</span><span class="special">;</span>
<span class="identifier">http</span><span class="special">::</span><span class="identifier">request</span> <span class="identifier">request</span><span class="special">(</span><span class="string">"http://www.boost.org/"</span><span class="special">);</span>
<span class="identifier">http</span><span class="special">::</span><span class="identifier">client</span> <span class="identifier">client</span><span class="special">;</span>
<span class="identifier">http</span><span class="special">::</span><span class="identifier">response</span> <span class="identifier">response</span> <span class="special">=</span> <span class="identifier">client</span><span class="special">.</span><span class="identifier">get</span><span class="special">(</span><span class="identifier">request</span><span class="special">);</span>
<span class="comment">// print response headers
</span> <span class="identifier">headers_range</span><span class="special">&lt;</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">response</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">hdrs</span> <span class="special">=</span> <span class="identifier">headers</span><span class="special">(</span><span class="identifier">response</span><span class="special">);</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_iterator</span><span class="special">&lt;</span><span class="identifier">headers_range</span><span class="special">&lt;</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">response</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">&gt;::</span><span class="identifier">type</span>
<span class="identifier">it</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">begin</span><span class="special">(</span><span class="identifier">hdrs</span><span class="special">),</span> <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">end</span><span class="special">(</span><span class="identifier">hdrs</span><span class="special">);</span>
<span class="keyword">for</span> <span class="special">(;</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">end</span><span class="special">;</span> <span class="special">++</span><span class="identifier">it</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">first</span> <span class="special">&lt;&lt;</span> <span class="string">": "</span> <span class="special">&lt;&lt;</span> <span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">second</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
<span class="comment">// print response body
</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">body</span><span class="special">(</span><span class="identifier">response</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
Before walking through exactly what is happening in this example, the principle
components are described below:
</p>
<a name="c___network_library.protocol.http.http_request"></a><h5>
<a name="id2760216"></a>
<a class="link" href="http.html#c___network_library.protocol.http.http_request">HTTP Request</a>
</h5>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">network</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">http</span> <span class="special">{</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Tag</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">basic_request</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">basic_request</span><span class="special">&lt;</span><span class="identifier">tags</span><span class="special">::</span><span class="identifier">default_</span><span class="special">&gt;</span> <span class="identifier">request</span><span class="special">;</span>
<span class="special">}}}</span>
</pre>
<p>
The <code class="literal">request</code> encapsulates information about the request
and the resource.
</p>
<a name="c___network_library.protocol.http.http_client"></a><h5>
<a name="id2760380"></a>
<a class="link" href="http.html#c___network_library.protocol.http.http_client">HTTP Client</a>
</h5>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">network</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">http</span> <span class="special">{</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Tag</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">basic_client</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">basic_client</span><span class="special">&lt;</span><span class="identifier">tags</span><span class="special">::</span><span class="identifier">default_</span><span class="special">&gt;</span> <span class="identifier">client</span><span class="special">;</span>
<span class="special">}}}</span>
</pre>
<p>
The <code class="literal">client</code> encapsulates the connection-mapping logic between
the domain and the underlying socket. Access to a resource is managed through
the <code class="literal">client</code> object.
</p>
<a name="c___network_library.protocol.http.http_response"></a><h5>
<a name="id2760550"></a>
<a class="link" href="http.html#c___network_library.protocol.http.http_response">HTTP Response</a>
</h5>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">network</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">http</span> <span class="special">{</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Tag</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">basic_response</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">basic_response</span><span class="special">&lt;</span><span class="identifier">tags</span><span class="special">::</span><span class="identifier">default_</span><span class="special">&gt;</span> <span class="identifier">response</span><span class="special">;</span>
<span class="special">}}}</span>
</pre>
<p>
The <code class="literal">response</code> encapsulates the data received from the server.
</p>
<a name="c___network_library.protocol.http.walkthrough"></a><h5>
<a name="id2760713"></a>
<a class="link" href="http.html#c___network_library.protocol.http.walkthrough">Walkthrough</a>
</h5>
<pre class="programlisting"><span class="identifier">http</span><span class="special">::</span><span class="identifier">request</span> <span class="identifier">request</span><span class="special">(</span><span class="string">"http://www.boost.org/"</span><span class="special">);</span>
</pre>
<p>
This line frames the request for the resource <a href="http://www.boost.org/" target="_top">http://www.boost.org/</a>.
</p>
<pre class="programlisting"><span class="identifier">http</span><span class="special">::</span><span class="identifier">client</span> <span class="identifier">client</span><span class="special">;</span>
</pre>
<p>
Then a client object is created that handles all HTTP requests and responses.
</p>
<pre class="programlisting"><span class="identifier">http</span><span class="special">::</span><span class="identifier">response</span> <span class="identifier">response</span> <span class="special">=</span> <span class="identifier">client</span><span class="special">.</span><span class="identifier">get</span><span class="special">(</span><span class="identifier">request</span><span class="special">);</span>
</pre>
<p>
The client simply performs the requests. The interface is trivially easy.
All HTTP methods are supported (HEAD, GET, POST, PUT, DELETE).
</p>
<p>
There are several advantages to this design:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
A <code class="literal">client</code> object manages the connection, unencumbering
the developer with this task;
</li>
<li class="listitem">
A <code class="literal">request</code> can be used with any instance of the <code class="literal">client</code>
without binding the <code class="literal">client</code> to any destination;
</li>
<li class="listitem">
By decoupling the method from the <code class="literal">request</code> object it
allows developers to create requests that may be re-used (e.g. perform
a HEAD first; if the the headers don't fulfil a certain criteria, perform
a GET using the same resource).
</li>
</ol></div>
<pre class="programlisting"><span class="comment">// print response headers
</span><span class="identifier">headers_range</span><span class="special">&lt;</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">response</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">hdrs</span> <span class="special">=</span> <span class="identifier">headers</span><span class="special">(</span><span class="identifier">response</span><span class="special">);</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_iterator</span><span class="special">&lt;</span><span class="identifier">headers_range</span><span class="special">&lt;</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">response</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">&gt;::</span><span class="identifier">type</span>
<span class="identifier">it</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">begin</span><span class="special">(</span><span class="identifier">hdrs</span><span class="special">),</span> <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">end</span><span class="special">(</span><span class="identifier">hdrs</span><span class="special">);</span>
<span class="keyword">for</span> <span class="special">(;</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">end</span><span class="special">;</span> <span class="special">++</span><span class="identifier">it</span><span class="special">)</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">first</span> <span class="special">&lt;&lt;</span> <span class="string">": "</span> <span class="special">&lt;&lt;</span> <span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">second</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
<span class="comment">// print response body
</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">body</span><span class="special">(</span><span class="identifier">response</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
</pre>
<p>
Once the request has been made, and the <code class="literal">client</code> returns
a <code class="literal">response</code> object, the rest is simple. This example outputs
all the response headers and body, in this case just the Boost homepage.
</p>
<a name="c___network_library.protocol.http.using__literal_http__client__literal_"></a><h5>
<a name="id2761333"></a>
<a class="link" href="http.html#c___network_library.protocol.http.using__literal_http__client__literal_">Using
<code class="literal">http::client</code></a>
</h5>
<p>
The <code class="literal">http::client</code> supports the following operations:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><code class="literal">http::client::head</code></li>
<li class="listitem"><code class="literal">http::client::get</code></li>
<li class="listitem"><code class="literal">http::client::post</code></li>
<li class="listitem"><code class="literal">http::client::put</code></li>
<li class="listitem"><code class="literal">http::client::delete_</code></li>
</ul></div>
<p>
HTTP features can be enabled by using constructor arguments:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><code class="literal">http::client(http::client::cache_resolved)</code></li>
<li class="listitem"><code class="literal">http::client(http::client::follow_redirect)</code></li>
</ul></div>
<a name="c___network_library.protocol.http._literal_http__client__cache_resolved__literal_"></a><h6>
<a name="id2761438"></a>
<a class="link" href="http.html#c___network_library.protocol.http._literal_http__client__cache_resolved__literal_"><code class="literal">http::client::cache_resolved</code></a>
</h6>
<p>
This argument enables the caching of resolved endpoints and prevents the
client from resolving IP addresses of previously resolved hostnames.
</p>
<a name="c___network_library.protocol.http._literal_http__client__follow_redirect_s___literal_"></a><h6>
<a name="id2761470"></a>
<a class="link" href="http.html#c___network_library.protocol.http._literal_http__client__follow_redirect_s___literal_"><code class="literal">http::client::follow_redirect(s)</code></a>
</h6>
<p>
<code class="literal">http::client::follow_redirects</code> / <code class="literal">http::client::follow_redirect</code>
follow HTTP redirect(s) (300..307) by looking at the "Location"
header provided by the response(s); headers present in the original request
are preserved in the subsequent request(s).
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2008 , 2009 Glyn Matthews Dean Michael Berris<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../protocol.html"><img src="http://www.boost.org/doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../protocol.html"><img src="http://www.boost.org/doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="http://www.boost.org/doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="../examples.html"><img src="http://www.boost.org/doc/html/images/next.png" alt="Next"></a>
</div>
</body>
</html>