~ubuntu-branches/debian/sid/pion/sid

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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>pion: include/pion/http/response_writer.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.1 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
    </ul>
  </div>
<h1>include/pion/http/response_writer.hpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// ---------------------------------------------------------------------</span>
<a name="l00002"></a>00002 <span class="comment">// pion:  a Boost C++ framework for building lightweight HTTP interfaces</span>
<a name="l00003"></a>00003 <span class="comment">// ---------------------------------------------------------------------</span>
<a name="l00004"></a>00004 <span class="comment">// Copyright (C) 2007-2012 Cloudmeter, Inc.  (http://www.cloudmeter.com)</span>
<a name="l00005"></a>00005 <span class="comment">//</span>
<a name="l00006"></a>00006 <span class="comment">// Distributed under the Boost Software License, Version 1.0.</span>
<a name="l00007"></a>00007 <span class="comment">// See http://www.boost.org/LICENSE_1_0.txt</span>
<a name="l00008"></a>00008 <span class="comment">//</span>
<a name="l00009"></a>00009 
<a name="l00010"></a>00010 <span class="preprocessor">#ifndef __PION_HTTP_RESPONSE_WRITER_HEADER__</span>
<a name="l00011"></a>00011 <span class="preprocessor"></span><span class="preprocessor">#define __PION_HTTP_RESPONSE_WRITER_HEADER__</span>
<a name="l00012"></a>00012 <span class="preprocessor"></span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &lt;boost/asio.hpp&gt;</span>
<a name="l00014"></a>00014 <span class="preprocessor">#include &lt;boost/bind.hpp&gt;</span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &lt;boost/noncopyable.hpp&gt;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &lt;boost/shared_ptr.hpp&gt;</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include &lt;boost/enable_shared_from_this.hpp&gt;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &lt;pion/config.hpp&gt;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &lt;pion/http/writer.hpp&gt;</span>
<a name="l00020"></a>00020 <span class="preprocessor">#include &lt;pion/http/request.hpp&gt;</span>
<a name="l00021"></a>00021 <span class="preprocessor">#include &lt;pion/http/response.hpp&gt;</span>
<a name="l00022"></a>00022 
<a name="l00023"></a>00023 
<a name="l00024"></a>00024 <span class="keyword">namespace </span>pion {    <span class="comment">// begin namespace pion</span>
<a name="l00025"></a>00025 <span class="keyword">namespace </span>http {    <span class="comment">// begin namespace http</span>
<a name="l00026"></a>00026 
<a name="l00027"></a>00027 
<a name="l00031"></a><a class="code" href="classpion_1_1http_1_1response__writer.html">00031</a> <span class="keyword">class </span>PION_API <a class="code" href="classpion_1_1http_1_1response__writer.html">response_writer</a> :
<a name="l00032"></a>00032     <span class="keyword">public</span> http::<a class="code" href="classpion_1_1http_1_1writer.html">writer</a>,
<a name="l00033"></a>00033     <span class="keyword">public</span> boost::enable_shared_from_this&lt;response_writer&gt;
<a name="l00034"></a>00034 {
<a name="l00035"></a>00035 <span class="keyword">public</span>:
<a name="l00036"></a>00036     
<a name="l00038"></a><a class="code" href="classpion_1_1http_1_1response__writer.html#a5e5e539e05550960f3a2819fe5b13a1a">00038</a>     <span class="keyword">virtual</span> <a class="code" href="classpion_1_1http_1_1response__writer.html#a5e5e539e05550960f3a2819fe5b13a1a" title="default destructor">~response_writer</a>() {}
<a name="l00039"></a>00039 
<a name="l00050"></a><a class="code" href="classpion_1_1http_1_1response__writer.html#a75084960d3162f724342ae947e7d07e3">00050</a>     <span class="keyword">static</span> <span class="keyword">inline</span> boost::shared_ptr&lt;response_writer&gt; create(tcp::connection_ptr&amp; tcp_conn,
<a name="l00051"></a>00051                                                                http::response_ptr&amp; http_response_ptr,
<a name="l00052"></a>00052                                                                finished_handler_t handler = finished_handler_t())
<a name="l00053"></a>00053     {
<a name="l00054"></a>00054         <span class="keywordflow">return</span> boost::shared_ptr&lt;response_writer&gt;(<span class="keyword">new</span> <a class="code" href="classpion_1_1http_1_1response__writer.html">response_writer</a>(tcp_conn, http_response_ptr, handler));
<a name="l00055"></a>00055     }
<a name="l00056"></a>00056 
<a name="l00067"></a><a class="code" href="classpion_1_1http_1_1response__writer.html#a6d4e70e5acce84272f8acdc5833320f4">00067</a>     <span class="keyword">static</span> <span class="keyword">inline</span> boost::shared_ptr&lt;response_writer&gt; create(tcp::connection_ptr&amp; tcp_conn,
<a name="l00068"></a>00068                                                                <span class="keyword">const</span> <a class="code" href="classpion_1_1http_1_1request.html">http::request</a>&amp; http_request,
<a name="l00069"></a>00069                                                                finished_handler_t handler = finished_handler_t())
<a name="l00070"></a>00070     {
<a name="l00071"></a>00071         <span class="keywordflow">return</span> boost::shared_ptr&lt;response_writer&gt;(<span class="keyword">new</span> <a class="code" href="classpion_1_1http_1_1response__writer.html">response_writer</a>(tcp_conn, http_request, handler));
<a name="l00072"></a>00072     }
<a name="l00073"></a>00073     
<a name="l00075"></a><a class="code" href="classpion_1_1http_1_1response__writer.html#a65ab6d2a64ac7c76b8bccf4fcf8fc603">00075</a>     <span class="keyword">inline</span> <a class="code" href="classpion_1_1http_1_1response.html">http::response</a>&amp; <a class="code" href="classpion_1_1http_1_1response__writer.html#a65ab6d2a64ac7c76b8bccf4fcf8fc603" title="returns a non-const reference to the response that will be sent">get_response</a>(<span class="keywordtype">void</span>) { <span class="keywordflow">return</span> *m_http_response; }
<a name="l00076"></a>00076     
<a name="l00077"></a>00077     
<a name="l00078"></a>00078 <span class="keyword">protected</span>:
<a name="l00079"></a>00079     
<a name="l00087"></a><a class="code" href="classpion_1_1http_1_1response__writer.html#a7203e168df87def5ad07cdfa8bae16af">00087</a>     <a class="code" href="classpion_1_1http_1_1response__writer.html">response_writer</a>(tcp::connection_ptr&amp; tcp_conn, http::response_ptr&amp; http_response_ptr,
<a name="l00088"></a>00088                        finished_handler_t handler)
<a name="l00089"></a>00089         : http::<a class="code" href="classpion_1_1http_1_1writer.html">writer</a>(tcp_conn, handler), m_http_response(http_response_ptr)
<a name="l00090"></a>00090     {
<a name="l00091"></a>00091         set_logger(PION_GET_LOGGER(<span class="stringliteral">&quot;pion.http.response_writer&quot;</span>));
<a name="l00092"></a>00092         <span class="comment">// tell the http::writer base class whether or not the client supports chunks</span>
<a name="l00093"></a>00093         supports_chunked_messages(m_http_response-&gt;get_chunks_supported());
<a name="l00094"></a>00094         <span class="comment">// check if we should initialize the payload content using</span>
<a name="l00095"></a>00095         <span class="comment">// the response&apos;s content buffer</span>
<a name="l00096"></a>00096         <span class="keywordflow">if</span> (m_http_response-&gt;get_content_length() &gt; 0
<a name="l00097"></a>00097             &amp;&amp; m_http_response-&gt;get_content() != NULL
<a name="l00098"></a>00098             &amp;&amp; m_http_response-&gt;get_content()[0] != <span class="charliteral">&apos;\0&apos;</span>)
<a name="l00099"></a>00099         {
<a name="l00100"></a>00100             write_no_copy(m_http_response-&gt;get_content(), m_http_response-&gt;get_content_length());
<a name="l00101"></a>00101         }
<a name="l00102"></a>00102     }
<a name="l00103"></a>00103     
<a name="l00111"></a><a class="code" href="classpion_1_1http_1_1response__writer.html#abd234468a489ea3efe1e9280256efddd">00111</a>     <a class="code" href="classpion_1_1http_1_1response__writer.html">response_writer</a>(tcp::connection_ptr&amp; tcp_conn, <span class="keyword">const</span> <a class="code" href="classpion_1_1http_1_1request.html">http::request</a>&amp; http_request,
<a name="l00112"></a>00112                        finished_handler_t handler)
<a name="l00113"></a>00113         : http::<a class="code" href="classpion_1_1http_1_1writer.html">writer</a>(tcp_conn, handler), m_http_response(new http::<a class="code" href="classpion_1_1http_1_1response.html">response</a>(http_request))
<a name="l00114"></a>00114     {
<a name="l00115"></a>00115         set_logger(PION_GET_LOGGER(<span class="stringliteral">&quot;pion.http.response_writer&quot;</span>));
<a name="l00116"></a>00116         <span class="comment">// tell the http::writer base class whether or not the client supports chunks</span>
<a name="l00117"></a>00117         supports_chunked_messages(m_http_response-&gt;get_chunks_supported());
<a name="l00118"></a>00118     }
<a name="l00119"></a>00119     
<a name="l00120"></a>00120     
<a name="l00126"></a><a class="code" href="classpion_1_1http_1_1response__writer.html#a5f18282e90307978ca33be6c4e4f7bf9">00126</a>     <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1http_1_1response__writer.html#a5f18282e90307978ca33be6c4e4f7bf9">prepare_buffers_for_send</a>(<a class="code" href="classpion_1_1http_1_1message.html#aacf9a6a7677c32e4ab764ac97d0b5e7b" title="data type for I/O write buffers (these wrap existing data to be sent)">http::message::write_buffers_t</a>&amp; write_buffers) {
<a name="l00127"></a>00127         <span class="keywordflow">if</span> (get_content_length() &gt; 0)
<a name="l00128"></a>00128             m_http_response-&gt;set_content_length(get_content_length());
<a name="l00129"></a>00129         m_http_response-&gt;prepare_buffers_for_send(write_buffers,
<a name="l00130"></a>00130                                                get_connection()-&gt;get_keep_alive(),
<a name="l00131"></a>00131                                                sending_chunked_message());
<a name="l00132"></a>00132     }   
<a name="l00133"></a>00133 
<a name="l00135"></a><a class="code" href="classpion_1_1http_1_1response__writer.html#a95b3d41b77828f8ec14862c03accb58a">00135</a>     <span class="keyword">virtual</span> write_handler_t <a class="code" href="classpion_1_1http_1_1response__writer.html#a95b3d41b77828f8ec14862c03accb58a" title="returns a function bound to http::writer::handle_write()">bind_to_write_handler</a>(<span class="keywordtype">void</span>) {
<a name="l00136"></a>00136         <span class="keywordflow">return</span> boost::bind(&amp;<a class="code" href="classpion_1_1http_1_1response__writer.html#a216d682fd204bd15e92b2babb55c216a">response_writer::handle_write</a>, shared_from_this(),
<a name="l00137"></a>00137                            boost::asio::placeholders::error,
<a name="l00138"></a>00138                            boost::asio::placeholders::bytes_transferred);
<a name="l00139"></a>00139     }
<a name="l00140"></a>00140 
<a name="l00147"></a><a class="code" href="classpion_1_1http_1_1response__writer.html#a216d682fd204bd15e92b2babb55c216a">00147</a>     <span class="keyword">virtual</span> <span class="keywordtype">void</span> handle_write(<span class="keyword">const</span> boost::system::error_code&amp; write_error,
<a name="l00148"></a>00148                              std::size_t bytes_written)
<a name="l00149"></a>00149     {
<a name="l00150"></a>00150         logger log_ptr(get_logger());
<a name="l00151"></a>00151         <span class="keywordflow">if</span> (!write_error) {
<a name="l00152"></a>00152             <span class="comment">// response sent OK</span>
<a name="l00153"></a>00153             <span class="keywordflow">if</span> (sending_chunked_message()) {
<a name="l00154"></a>00154                 PION_LOG_DEBUG(log_ptr, <span class="stringliteral">&quot;Sent HTTP response chunk of &quot;</span> &lt;&lt; bytes_written &lt;&lt; <span class="stringliteral">&quot; bytes&quot;</span>);
<a name="l00155"></a>00155             } <span class="keywordflow">else</span> {
<a name="l00156"></a>00156                 PION_LOG_DEBUG(log_ptr, <span class="stringliteral">&quot;Sent HTTP response of &quot;</span> &lt;&lt; bytes_written &lt;&lt; <span class="stringliteral">&quot; bytes (&quot;</span>
<a name="l00157"></a>00157                                &lt;&lt; (get_connection()-&gt;get_keep_alive() ? <span class="stringliteral">&quot;keeping alive)&quot;</span> : <span class="stringliteral">&quot;closing)&quot;</span>));
<a name="l00158"></a>00158             }
<a name="l00159"></a>00159         }
<a name="l00160"></a>00160         finished_writing(write_error);
<a name="l00161"></a>00161     }
<a name="l00162"></a>00162 
<a name="l00163"></a>00163     
<a name="l00164"></a>00164 <span class="keyword">private</span>:
<a name="l00165"></a>00165     
<a name="l00167"></a>00167     http::response_ptr      m_http_response;
<a name="l00168"></a>00168     
<a name="l00170"></a>00170     std::string             m_response_line;
<a name="l00171"></a>00171 };
<a name="l00172"></a>00172 
<a name="l00173"></a>00173 
<a name="l00175"></a>00175 <span class="keyword">typedef</span> boost::shared_ptr&lt;response_writer&gt;   response_writer_ptr;
<a name="l00176"></a>00176 
<a name="l00177"></a>00177 
<a name="l00179"></a>00179 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;
<a name="l00180"></a>00180 <span class="keyword">const</span> response_writer_ptr&amp; operator&lt;&lt;(<span class="keyword">const</span> response_writer_ptr&amp; writer, <span class="keyword">const</span> T&amp; data) {
<a name="l00181"></a>00181     writer-&gt;write(data);
<a name="l00182"></a>00182     <span class="keywordflow">return</span> writer;
<a name="l00183"></a>00183 }
<a name="l00184"></a>00184 
<a name="l00185"></a>00185 <span class="keyword">inline</span> response_writer_ptr&amp; operator&lt;&lt;(response_writer_ptr&amp; writer, std::ostream&amp; (*iomanip)(std::ostream&amp;)) {
<a name="l00186"></a>00186     writer-&gt;write(iomanip);
<a name="l00187"></a>00187     <span class="keywordflow">return</span> writer;
<a name="l00188"></a>00188 }
<a name="l00189"></a>00189 
<a name="l00190"></a>00190 }   <span class="comment">// end namespace http</span>
<a name="l00191"></a>00191 }   <span class="comment">// end namespace pion</span>
<a name="l00192"></a>00192 
<a name="l00193"></a>00193 <span class="preprocessor">#endif</span>
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated on 16 Sep 2013 for pion by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>
</body>
</html>