📄 _echo_module_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/modules/EchoModule.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/modules/EchoModule.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 "EchoModule.hpp"</span><a name="l00011"></a>00011 <span class="preprocessor">#include <libpion/HTTPResponse.hpp></span><a name="l00012"></a>00012 <span class="preprocessor">#include <boost/bind.hpp></span><a name="l00013"></a>00013 <a name="l00014"></a>00014 <span class="keyword">using namespace </span>pion;<a name="l00015"></a>00015 <a name="l00016"></a>00016 <a name="l00018"></a>00018 <span class="keywordtype">void</span> writeDictionaryTerm(HTTPResponsePtr& response,<a name="l00019"></a>00019 <span class="keyword">const</span> HTTPTypes::StringDictionary::value_type& val,<a name="l00020"></a>00020 <span class="keyword">const</span> <span class="keywordtype">bool</span> decode)<a name="l00021"></a>00021 {<a name="l00022"></a>00022 <span class="comment">// text is copied into response text cache</span><a name="l00023"></a>00023 response << val.first << HTTPTypes::HEADER_NAME_VALUE_DELIMINATOR<a name="l00024"></a>00024 << (decode ? HTTPTypes::url_decode(val.second) : val.second)<a name="l00025"></a>00025 << <a class="code" href="structpion_1_1_h_t_t_p_types.html">HTTPTypes</a>::STRING_CRLF;<a name="l00026"></a>00026 }<a name="l00027"></a>00027 <a name="l00028"></a>00028 <a name="l00029"></a>00029 <span class="comment">// EchoModule member functions</span><a name="l00030"></a>00030 <a name="l00032"></a><a class="code" href="class_echo_module.html#22e70c4559bfa2ac12ed88bbe870885f">00032</a> <span class="keywordtype">bool</span> <a class="code" href="class_echo_module.html#22e70c4559bfa2ac12ed88bbe870885f" title="handles requests for EchoModule">EchoModule::handleRequest</a>(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn)<a name="l00033"></a>00033 {<a name="l00034"></a>00034 <span class="comment">// this modules uses static text to test the mixture of "copied" with</span><a name="l00035"></a>00035 <span class="comment">// "static" (no-copy) text</span><a name="l00036"></a>00036 <span class="keyword">static</span> <span class="keyword">const</span> std::string REQUEST_ECHO_TEXT(<span class="stringliteral">"[Request Echo]"</span>);<a name="l00037"></a>00037 <span class="keyword">static</span> <span class="keyword">const</span> std::string REQUEST_HEADERS_TEXT(<span class="stringliteral">"[Request Headers]"</span>);<a name="l00038"></a>00038 <span class="keyword">static</span> <span class="keyword">const</span> std::string QUERY_PARAMS_TEXT(<span class="stringliteral">"[Query Parameters]"</span>);<a name="l00039"></a>00039 <span class="keyword">static</span> <span class="keyword">const</span> std::string COOKIE_PARAMS_TEXT(<span class="stringliteral">"[Cookie Parameters]"</span>);<a name="l00040"></a>00040 <span class="keyword">static</span> <span class="keyword">const</span> std::string POST_CONTENT_TEXT(<span class="stringliteral">"[POST Content]"</span>);<a name="l00041"></a>00041 <a name="l00042"></a>00042 <span class="comment">// Set Content-type to "text/plain" (plain ascii text)</span><a name="l00043"></a>00043 <a class="code" href="namespacepion.html#e27a744c3e8cf19c3620400bfe68fb69" title="data type for a HTTPResponse pointer">HTTPResponsePtr</a> response(HTTPResponse::create());<a name="l00044"></a>00044 response->setContentType(HTTPTypes::CONTENT_TYPE_TEXT);<a name="l00045"></a>00045 <a name="l00046"></a>00046 <span class="comment">// write request information</span><a name="l00047"></a>00047 response->writeNoCopy(REQUEST_ECHO_TEXT);<a name="l00048"></a>00048 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00049"></a>00049 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00050"></a>00050 response<a name="l00051"></a>00051 << <span class="stringliteral">"Request method: "</span><a name="l00052"></a>00052 << request->getMethod()<a name="l00053"></a>00053 << HTTPTypes::STRING_CRLF<a name="l00054"></a>00054 << <span class="stringliteral">"Resource requested: "</span><a name="l00055"></a>00055 << request->getResource()<a name="l00056"></a>00056 << HTTPTypes::STRING_CRLF<a name="l00057"></a>00057 << <span class="stringliteral">"Query string: "</span><a name="l00058"></a>00058 << request->getQueryString()<a name="l00059"></a>00059 << HTTPTypes::STRING_CRLF<a name="l00060"></a>00060 << <span class="stringliteral">"HTTP version: "</span><a name="l00061"></a>00061 << request->getVersionMajor() << <span class="charliteral">'.'</span> << request->getVersionMinor()<a name="l00062"></a>00062 << HTTPTypes::STRING_CRLF<a name="l00063"></a>00063 << <span class="stringliteral">"Content length: "</span><a name="l00064"></a>00064 << request->getContentLength()<a name="l00065"></a>00065 << HTTPTypes::STRING_CRLF<a name="l00066"></a>00066 << HTTPTypes::STRING_CRLF;<a name="l00067"></a>00067 <a name="l00068"></a>00068 <span class="comment">// write request headers</span><a name="l00069"></a>00069 response->writeNoCopy(REQUEST_HEADERS_TEXT);<a name="l00070"></a>00070 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00071"></a>00071 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00072"></a>00072 std::for_each(request->getHeaders().begin(), request->getHeaders().end(),<a name="l00073"></a>00073 boost::bind(&writeDictionaryTerm, response, _1, <span class="keyword">false</span>));<a name="l00074"></a>00074 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00075"></a>00075 <a name="l00076"></a>00076 <span class="comment">// write query parameters</span><a name="l00077"></a>00077 response->writeNoCopy(QUERY_PARAMS_TEXT);<a name="l00078"></a>00078 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00079"></a>00079 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00080"></a>00080 std::for_each(request->getQueryParams().begin(), request->getQueryParams().end(),<a name="l00081"></a>00081 boost::bind(&writeDictionaryTerm, response, _1, <span class="keyword">true</span>));<a name="l00082"></a>00082 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00083"></a>00083 <a name="l00084"></a>00084 <span class="comment">// write cookie parameters</span><a name="l00085"></a>00085 response->writeNoCopy(COOKIE_PARAMS_TEXT);<a name="l00086"></a>00086 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00087"></a>00087 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00088"></a>00088 std::for_each(request->getCookieParams().begin(), request->getCookieParams().end(),<a name="l00089"></a>00089 boost::bind(&writeDictionaryTerm, response, _1, <span class="keyword">false</span>));<a name="l00090"></a>00090 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00091"></a>00091 <a name="l00092"></a>00092 <span class="comment">// write POST content</span><a name="l00093"></a>00093 response->writeNoCopy(POST_CONTENT_TEXT);<a name="l00094"></a>00094 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00095"></a>00095 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00096"></a>00096 <span class="keywordflow">if</span> (request->getContentLength() != 0) {<a name="l00097"></a>00097 response->write(request->getPostContent(), request->getContentLength());<a name="l00098"></a>00098 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00099"></a>00099 response->writeNoCopy(HTTPTypes::STRING_CRLF);<a name="l00100"></a>00100 }<a name="l00101"></a>00101 <a name="l00102"></a>00102 <span class="comment">// send the response</span><a name="l00103"></a>00103 response->send(tcp_conn);<a name="l00104"></a>00104 <span class="keywordflow">return</span> <span class="keyword">true</span>;<a name="l00105"></a>00105 }<a name="l00106"></a>00106 <a name="l00107"></a>00107 <a name="l00109"></a>00109 <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <a class="code" href="class_echo_module.html">EchoModule</a> *pion_create_EchoModule(<span class="keywordtype">void</span>)<a name="l00110"></a>00110 {<a name="l00111"></a>00111 <span class="keywordflow">return</span> <span class="keyword">new</span> <a class="code" href="class_echo_module.html#0e90f616c1cba1f5939bc9ac6c018542">EchoModule</a>();<a name="l00112"></a>00112 }<a name="l00113"></a>00113 <a name="l00114"></a>00114 <a name="l00116"></a>00116 <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <span class="keywordtype">void</span> pion_destroy_EchoModule(<a class="code" href="class_echo_module.html">EchoModule</a> *module_ptr)<a name="l00117"></a>00117 {<a name="l00118"></a>00118 <span class="keyword">delete</span> module_ptr;<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 + -