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

📄 _file_module_8cpp-source.html

📁 用c++编写http server的源码库,对socket等网络处理的代码可迅速转为己用.
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!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/FileModule.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&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/modules/FileModule.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 "FileModule.hpp"</span><a name="l00011"></a>00011 <span class="preprocessor">#include &lt;libpion/PionPlugin.hpp&gt;</span><a name="l00012"></a>00012 <span class="preprocessor">#include &lt;libpion/HTTPResponse.hpp&gt;</span><a name="l00013"></a>00013 <span class="preprocessor">#include &lt;boost/scoped_array.hpp&gt;</span><a name="l00014"></a>00014 <span class="preprocessor">#include &lt;boost/filesystem/operations.hpp&gt;</span><a name="l00015"></a>00015 <span class="preprocessor">#include &lt;boost/filesystem/fstream.hpp&gt;</span><a name="l00016"></a>00016 <span class="preprocessor">#include &lt;algorithm&gt;</span><a name="l00017"></a>00017 <a name="l00018"></a>00018 <span class="keyword">using namespace </span>pion;<a name="l00019"></a>00019 <a name="l00020"></a>00020 <a name="l00021"></a>00021 <span class="comment">// static members of FileModule</span><a name="l00022"></a>00022 <a name="l00023"></a>00023 <span class="keyword">const</span> std::string           FileModule::DEFAULT_MIME_TYPE(<span class="stringliteral">"application/octet-stream"</span>);<a name="l00024"></a>00024 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>          FileModule::DEFAULT_CACHE_SETTING = 1;<a name="l00025"></a>00025 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>          FileModule::DEFAULT_SCAN_SETTING = 0;<a name="l00026"></a>00026 boost::once_flag            FileModule::m_mime_types_init_flag = BOOST_ONCE_INIT;<a name="l00027"></a>00027 <a class="code" href="class_file_module.html#2159dcd07ecffdf2aa1ee93d697308d8" title="data type for map of file extensions to MIME types">FileModule::MIMETypeMap</a> *   FileModule::m_mime_types_ptr = NULL;<a name="l00028"></a>00028 <a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="comment">// FileModule member functions</span><a name="l00031"></a>00031 <a name="l00032"></a><a class="code" href="class_file_module.html#89750297878b91a8bff76e3470ea0083">00032</a> <a class="code" href="class_file_module.html#89750297878b91a8bff76e3470ea0083">FileModule::FileModule</a>(<span class="keywordtype">void</span>)<a name="l00033"></a>00033     : m_logger(PION_GET_LOGGER(<span class="stringliteral">"FileModule"</span>)),<a name="l00034"></a>00034     m_cache_setting(DEFAULT_CACHE_SETTING),<a name="l00035"></a>00035     m_scan_setting(DEFAULT_SCAN_SETTING)<a name="l00036"></a>00036 {<a name="l00037"></a>00037     PION_LOG_SETLEVEL_WARN(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>);<a name="l00038"></a>00038 }<a name="l00039"></a>00039 <a name="l00040"></a><a class="code" href="class_file_module.html#e2bb1db0158386d16e5b6a9fb264f64e">00040</a> <span class="keywordtype">void</span> <a class="code" href="class_file_module.html#e2bb1db0158386d16e5b6a9fb264f64e">FileModule::setOption</a>(<span class="keyword">const</span> std::string&amp; name, <span class="keyword">const</span> std::string&amp; value)<a name="l00041"></a>00041 {<a name="l00042"></a>00042     <span class="keywordflow">if</span> (name == <span class="stringliteral">"directory"</span>) {<a name="l00043"></a>00043         m_directory = value;<a name="l00044"></a>00044         PionPlugin::checkCygwinPath(m_directory, value);<a name="l00045"></a>00045         <span class="comment">// make sure that the directory exists</span><a name="l00046"></a>00046         <span class="keywordflow">if</span> (! boost::filesystem::exists(m_directory) )<a name="l00047"></a>00047             <span class="keywordflow">throw</span> <a class="code" href="class_file_module_1_1_directory_not_found_exception.html" title="exception thrown if the directory configured is not found">DirectoryNotFoundException</a>(value);<a name="l00048"></a>00048         <span class="keywordflow">if</span> (! boost::filesystem::is_directory(m_directory) )<a name="l00049"></a>00049             <span class="keywordflow">throw</span> <a class="code" href="class_file_module_1_1_not_a_directory_exception.html" title="exception thrown if the directory configuration option is not a directory">NotADirectoryException</a>(value);<a name="l00050"></a>00050     } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (name == <span class="stringliteral">"file"</span>) {<a name="l00051"></a>00051         m_file = value;<a name="l00052"></a>00052         PionPlugin::checkCygwinPath(m_file, value);<a name="l00053"></a>00053         <span class="comment">// make sure that the directory exists</span><a name="l00054"></a>00054         <span class="keywordflow">if</span> (! boost::filesystem::exists(m_file) )<a name="l00055"></a>00055             <span class="keywordflow">throw</span> <a class="code" href="class_file_module_1_1_file_not_found_exception.html" title="exception thrown if the file configured is not found">FileNotFoundException</a>(value);<a name="l00056"></a>00056         <span class="keywordflow">if</span> (boost::filesystem::is_directory(m_file) )<a name="l00057"></a>00057             <span class="keywordflow">throw</span> <a class="code" href="class_file_module_1_1_not_a_file_exception.html" title="exception thrown if the file configuration option is not a file">NotAFileException</a>(value);<a name="l00058"></a>00058     } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (name == <span class="stringliteral">"cache"</span>) {<a name="l00059"></a>00059         <span class="keywordflow">if</span> (value == <span class="stringliteral">"0"</span>) {<a name="l00060"></a>00060             m_cache_setting = 0;<a name="l00061"></a>00061         } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value == <span class="stringliteral">"1"</span>) {<a name="l00062"></a>00062             m_cache_setting = 1;<a name="l00063"></a>00063         } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value == <span class="stringliteral">"2"</span>) {<a name="l00064"></a>00064             m_cache_setting = 2;<a name="l00065"></a>00065         } <span class="keywordflow">else</span> {<a name="l00066"></a>00066             <span class="keywordflow">throw</span> <a class="code" href="class_file_module_1_1_invalid_cache_exception.html" title="exception thrown if the cache option is set to an invalid value">InvalidCacheException</a>(value);<a name="l00067"></a>00067         }<a name="l00068"></a>00068     } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (name == <span class="stringliteral">"scan"</span>) {<a name="l00069"></a>00069         <span class="keywordflow">if</span> (value == <span class="stringliteral">"0"</span>) {<a name="l00070"></a>00070             m_scan_setting = 0;<a name="l00071"></a>00071         } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value == <span class="stringliteral">"1"</span>) {<a name="l00072"></a>00072             m_scan_setting = 1;<a name="l00073"></a>00073         } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value == <span class="stringliteral">"2"</span>) {<a name="l00074"></a>00074             m_scan_setting = 2;<a name="l00075"></a>00075         } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value == <span class="stringliteral">"3"</span>) {<a name="l00076"></a>00076             m_scan_setting = 3;<a name="l00077"></a>00077         } <span class="keywordflow">else</span> {<a name="l00078"></a>00078             <span class="keywordflow">throw</span> <a class="code" href="class_file_module_1_1_invalid_scan_exception.html" title="exception thrown if the cache option is set to an invalid value">InvalidScanException</a>(value);<a name="l00079"></a>00079         }<a name="l00080"></a>00080     } <span class="keywordflow">else</span> {<a name="l00081"></a>00081         <span class="keywordflow">throw</span> UnknownOptionException(name);<a name="l00082"></a>00082     }<a name="l00083"></a>00083 }<a name="l00084"></a>00084 <a name="l00085"></a><a class="code" href="class_file_module.html#b9f46dabadef14ba1ca3b5698b310106">00085</a> <span class="keywordtype">bool</span> <a class="code" href="class_file_module.html#b9f46dabadef14ba1ca3b5698b310106" title="handles requests for FileModule">FileModule::handleRequest</a>(HTTPRequestPtr&amp; request, TCPConnectionPtr&amp; tcp_conn)<a name="l00086"></a>00086 {<a name="l00087"></a>00087     <span class="comment">// the type of response we will send</span><a name="l00088"></a>00088     <span class="keyword">enum</span> ResponseType {<a name="l00089"></a>00089         RESPONSE_UNDEFINED,     <span class="comment">// initial state until we know how to respond</span><a name="l00090"></a>00090         RESPONSE_OK,            <span class="comment">// normal response that includes the file's content</span><a name="l00091"></a>00091         RESPONSE_HEAD_OK,       <span class="comment">// response to HEAD request (would send file's content)</span><a name="l00092"></a>00092         RESPONSE_NOT_MODIFIED   <span class="comment">// Not Modified (304) response to If-Modified-Since</span><a name="l00093"></a>00093     } response_type = RESPONSE_UNDEFINED;<a name="l00094"></a>00094 <a name="l00095"></a>00095     <span class="comment">// used to hold our response information</span><a name="l00096"></a>00096     <a class="code" href="struct_file_module_1_1_disk_file.html" title="data type representing files on disk">DiskFile</a> response_file;<a name="l00097"></a>00097     <a name="l00098"></a>00098     <span class="comment">// get the If-Modified-Since request header</span><a name="l00099"></a>00099     <span class="keyword">const</span> std::string if_modified_since(request-&gt;getHeader(HTTPTypes::HEADER_IF_MODIFIED_SINCE));<a name="l00100"></a>00100     <a name="l00101"></a>00101     <span class="comment">// get the relative resource path for the request</span><a name="l00102"></a>00102     <span class="keyword">const</span> std::string relative_path(<a class="code" href="classpion_1_1_h_t_t_p_module.html#d6371c2480fef34429845567067c072e" title="returns the path to the resource requested, relative to the module's location">getRelativeResource</a>(request-&gt;getResource()));<a name="l00103"></a>00103     <a name="l00104"></a>00104     <span class="comment">// check the cache for a corresponding entry (if enabled)</span><a name="l00105"></a>00105     <span class="comment">// note that m_cache_setting may equal 0 if m_scan_setting == 1</span><a name="l00106"></a>00106     <span class="keywordflow">if</span> (m_cache_setting &gt; 0 || m_scan_setting &gt; 0) {<a name="l00107"></a>00107         <a name="l00108"></a>00108         <span class="comment">// search for a matching cache entry</span><a name="l00109"></a>00109         boost::mutex::scoped_lock cache_lock(m_cache_mutex);<a name="l00110"></a>00110         CacheMap::iterator cache_itr = m_cache_map.find(relative_path);<a name="l00111"></a>00111 <a name="l00112"></a>00112         <span class="keywordflow">if</span> (cache_itr == m_cache_map.end()) {<a name="l00113"></a>00113             <span class="comment">// no existing cache entries found</span><a name="l00114"></a>00114             <a name="l00115"></a>00115             <span class="keywordflow">if</span> (m_scan_setting == 1 || m_scan_setting == 3) {<a name="l00116"></a>00116                 <span class="comment">// do not allow files to be added;</span>

⌨️ 快捷键说明

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