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

📄 _file_module_8cpp-source.html

📁 用c++编写http server的源码库,对socket等网络处理的代码可迅速转为己用.
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<a name="l00379"></a><a class="code" href="class_file_module.html#5df44719239d16aedf792fe5c9a19e5a">00379</a> <a class="code" href="class_file_module.html#5df44719239d16aedf792fe5c9a19e5a">FileModule::addCacheEntry</a>(<span class="keyword">const</span> std::string&amp; relative_path,<a name="l00380"></a>00380                           <span class="keyword">const</span> boost::filesystem::path&amp; file_path,<a name="l00381"></a>00381                           <span class="keyword">const</span> <span class="keywordtype">bool</span> placeholder)<a name="l00382"></a>00382 {<a name="l00383"></a>00383     <a class="code" href="struct_file_module_1_1_disk_file.html" title="data type representing files on disk">DiskFile</a> cache_entry(file_path, NULL, 0, 0, <a class="code" href="class_file_module.html#8bd10d069624bf87d81343120aebe723">findMIMEType</a>(file_path.leaf()));<a name="l00384"></a>00384     <span class="keywordflow">if</span> (! placeholder) {<a name="l00385"></a>00385         cache_entry.<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="l00386"></a>00386         <span class="keywordflow">try</span> { cache_entry.<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="l00387"></a>00387         <span class="keywordflow">catch</span> (std::exception&amp; e) {<a name="l00388"></a>00388             PION_LOG_ERROR(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Unable to add file to cache: "</span><a name="l00389"></a>00389                            &lt;&lt; file_path.file_string());<a name="l00390"></a>00390             <span class="keywordflow">return</span> std::make_pair(m_cache_map.end(), <span class="keyword">false</span>);<a name="l00391"></a>00391         }<a name="l00392"></a>00392     }<a name="l00393"></a>00393     <a name="l00394"></a>00394     std::pair&lt;CacheMap::iterator, bool&gt; add_entry_result<a name="l00395"></a>00395         = m_cache_map.insert( std::make_pair(relative_path, cache_entry) );<a name="l00396"></a>00396     <a name="l00397"></a>00397     <span class="keywordflow">if</span> (add_entry_result.second) {<a name="l00398"></a>00398         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">"Added file to cache: "</span><a name="l00399"></a>00399                        &lt;&lt; file_path.file_string());<a name="l00400"></a>00400     } <span class="keywordflow">else</span> {<a name="l00401"></a>00401         PION_LOG_ERROR(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Unable to insert cache entry for file: "</span><a name="l00402"></a>00402                        &lt;&lt; file_path.file_string());<a name="l00403"></a>00403     }<a name="l00404"></a>00404     <a name="l00405"></a>00405     <span class="keywordflow">return</span> add_entry_result;<a name="l00406"></a>00406 }<a name="l00407"></a>00407 <a name="l00408"></a><a class="code" href="class_file_module.html#8bd10d069624bf87d81343120aebe723">00408</a> std::string <a class="code" href="class_file_module.html#8bd10d069624bf87d81343120aebe723">FileModule::findMIMEType</a>(<span class="keyword">const</span> std::string&amp; file_name) {<a name="l00409"></a>00409     <span class="comment">// initialize m_mime_types if it hasn't been done already</span><a name="l00410"></a>00410     boost::call_once(FileModule::createMIMETypes, m_mime_types_init_flag);<a name="l00411"></a>00411     <a name="l00412"></a>00412     <span class="comment">// determine the file's extension</span><a name="l00413"></a>00413     std::string extension(file_name.substr(file_name.find_last_of(<span class="charliteral">'.'</span>) + 1));<a name="l00414"></a>00414     std::transform(extension.begin(), extension.end(),<a name="l00415"></a>00415                    extension.begin(), tolower);<a name="l00416"></a>00416     <a name="l00417"></a>00417     <span class="comment">// search for the matching mime type and return the result</span><a name="l00418"></a>00418     MIMETypeMap::iterator i = m_mime_types_ptr-&gt;find(extension);<a name="l00419"></a>00419     <span class="keywordflow">return</span> (i == m_mime_types_ptr-&gt;end() ? DEFAULT_MIME_TYPE : i-&gt;second);<a name="l00420"></a>00420 }<a name="l00421"></a>00421 <a name="l00422"></a>00422 <span class="keywordtype">void</span> FileModule::createMIMETypes(<span class="keywordtype">void</span>) {<a name="l00423"></a>00423     <span class="comment">// create the map</span><a name="l00424"></a>00424     <span class="keyword">static</span> <a class="code" href="class_file_module.html#2159dcd07ecffdf2aa1ee93d697308d8" title="data type for map of file extensions to MIME types">MIMETypeMap</a> mime_types;<a name="l00425"></a>00425     <a name="l00426"></a>00426     <span class="comment">// populate mime types</span><a name="l00427"></a>00427     mime_types[<span class="stringliteral">"js"</span>] = <span class="stringliteral">"text/javascript"</span>;<a name="l00428"></a>00428     mime_types[<span class="stringliteral">"txt"</span>] = <span class="stringliteral">"text/plain"</span>;<a name="l00429"></a>00429     mime_types[<span class="stringliteral">"xml"</span>] = <span class="stringliteral">"text/xml"</span>;<a name="l00430"></a>00430     mime_types[<span class="stringliteral">"css"</span>] = <span class="stringliteral">"text/css"</span>;<a name="l00431"></a>00431     mime_types[<span class="stringliteral">"htm"</span>] = <span class="stringliteral">"text/html"</span>;<a name="l00432"></a>00432     mime_types[<span class="stringliteral">"html"</span>] = <span class="stringliteral">"text/html"</span>;<a name="l00433"></a>00433     mime_types[<span class="stringliteral">"xhtml"</span>] = <span class="stringliteral">"text/html"</span>;<a name="l00434"></a>00434     mime_types[<span class="stringliteral">"gif"</span>] = <span class="stringliteral">"image/gif"</span>;<a name="l00435"></a>00435     mime_types[<span class="stringliteral">"png"</span>] = <span class="stringliteral">"image/png"</span>;<a name="l00436"></a>00436     mime_types[<span class="stringliteral">"jpg"</span>] = <span class="stringliteral">"image/jpeg"</span>;<a name="l00437"></a>00437     mime_types[<span class="stringliteral">"jpeg"</span>] = <span class="stringliteral">"image/jpeg"</span>;<a name="l00438"></a>00438     <span class="comment">// ...</span><a name="l00439"></a>00439     <a name="l00440"></a>00440     <span class="comment">// set the static pointer</span><a name="l00441"></a>00441     m_mime_types_ptr = &amp;mime_types;<a name="l00442"></a>00442 }<a name="l00443"></a>00443 <a name="l00444"></a>00444 <a name="l00445"></a>00445 <span class="comment">// FileModule::DiskFile member functions</span><a name="l00446"></a>00446 <a name="l00447"></a><a class="code" href="struct_file_module_1_1_disk_file.html#b60a58a57d7a32adb62b1c0e5d20f0df">00447</a> <span class="keywordtype">void</span> <a class="code" href="struct_file_module_1_1_disk_file.html#b60a58a57d7a32adb62b1c0e5d20f0df" title="updates the file_size and last_modified timestamp to disk">FileModule::DiskFile::update</a>(<span class="keywordtype">void</span>)<a name="l00448"></a>00448 {<a name="l00449"></a>00449     <span class="comment">// set file_size and last_modified</span><a name="l00450"></a>00450     <a class="code" href="struct_file_module_1_1_disk_file.html#bca608cf97377af55fa929895c9fb469" title="size of the file's content">file_size</a> = boost::filesystem::file_size( <a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a> );<a name="l00451"></a>00451     <a class="code" href="struct_file_module_1_1_disk_file.html#4295e8fe34d30c0cbc6e838bcae3a651" title="timestamp that the cached file was last modified (0 = cache disabled)">last_modified</a> = boost::filesystem::last_write_time( <a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a> );<a name="l00452"></a>00452     <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> = HTTPTypes::get_date_string( <a class="code" href="struct_file_module_1_1_disk_file.html#4295e8fe34d30c0cbc6e838bcae3a651" title="timestamp that the cached file was last modified (0 = cache disabled)">last_modified</a> );<a name="l00453"></a>00453 }<a name="l00454"></a>00454 <a name="l00455"></a><a class="code" href="struct_file_module_1_1_disk_file.html#a726bc66b4d290fe09c1edf02179aae6">00455</a> <span class="keywordtype">void</span> <a class="code" href="struct_file_module_1_1_disk_file.html#a726bc66b4d290fe09c1edf02179aae6" title="reads content from disk into file_content buffer">FileModule::DiskFile::read</a>(<span class="keywordtype">void</span>)<a name="l00456"></a>00456 {<a name="l00457"></a>00457     <span class="comment">// re-allocate storage buffer for the file's content</span><a name="l00458"></a>00458     <a class="code" href="struct_file_module_1_1_disk_file.html#11122c2b7e3117e02662fb9b597f9a55" title="content of the cached file">file_content</a>.reset(<span class="keyword">new</span> <span class="keywordtype">char</span>[<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="l00459"></a>00459     <a name="l00460"></a>00460     <span class="comment">// open the file for reading</span><a name="l00461"></a>00461     boost::filesystem::ifstream file_stream;<a name="l00462"></a>00462     file_stream.open(<a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a>, std::ios::in | std::ios::binary);<a name="l00463"></a>00463 <a name="l00464"></a>00464     <span class="comment">// read the file into memory</span><a name="l00465"></a>00465     <span class="keywordflow">if</span> (!file_stream.is_open() || !file_stream.read(<a class="code" href="struct_file_module_1_1_disk_file.html#11122c2b7e3117e02662fb9b597f9a55" title="content of the cached file">file_content</a>.get(), file_size))<a name="l00466"></a>00466         <span class="keywordflow">throw</span> <a class="code" href="class_file_module_1_1_file_read_exception.html" title="exception thrown if we are unable to read a file from disk">FileModule::FileReadException</a>(<a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a>.file_string());<a name="l00467"></a>00467 }<a name="l00468"></a>00468 <a name="l00469"></a><a class="code" href="struct_file_module_1_1_disk_file.html#5035f7db79097b1420ff056ac2632ec3">00469</a> <span class="keywordtype">bool</span> <a class="code" href="struct_file_module_1_1_disk_file.html#5035f7db79097b1420ff056ac2632ec3">FileModule::DiskFile::checkUpdated</a>(<span class="keywordtype">void</span>)<a name="l00470"></a>00470 {<a name="l00471"></a>00471     <span class="comment">// get current values</span><a name="l00472"></a>00472     <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> cur_size = boost::filesystem::file_size( <a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a> );<a name="l00473"></a>00473     time_t cur_modified = boost::filesystem::last_write_time( <a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a> );<a name="l00474"></a>00474 <a name="l00475"></a>00475     <span class="comment">// check if file has not been updated</span><a name="l00476"></a>00476     <span class="keywordflow">if</span> (cur_modified == <a class="code" href="struct_file_module_1_1_disk_file.html#4295e8fe34d30c0cbc6e838bcae3a651" title="timestamp that the cached file was last modified (0 = cache disabled)">last_modified</a> &amp;&amp; cur_size == <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="l00477"></a>00477         <span class="keywordflow">return</span> <span class="keyword">false</span>;<a name="l00478"></a>00478 <a name="l00479"></a>00479     <span class="comment">// file has been updated</span><a name="l00480"></a>00480         <a name="l00481"></a>00481     <span class="comment">// update file_size and last_modified timestamp</span><a name="l00482"></a>00482     <a class="code" href="struct_file_module_1_1_disk_file.html#bca608cf97377af55fa929895c9fb469" title="size of the file's content">file_size</a> = cur_size;<a name="l00483"></a>00483     <a class="code" href="struct_file_module_1_1_disk_file.html#4295e8fe34d30c0cbc6e838bcae3a651" title="timestamp that the cached file was last modified (0 = cache disabled)">last_modified</a> = cur_modified;<a name="l00484"></a>00484     <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> = HTTPTypes::get_date_string( <a class="code" href="struct_file_module_1_1_disk_file.html#4295e8fe34d30c0cbc6e838bcae3a651" title="timestamp that the cached file was last modified (0 = cache disabled)">last_modified</a> );<a name="l00485"></a>00485 <a name="l00486"></a>00486     <span class="comment">// read new contents</span><a name="l00487"></a>00487     <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="l00488"></a>00488     <a name="l00489"></a>00489     <span class="keywordflow">return</span> <span class="keyword">true</span>;<a name="l00490"></a>00490 }<a name="l00491"></a>00491 <a name="l00492"></a>00492 <a name="l00494"></a>00494 <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <a class="code" href="class_file_module.html">FileModule</a> *pion_create_FileModule(<span class="keywordtype">void</span>)<a name="l00495"></a>00495 {<a name="l00496"></a>00496     <span class="keywordflow">return</span> <span class="keyword">new</span> <a class="code" href="class_file_module.html#89750297878b91a8bff76e3470ea0083">FileModule</a>();<a name="l00497"></a>00497 }<a name="l00498"></a>00498 <a name="l00499"></a>00499 <a name="l00501"></a>00501 <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <span class="keywordtype">void</span> pion_destroy_FileModule(<a class="code" href="class_file_module.html">FileModule</a> *module_ptr)<a name="l00502"></a>00502 {<a name="l00503"></a>00503     <span class="keyword">delete</span> module_ptr;<a name="l00504"></a>00504 }</pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Tue Jun 19 13:29:22 2007 for libpion by&nbsp;<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 + -