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

📄 _file_module_8cpp-source.html

📁 用c++编写http server的源码库,对socket等网络处理的代码可迅速转为己用.
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<a name="l00248"></a>00248             <span class="comment">// request does not match resource, and no directory is defined</span><a name="l00249"></a>00249             <span class="keywordflow">return</span> <span class="keyword">false</span>;<a name="l00250"></a>00250         }<a name="l00251"></a>00251 <a name="l00252"></a>00252         PION_LOG_DEBUG(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Found file for request ("</span><a name="l00253"></a>00253                        &lt;&lt; <a class="code" href="classpion_1_1_h_t_t_p_module.html#aad5404f6bb1cdda1d75da7df1ca8022" title="returns the URI stem or resource that is bound to the module">getResource</a>() &lt;&lt; <span class="stringliteral">"): "</span> &lt;&lt; relative_path);<a name="l00254"></a>00254 <a name="l00255"></a>00255         <span class="comment">// determine the MIME type</span><a name="l00256"></a>00256         response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#a1d1ffa0623d297951030e8c69a1d749" title="mime type for the cached file">mime_type</a> = <a class="code" href="class_file_module.html#8bd10d069624bf87d81343120aebe723">findMIMEType</a>( response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a>.leaf() );<a name="l00257"></a>00257 <a name="l00258"></a>00258         <span class="comment">// get the file_size and last_modified timestamp</span><a name="l00259"></a>00259         response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#b60a58a57d7a32adb62b1c0e5d20f0df" title="updates the file_size and last_modified timestamp to disk">update</a>();<a name="l00260"></a>00260 <a name="l00261"></a>00261         <span class="comment">// just compare strings for simplicity (parsing this date format sucks!)</span><a name="l00262"></a>00262         <span class="keywordflow">if</span> (response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#cc967ce35332fea0dd00f6ab9a12df9a" title="timestamp that the cached file was last modified (string format)">last_modified_string</a> == if_modified_since) {<a name="l00263"></a>00263             <span class="comment">// no need to read the file; the modified times match!</span><a name="l00264"></a>00264             response_type = RESPONSE_NOT_MODIFIED;<a name="l00265"></a>00265         } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (request-&gt;getMethod() == HTTPTypes::REQUEST_METHOD_HEAD) {<a name="l00266"></a>00266             response_type = RESPONSE_HEAD_OK;<a name="l00267"></a>00267         } <span class="keywordflow">else</span> {<a name="l00268"></a>00268             response_type = RESPONSE_OK;<a name="l00269"></a>00269             <span class="comment">// read the file (may throw exception)</span><a name="l00270"></a>00270             response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#a726bc66b4d290fe09c1edf02179aae6" title="reads content from disk into file_content buffer">read</a>();<a name="l00271"></a>00271             <span class="keywordflow">if</span> (m_cache_setting != 0) {<a name="l00272"></a>00272                 <span class="comment">// add new entry to the cache</span><a name="l00273"></a>00273                 PION_LOG_DEBUG(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Adding cache entry for request ("</span><a name="l00274"></a>00274                                &lt;&lt; <a class="code" href="classpion_1_1_h_t_t_p_module.html#aad5404f6bb1cdda1d75da7df1ca8022" title="returns the URI stem or resource that is bound to the module">getResource</a>() &lt;&lt; <span class="stringliteral">"): "</span> &lt;&lt; relative_path);<a name="l00275"></a>00275                 boost::mutex::scoped_lock cache_lock(m_cache_mutex);<a name="l00276"></a>00276                 m_cache_map.insert( std::make_pair(relative_path, response_file) );<a name="l00277"></a>00277             }<a name="l00278"></a>00278         }<a name="l00279"></a>00279     }<a name="l00280"></a>00280         <a name="l00281"></a>00281     <span class="comment">// prepare a response and set the Content-Type</span><a name="l00282"></a>00282     <a class="code" href="namespacepion.html#e27a744c3e8cf19c3620400bfe68fb69" title="data type for a HTTPResponse pointer">HTTPResponsePtr</a> response(HTTPResponse::create());<a name="l00283"></a>00283     response-&gt;setContentType(response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#a1d1ffa0623d297951030e8c69a1d749" title="mime type for the cached file">mime_type</a>);<a name="l00284"></a>00284     <a name="l00285"></a>00285     <span class="comment">// set Last-Modified header to enable client-side caching</span><a name="l00286"></a>00286     response-&gt;addHeader(HTTPTypes::HEADER_LAST_MODIFIED, response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#cc967ce35332fea0dd00f6ab9a12df9a" title="timestamp that the cached file was last modified (string format)">last_modified_string</a>);<a name="l00287"></a>00287 <a name="l00288"></a>00288     <span class="keywordflow">switch</span>(response_type) {<a name="l00289"></a>00289         <span class="keywordflow">case</span> RESPONSE_UNDEFINED:<a name="l00290"></a>00290             <span class="comment">// this should never happen</span><a name="l00291"></a>00291             <span class="keywordflow">throw</span> <a class="code" href="class_file_module_1_1_undefined_response_exception.html" title="exception thrown if we do not know how to response (should never happen)">UndefinedResponseException</a>(request-&gt;getResource());<a name="l00292"></a>00292             <span class="keywordflow">break</span>;<a name="l00293"></a>00293         <span class="keywordflow">case</span> RESPONSE_NOT_MODIFIED:<a name="l00294"></a>00294             <span class="comment">// set "Not Modified" response</span><a name="l00295"></a>00295             response-&gt;setResponseCode(HTTPTypes::RESPONSE_CODE_NOT_MODIFIED);<a name="l00296"></a>00296             response-&gt;setResponseMessage(HTTPTypes::RESPONSE_MESSAGE_NOT_MODIFIED);<a name="l00297"></a>00297             <span class="keywordflow">break</span>;<a name="l00298"></a>00298         <span class="keywordflow">case</span> RESPONSE_OK:<a name="l00299"></a>00299             <span class="comment">// write the file's contents to the response stream</span><a name="l00300"></a>00300             <span class="keywordflow">if</span> (response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#bca608cf97377af55fa929895c9fb469" title="size of the file's content">file_size</a> != 0)<a name="l00301"></a>00301                 response-&gt;write(response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#11122c2b7e3117e02662fb9b597f9a55" title="content of the cached file">file_content</a>.get(), response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#bca608cf97377af55fa929895c9fb469" title="size of the file's content">file_size</a>);<a name="l00302"></a>00302             <span class="comment">// fall through to RESPONSE_HEAD_OK</span><a name="l00303"></a>00303         <span class="keywordflow">case</span> RESPONSE_HEAD_OK:<a name="l00304"></a>00304             <span class="comment">// set "OK" response (not really necessary since this is the default)</span><a name="l00305"></a>00305             response-&gt;setResponseCode(HTTPTypes::RESPONSE_CODE_OK);<a name="l00306"></a>00306             response-&gt;setResponseMessage(HTTPTypes::RESPONSE_MESSAGE_OK);<a name="l00307"></a>00307             <span class="keywordflow">break</span>;<a name="l00308"></a>00308     }<a name="l00309"></a>00309     <a name="l00310"></a>00310     <span class="comment">// send the response</span><a name="l00311"></a>00311     response-&gt;send(tcp_conn);<a name="l00312"></a>00312 <a name="l00313"></a>00313     <span class="keywordflow">return</span> <span class="keyword">true</span>;<a name="l00314"></a>00314 }<a name="l00315"></a>00315 <a name="l00316"></a><a class="code" href="class_file_module.html#19565fca50d8699b17520b0733d76a9b">00316</a> <span class="keywordtype">void</span> <a class="code" href="class_file_module.html#19565fca50d8699b17520b0733d76a9b" title="called when the module's server is starting">FileModule::start</a>(<span class="keywordtype">void</span>)<a name="l00317"></a>00317 {<a name="l00318"></a>00318     PION_LOG_DEBUG(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Starting up resource ("</span> &lt;&lt; <a class="code" href="classpion_1_1_h_t_t_p_module.html#aad5404f6bb1cdda1d75da7df1ca8022" title="returns the URI stem or resource that is bound to the module">getResource</a>() &lt;&lt; <span class="charliteral">')'</span>);<a name="l00319"></a>00319 <a name="l00320"></a>00320     <span class="comment">// scan directory/file if scan setting != 0</span><a name="l00321"></a>00321     <span class="keywordflow">if</span> (m_scan_setting != 0) {<a name="l00322"></a>00322         <span class="comment">// force caching if scan == (2 | 3)</span><a name="l00323"></a>00323         <span class="keywordflow">if</span> (m_cache_setting == 0 &amp;&amp; m_scan_setting &gt; 1)<a name="l00324"></a>00324             m_cache_setting = 1;<a name="l00325"></a>00325         <a name="l00326"></a>00326         boost::mutex::scoped_lock cache_lock(m_cache_mutex);<a name="l00327"></a>00327 <a name="l00328"></a>00328         <span class="comment">// add entry for file if one is defined</span><a name="l00329"></a>00329         <span class="keywordflow">if</span> (! m_file.empty()) {<a name="l00330"></a>00330             <span class="comment">// use empty relative_path for file option</span><a name="l00331"></a>00331             <span class="comment">// use placeholder entry (do not pre-populate) if scan == 1</span><a name="l00332"></a>00332             <a class="code" href="class_file_module.html#5df44719239d16aedf792fe5c9a19e5a">addCacheEntry</a>(<span class="stringliteral">""</span>, m_file, m_scan_setting == 1);<a name="l00333"></a>00333         }<a name="l00334"></a>00334         <a name="l00335"></a>00335         <span class="comment">// scan directory if one is defined</span><a name="l00336"></a>00336         <span class="keywordflow">if</span> (! m_directory.empty())<a name="l00337"></a>00337             <a class="code" href="class_file_module.html#1ad8c01c7d0253b5da3068d574169d82">scanDirectory</a>(m_directory);<a name="l00338"></a>00338     }<a name="l00339"></a>00339 }<a name="l00340"></a>00340 <a name="l00341"></a><a class="code" href="class_file_module.html#72e5556e073da7eb286dd10f6e693abe">00341</a> <span class="keywordtype">void</span> <a class="code" href="class_file_module.html#72e5556e073da7eb286dd10f6e693abe" title="called when the module's server is stopping">FileModule::stop</a>(<span class="keywordtype">void</span>)<a name="l00342"></a>00342 {<a name="l00343"></a>00343     PION_LOG_DEBUG(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Shutting down resource ("</span> &lt;&lt; <a class="code" href="classpion_1_1_h_t_t_p_module.html#aad5404f6bb1cdda1d75da7df1ca8022" title="returns the URI stem or resource that is bound to the module">getResource</a>() &lt;&lt; <span class="charliteral">')'</span>);<a name="l00344"></a>00344     <span class="comment">// clear cached files (if started again, it will re-scan)</span><a name="l00345"></a>00345     boost::mutex::scoped_lock cache_lock(m_cache_mutex);<a name="l00346"></a>00346     m_cache_map.clear();<a name="l00347"></a>00347 }<a name="l00348"></a>00348 <a name="l00349"></a><a class="code" href="class_file_module.html#1ad8c01c7d0253b5da3068d574169d82">00349</a> <span class="keywordtype">void</span> <a class="code" href="class_file_module.html#1ad8c01c7d0253b5da3068d574169d82">FileModule::scanDirectory</a>(<span class="keyword">const</span> boost::filesystem::path&amp; dir_path)<a name="l00350"></a>00350 {<a name="l00351"></a>00351     PION_LOG_DEBUG(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Scanning directory ("</span> &lt;&lt; <a class="code" href="classpion_1_1_h_t_t_p_module.html#aad5404f6bb1cdda1d75da7df1ca8022" title="returns the URI stem or resource that is bound to the module">getResource</a>() &lt;&lt; <span class="stringliteral">"): "</span><a name="l00352"></a>00352                    &lt;&lt; dir_path.directory_string());<a name="l00353"></a>00353     <a name="l00354"></a>00354     <span class="comment">// iterate through items in the directory</span><a name="l00355"></a>00355     boost::filesystem::directory_iterator end_itr;<a name="l00356"></a>00356     <span class="keywordflow">for</span> ( boost::filesystem::directory_iterator itr( dir_path );<a name="l00357"></a>00357           itr != end_itr; ++itr )<a name="l00358"></a>00358     {<a name="l00359"></a>00359         <span class="keywordflow">if</span> ( boost::filesystem::is_directory(*itr) ) {<a name="l00360"></a>00360             <span class="comment">// item is a sub-directory</span><a name="l00361"></a>00361             <a name="l00362"></a>00362             <span class="comment">// recursively call scanDirectory()</span><a name="l00363"></a>00363             <a class="code" href="class_file_module.html#1ad8c01c7d0253b5da3068d574169d82">scanDirectory</a>(*itr);<a name="l00364"></a>00364             <a name="l00365"></a>00365         } <span class="keywordflow">else</span> {<a name="l00366"></a>00366             <span class="comment">// item is a regular file</span><a name="l00367"></a>00367             <a name="l00368"></a>00368             <span class="comment">// figure out relative path to the file</span><a name="l00369"></a>00369             std::string file_path_string( itr-&gt;path().file_string() );<a name="l00370"></a>00370             std::string relative_path( file_path_string.substr(m_directory.directory_string().size() + 1) );<a name="l00371"></a>00371             <a name="l00372"></a>00372             <span class="comment">// add item to cache (use placeholder if scan == 1)</span><a name="l00373"></a>00373             <a class="code" href="class_file_module.html#5df44719239d16aedf792fe5c9a19e5a">addCacheEntry</a>(relative_path, *itr, m_scan_setting == 1);<a name="l00374"></a>00374         }<a name="l00375"></a>00375     }<a name="l00376"></a>00376 }<a name="l00377"></a>00377 <a name="l00378"></a>00378 std::pair&lt;FileModule::CacheMap::iterator, bool&gt;

⌨️ 快捷键说明

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