1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>HEAD response (Server) </title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast"> 8<link rel="up" href="../more_examples.html" title="HTTP Examples"> 9<link rel="prev" href="head_request_client.html" title="HEAD request (Client) "> 10<link rel="next" href="http_relay.html" title="HTTP Relay "> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="head_request_client.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../more_examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="http_relay.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="beast.more_examples.head_response_server"></a><a class="link" href="head_response_server.html" title="HEAD response (Server) ">HEAD response 28 (Server) </a> 29</h3></div></div></div> 30<p> 31 When a server receives a <a href="https://tools.ietf.org/html/rfc7231#section-4.3.2" target="_top">HEAD 32 request</a>, the response should contain the entire header that would 33 be delivered if the method was GET, except that the body is omitted. 34 </p> 35<pre class="programlisting"><span class="comment">/** Handle a HEAD request for a resource. 36*/</span> 37<span class="keyword">template</span><span class="special"><</span> 38 <span class="keyword">class</span> <span class="identifier">SyncStream</span><span class="special">,</span> 39 <span class="keyword">class</span> <span class="identifier">DynamicBuffer</span> 40<span class="special">></span> 41<span class="keyword">void</span> <span class="identifier">do_server_head</span><span class="special">(</span> 42 <span class="identifier">SyncStream</span><span class="special">&</span> <span class="identifier">stream</span><span class="special">,</span> 43 <span class="identifier">DynamicBuffer</span><span class="special">&</span> <span class="identifier">buffer</span><span class="special">,</span> 44 <span class="identifier">error_code</span><span class="special">&</span> <span class="identifier">ec</span><span class="special">)</span> 45<span class="special">{</span> 46 <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_sync_stream</span><span class="special"><</span><span class="identifier">SyncStream</span><span class="special">>::</span><span class="identifier">value</span><span class="special">,</span> 47 <span class="string">"SyncStream requirements not met"</span><span class="special">);</span> 48 <span class="keyword">static_assert</span><span class="special">(</span> 49 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">is_dynamic_buffer</span><span class="special"><</span><span class="identifier">DynamicBuffer</span><span class="special">>::</span><span class="identifier">value</span><span class="special">,</span> 50 <span class="string">"DynamicBuffer requirements not met"</span><span class="special">);</span> 51 52 <span class="comment">// We deliver this payload for all GET requests</span> 53 <span class="keyword">static</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span> <span class="identifier">payload</span> <span class="special">=</span> <span class="string">"Hello, world!"</span><span class="special">;</span> 54 55 <span class="comment">// Read the request</span> 56 <span class="identifier">request</span><span class="special"><</span><span class="identifier">string_body</span><span class="special">></span> <span class="identifier">req</span><span class="special">;</span> 57 <span class="identifier">read</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">buffer</span><span class="special">,</span> <span class="identifier">req</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span> 58 <span class="keyword">if</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span> 59 <span class="keyword">return</span><span class="special">;</span> 60 61 <span class="comment">// Set up the response, starting with the common fields</span> 62 <span class="identifier">response</span><span class="special"><</span><span class="identifier">string_body</span><span class="special">></span> <span class="identifier">res</span><span class="special">;</span> 63 <span class="identifier">res</span><span class="special">.</span><span class="identifier">version</span><span class="special">(</span><span class="number">11</span><span class="special">);</span> 64 <span class="identifier">res</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">server</span><span class="special">,</span> <span class="string">"test"</span><span class="special">);</span> 65 66 <span class="comment">// Now handle request-specific fields</span> 67 <span class="keyword">switch</span><span class="special">(</span><span class="identifier">req</span><span class="special">.</span><span class="identifier">method</span><span class="special">())</span> 68 <span class="special">{</span> 69 <span class="keyword">case</span> <span class="identifier">verb</span><span class="special">::</span><span class="identifier">head</span><span class="special">:</span> 70 <span class="keyword">case</span> <span class="identifier">verb</span><span class="special">::</span><span class="identifier">get</span><span class="special">:</span> 71 <span class="special">{</span> 72 <span class="comment">// A HEAD request is handled by delivering the same</span> 73 <span class="comment">// set of headers that would be sent for a GET request,</span> 74 <span class="comment">// including the Content-Length, except for the body.</span> 75 <span class="identifier">res</span><span class="special">.</span><span class="identifier">result</span><span class="special">(</span><span class="identifier">status</span><span class="special">::</span><span class="identifier">ok</span><span class="special">);</span> 76 <span class="identifier">res</span><span class="special">.</span><span class="identifier">content_length</span><span class="special">(</span><span class="identifier">payload</span><span class="special">.</span><span class="identifier">size</span><span class="special">());</span> 77 78 <span class="comment">// For GET requests, we include the body</span> 79 <span class="keyword">if</span><span class="special">(</span><span class="identifier">req</span><span class="special">.</span><span class="identifier">method</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">verb</span><span class="special">::</span><span class="identifier">get</span><span class="special">)</span> 80 <span class="special">{</span> 81 <span class="comment">// We deliver the same payload for GET requests</span> 82 <span class="comment">// regardless of the target. A real server might</span> 83 <span class="comment">// deliver a file based on the target.</span> 84 <span class="identifier">res</span><span class="special">.</span><span class="identifier">body</span><span class="special">()</span> <span class="special">=</span> <span class="identifier">payload</span><span class="special">;</span> 85 <span class="special">}</span> 86 <span class="keyword">break</span><span class="special">;</span> 87 <span class="special">}</span> 88 89 <span class="keyword">default</span><span class="special">:</span> 90 <span class="special">{</span> 91 <span class="comment">// We return responses indicating an error if</span> 92 <span class="comment">// we do not recognize the request method.</span> 93 <span class="identifier">res</span><span class="special">.</span><span class="identifier">result</span><span class="special">(</span><span class="identifier">status</span><span class="special">::</span><span class="identifier">bad_request</span><span class="special">);</span> 94 <span class="identifier">res</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">content_type</span><span class="special">,</span> <span class="string">"text/plain"</span><span class="special">);</span> 95 <span class="identifier">res</span><span class="special">.</span><span class="identifier">body</span><span class="special">()</span> <span class="special">=</span> <span class="string">"Invalid request-method '"</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">req</span><span class="special">.</span><span class="identifier">method_string</span><span class="special">())</span> <span class="special">+</span> <span class="string">"'"</span><span class="special">;</span> 96 <span class="identifier">res</span><span class="special">.</span><span class="identifier">prepare_payload</span><span class="special">();</span> 97 <span class="keyword">break</span><span class="special">;</span> 98 <span class="special">}</span> 99 <span class="special">}</span> 100 101 <span class="comment">// Send the response</span> 102 <span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">res</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span> 103 <span class="keyword">if</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span> 104 <span class="keyword">return</span><span class="special">;</span> 105<span class="special">}</span> 106</pre> 107</div> 108<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 109<td align="left"></td> 110<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie 111 Falco<p> 112 Distributed under the Boost Software License, Version 1.0. (See accompanying 113 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>) 114 </p> 115</div></td> 116</tr></table> 117<hr> 118<div class="spirit-nav"> 119<a accesskey="p" href="head_request_client.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../more_examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="http_relay.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 120</div> 121</body> 122</html> 123