📄 _file_module_8cpp-source.html
字号:
<a name="l00117"></a>00117 <span class="comment">// all requests must correspond with existing cache entries</span><a name="l00118"></a>00118 <span class="comment">// since no match was found, just return file not found</span><a name="l00119"></a>00119 PION_LOG_WARN(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Request for unknown file ("</span><a name="l00120"></a>00120 << <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>() << <span class="stringliteral">"): "</span> << relative_path);<a name="l00121"></a>00121 <span class="keywordflow">return</span> <span class="keyword">false</span>;<a name="l00122"></a>00122 } <span class="keywordflow">else</span> {<a name="l00123"></a>00123 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">"No cache entry for request ("</span><a name="l00124"></a>00124 << <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>() << <span class="stringliteral">"): "</span> << relative_path);<a name="l00125"></a>00125 }<a name="l00126"></a>00126 <a name="l00127"></a>00127 } <span class="keywordflow">else</span> {<a name="l00128"></a>00128 <span class="comment">// found an existing cache entry</span><a name="l00129"></a>00129 <a name="l00130"></a>00130 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 cache entry for request ("</span><a name="l00131"></a>00131 << <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>() << <span class="stringliteral">"): "</span> << relative_path);<a name="l00132"></a>00132 <a name="l00133"></a>00133 <span class="keywordflow">if</span> (m_cache_setting == 0) {<a name="l00134"></a>00134 <span class="comment">// cache is disabled</span><a name="l00135"></a>00135 <a name="l00136"></a>00136 <span class="comment">// copy & re-use file_path and mime_type</span><a name="l00137"></a>00137 response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a> = cache_itr->second.file_path;<a name="l00138"></a>00138 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> = cache_itr->second.mime_type;<a name="l00139"></a>00139 <a name="l00140"></a>00140 <span class="comment">// get the file_size and last_modified timestamp</span><a name="l00141"></a>00141 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="l00142"></a>00142 <a name="l00143"></a>00143 <span class="comment">// just compare strings for simplicity (parsing this date format sucks!)</span><a name="l00144"></a>00144 <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="l00145"></a>00145 <span class="comment">// no need to read the file; the modified times match!</span><a name="l00146"></a>00146 response_type = RESPONSE_NOT_MODIFIED;<a name="l00147"></a>00147 } <span class="keywordflow">else</span> {<a name="l00148"></a>00148 <span class="keywordflow">if</span> (request->getMethod() == HTTPTypes::REQUEST_METHOD_HEAD) {<a name="l00149"></a>00149 response_type = RESPONSE_HEAD_OK;<a name="l00150"></a>00150 } <span class="keywordflow">else</span> {<a name="l00151"></a>00151 response_type = RESPONSE_OK;<a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="comment">// read the file (may throw exception)</span><a name="l00154"></a>00154 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="l00155"></a>00155 <a name="l00156"></a>00156 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">"Cache disabled, reading file ("</span><a name="l00157"></a>00157 << <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>() << <span class="stringliteral">"): "</span> << relative_path);<a name="l00158"></a>00158 }<a name="l00159"></a>00159 }<a name="l00160"></a>00160 <a name="l00161"></a>00161 } <span class="keywordflow">else</span> {<a name="l00162"></a>00162 <span class="comment">// cache is enabled</span><a name="l00163"></a>00163 <a name="l00164"></a>00164 <span class="comment">// true if the entry was updated (used for log message)</span><a name="l00165"></a>00165 <span class="keywordtype">bool</span> cache_was_updated = <span class="keyword">false</span>;<a name="l00166"></a>00166 <a name="l00167"></a>00167 <span class="keywordflow">if</span> (cache_itr->second.last_modified == 0) {<a name="l00168"></a>00168 <a name="l00169"></a>00169 <span class="comment">// cache file for the first time</span><a name="l00170"></a>00170 cache_was_updated = <span class="keyword">true</span>;<a name="l00171"></a>00171 cache_itr->second.update();<a name="l00172"></a>00172 <span class="comment">// read the file (may throw exception)</span><a name="l00173"></a>00173 cache_itr->second.read();<a name="l00174"></a>00174 <a name="l00175"></a>00175 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (m_cache_setting == 1) {<a name="l00176"></a>00176 <a name="l00177"></a>00177 <span class="comment">// check if file has been updated (may throw exception)</span><a name="l00178"></a>00178 cache_was_updated = cache_itr->second.checkUpdated();<a name="l00179"></a>00179 <a name="l00180"></a>00180 } <span class="comment">// else cache_setting == 2 (use existing values)</span><a name="l00181"></a>00181 <a name="l00182"></a>00182 <span class="comment">// get the response type</span><a name="l00183"></a>00183 <span class="keywordflow">if</span> (cache_itr->second.last_modified_string == if_modified_since) {<a name="l00184"></a>00184 response_type = RESPONSE_NOT_MODIFIED;<a name="l00185"></a>00185 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (request->getMethod() == HTTPTypes::REQUEST_METHOD_HEAD) {<a name="l00186"></a>00186 response_type = RESPONSE_HEAD_OK;<a name="l00187"></a>00187 } <span class="keywordflow">else</span> {<a name="l00188"></a>00188 response_type = RESPONSE_OK;<a name="l00189"></a>00189 }<a name="l00190"></a>00190 <a name="l00191"></a>00191 <span class="comment">// copy cache contents so that we can release the mutex</span><a name="l00192"></a>00192 response_file = cache_itr->second;<a name="l00193"></a>00193 <a name="l00194"></a>00194 PION_LOG_DEBUG(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, (cache_was_updated ? <span class="stringliteral">"Updated"</span> : <span class="stringliteral">"Using"</span>)<a name="l00195"></a>00195 << <span class="stringliteral">" cache entry for request ("</span><a name="l00196"></a>00196 << <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>() << <span class="stringliteral">"): "</span> << relative_path);<a name="l00197"></a>00197 }<a name="l00198"></a>00198 }<a name="l00199"></a>00199 }<a name="l00200"></a>00200 <a name="l00201"></a>00201 <span class="keywordflow">if</span> (response_type == RESPONSE_UNDEFINED) {<a name="l00202"></a>00202 <span class="comment">// determine the path to the file</span><a name="l00203"></a>00203 <a name="l00204"></a>00204 <span class="keywordflow">if</span> (relative_path.empty()) {<a name="l00205"></a>00205 <span class="comment">// request matches resource exactly</span><a name="l00206"></a>00206 <a name="l00207"></a>00207 <span class="comment">// return false if no file defined</span><a name="l00208"></a>00208 <span class="comment">// module's directory is not valid</span><a name="l00209"></a>00209 <span class="keywordflow">if</span> (m_file.empty()) {<a name="l00210"></a>00210 PION_LOG_WARN(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"No file option defined ("</span><a name="l00211"></a>00211 << <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>() << <span class="stringliteral">"): "</span> << relative_path);<a name="l00212"></a>00212 <span class="keywordflow">return</span> <span class="keyword">false</span>;<a name="l00213"></a>00213 }<a name="l00214"></a>00214 <a name="l00215"></a>00215 <span class="comment">// use file to service directory request</span><a name="l00216"></a>00216 response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a> = m_file;<a name="l00217"></a>00217 <a name="l00218"></a>00218 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (! m_directory.empty()) {<a name="l00219"></a>00219 <span class="comment">// request does not match resource, and a directory is defined</span><a name="l00220"></a>00220 <a name="l00221"></a>00221 <span class="comment">// calculate the location of the file being requested</span><a name="l00222"></a>00222 response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a> = m_directory;<a name="l00223"></a>00223 response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a> /= relative_path;<a name="l00224"></a>00224 <a name="l00225"></a>00225 <span class="comment">// make sure that the file exists</span><a name="l00226"></a>00226 if (! boost::filesystem::exists(response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a>) ) {<a name="l00227"></a>00227 PION_LOG_WARN(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"File not found ("</span><a name="l00228"></a>00228 << <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>() << <span class="stringliteral">"): "</span> << relative_path);<a name="l00229"></a>00229 <span class="keywordflow">return</span> <span class="keyword">false</span>;<a name="l00230"></a>00230 }<a name="l00231"></a>00231 <a name="l00232"></a>00232 <span class="comment">// make sure that it is not a directory</span><a name="l00233"></a>00233 <span class="keywordflow">if</span> ( boost::filesystem::is_directory(response_file.<a class="code" href="struct_file_module_1_1_disk_file.html#41a8575deadf1d75d6aaeb76c057300c" title="path to the cached file">file_path</a>) ) {<a name="l00234"></a>00234 PION_LOG_WARN(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Request for directory ("</span><a name="l00235"></a>00235 << <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>() << <span class="stringliteral">"): "</span> << relative_path);<a name="l00236"></a>00236 <span class="keywordflow">return</span> <span class="keyword">false</span>;<a name="l00237"></a>00237 }<a name="l00238"></a>00238 <a name="l00239"></a>00239 <span class="comment">// make sure that the file is within the configured directory</span><a name="l00240"></a>00240 std::string file_string = response_file.<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="l00241"></a>00241 <span class="keywordflow">if</span> (file_string.find(m_directory.directory_string()) != 0) {<a name="l00242"></a>00242 PION_LOG_WARN(<a class="code" href="class_file_module.html#789b2863d2937993e7152aa02c55f0ac" title="primary logging interface used by this class">m_logger</a>, <span class="stringliteral">"Request for file outside of directory ("</span><a name="l00243"></a>00243 << <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>() << <span class="stringliteral">"): "</span> << relative_path);<a name="l00244"></a>00244 <span class="keywordflow">return</span> <span class="keyword">false</span>;<a name="l00245"></a>00245 }<a name="l00246"></a>00246 <a name="l00247"></a>00247 } <span class="keywordflow">else</span> {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -