⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _h_t_t_p_response_8hpp-source.html

📁 用c++编写http server的源码库,对socket等网络处理的代码可迅速转为己用.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<a name="l00178"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#c2038ae5462f5159f1e28a3696890aa9">00178</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#92a113a21a6b8dae87653c4d971c279e" title="deletes cookie called name by adding a Set-Cookie header (cookie has no path)">deleteCookie</a>(<span class="keyword">const</span> std::string&amp; name, <span class="keyword">const</span> std::string&amp; path) {<a name="l00179"></a>00179         std::string set_cookie_header(<a class="code" href="classpion_1_1_h_t_t_p_response.html#756f5272417267ec27de4fc7cd7ababf">makeSetCookieHeader</a>(name, <span class="stringliteral">""</span>, path, <span class="keyword">true</span>, 0));<a name="l00180"></a>00180         <a class="code" href="classpion_1_1_h_t_t_p_response.html#10a7ef4e77a4e7903a50657f78142777" title="adds an HTTP response header">addHeader</a>(<a class="code" href="structpion_1_1_h_t_t_p_types.html#a976265652696929195de67fa1427ed0">HTTPTypes::HEADER_SET_COOKIE</a>, set_cookie_header);<a name="l00181"></a>00181     }<a name="l00182"></a>00182 <a name="l00184"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#0fccb1e11ae8811c4ec4e81eb4e3f57f">00184</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#0fccb1e11ae8811c4ec4e81eb4e3f57f" title="sets the response or status code to send">setResponseCode</a>(<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n) {<a name="l00185"></a>00185         <span class="comment">// add space character to beginning and end</span><a name="l00186"></a>00186         m_response_code = <span class="charliteral">' '</span>;<a name="l00187"></a>00187         m_response_code += boost::lexical_cast&lt;std::string&gt;(n);<a name="l00188"></a>00188         m_response_code += <span class="charliteral">' '</span>;<a name="l00189"></a>00189     }<a name="l00190"></a>00190     <a name="l00192"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#1604834dc004a4a11a6c667ccbedabcf">00192</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#1604834dc004a4a11a6c667ccbedabcf" title="sets the time that the response was last modified (Last-Modified)">setLastModified</a>(<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> t) {<a name="l00193"></a>00193         <a class="code" href="classpion_1_1_h_t_t_p_response.html#10a7ef4e77a4e7903a50657f78142777" title="adds an HTTP response header">addHeader</a>(<a class="code" href="structpion_1_1_h_t_t_p_types.html#e3bd76fdbaae0dddba361fd3c5e6d5e1">HTTPTypes::HEADER_LAST_MODIFIED</a>, <a class="code" href="structpion_1_1_h_t_t_p_types.html#199c55b8c43a2a16a2963e0ac5f51874" title="converts time_t format into an HTTP-date string">HTTPTypes::get_date_string</a>(t));<a name="l00194"></a>00194     }<a name="l00195"></a>00195     <a name="l00197"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#edbab12b67358304a494bb50bd048d23">00197</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#edbab12b67358304a494bb50bd048d23" title="sets the response or status message to send">setResponseMessage</a>(<span class="keyword">const</span> std::string&amp; m) { m_response_message = m; }<a name="l00198"></a>00198 <a name="l00200"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#b706516101fb22bd547517d6cfd3b790">00200</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#b706516101fb22bd547517d6cfd3b790" title="sets the type of response content to be sent (Content-Type)">setContentType</a>(<span class="keyword">const</span> std::string&amp; t) { m_content_type = t; }<a name="l00201"></a>00201 <a name="l00203"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#036efba6a21aa579e60778f91046a56b">00203</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#036efba6a21aa579e60778f91046a56b" title="sets the logger to be used">setLogger</a>(<a class="code" href="namespacepion.html#917bc483e4f692504156efb93c11b322">PionLogger</a> log_ptr) { m_logger = log_ptr; }<a name="l00204"></a>00204     <a name="l00206"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#40dec2e9ff7a8829babfcd1051a811e6">00206</a>     <span class="keyword">inline</span> <a class="code" href="namespacepion.html#917bc483e4f692504156efb93c11b322">PionLogger</a> <a class="code" href="classpion_1_1_h_t_t_p_response.html#40dec2e9ff7a8829babfcd1051a811e6" title="returns the logger currently in use">getLogger</a>(<span class="keywordtype">void</span>) { <span class="keywordflow">return</span> m_logger; }<a name="l00207"></a>00207 <a name="l00208"></a>00208     <a name="l00214"></a>00214     <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#ca27596209b962252fc2afc998de6978">send</a>(<a class="code" href="namespacepion.html#1858b89656b28c6498ad9efd6b7e9d41" title="data type for a TCPConnection pointer">TCPConnectionPtr</a>&amp; tcp_conn);<a name="l00215"></a>00215 <a name="l00216"></a>00216     <a name="l00217"></a>00217 <span class="keyword">protected</span>:<a name="l00218"></a>00218         <a name="l00220"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#6cb282b8f63af8996331cf5a8a534c49">00220</a>     <a class="code" href="classpion_1_1_h_t_t_p_response.html#6cb282b8f63af8996331cf5a8a534c49" title="protected constructor restricts creation of objects (use create())">HTTPResponse</a>(<span class="keywordtype">void</span>)<a name="l00221"></a>00221         : m_logger(PION_GET_LOGGER(<span class="stringliteral">"Pion.HTTPResponse"</span>)),<a name="l00222"></a>00222         m_stream_is_empty(true), <a name="l00223"></a>00223         m_response_message(<a class="code" href="structpion_1_1_h_t_t_p_types.html">HTTPTypes</a>::RESPONSE_MESSAGE_OK),<a name="l00224"></a>00224         m_content_type(<a class="code" href="structpion_1_1_h_t_t_p_types.html">HTTPTypes</a>::CONTENT_TYPE_HTML),<a name="l00225"></a>00225         m_content_length(0)<a name="l00226"></a>00226     {<a name="l00227"></a>00227             <a class="code" href="classpion_1_1_h_t_t_p_response.html#0fccb1e11ae8811c4ec4e81eb4e3f57f" title="sets the response or status code to send">setResponseCode</a>(<a class="code" href="structpion_1_1_h_t_t_p_types.html#6fcaa066cae14d0e6e88c087d7b31b79">HTTPTypes::RESPONSE_CODE_OK</a>);<a name="l00228"></a>00228     }<a name="l00229"></a>00229 <a name="l00241"></a>00241      std::string <a class="code" href="classpion_1_1_h_t_t_p_response.html#756f5272417267ec27de4fc7cd7ababf">makeSetCookieHeader</a>(<span class="keyword">const</span> std::string&amp; name, <span class="keyword">const</span> std::string&amp; value,<a name="l00242"></a>00242                                      <span class="keyword">const</span> std::string&amp; path, <span class="keyword">const</span> <span class="keywordtype">bool</span> has_max_age = <span class="keyword">false</span>,<a name="l00243"></a>00243                                      <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> max_age = 0);<a name="l00244"></a>00244     <a name="l00245"></a>00245 <span class="keyword">private</span>:<a name="l00246"></a>00246     <a name="l00254"></a>00254     <span class="keywordtype">void</span> handleWrite(<a class="code" href="namespacepion.html#1858b89656b28c6498ad9efd6b7e9d41" title="data type for a TCPConnection pointer">TCPConnectionPtr</a> tcp_conn,<a name="l00255"></a>00255                      <span class="keyword">const</span> boost::system::error_code&amp; write_error,<a name="l00256"></a>00256                      std::size_t bytes_written);<a name="l00257"></a>00257 <a name="l00259"></a>00259     <span class="keyword">inline</span> <span class="keywordtype">void</span> flushContentStream(<span class="keywordtype">void</span>) {<a name="l00260"></a>00260         <span class="keywordflow">if</span> (! m_stream_is_empty) {<a name="l00261"></a>00261             m_text_cache.push_back(m_content_stream.str());<a name="l00262"></a>00262             m_content_buffers.push_back(boost::asio::buffer(m_text_cache.back()));<a name="l00263"></a>00263             m_content_length += m_text_cache.back().size();<a name="l00264"></a>00264             m_content_stream.str(<span class="stringliteral">""</span>);<a name="l00265"></a>00265             m_stream_is_empty = <span class="keyword">true</span>;<a name="l00266"></a>00266         }<a name="l00267"></a>00267     }<a name="l00268"></a>00268     <a name="l00270"></a>00270     <span class="keyword">class </span>BinaryCache : <span class="keyword">public</span> std::vector&lt;std::pair&lt;const char *, size_t&gt; &gt; {<a name="l00271"></a>00271     <span class="keyword">public</span>:<a name="l00272"></a>00272         ~BinaryCache() {<a name="l00273"></a>00273             <span class="keywordflow">for</span> (iterator i=begin(); i!=end(); ++i) {<a name="l00274"></a>00274                 <span class="keyword">delete</span>[] i-&gt;first;<a name="l00275"></a>00275             }<a name="l00276"></a>00276         }<a name="l00277"></a>00277         <span class="keyword">inline</span> boost::asio::const_buffer <span class="keyword">add</span>(<span class="keyword">const</span> <span class="keywordtype">void</span> *ptr, <span class="keyword">const</span> <span class="keywordtype">size_t</span> size) {<a name="l00278"></a>00278             <span class="keywordtype">char</span> *data_ptr = <span class="keyword">new</span> <span class="keywordtype">char</span>[size];<a name="l00279"></a>00279             memcpy(data_ptr, ptr, size);<a name="l00280"></a>00280             push_back( std::make_pair(data_ptr, size) );<a name="l00281"></a>00281             <span class="keywordflow">return</span> boost::asio::buffer(data_ptr, size);<a name="l00282"></a>00282         }<a name="l00283"></a>00283     };<a name="l00284"></a>00284     <a name="l00286"></a>00286     <span class="keyword">typedef</span> std::vector&lt;std::string&gt;                TextCache;<a name="l00287"></a>00287 <a name="l00289"></a>00289     <span class="keyword">typedef</span> std::vector&lt;boost::asio::const_buffer&gt;  WriteBuffers;<a name="l00290"></a>00290     <a name="l00291"></a>00291     <a name="l00293"></a>00293     <a class="code" href="namespacepion.html#917bc483e4f692504156efb93c11b322">PionLogger</a>                              m_logger;<a name="l00294"></a>00294 <a name="l00296"></a>00296     WriteBuffers                            m_content_buffers;<a name="l00297"></a>00297     <a name="l00299"></a>00299     BinaryCache                             m_binary_cache;<a name="l00300"></a>00300 <a name="l00302"></a>00302     TextCache                               m_text_cache;<a name="l00303"></a>00303     <a name="l00305"></a>00305     std::ostringstream                      m_content_stream;<a name="l00306"></a>00306     <a name="l00308"></a>00308     <span class="keywordtype">bool</span>                                    m_stream_is_empty;<a name="l00309"></a>00309     <a name="l00311"></a>00311     <a class="code" href="structpion_1_1_h_t_t_p_types.html#c860abe7b994d3c0311318f2e335086e" title="data type for a dictionary of strings (used for HTTP headers)">HTTPTypes::StringDictionary</a>             m_response_headers;<a name="l00312"></a>00312     <a name="l00314"></a>00314     std::string                             m_response_code;<a name="l00315"></a>00315 <a name="l00317"></a>00317     std::string                             m_response_message;<a name="l00318"></a>00318     <a name="l00320"></a>00320     std::string                             m_content_type;<a name="l00321"></a>00321     <a name="l00323"></a>00323     <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>                           m_content_length;<a name="l00324"></a>00324 };<a name="l00325"></a>00325 <a name="l00326"></a>00326 <a name="l00328"></a><a class="code" href="namespacepion.html#e27a744c3e8cf19c3620400bfe68fb69">00328</a> <span class="keyword">typedef</span> boost::shared_ptr&lt;HTTPResponse&gt;     <a class="code" href="namespacepion.html#e27a744c3e8cf19c3620400bfe68fb69" title="data type for a HTTPResponse pointer">HTTPResponsePtr</a>;<a name="l00329"></a>00329 <a name="l00330"></a>00330 <a name="l00332"></a>00332 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;<a name="l00333"></a><a class="code" href="namespacepion.html#98660c3a06a18335d0ba3bd45abdf6a2">00333</a> <a class="code" href="namespacepion.html#e27a744c3e8cf19c3620400bfe68fb69" title="data type for a HTTPResponse pointer">HTTPResponsePtr</a>&amp; <a class="code" href="namespacepion.html#98660c3a06a18335d0ba3bd45abdf6a2" title="override operator<< for convenience">operator&lt;&lt;</a>(<a class="code" href="namespacepion.html#e27a744c3e8cf19c3620400bfe68fb69" title="data type for a HTTPResponse pointer">HTTPResponsePtr</a>&amp; response, <span class="keyword">const</span> T&amp; data) {<a name="l00334"></a>00334     response-&gt;write(data);<a name="l00335"></a>00335     <span class="keywordflow">return</span> response;<a name="l00336"></a>00336 }<a name="l00337"></a>00337 <a name="l00338"></a>00338 <a name="l00339"></a>00339 }   <span class="comment">// end namespace pion</span><a name="l00340"></a>00340 <a name="l00341"></a>00341 <span class="preprocessor">#endif</span></pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Tue Jun 19 13:29:22 2007 for libpion by&nbsp;<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.2 </small></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -