D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
cpanel
/
ea-ruby27
/
root
/
usr
/
share
/
gems
/
doc
/
rack-2.2.10
/
rdoc
/
Rack
/
Filename :
Directory.html
back
Copy
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>class Rack::Directory - rack-2.2.10 Documentation</title> <script type="text/javascript"> var rdoc_rel_prefix = "../"; var index_rel_prefix = "../"; </script> <script src="../js/navigation.js" defer></script> <script src="../js/search.js" defer></script> <script src="../js/search_index.js" defer></script> <script src="../js/searcher.js" defer></script> <script src="../js/darkfish.js" defer></script> <link href="../css/fonts.css" rel="stylesheet"> <link href="../css/rdoc.css" rel="stylesheet"> <body id="top" role="document" class="class"> <nav role="navigation"> <div id="project-navigation"> <div id="home-section" role="region" title="Quick navigation" class="nav-section"> <h2> <a href="../index.html" rel="home">Home</a> </h2> <div id="table-of-contents-navigation"> <a href="../table_of_contents.html#pages">Pages</a> <a href="../table_of_contents.html#classes">Classes</a> <a href="../table_of_contents.html#methods">Methods</a> </div> </div> <div id="search-section" role="search" class="project-section initially-hidden"> <form action="#" method="get" accept-charset="utf-8"> <div id="search-field-wrapper"> <input id="search-field" role="combobox" aria-label="Search" aria-autocomplete="list" aria-controls="search-results" type="text" name="search" placeholder="Search" spellcheck="false" title="Type to search, Up and Down to navigate, Enter to load"> </div> <ul id="search-results" aria-label="Search Results" aria-busy="false" aria-expanded="false" aria-atomic="false" class="initially-hidden"></ul> </form> </div> </div> <div id="class-metadata"> <div id="parent-class-section" class="nav-section"> <h3>Parent</h3> <p class="link">Object </div> <!-- Method Quickref --> <div id="method-list-section" class="nav-section"> <h3>Methods</h3> <ul class="link-list" role="directory"> <li ><a href="#method-c-new">::new</a> <li ><a href="#method-i-call">#call</a> <li ><a href="#method-i-check_bad_request">#check_bad_request</a> <li ><a href="#method-i-check_forbidden">#check_forbidden</a> <li ><a href="#method-i-entity_not_found">#entity_not_found</a> <li ><a href="#method-i-filesize_format">#filesize_format</a> <li ><a href="#method-i-get">#get</a> <li ><a href="#method-i-list_directory">#list_directory</a> <li ><a href="#method-i-list_path">#list_path</a> <li ><a href="#method-i-stat">#stat</a> </ul> </div> </div> </nav> <main role="main" aria-labelledby="class-Rack::Directory"> <h1 id="class-Rack::Directory" class="class"> class Rack::Directory </h1> <section class="description"> <p><a href="Directory.html"><code>Rack::Directory</code></a> serves entries below the <code>root</code> given, according to the path info of the <a href="../Rack.html"><code>Rack</code></a> request. If a directory is found, the file's contents will be presented in an html based index. If a file is found, the env will be passed to the specified <code>app</code>.</p> <p>If <code>app</code> is not specified, a <a href="Files.html"><code>Rack::Files</code></a> of the same <code>root</code> will be used.</p> </section> <section id="5Buntitled-5D" class="documentation-section"> <section class="constants-list"> <header> <h3>Constants</h3> </header> <dl> <dt id="DIR_FILE">DIR_FILE <dd> <dt id="DIR_PAGE_FOOTER">DIR_PAGE_FOOTER <dd> <dt id="DIR_PAGE_HEADER">DIR_PAGE_HEADER <dd> <dt id="FILESIZE_FORMAT">FILESIZE_FORMAT <dd><p>Stolen from Ramaze</p> </dl> </section> <section class="attribute-method-details" class="method-section"> <header> <h3>Attributes</h3> </header> <div id="attribute-i-root" class="method-detail"> <div class="method-heading attribute-method-heading"> <span class="method-name">root</span><span class="attribute-access-type">[R]</span> </div> <div class="method-description"> <p>The root of the directory hierarchy. Only requests for files and directories inside of the root directory are supported.</p> </div> </div> </section> <section id="public-class-5Buntitled-5D-method-details" class="method-section"> <header> <h3>Public Class Methods</h3> </header> <div id="method-c-new" class="method-detail "> <div class="method-heading"> <span class="method-name">new</span><span class="method-args">(root, app = nil)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Set the root directory and application for serving files.</p> <div class="method-source-code" id="new-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 77</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">root</span>, <span class="ruby-identifier">app</span> = <span class="ruby-keyword">nil</span>) <span class="ruby-ivar">@root</span> = <span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">expand_path</span>(<span class="ruby-identifier">root</span>) <span class="ruby-ivar">@app</span> = <span class="ruby-identifier">app</span> <span class="ruby-operator">||</span> <span class="ruby-constant">Files</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@root</span>) <span class="ruby-ivar">@head</span> = <span class="ruby-constant">Head</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">method</span>(<span class="ruby-value">:get</span>)) <span class="ruby-keyword">end</span></pre> </div> </div> </div> </section> <section id="public-instance-5Buntitled-5D-method-details" class="method-section"> <header> <h3>Public Instance Methods</h3> </header> <div id="method-i-call" class="method-detail "> <div class="method-heading"> <span class="method-name">call</span><span class="method-args">(env)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="call-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 83</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">call</span>(<span class="ruby-identifier">env</span>) <span class="ruby-comment"># strip body if this is a HEAD call</span> <span class="ruby-ivar">@head</span>.<span class="ruby-identifier">call</span> <span class="ruby-identifier">env</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-check_bad_request" class="method-detail "> <div class="method-heading"> <span class="method-name">check_bad_request</span><span class="method-args">(path_info)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p><a href="../Rack.html"><code>Rack</code></a> response to use for requests with invalid paths, or nil if path is valid.</p> <div class="method-source-code" id="check_bad_request-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 103</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">check_bad_request</span>(<span class="ruby-identifier">path_info</span>) <span class="ruby-keyword">return</span> <span class="ruby-keyword">if</span> <span class="ruby-constant">Utils</span>.<span class="ruby-identifier">valid_path?</span>(<span class="ruby-identifier">path_info</span>) <span class="ruby-identifier">body</span> = <span class="ruby-string">"Bad Request\n"</span> [<span class="ruby-value">400</span>, { <span class="ruby-constant">CONTENT_TYPE</span> <span class="ruby-operator">=></span> <span class="ruby-string">"text/plain"</span>, <span class="ruby-constant">CONTENT_LENGTH</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">body</span>.<span class="ruby-identifier">bytesize</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-string">"X-Cascade"</span> <span class="ruby-operator">=></span> <span class="ruby-string">"pass"</span> }, [<span class="ruby-identifier">body</span>]] <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-check_forbidden" class="method-detail "> <div class="method-heading"> <span class="method-name">check_forbidden</span><span class="method-args">(path_info)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p><a href="../Rack.html"><code>Rack</code></a> response to use for requests with paths outside the root, or nil if path is inside the root.</p> <div class="method-source-code" id="check_forbidden-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 113</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">check_forbidden</span>(<span class="ruby-identifier">path_info</span>) <span class="ruby-keyword">return</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">path_info</span>.<span class="ruby-identifier">include?</span> <span class="ruby-string">".."</span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">if</span> <span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">expand_path</span>(<span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-ivar">@root</span>, <span class="ruby-identifier">path_info</span>)).<span class="ruby-identifier">start_with?</span>(<span class="ruby-ivar">@root</span>) <span class="ruby-identifier">body</span> = <span class="ruby-string">"Forbidden\n"</span> [<span class="ruby-value">403</span>, { <span class="ruby-constant">CONTENT_TYPE</span> <span class="ruby-operator">=></span> <span class="ruby-string">"text/plain"</span>, <span class="ruby-constant">CONTENT_LENGTH</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">body</span>.<span class="ruby-identifier">bytesize</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-string">"X-Cascade"</span> <span class="ruby-operator">=></span> <span class="ruby-string">"pass"</span> }, [<span class="ruby-identifier">body</span>]] <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-entity_not_found" class="method-detail "> <div class="method-heading"> <span class="method-name">entity_not_found</span><span class="method-args">(path_info)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p><a href="../Rack.html"><code>Rack</code></a> response to use for unreadable and non-file, non-directory entries.</p> <div class="method-source-code" id="entity_not_found-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 175</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">entity_not_found</span>(<span class="ruby-identifier">path_info</span>) <span class="ruby-identifier">body</span> = <span class="ruby-node">"Entity not found: #{path_info}\n"</span> [<span class="ruby-value">404</span>, { <span class="ruby-constant">CONTENT_TYPE</span> <span class="ruby-operator">=></span> <span class="ruby-string">"text/plain"</span>, <span class="ruby-constant">CONTENT_LENGTH</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">body</span>.<span class="ruby-identifier">bytesize</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-string">"X-Cascade"</span> <span class="ruby-operator">=></span> <span class="ruby-string">"pass"</span> }, [<span class="ruby-identifier">body</span>]] <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-filesize_format" class="method-detail "> <div class="method-heading"> <span class="method-name">filesize_format</span><span class="method-args">(int)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Provide human readable file sizes</p> <div class="method-source-code" id="filesize_format-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 191</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">filesize_format</span>(<span class="ruby-identifier">int</span>) <span class="ruby-constant">FILESIZE_FORMAT</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">format</span>, <span class="ruby-identifier">size</span><span class="ruby-operator">|</span> <span class="ruby-keyword">return</span> <span class="ruby-identifier">format</span> <span class="ruby-operator">%</span> (<span class="ruby-identifier">int</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-identifier">size</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">int</span> <span class="ruby-operator">>=</span> <span class="ruby-identifier">size</span> <span class="ruby-keyword">end</span> <span class="ruby-node">"#{int}B"</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-get" class="method-detail "> <div class="method-heading"> <span class="method-name">get</span><span class="method-args">(env)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Internals of request handling. Similar to call but does not remove body for HEAD requests.</p> <div class="method-source-code" id="get-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 90</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get</span>(<span class="ruby-identifier">env</span>) <span class="ruby-identifier">script_name</span> = <span class="ruby-identifier">env</span>[<span class="ruby-constant">SCRIPT_NAME</span>] <span class="ruby-identifier">path_info</span> = <span class="ruby-constant">Utils</span>.<span class="ruby-identifier">unescape_path</span>(<span class="ruby-identifier">env</span>[<span class="ruby-constant">PATH_INFO</span>]) <span class="ruby-keyword">if</span> <span class="ruby-identifier">client_error_response</span> = <span class="ruby-identifier">check_bad_request</span>(<span class="ruby-identifier">path_info</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">check_forbidden</span>(<span class="ruby-identifier">path_info</span>) <span class="ruby-identifier">client_error_response</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">path</span> = <span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-ivar">@root</span>, <span class="ruby-identifier">path_info</span>) <span class="ruby-identifier">list_path</span>(<span class="ruby-identifier">env</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">path_info</span>, <span class="ruby-identifier">script_name</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-list_directory" class="method-detail "> <div class="method-heading"> <span class="method-name">list_directory</span><span class="method-args">(path_info, path, script_name)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p><a href="../Rack.html"><code>Rack</code></a> response to use for directories under the root.</p> <div class="method-source-code" id="list_directory-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 124</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">list_directory</span>(<span class="ruby-identifier">path_info</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">script_name</span>) <span class="ruby-identifier">url_head</span> = (<span class="ruby-identifier">script_name</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">'/'</span>) <span class="ruby-operator">+</span> <span class="ruby-identifier">path_info</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">'/'</span>)).<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">part</span><span class="ruby-operator">|</span> <span class="ruby-constant">Utils</span>.<span class="ruby-identifier">escape_path</span> <span class="ruby-identifier">part</span> <span class="ruby-keyword">end</span> <span class="ruby-comment"># Globbing not safe as path could contain glob metacharacters</span> <span class="ruby-identifier">body</span> = <span class="ruby-constant">DirectoryBody</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@root</span>, <span class="ruby-identifier">path</span>, <span class="ruby-operator">-></span>(<span class="ruby-identifier">basename</span>) <span class="ruby-keyword">do</span> <span class="ruby-identifier">stat</span> = <span class="ruby-identifier">stat</span>(<span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">basename</span>)) <span class="ruby-keyword">next</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">stat</span> <span class="ruby-identifier">url</span> = <span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">url_head</span> <span class="ruby-operator">+</span> [<span class="ruby-constant">Utils</span>.<span class="ruby-identifier">escape_path</span>(<span class="ruby-identifier">basename</span>)]) <span class="ruby-identifier">mtime</span> = <span class="ruby-identifier">stat</span>.<span class="ruby-identifier">mtime</span>.<span class="ruby-identifier">httpdate</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">stat</span>.<span class="ruby-identifier">directory?</span> <span class="ruby-identifier">type</span> = <span class="ruby-string">'directory'</span> <span class="ruby-identifier">size</span> = <span class="ruby-string">'-'</span> <span class="ruby-identifier">url</span> <span class="ruby-operator"><<</span> <span class="ruby-string">'/'</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">basename</span> <span class="ruby-operator">==</span> <span class="ruby-string">'..'</span> <span class="ruby-identifier">basename</span> = <span class="ruby-string">'Parent Directory'</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">basename</span> <span class="ruby-operator"><<</span> <span class="ruby-string">'/'</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">type</span> = <span class="ruby-constant">Mime</span>.<span class="ruby-identifier">mime_type</span>(<span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">extname</span>(<span class="ruby-identifier">basename</span>)) <span class="ruby-identifier">size</span> = <span class="ruby-identifier">filesize_format</span>(<span class="ruby-identifier">stat</span>.<span class="ruby-identifier">size</span>) <span class="ruby-keyword">end</span> [ <span class="ruby-identifier">url</span>, <span class="ruby-identifier">basename</span>, <span class="ruby-identifier">size</span>, <span class="ruby-identifier">type</span>, <span class="ruby-identifier">mtime</span> ] <span class="ruby-keyword">end</span>) [ <span class="ruby-value">200</span>, { <span class="ruby-constant">CONTENT_TYPE</span> <span class="ruby-operator">=></span> <span class="ruby-string">'text/html; charset=utf-8'</span> }, <span class="ruby-identifier">body</span> ] <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-list_path" class="method-detail "> <div class="method-heading"> <span class="method-name">list_path</span><span class="method-args">(env, path, path_info, script_name)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p><a href="../Rack.html"><code>Rack</code></a> response to use for files and directories under the root. Unreadable and non-file, non-directory entries will get a 404 response.</p> <div class="method-source-code" id="list_path-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 165</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">list_path</span>(<span class="ruby-identifier">env</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">path_info</span>, <span class="ruby-identifier">script_name</span>) <span class="ruby-keyword">if</span> (<span class="ruby-identifier">stat</span> = <span class="ruby-identifier">stat</span>(<span class="ruby-identifier">path</span>)) <span class="ruby-operator">&&</span> <span class="ruby-identifier">stat</span>.<span class="ruby-identifier">readable?</span> <span class="ruby-keyword">return</span> <span class="ruby-ivar">@app</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">stat</span>.<span class="ruby-identifier">file?</span> <span class="ruby-keyword">return</span> <span class="ruby-identifier">list_directory</span>(<span class="ruby-identifier">path_info</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">script_name</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">stat</span>.<span class="ruby-identifier">directory?</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">entity_not_found</span>(<span class="ruby-identifier">path_info</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-stat" class="method-detail "> <div class="method-heading"> <span class="method-name">stat</span><span class="method-args">(path)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>File::Stat for the given path, but return nil for missing/bad entries.</p> <div class="method-source-code" id="stat-source"> <pre><span class="ruby-comment"># File lib/rack/directory.rb, line 157</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">stat</span>(<span class="ruby-identifier">path</span>) <span class="ruby-operator">::</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">stat</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">ENOENT</span>, <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">ELOOP</span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> </section> </section> </main> <footer id="validator-badges" role="contentinfo"> <p><a href="https://validator.w3.org/check/referer">Validate</a> <p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1. <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>. </footer>