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

📄 _h_t_t_p_response_8hpp-source.html

📁 用c++编写http server的源码库,对socket等网络处理的代码可迅速转为己用.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>libpion: /Users/mdickey/code/libpion-export/include/libpion/HTTPResponse.hpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><div class="tabs">  <ul>    <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>    <li><a href="namespaces.html"><span>Namespaces</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><h1>/Users/mdickey/code/libpion-export/include/libpion/HTTPResponse.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">// libpion: a 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 Atomic Labs, Inc.  (http://www.atomiclabs.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 accompanying file COPYING or copy at 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_HTTPRESPONSE_HEADER__</span><a name="l00011"></a>00011 <span class="preprocessor"></span><span class="preprocessor">#define __PION_HTTPRESPONSE_HEADER__</span><a name="l00012"></a>00012 <span class="preprocessor"></span><a name="l00013"></a>00013 <span class="preprocessor">#include &lt;libpion/PionConfig.hpp&gt;</span><a name="l00014"></a>00014 <span class="preprocessor">#include &lt;libpion/PionLogger.hpp&gt;</span><a name="l00015"></a>00015 <span class="preprocessor">#include &lt;libpion/TCPConnection.hpp&gt;</span><a name="l00016"></a>00016 <span class="preprocessor">#include &lt;libpion/HTTPTypes.hpp&gt;</span><a name="l00017"></a>00017 <span class="preprocessor">#include &lt;boost/asio.hpp&gt;</span><a name="l00018"></a>00018 <span class="preprocessor">#include &lt;boost/asio/buffer.hpp&gt;</span><a name="l00019"></a>00019 <span class="preprocessor">#include &lt;boost/noncopyable.hpp&gt;</span><a name="l00020"></a>00020 <span class="preprocessor">#include &lt;boost/enable_shared_from_this.hpp&gt;</span><a name="l00021"></a>00021 <span class="preprocessor">#include &lt;boost/shared_ptr.hpp&gt;</span><a name="l00022"></a>00022 <span class="preprocessor">#include &lt;boost/lexical_cast.hpp&gt;</span><a name="l00023"></a>00023 <span class="preprocessor">#include &lt;vector&gt;</span><a name="l00024"></a>00024 <span class="preprocessor">#include &lt;string&gt;</span><a name="l00025"></a>00025 <a name="l00026"></a>00026 <a name="l00027"></a>00027 <span class="keyword">namespace </span>pion {    <span class="comment">// begin namespace pion</span><a name="l00028"></a>00028 <a name="l00032"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html">00032</a> <span class="keyword">class </span><a class="code" href="classpion_1_1_h_t_t_p_response.html">HTTPResponse</a> :<a name="l00033"></a>00033     <span class="keyword">public</span> boost::enable_shared_from_this&lt;HTTPResponse&gt;,<a name="l00034"></a>00034     <span class="keyword">private</span> boost::noncopyable<a name="l00035"></a>00035 {<a name="l00036"></a>00036     <a name="l00037"></a>00037 <span class="keyword">public</span>:<a name="l00038"></a>00038 <a name="l00040"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#bebd43da5cc60c5ee4245066c58fe244">00040</a>     <span class="keyword">static</span> <span class="keyword">inline</span> boost::shared_ptr&lt;HTTPResponse&gt; <a class="code" href="classpion_1_1_h_t_t_p_response.html#bebd43da5cc60c5ee4245066c58fe244" title="creates new HTTPResponse objects">create</a>(<span class="keywordtype">void</span>) {<a name="l00041"></a>00041         <span class="keywordflow">return</span> boost::shared_ptr&lt;HTTPResponse&gt;(<span class="keyword">new</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html">HTTPResponse</a>);<a name="l00042"></a>00042     }<a name="l00043"></a>00043     <a name="l00045"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#99b714d17753868a80474d3472522f61">00045</a>     <span class="keyword">virtual</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#99b714d17753868a80474d3472522f61" title="default destructor">~HTTPResponse</a>() {}<a name="l00046"></a>00046 <a name="l00052"></a>00052     <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;<a name="l00053"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#e8939b9db481fc1f14c359965c933a23">00053</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#e8939b9db481fc1f14c359965c933a23">write</a>(<span class="keyword">const</span> T&amp; data) {<a name="l00054"></a>00054         m_content_stream &lt;&lt; data;<a name="l00055"></a>00055         <span class="keywordflow">if</span> (m_stream_is_empty) m_stream_is_empty = <span class="keyword">false</span>;<a name="l00056"></a>00056     }<a name="l00057"></a>00057     <a name="l00064"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#dd72716c96b9ff539675e545552d6a9b">00064</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#e8939b9db481fc1f14c359965c933a23">write</a>(<span class="keyword">const</span> <span class="keywordtype">void</span> *data, <span class="keywordtype">size_t</span> length) {<a name="l00065"></a>00065         flushContentStream();<a name="l00066"></a>00066         m_content_buffers.push_back(m_binary_cache.add(data, length));<a name="l00067"></a>00067         m_content_length += length;<a name="l00068"></a>00068     }<a name="l00069"></a>00069     <a name="l00076"></a>00076     <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;<a name="l00077"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#1738cab55e8fd24a3a145f1f07d8f13a">00077</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#1738cab55e8fd24a3a145f1f07d8f13a">writeNoCopy</a>(<span class="keyword">const</span> T&amp; data) {<a name="l00078"></a>00078         flushContentStream();<a name="l00079"></a>00079         std::string as_string(boost::lexical_cast&lt;std::string&gt;(data));<a name="l00080"></a>00080         m_content_buffers.push_back(boost::asio::buffer(as_string));<a name="l00081"></a>00081         m_content_length += as_string.size();<a name="l00082"></a>00082     }<a name="l00083"></a>00083 <a name="l00091"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#990278957e0b9201cbce0f2e1ea8865c">00091</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#1738cab55e8fd24a3a145f1f07d8f13a">writeNoCopy</a>(<span class="keyword">const</span> std::string&amp; data) {<a name="l00092"></a>00092         flushContentStream();<a name="l00093"></a>00093         m_content_buffers.push_back(boost::asio::buffer(data));<a name="l00094"></a>00094         m_content_length += data.size();<a name="l00095"></a>00095     }<a name="l00096"></a>00096     <a name="l00104"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#f21b12e9708e6762c7b22d2ea48216d9">00104</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#1738cab55e8fd24a3a145f1f07d8f13a">writeNoCopy</a>(<span class="keywordtype">void</span> *data, <span class="keywordtype">size_t</span> length) {<a name="l00105"></a>00105         flushContentStream();<a name="l00106"></a>00106         m_content_buffers.push_back(boost::asio::buffer(data, length));<a name="l00107"></a>00107         m_content_length += length;<a name="l00108"></a>00108     }<a name="l00109"></a>00109 <a name="l00111"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#10a7ef4e77a4e7903a50657f78142777">00111</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#10a7ef4e77a4e7903a50657f78142777" title="adds an HTTP response header">addHeader</a>(<span class="keyword">const</span> std::string&amp; key, <span class="keyword">const</span> std::string&amp; value) {<a name="l00112"></a>00112         m_response_headers.insert(std::make_pair(key, value));<a name="l00113"></a>00113     }<a name="l00114"></a>00114 <a name="l00122"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#01e450de281fb46e722431c5033cdffd">00122</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#01e450de281fb46e722431c5033cdffd">setCookie</a>(<span class="keyword">const</span> std::string&amp; name, <span class="keyword">const</span> std::string&amp; value) {<a name="l00123"></a>00123         std::string set_cookie_header(<a class="code" href="classpion_1_1_h_t_t_p_response.html#756f5272417267ec27de4fc7cd7ababf">makeSetCookieHeader</a>(name, value, <span class="stringliteral">""</span>));<a name="l00124"></a>00124         <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="l00125"></a>00125     }<a name="l00126"></a>00126     <a name="l00135"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#ebf871a686e33012dc9b465c110b23b8">00135</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#01e450de281fb46e722431c5033cdffd">setCookie</a>(<span class="keyword">const</span> std::string&amp; name, <span class="keyword">const</span> std::string&amp; value,<a name="l00136"></a>00136                           <span class="keyword">const</span> std::string&amp; path)<a name="l00137"></a>00137     {<a name="l00138"></a>00138         std::string set_cookie_header(<a class="code" href="classpion_1_1_h_t_t_p_response.html#756f5272417267ec27de4fc7cd7ababf">makeSetCookieHeader</a>(name, value, path));<a name="l00139"></a>00139         <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="l00140"></a>00140     }<a name="l00141"></a>00141     <a name="l00150"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#9d5d2abe9d8a07ed77671825693a446e">00150</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#01e450de281fb46e722431c5033cdffd">setCookie</a>(<span class="keyword">const</span> std::string&amp; name, <span class="keyword">const</span> std::string&amp; value,<a name="l00151"></a>00151                           <span class="keyword">const</span> std::string&amp; path, <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> max_age)<a name="l00152"></a>00152     {<a name="l00153"></a>00153         std::string set_cookie_header(<a class="code" href="classpion_1_1_h_t_t_p_response.html#756f5272417267ec27de4fc7cd7ababf">makeSetCookieHeader</a>(name, value, path, <span class="keyword">true</span>, max_age));<a name="l00154"></a>00154         <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="l00155"></a>00155     }<a name="l00156"></a>00156 <a name="l00164"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#4a4e5eb443e03d4bb2cbb9d69232ea5d">00164</a>     <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#01e450de281fb46e722431c5033cdffd">setCookie</a>(<span class="keyword">const</span> std::string&amp; name, <span class="keyword">const</span> std::string&amp; value,<a name="l00165"></a>00165                           <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> max_age)<a name="l00166"></a>00166     {<a name="l00167"></a>00167         std::string set_cookie_header(<a class="code" href="classpion_1_1_h_t_t_p_response.html#756f5272417267ec27de4fc7cd7ababf">makeSetCookieHeader</a>(name, value, <span class="stringliteral">""</span>, <span class="keyword">true</span>, max_age));<a name="l00168"></a>00168         <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="l00169"></a>00169     }<a name="l00170"></a>00170     <a name="l00172"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#92a113a21a6b8dae87653c4d971c279e">00172</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) {<a name="l00173"></a>00173         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>, <span class="stringliteral">""</span>, <span class="keyword">true</span>, 0));<a name="l00174"></a>00174         <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="l00175"></a>00175     }<a name="l00176"></a>00176     

⌨️ 快捷键说明

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