378 lines
16 KiB
HTML
378 lines
16 KiB
HTML
|
<?xml version="1.0" encoding="utf-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
|
||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||
|
<head>
|
||
|
<title>Class: Rack::File</title>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
||
|
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
||
|
<script type="text/javascript">
|
||
|
// <![CDATA[
|
||
|
|
||
|
function popupCode( url ) {
|
||
|
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
||
|
}
|
||
|
|
||
|
function toggleCode( id ) {
|
||
|
if ( document.getElementById )
|
||
|
elem = document.getElementById( id );
|
||
|
else if ( document.all )
|
||
|
elem = eval( "document.all." + id );
|
||
|
else
|
||
|
return false;
|
||
|
|
||
|
elemStyle = elem.style;
|
||
|
|
||
|
if ( elemStyle.display != "block" ) {
|
||
|
elemStyle.display = "block"
|
||
|
} else {
|
||
|
elemStyle.display = "none"
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// Make codeblocks hidden by default
|
||
|
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
||
|
|
||
|
// ]]>
|
||
|
</script>
|
||
|
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
|
||
|
|
||
|
<div id="classHeader">
|
||
|
<table class="header-table">
|
||
|
<tr class="top-aligned-row">
|
||
|
<td><strong>Class</strong></td>
|
||
|
<td class="class-name-in-header">Rack::File</td>
|
||
|
</tr>
|
||
|
<tr class="top-aligned-row">
|
||
|
<td><strong>In:</strong></td>
|
||
|
<td>
|
||
|
<a href="../../files/lib/rack/file_rb.html">
|
||
|
lib/rack/file.rb
|
||
|
</a>
|
||
|
<br />
|
||
|
</td>
|
||
|
</tr>
|
||
|
|
||
|
<tr class="top-aligned-row">
|
||
|
<td><strong>Parent:</strong></td>
|
||
|
<td>
|
||
|
Object
|
||
|
</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div>
|
||
|
<!-- banner header -->
|
||
|
|
||
|
<div id="bodyContent">
|
||
|
|
||
|
|
||
|
|
||
|
<div id="contextContent">
|
||
|
|
||
|
<div id="description">
|
||
|
<p>
|
||
|
<a href="File.html">Rack::File</a> serves files below the <tt>root</tt>
|
||
|
given, according to the path info of the <a href="../Rack.html">Rack</a>
|
||
|
request.
|
||
|
</p>
|
||
|
<p>
|
||
|
Handlers can detect if bodies are a <a href="File.html">Rack::File</a>, and
|
||
|
use mechanisms like sendfile on the <tt>path</tt>.
|
||
|
</p>
|
||
|
|
||
|
</div>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
|
||
|
<div id="method-list">
|
||
|
<h3 class="section-bar">Methods</h3>
|
||
|
|
||
|
<div class="name-list">
|
||
|
<a href="#M000206">_call</a>
|
||
|
<a href="#M000205">call</a>
|
||
|
<a href="#M000210">each</a>
|
||
|
<a href="#M000207">forbidden</a>
|
||
|
<a href="#M000204">new</a>
|
||
|
<a href="#M000209">not_found</a>
|
||
|
<a href="#M000208">serving</a>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
</div>
|
||
|
|
||
|
|
||
|
<!-- if includes -->
|
||
|
|
||
|
<div id="section">
|
||
|
|
||
|
|
||
|
<div id="constants-list">
|
||
|
<h3 class="section-bar">Constants</h3>
|
||
|
|
||
|
<div class="name-list">
|
||
|
<table summary="Constants">
|
||
|
<tr class="top-aligned-row context-row">
|
||
|
<td class="context-item-name">F</td>
|
||
|
<td>=</td>
|
||
|
<td class="context-item-value">::File</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
|
||
|
<div id="attribute-list">
|
||
|
<h3 class="section-bar">Attributes</h3>
|
||
|
|
||
|
<div class="name-list">
|
||
|
<table>
|
||
|
<tr class="top-aligned-row context-row">
|
||
|
<td class="context-item-name">path</td>
|
||
|
<td class="context-item-value"> [RW] </td>
|
||
|
<td class="context-item-desc"></td>
|
||
|
</tr>
|
||
|
<tr class="top-aligned-row context-row">
|
||
|
<td class="context-item-name">root</td>
|
||
|
<td class="context-item-value"> [RW] </td>
|
||
|
<td class="context-item-desc"></td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
|
||
|
<!-- if method_list -->
|
||
|
<div id="methods">
|
||
|
<h3 class="section-bar">Public Class methods</h3>
|
||
|
|
||
|
<div id="method-M000204" class="method-detail">
|
||
|
<a name="M000204"></a>
|
||
|
|
||
|
<div class="method-heading">
|
||
|
<a href="#M000204" class="method-signature">
|
||
|
<span class="method-name">new</span><span class="method-args">(root)</span>
|
||
|
</a>
|
||
|
</div>
|
||
|
|
||
|
<div class="method-description">
|
||
|
<p><a class="source-toggle" href="#"
|
||
|
onclick="toggleCode('M000204-source');return false;">[Source]</a></p>
|
||
|
<div class="method-source-code" id="M000204-source">
|
||
|
<pre>
|
||
|
<span class="ruby-comment cmt"># File lib/rack/file.rb, line 15</span>
|
||
|
15: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">root</span>)
|
||
|
16: <span class="ruby-ivar">@root</span> = <span class="ruby-identifier">root</span>
|
||
|
17: <span class="ruby-keyword kw">end</span>
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<h3 class="section-bar">Public Instance methods</h3>
|
||
|
|
||
|
<div id="method-M000206" class="method-detail">
|
||
|
<a name="M000206"></a>
|
||
|
|
||
|
<div class="method-heading">
|
||
|
<a href="#M000206" class="method-signature">
|
||
|
<span class="method-name">_call</span><span class="method-args">(env)</span>
|
||
|
</a>
|
||
|
</div>
|
||
|
|
||
|
<div class="method-description">
|
||
|
<p><a class="source-toggle" href="#"
|
||
|
onclick="toggleCode('M000206-source');return false;">[Source]</a></p>
|
||
|
<div class="method-source-code" id="M000206-source">
|
||
|
<pre>
|
||
|
<span class="ruby-comment cmt"># File lib/rack/file.rb, line 25</span>
|
||
|
25: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">_call</span>(<span class="ruby-identifier">env</span>)
|
||
|
26: <span class="ruby-ivar">@path_info</span> = <span class="ruby-constant">Utils</span>.<span class="ruby-identifier">unescape</span>(<span class="ruby-identifier">env</span>[<span class="ruby-value str">"PATH_INFO"</span>])
|
||
|
27: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">forbidden</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@path_info</span>.<span class="ruby-identifier">include?</span> <span class="ruby-value str">".."</span>
|
||
|
28:
|
||
|
29: <span class="ruby-ivar">@path</span> = <span class="ruby-constant">F</span>.<span class="ruby-identifier">join</span>(<span class="ruby-ivar">@root</span>, <span class="ruby-ivar">@path_info</span>)
|
||
|
30:
|
||
|
31: <span class="ruby-keyword kw">begin</span>
|
||
|
32: <span class="ruby-keyword kw">if</span> <span class="ruby-constant">F</span>.<span class="ruby-identifier">file?</span>(<span class="ruby-ivar">@path</span>) <span class="ruby-operator">&&</span> <span class="ruby-constant">F</span>.<span class="ruby-identifier">readable?</span>(<span class="ruby-ivar">@path</span>)
|
||
|
33: <span class="ruby-identifier">serving</span>
|
||
|
34: <span class="ruby-keyword kw">else</span>
|
||
|
35: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EPERM</span>
|
||
|
36: <span class="ruby-keyword kw">end</span>
|
||
|
37: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">SystemCallError</span>
|
||
|
38: <span class="ruby-identifier">not_found</span>
|
||
|
39: <span class="ruby-keyword kw">end</span>
|
||
|
40: <span class="ruby-keyword kw">end</span>
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div id="method-M000205" class="method-detail">
|
||
|
<a name="M000205"></a>
|
||
|
|
||
|
<div class="method-heading">
|
||
|
<a href="#M000205" class="method-signature">
|
||
|
<span class="method-name">call</span><span class="method-args">(env)</span>
|
||
|
</a>
|
||
|
</div>
|
||
|
|
||
|
<div class="method-description">
|
||
|
<p><a class="source-toggle" href="#"
|
||
|
onclick="toggleCode('M000205-source');return false;">[Source]</a></p>
|
||
|
<div class="method-source-code" id="M000205-source">
|
||
|
<pre>
|
||
|
<span class="ruby-comment cmt"># File lib/rack/file.rb, line 19</span>
|
||
|
19: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>)
|
||
|
20: <span class="ruby-identifier">dup</span>.<span class="ruby-identifier">_call</span>(<span class="ruby-identifier">env</span>)
|
||
|
21: <span class="ruby-keyword kw">end</span>
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div id="method-M000210" class="method-detail">
|
||
|
<a name="M000210"></a>
|
||
|
|
||
|
<div class="method-heading">
|
||
|
<a href="#M000210" class="method-signature">
|
||
|
<span class="method-name">each</span><span class="method-args">() {|part| ...}</span>
|
||
|
</a>
|
||
|
</div>
|
||
|
|
||
|
<div class="method-description">
|
||
|
<p><a class="source-toggle" href="#"
|
||
|
onclick="toggleCode('M000210-source');return false;">[Source]</a></p>
|
||
|
<div class="method-source-code" id="M000210-source">
|
||
|
<pre>
|
||
|
<span class="ruby-comment cmt"># File lib/rack/file.rb, line 77</span>
|
||
|
77: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">each</span>
|
||
|
78: <span class="ruby-constant">F</span>.<span class="ruby-identifier">open</span>(<span class="ruby-ivar">@path</span>, <span class="ruby-value str">"rb"</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
|
||
|
79: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">part</span> = <span class="ruby-identifier">file</span>.<span class="ruby-identifier">read</span>(<span class="ruby-value">8192</span>)
|
||
|
80: <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">part</span>
|
||
|
81: <span class="ruby-keyword kw">end</span>
|
||
|
82: }
|
||
|
83: <span class="ruby-keyword kw">end</span>
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div id="method-M000207" class="method-detail">
|
||
|
<a name="M000207"></a>
|
||
|
|
||
|
<div class="method-heading">
|
||
|
<a href="#M000207" class="method-signature">
|
||
|
<span class="method-name">forbidden</span><span class="method-args">()</span>
|
||
|
</a>
|
||
|
</div>
|
||
|
|
||
|
<div class="method-description">
|
||
|
<p><a class="source-toggle" href="#"
|
||
|
onclick="toggleCode('M000207-source');return false;">[Source]</a></p>
|
||
|
<div class="method-source-code" id="M000207-source">
|
||
|
<pre>
|
||
|
<span class="ruby-comment cmt"># File lib/rack/file.rb, line 42</span>
|
||
|
42: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">forbidden</span>
|
||
|
43: <span class="ruby-identifier">body</span> = <span class="ruby-value str">"Forbidden\n"</span>
|
||
|
44: [<span class="ruby-value">403</span>, {<span class="ruby-value str">"Content-Type"</span> =<span class="ruby-operator">></span> <span class="ruby-value str">"text/plain"</span>,
|
||
|
45: <span class="ruby-value str">"Content-Length"</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">body</span>.<span class="ruby-identifier">size</span>.<span class="ruby-identifier">to_s</span>},
|
||
|
46: [<span class="ruby-identifier">body</span>]]
|
||
|
47: <span class="ruby-keyword kw">end</span>
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div id="method-M000209" class="method-detail">
|
||
|
<a name="M000209"></a>
|
||
|
|
||
|
<div class="method-heading">
|
||
|
<a href="#M000209" class="method-signature">
|
||
|
<span class="method-name">not_found</span><span class="method-args">()</span>
|
||
|
</a>
|
||
|
</div>
|
||
|
|
||
|
<div class="method-description">
|
||
|
<p><a class="source-toggle" href="#"
|
||
|
onclick="toggleCode('M000209-source');return false;">[Source]</a></p>
|
||
|
<div class="method-source-code" id="M000209-source">
|
||
|
<pre>
|
||
|
<span class="ruby-comment cmt"># File lib/rack/file.rb, line 70</span>
|
||
|
70: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">not_found</span>
|
||
|
71: <span class="ruby-identifier">body</span> = <span class="ruby-node">"File not found: #{@path_info}\n"</span>
|
||
|
72: [<span class="ruby-value">404</span>, {<span class="ruby-value str">"Content-Type"</span> =<span class="ruby-operator">></span> <span class="ruby-value str">"text/plain"</span>,
|
||
|
73: <span class="ruby-value str">"Content-Length"</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">body</span>.<span class="ruby-identifier">size</span>.<span class="ruby-identifier">to_s</span>},
|
||
|
74: [<span class="ruby-identifier">body</span>]]
|
||
|
75: <span class="ruby-keyword kw">end</span>
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div id="method-M000208" class="method-detail">
|
||
|
<a name="M000208"></a>
|
||
|
|
||
|
<div class="method-heading">
|
||
|
<a href="#M000208" class="method-signature">
|
||
|
<span class="method-name">serving</span><span class="method-args">()</span>
|
||
|
</a>
|
||
|
</div>
|
||
|
|
||
|
<div class="method-description">
|
||
|
<p>
|
||
|
NOTE:
|
||
|
</p>
|
||
|
<pre>
|
||
|
We check via File::size? whether this file provides size info
|
||
|
via stat (e.g. /proc files often don't), otherwise we have to
|
||
|
figure it out by reading the whole file into memory. And while
|
||
|
we're at it we also use this as body then.
|
||
|
</pre>
|
||
|
<p><a class="source-toggle" href="#"
|
||
|
onclick="toggleCode('M000208-source');return false;">[Source]</a></p>
|
||
|
<div class="method-source-code" id="M000208-source">
|
||
|
<pre>
|
||
|
<span class="ruby-comment cmt"># File lib/rack/file.rb, line 55</span>
|
||
|
55: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">serving</span>
|
||
|
56: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">size</span> = <span class="ruby-constant">F</span>.<span class="ruby-identifier">size?</span>(<span class="ruby-ivar">@path</span>)
|
||
|
57: <span class="ruby-identifier">body</span> = <span class="ruby-keyword kw">self</span>
|
||
|
58: <span class="ruby-keyword kw">else</span>
|
||
|
59: <span class="ruby-identifier">body</span> = [<span class="ruby-constant">F</span>.<span class="ruby-identifier">read</span>(<span class="ruby-ivar">@path</span>)]
|
||
|
60: <span class="ruby-identifier">size</span> = <span class="ruby-identifier">body</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">size</span>
|
||
|
61: <span class="ruby-keyword kw">end</span>
|
||
|
62:
|
||
|
63: [<span class="ruby-value">200</span>, {
|
||
|
64: <span class="ruby-value str">"Last-Modified"</span> =<span class="ruby-operator">></span> <span class="ruby-constant">F</span>.<span class="ruby-identifier">mtime</span>(<span class="ruby-ivar">@path</span>).<span class="ruby-identifier">httpdate</span>,
|
||
|
65: <span class="ruby-value str">"Content-Type"</span> =<span class="ruby-operator">></span> <span class="ruby-constant">Mime</span>.<span class="ruby-identifier">mime_type</span>(<span class="ruby-constant">F</span>.<span class="ruby-identifier">extname</span>(<span class="ruby-ivar">@path</span>), <span class="ruby-value str">'text/plain'</span>),
|
||
|
66: <span class="ruby-value str">"Content-Length"</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">size</span>.<span class="ruby-identifier">to_s</span>
|
||
|
67: }, <span class="ruby-identifier">body</span>]
|
||
|
68: <span class="ruby-keyword kw">end</span>
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
|
||
|
|
||
|
<div id="validator-badges">
|
||
|
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
||
|
</div>
|
||
|
|
||
|
</body>
|
||
|
</html>
|