📄 _h_t_t_p_server_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/HTTPServer.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/HTTPServer.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/HTTPServer.hpp></span><a name="l00011"></a>00011 <span class="preprocessor">#include <libpion/HTTPRequest.hpp></span><a name="l00012"></a>00012 <span class="preprocessor">#include <libpion/HTTPResponse.hpp></span><a name="l00013"></a>00013 <span class="preprocessor">#include <libpion/HTTPRequestParser.hpp></span><a name="l00014"></a>00014 <span class="preprocessor">#include <boost/bind.hpp></span><a name="l00015"></a>00015 <span class="preprocessor">#include <boost/asio.hpp></span><a name="l00016"></a>00016 <span class="preprocessor">#include <fstream></span><a name="l00017"></a>00017 <a name="l00018"></a>00018 <a name="l00019"></a>00019 <span class="keyword">namespace </span>pion { <span class="comment">// begin namespace pion</span><a name="l00020"></a>00020 <a name="l00021"></a>00021 <span class="comment">// HTTPServer member functions</span><a name="l00022"></a>00022 <a name="l00023"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#8d56ea8d41de7e5cfb3ce17addbd0a8b">00023</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#8d56ea8d41de7e5cfb3ce17addbd0a8b">HTTPServer::handleConnection</a>(<a class="code" href="namespacepion.html#1858b89656b28c6498ad9efd6b7e9d41" title="data type for a TCPConnection pointer">TCPConnectionPtr</a>& tcp_conn)<a name="l00024"></a>00024 {<a name="l00025"></a>00025 tcp_conn->setKeepAlive(<span class="keyword">false</span>); <span class="comment">// default to closing the connection</span><a name="l00026"></a>00026 <a class="code" href="namespacepion.html#424e38a815cd04fe411a77175665f030" title="data type for a HTTPRequestParser pointer">HTTPRequestParserPtr</a> request_parser(<a class="code" href="classpion_1_1_h_t_t_p_request_parser.html#51ffa6adfe35890ee0fad74a662be30d">HTTPRequestParser::create</a>(boost::bind(&<a class="code" href="classpion_1_1_h_t_t_p_server.html#ce510154e117873d528e04bbf6f30d5b">HTTPServer::handleRequest</a>,<a name="l00027"></a>00027 <span class="keyword">this</span>, _1, _2), tcp_conn));<a name="l00028"></a>00028 request_parser->readRequest();<a name="l00029"></a>00029 }<a name="l00030"></a>00030 <a name="l00031"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#ce510154e117873d528e04bbf6f30d5b">00031</a> <span class="keywordtype">void</span> HTTPServer::handleRequest(HTTPRequestPtr& http_request,<a name="l00032"></a>00032 TCPConnectionPtr& tcp_conn)<a name="l00033"></a>00033 {<a name="l00034"></a>00034 <span class="keywordflow">if</span> (! http_request->isValid()) {<a name="l00035"></a>00035 <span class="comment">// the request is invalid or an error occured</span><a name="l00036"></a>00036 PION_LOG_INFO(<a class="code" href="classpion_1_1_t_c_p_server.html#028f2f5c2af69741b003b438ce682029" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Received an invalid HTTP request"</span>);<a name="l00037"></a>00037 m_bad_request_handler(http_request, tcp_conn);<a name="l00038"></a>00038 <span class="keywordflow">return</span>;<a name="l00039"></a>00039 }<a name="l00040"></a>00040 <a name="l00041"></a>00041 PION_LOG_DEBUG(<a class="code" href="classpion_1_1_t_c_p_server.html#028f2f5c2af69741b003b438ce682029" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Received a valid HTTP request"</span>);<a name="l00042"></a>00042 <a name="l00043"></a>00043 <span class="comment">// set the connection's keep_alive flag</span><a name="l00044"></a>00044 tcp_conn->setKeepAlive(http_request->checkKeepAlive());<a name="l00045"></a>00045 <a name="l00046"></a>00046 <span class="comment">// strip off trailing slash if the request has one</span><a name="l00047"></a>00047 std::string resource(http_request->getResource());<a name="l00048"></a>00048 <span class="keywordflow">if</span> (! resource.empty() && resource[resource.size() - 1] == <span class="charliteral">'/'</span>)<a name="l00049"></a>00049 resource.resize( resource.size() - 1 );<a name="l00050"></a>00050 <a name="l00051"></a>00051 <span class="comment">// true if a module successfully handled the request</span><a name="l00052"></a>00052 <span class="keywordtype">bool</span> request_was_handled = <span class="keyword">false</span>;<a name="l00053"></a>00053 <a name="l00054"></a>00054 <span class="comment">// lock mutex for thread safety (this should probably use ref counters)</span><a name="l00055"></a>00055 boost::mutex::scoped_lock modules_lock(m_mutex);<a name="l00056"></a>00056 <a name="l00057"></a>00057 <span class="keywordflow">if</span> (m_modules.empty()) {<a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="comment">// no modules configured</span><a name="l00060"></a>00060 PION_LOG_WARN(<a class="code" href="classpion_1_1_t_c_p_server.html#028f2f5c2af69741b003b438ce682029" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"No modules configured"</span>);<a name="l00061"></a>00061 <a name="l00062"></a>00062 } <span class="keywordflow">else</span> {<a name="l00063"></a>00063 <a name="l00064"></a>00064 <span class="comment">// iterate through each module that may be able to handle the request</span><a name="l00065"></a>00065 ModuleMap::iterator i = m_modules.upper_bound(resource);<a name="l00066"></a>00066 <span class="keywordflow">while</span> (i != m_modules.begin()) {<a name="l00067"></a>00067 --i;<a name="l00068"></a>00068 <a name="l00069"></a>00069 <span class="comment">// keep checking while the first part of the strings match</span><a name="l00070"></a>00070 <span class="keywordflow">if</span> (resource.compare(0, i->first.size(), i->first) != 0) {<a name="l00071"></a>00071 <span class="comment">// we've gone to far; the first part no longer matches</span><a name="l00072"></a>00072 <span class="keywordflow">break</span>;<a name="l00073"></a>00073 }<a name="l00074"></a>00074 <a name="l00075"></a>00075 <span class="comment">// only try the module if the request matches the module name or</span><a name="l00076"></a>00076 <span class="comment">// if module name is followed first with a '/' character</span><a name="l00077"></a>00077 <span class="keywordflow">if</span> (resource.size() == i->first.size() || resource[i->first.size()]==<span class="charliteral">'/'</span>) {<a name="l00078"></a>00078 <a name="l00079"></a>00079 <span class="comment">// try to handle the request with the module</span><a name="l00080"></a>00080 <span class="keywordflow">try</span> {<a name="l00081"></a>00081 request_was_handled = i->second.first->handleRequest(http_request, tcp_conn);<a name="l00082"></a>00082 } <span class="keywordflow">catch</span> (std::bad_alloc&) {<a name="l00083"></a>00083 <span class="comment">// propagate memory errors (FATAL)</span><a name="l00084"></a>00084 <span class="keywordflow">throw</span>;<a name="l00085"></a>00085 } <span class="keywordflow">catch</span> (std::exception& e) {<a name="l00086"></a>00086 <span class="comment">// recover gracefully from other exceptions thrown by modules</span><a name="l00087"></a>00087 PION_LOG_ERROR(<a class="code" href="classpion_1_1_t_c_p_server.html#028f2f5c2af69741b003b438ce682029" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"HTTP module ("</span> << resource << <span class="stringliteral">") exception: "</span> << e.what());<a name="l00088"></a>00088 m_server_error_handler(http_request, tcp_conn, e.what());<a name="l00089"></a>00089 request_was_handled = <span class="keyword">true</span>;<a name="l00090"></a>00090 <span class="keywordflow">break</span>;<a name="l00091"></a>00091 }<a name="l00092"></a>00092 <a name="l00093"></a>00093 <span class="keywordflow">if</span> (request_was_handled) {<a name="l00094"></a>00094 <span class="comment">// the module successfully handled the request</span><a name="l00095"></a>00095 PION_LOG_DEBUG(<a class="code" href="classpion_1_1_t_c_p_server.html#028f2f5c2af69741b003b438ce682029" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"HTTP request handled by module ("</span><a name="l00096"></a>00096 << i->first << <span class="stringliteral">"): "</span><a name="l00097"></a>00097 << http_request->getResource());<a name="l00098"></a>00098 <span class="keywordflow">break</span>;<a name="l00099"></a>00099 }<a name="l00100"></a>00100 }<a name="l00101"></a>00101 }<a name="l00102"></a>00102 }<a name="l00103"></a>00103 <a name="l00104"></a>00104 <span class="keywordflow">if</span> (! request_was_handled) {<a name="l00105"></a>00105 <span class="comment">// no modules found that could handle the request</span><a name="l00106"></a>00106 PION_LOG_INFO(<a class="code" href="classpion_1_1_t_c_p_server.html#028f2f5c2af69741b003b438ce682029" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"No modules found to handle HTTP request: "</span> << resource);<a name="l00107"></a>00107 m_not_found_handler(http_request, tcp_conn);<a name="l00108"></a>00108 }<a name="l00109"></a>00109 }<a name="l00110"></a>00110 <a name="l00111"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#85ab5540af5eadd957e102b4d848f095">00111</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#85ab5540af5eadd957e102b4d848f095" title="called before the TCP server starts listening for new connections">HTTPServer::beforeStarting</a>(<span class="keywordtype">void</span>)<a name="l00112"></a>00112 {<a name="l00113"></a>00113 <span class="comment">// call the start() method for each module associated with this server</span><a name="l00114"></a>00114 boost::mutex::scoped_lock modules_lock(m_mutex);<a name="l00115"></a>00115 <span class="keywordflow">for</span> (ModuleMap::iterator i = m_modules.begin(); i != m_modules.end(); ++i)<a name="l00116"></a>00116 i->second.first->start();<a name="l00117"></a>00117 }<a name="l00118"></a>00118 <a name="l00119"></a><a class="code" href="classpion_1_1_h_t_t_p_server.html#d093dfe54d6808f2b86116c81490ff3a">00119</a> <span class="keywordtype">void</span> <a class="code" href="classpion_1_1_h_t_t_p_server.html#d093dfe54d6808f2b86116c81490ff3a" title="called after the TCP server has stopped listing for new connections">HTTPServer::afterStopping</a>(<span class="keywordtype">void</span>)<a name="l00120"></a>00120 {<a name="l00121"></a>00121 <span class="comment">// call the stop() method for each module associated with this server</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -