📄 _h_t_t_p_response_8cpp-source.html
字号:
<!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/src/HTTPResponse.cpp 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 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/src/HTTPResponse.cpp</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">#include <libpion/HTTPResponse.hpp></span><a name="l00011"></a>00011 <span class="preprocessor">#include <boost/bind.hpp></span><a name="l00012"></a>00012 <a name="l00013"></a>00013 <a name="l00014"></a>00014 <span class="keyword">namespace </span>pion { <span class="comment">// begin namespace pion</span><a name="l00015"></a>00015 <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="comment">// HTTPResponse member functions</span><a name="l00018"></a>00018 <a name="l00019"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#ca27596209b962252fc2afc998de6978">00019</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_response.html#ca27596209b962252fc2afc998de6978">HTTPResponse::send</a>(<a class="code" href="namespacepion.html#1858b89656b28c6498ad9efd6b7e9d41" title="data type for a TCPConnection pointer">TCPConnectionPtr</a>& tcp_conn)<a name="l00020"></a>00020 {<a name="l00021"></a>00021 flushContentStream();<a name="l00022"></a>00022 <a name="l00023"></a>00023 <span class="comment">// update headers</span><a name="l00024"></a>00024 m_response_headers.insert(std::make_pair(<a class="code" href="structpion_1_1_h_t_t_p_types.html#e8e700a28694fa413ef6f504bfa2428b">HTTPTypes::HEADER_CONTENT_TYPE</a>, m_content_type));<a name="l00025"></a>00025 m_response_headers.insert(std::make_pair(<a class="code" href="structpion_1_1_h_t_t_p_types.html#41f4bc64d3ffedbcde771de646f9f920">HTTPTypes::HEADER_CONTENT_LENGTH</a>,<a name="l00026"></a>00026 boost::lexical_cast<std::string>(m_content_length)));<a name="l00027"></a>00027 m_response_headers.insert(std::make_pair(<a class="code" href="structpion_1_1_h_t_t_p_types.html#2da8e7f4b605c4049dfd6ad5a44a98fe">HTTPTypes::HEADER_CONNECTION</a>,<a name="l00028"></a>00028 (tcp_conn->getKeepAlive() ? <span class="stringliteral">"Keep-Alive"</span> : <span class="stringliteral">"close"</span>) ));<a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="comment">// combine I/O write buffers (headers and content) so that everything</span><a name="l00031"></a>00031 <span class="comment">// can be sent together; otherwise, we would have to send headers</span><a name="l00032"></a>00032 <span class="comment">// and content separately, which would not be as efficient</span><a name="l00033"></a>00033 <a name="l00034"></a>00034 WriteBuffers write_buffers;<a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="comment">// first response line (HTTP/1.1 200 OK)</span><a name="l00037"></a>00037 write_buffers.push_back(boost::asio::buffer(<a class="code" href="structpion_1_1_h_t_t_p_types.html#5161972898266539d6e4ff080bb77379">HTTPTypes::STRING_HTTP_VERSION</a>));<a name="l00038"></a>00038 write_buffers.push_back(boost::asio::buffer(m_response_code));<a name="l00039"></a>00039 write_buffers.push_back(boost::asio::buffer(m_response_message));<a name="l00040"></a>00040 write_buffers.push_back(boost::asio::buffer(<a class="code" href="structpion_1_1_h_t_t_p_types.html#b86a44a901e3676ea2b5f79bd4394930">HTTPTypes::STRING_CRLF</a>));<a name="l00041"></a>00041 <a name="l00042"></a>00042 <span class="comment">// HTTP headers</span><a name="l00043"></a>00043 <span class="keywordflow">for</span> (HTTPTypes::StringDictionary::const_iterator i = m_response_headers.begin();<a name="l00044"></a>00044 i != m_response_headers.end(); ++i)<a name="l00045"></a>00045 {<a name="l00046"></a>00046 write_buffers.push_back(boost::asio::buffer(i->first));<a name="l00047"></a>00047 write_buffers.push_back(boost::asio::buffer(<a class="code" href="structpion_1_1_h_t_t_p_types.html#37bb5c821af5772ca88fab7849fe969d">HTTPTypes::HEADER_NAME_VALUE_DELIMINATOR</a>));<a name="l00048"></a>00048 write_buffers.push_back(boost::asio::buffer(i->second));<a name="l00049"></a>00049 write_buffers.push_back(boost::asio::buffer(HTTPTypes::STRING_CRLF));<a name="l00050"></a>00050 }<a name="l00051"></a>00051 <a name="l00052"></a>00052 <span class="comment">// extra CRLF to end HTTP headers</span><a name="l00053"></a>00053 write_buffers.push_back(boost::asio::buffer(HTTPTypes::STRING_CRLF));<a name="l00054"></a>00054 <a name="l00055"></a>00055 <span class="comment">// append response content buffers</span><a name="l00056"></a>00056 write_buffers.insert(write_buffers.end(), m_content_buffers.begin(),<a name="l00057"></a>00057 m_content_buffers.end());<a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="comment">// send response</span><a name="l00060"></a>00060 <span class="keywordflow">if</span> (tcp_conn->getSSLFlag()) {<a name="l00061"></a>00061 #ifdef PION_HAVE_SSL<a name="l00062"></a>00062 boost::asio::async_write(tcp_conn->getSSLSocket(), write_buffers,<a name="l00063"></a>00063 boost::bind(&HTTPResponse::handleWrite, shared_from_this(),<a name="l00064"></a>00064 tcp_conn, boost::asio::placeholders::error,<a name="l00065"></a>00065 boost::asio::placeholders::bytes_transferred));<a name="l00066"></a>00066 <a name="l00067"></a>00067 <span class="preprocessor">#else</span><a name="l00068"></a>00068 <span class="preprocessor"></span> tcp_conn->finish();<a name="l00069"></a>00069 <span class="preprocessor">#endif</span><a name="l00070"></a>00070 <span class="preprocessor"></span> } <span class="keywordflow">else</span> {<a name="l00071"></a>00071 boost::asio::async_write(tcp_conn->getSocket(), write_buffers,<a name="l00072"></a>00072 boost::bind(&HTTPResponse::handleWrite, shared_from_this(),<a name="l00073"></a>00073 tcp_conn, boost::asio::placeholders::error,<a name="l00074"></a>00074 boost::asio::placeholders::bytes_transferred));<a name="l00075"></a>00075 }<a name="l00076"></a>00076 }<a name="l00077"></a>00077 <a name="l00078"></a>00078 <span class="keywordtype">void</span> HTTPResponse::handleWrite(TCPConnectionPtr tcp_conn,<a name="l00079"></a>00079 <span class="keyword">const</span> boost::system::error_code& write_error,<a name="l00080"></a>00080 std::size_t bytes_written)<a name="l00081"></a>00081 {<a name="l00082"></a>00082 <span class="keywordflow">if</span> (write_error) {<a name="l00083"></a>00083 <span class="comment">// encountered error sending response</span><a name="l00084"></a>00084 PION_LOG_INFO(m_logger, <span class="stringliteral">"Unable to send HTTP response due to I/O error"</span>);<a name="l00085"></a>00085 } <span class="keywordflow">else</span> {<a name="l00086"></a>00086 <span class="comment">// response sent OK</span><a name="l00087"></a>00087 PION_LOG_DEBUG(m_logger, <span class="stringliteral">"Sent HTTP response of "</span> << bytes_written << <span class="stringliteral">" bytes ("</span><a name="l00088"></a>00088 << (tcp_conn->getKeepAlive() ? <span class="stringliteral">"keeping alive"</span> : <span class="stringliteral">"closing"</span>) << <span class="stringliteral">")"</span>);<a name="l00089"></a>00089 }<a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="comment">// all finished handling the connection</span><a name="l00092"></a>00092 tcp_conn->finish();<a name="l00093"></a>00093 }<a name="l00094"></a>00094 <a name="l00095"></a><a class="code" href="classpion_1_1_h_t_t_p_response.html#756f5272417267ec27de4fc7cd7ababf">00095</a> std::string <a class="code" href="classpion_1_1_h_t_t_p_response.html#756f5272417267ec27de4fc7cd7ababf">HTTPResponse::makeSetCookieHeader</a>(<span class="keyword">const</span> std::string& name,<a name="l00096"></a>00096 <span class="keyword">const</span> std::string& value,<a name="l00097"></a>00097 <span class="keyword">const</span> std::string& path,<a name="l00098"></a>00098 <span class="keyword">const</span> <span class="keywordtype">bool</span> has_max_age,<a name="l00099"></a>00099 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> max_age)<a name="l00100"></a>00100 {<a name="l00101"></a>00101 std::string set_cookie_header(name);<a name="l00102"></a>00102 set_cookie_header += <span class="stringliteral">"=\""</span>;<a name="l00103"></a>00103 set_cookie_header += value;<a name="l00104"></a>00104 set_cookie_header += <span class="stringliteral">"\"; Version=\"1\""</span>;<a name="l00105"></a>00105 <span class="keywordflow">if</span> (! path.empty()) {<a name="l00106"></a>00106 set_cookie_header += <span class="stringliteral">"; Path=\""</span>;<a name="l00107"></a>00107 set_cookie_header += path;<a name="l00108"></a>00108 set_cookie_header += <span class="charliteral">'\"'</span>;<a name="l00109"></a>00109 }<a name="l00110"></a>00110 <span class="keywordflow">if</span> (has_max_age) {<a name="l00111"></a>00111 set_cookie_header += <span class="stringliteral">"; Max-Age=\""</span>;<a name="l00112"></a>00112 set_cookie_header += boost::lexical_cast<std::string>(max_age);<a name="l00113"></a>00113 set_cookie_header += <span class="charliteral">'\"'</span>;<a name="l00114"></a>00114 }<a name="l00115"></a>00115 <span class="keywordflow">return</span> set_cookie_header;<a name="l00116"></a>00116 }<a name="l00117"></a>00117 <a name="l00118"></a>00118 } <span class="comment">// end namespace pion</span><a name="l00119"></a>00119 </pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Tue Jun 19 13:29:22 2007 for libpion by <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 + -