AlkantarClanX12
Current Path : /proc/self/root/opt/cpanel/ea-ruby27/root/usr/share/gems/doc/rack-2.2.10/rdoc/Rack/ |
Current File : //proc/self/root/opt/cpanel/ea-ruby27/root/usr/share/gems/doc/rack-2.2.10/rdoc/Rack/Static.html |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>class Rack::Static - 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-add_index_root-3F">#add_index_root?</a> <li ><a href="#method-i-applicable_rules">#applicable_rules</a> <li ><a href="#method-i-call">#call</a> <li ><a href="#method-i-can_serve">#can_serve</a> <li ><a href="#method-i-overwrite_file_path">#overwrite_file_path</a> <li ><a href="#method-i-route_file">#route_file</a> </ul> </div> </div> </nav> <main role="main" aria-labelledby="class-Rack::Static"> <h1 id="class-Rack::Static" class="class"> class Rack::Static </h1> <section class="description"> <p>The <a href="Static.html"><code>Rack::Static</code></a> middleware intercepts requests for static files (javascript files, images, stylesheets, etc) based on the url prefixes or route mappings passed in the options, and serves them using a <a href="Files.html"><code>Rack::Files</code></a> object. This allows a <a href="../Rack.html"><code>Rack</code></a> stack to serve both static and dynamic content.</p> <p>Examples:</p> <p>Serve all requests beginning with /media from the “media” folder located in the current directory (ie media/*):</p> <pre class="ruby"><span class="ruby-identifier">use</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Static</span>, <span class="ruby-value">:urls</span> <span class="ruby-operator">=></span> [<span class="ruby-string">"/media"</span>] </pre> <p>Same as previous, but instead of returning 404 for missing files under /media, call the next middleware:</p> <pre class="ruby"><span class="ruby-identifier">use</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Static</span>, <span class="ruby-value">:urls</span> <span class="ruby-operator">=></span> [<span class="ruby-string">"/media"</span>], <span class="ruby-value">:cascade</span> <span class="ruby-operator">=></span> <span class="ruby-keyword">true</span> </pre> <p>Serve all requests beginning with /css or /images from the folder “public” in the current directory (ie public/css/* and public/images/*):</p> <pre class="ruby"><span class="ruby-identifier">use</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Static</span>, <span class="ruby-value">:urls</span> <span class="ruby-operator">=></span> [<span class="ruby-string">"/css"</span>, <span class="ruby-string">"/images"</span>], <span class="ruby-value">:root</span> <span class="ruby-operator">=></span> <span class="ruby-string">"public"</span> </pre> <p>Serve all requests to / with “index.html” from the folder “public” in the current directory (ie public/index.html):</p> <pre class="ruby"><span class="ruby-identifier">use</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Static</span>, <span class="ruby-value">:urls</span> <span class="ruby-operator">=></span> {<span class="ruby-string">"/"</span> <span class="ruby-operator">=></span> <span class="ruby-string">'index.html'</span>}, <span class="ruby-value">:root</span> <span class="ruby-operator">=></span> <span class="ruby-string">'public'</span> </pre> <p>Serve all requests normally from the folder “public” in the current directory but uses index.html as default route for “/”</p> <pre class="ruby"><span class="ruby-identifier">use</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Static</span>, <span class="ruby-value">:urls</span> <span class="ruby-operator">=></span> [<span class="ruby-string">""</span>], <span class="ruby-value">:root</span> <span class="ruby-operator">=></span> <span class="ruby-string">'public'</span>, <span class="ruby-value">:index</span> <span class="ruby-operator">=></span> <span class="ruby-string">'index.html'</span> </pre> <p>Set custom HTTP Headers for based on rules:</p> <pre> use Rack::Static, :root => 'public', :header_rules => [ [rule, {header_field => content, header_field => content}], [rule, {header_field => content}] ] Rules for selecting files: 1) All files Provide the :all symbol :all => Matches every file 2) Folders Provide the folder path as a string '/folder' or '/folder/subfolder' => Matches files in a certain folder 3) File Extensions Provide the file extensions as an array ['css', 'js'] or %w(css js) => Matches files ending in .css or .js 4) Regular Expressions / Regexp Provide a regular expression %r{\.(?:css|js)\z} => Matches files ending in .css or .js /\.(?:eot|ttf|otf|woff2|woff|svg)\z/ => Matches files ending in the most common web font formats (.eot, .ttf, .otf, .woff2, .woff, .svg) Note: This Regexp is available as a shortcut, using the :fonts rule 5) Font Shortcut Provide the :fonts symbol :fonts => Uses the Regexp rule stated right above to match all common web font endings Rule Ordering: Rules are applied in the order that they are provided. List rather general rules above special ones. Complete example use case including HTTP header rules: use Rack::Static, :root => 'public', :header_rules => [ # Cache all static files in public caches (e.g. Rack::Cache) # as well as in the browser [:all, {'Cache-Control' => 'public, max-age=31536000'}], # Provide web fonts with cross-origin access-control-headers # Firefox requires this when serving assets using a Content Delivery Network [:fonts, {'Access-Control-Allow-Origin' => '*'}] ]</pre> </section> <section id="5Buntitled-5D" class="documentation-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">(app, options = {})</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="new-source"> <pre><span class="ruby-comment"># File lib/rack/static.rb, line 91</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">app</span>, <span class="ruby-identifier">options</span> = {}) <span class="ruby-ivar">@app</span> = <span class="ruby-identifier">app</span> <span class="ruby-ivar">@urls</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:urls</span>] <span class="ruby-operator">||</span> [<span class="ruby-string">"/favicon.ico"</span>] <span class="ruby-ivar">@index</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:index</span>] <span class="ruby-ivar">@gzip</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:gzip</span>] <span class="ruby-ivar">@cascade</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:cascade</span>] <span class="ruby-identifier">root</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:root</span>] <span class="ruby-operator">||</span> <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">pwd</span> <span class="ruby-comment"># HTTP Headers</span> <span class="ruby-ivar">@header_rules</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:header_rules</span>] <span class="ruby-operator">||</span> [] <span class="ruby-comment"># Allow for legacy :cache_control option while prioritizing global header_rules setting</span> <span class="ruby-ivar">@header_rules</span>.<span class="ruby-identifier">unshift</span>([<span class="ruby-value">:all</span>, { <span class="ruby-constant">CACHE_CONTROL</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:cache_control</span>] }]) <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:cache_control</span>] <span class="ruby-ivar">@file_server</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Files</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">root</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-add_index_root-3F" class="method-detail "> <div class="method-heading"> <span class="method-name">add_index_root?</span><span class="method-args">(path)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="add_index_root-3F-source"> <pre><span class="ruby-comment"># File lib/rack/static.rb, line 107</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_index_root?</span>(<span class="ruby-identifier">path</span>) <span class="ruby-ivar">@index</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">route_file</span>(<span class="ruby-identifier">path</span>) <span class="ruby-operator">&&</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">end_with?</span>(<span class="ruby-string">'/'</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-applicable_rules" class="method-detail "> <div class="method-heading"> <span class="method-name">applicable_rules</span><span class="method-args">(path)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Convert HTTP header rules to HTTP headers</p> <div class="method-source-code" id="applicable_rules-source"> <pre><span class="ruby-comment"># File lib/rack/static.rb, line 166</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">applicable_rules</span>(<span class="ruby-identifier">path</span>) <span class="ruby-ivar">@header_rules</span>.<span class="ruby-identifier">find_all</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">rule</span>, <span class="ruby-identifier">new_headers</span><span class="ruby-operator">|</span> <span class="ruby-keyword">case</span> <span class="ruby-identifier">rule</span> <span class="ruby-keyword">when</span> <span class="ruby-value">:all</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">when</span> <span class="ruby-value">:fonts</span> <span class="ruby-regexp">/\.(?:ttf|otf|eot|woff2|woff|svg)\z/</span>.<span class="ruby-identifier">match?</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">when</span> <span class="ruby-constant">String</span> <span class="ruby-identifier">path</span> = <span class="ruby-operator">::</span><span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Utils</span>.<span class="ruby-identifier">unescape</span>(<span class="ruby-identifier">path</span>) <span class="ruby-identifier">path</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-identifier">rule</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">'/'</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">rule</span>) <span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span> <span class="ruby-regexp">/\.(#{rule.join('|')})\z/</span>.<span class="ruby-identifier">match?</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">when</span> <span class="ruby-constant">Regexp</span> <span class="ruby-identifier">rule</span>.<span class="ruby-identifier">match?</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">else</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <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/static.rb, line 123</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">call</span>(<span class="ruby-identifier">env</span>) <span class="ruby-identifier">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">can_serve</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">overwrite_file_path</span>(<span class="ruby-identifier">path</span>) <span class="ruby-identifier">env</span>[<span class="ruby-constant">PATH_INFO</span>] = (<span class="ruby-identifier">add_index_root?</span>(<span class="ruby-identifier">path</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">path</span> <span class="ruby-operator">+</span> <span class="ruby-ivar">@index</span> <span class="ruby-operator">:</span> <span class="ruby-ivar">@urls</span>[<span class="ruby-identifier">path</span>]) <span class="ruby-keyword">elsif</span> <span class="ruby-ivar">@gzip</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">env</span>[<span class="ruby-string">'HTTP_ACCEPT_ENCODING'</span>] <span class="ruby-operator">&&</span> <span class="ruby-regexp">/\bgzip\b/</span>.<span class="ruby-identifier">match?</span>(<span class="ruby-identifier">env</span>[<span class="ruby-string">'HTTP_ACCEPT_ENCODING'</span>]) <span class="ruby-identifier">path</span> = <span class="ruby-identifier">env</span>[<span class="ruby-constant">PATH_INFO</span>] <span class="ruby-identifier">env</span>[<span class="ruby-constant">PATH_INFO</span>] <span class="ruby-operator">+=</span> <span class="ruby-string">'.gz'</span> <span class="ruby-identifier">response</span> = <span class="ruby-ivar">@file_server</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>) <span class="ruby-identifier">env</span>[<span class="ruby-constant">PATH_INFO</span>] = <span class="ruby-identifier">path</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">response</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-value">404</span> <span class="ruby-identifier">response</span> = <span class="ruby-keyword">nil</span> <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">response</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-value">304</span> <span class="ruby-comment"># Do nothing, leave headers as is</span> <span class="ruby-keyword">else</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">mime_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">path</span>), <span class="ruby-string">'text/plain'</span>) <span class="ruby-identifier">response</span>[<span class="ruby-value">1</span>][<span class="ruby-constant">CONTENT_TYPE</span>] = <span class="ruby-identifier">mime_type</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">response</span>[<span class="ruby-value">1</span>][<span class="ruby-string">'Content-Encoding'</span>] = <span class="ruby-string">'gzip'</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">path</span> = <span class="ruby-identifier">env</span>[<span class="ruby-constant">PATH_INFO</span>] <span class="ruby-identifier">response</span> <span class="ruby-operator">||=</span> <span class="ruby-ivar">@file_server</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>) <span class="ruby-keyword">if</span> <span class="ruby-ivar">@cascade</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">response</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-value">404</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">end</span> <span class="ruby-identifier">headers</span> = <span class="ruby-identifier">response</span>[<span class="ruby-value">1</span>] <span class="ruby-identifier">applicable_rules</span>(<span class="ruby-identifier">path</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">rule</span>, <span class="ruby-identifier">new_headers</span><span class="ruby-operator">|</span> <span class="ruby-identifier">new_headers</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">field</span>, <span class="ruby-identifier">content</span><span class="ruby-operator">|</span> <span class="ruby-identifier">headers</span>[<span class="ruby-identifier">field</span>] = <span class="ruby-identifier">content</span> } <span class="ruby-keyword">end</span> <span class="ruby-identifier">response</span> <span class="ruby-keyword">else</span> <span class="ruby-ivar">@app</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-can_serve" class="method-detail "> <div class="method-heading"> <span class="method-name">can_serve</span><span class="method-args">(path)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="can_serve-source"> <pre><span class="ruby-comment"># File lib/rack/static.rb, line 119</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">can_serve</span>(<span class="ruby-identifier">path</span>) <span class="ruby-identifier">route_file</span>(<span class="ruby-identifier">path</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">overwrite_file_path</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-overwrite_file_path" class="method-detail "> <div class="method-heading"> <span class="method-name">overwrite_file_path</span><span class="method-args">(path)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="overwrite_file_path-source"> <pre><span class="ruby-comment"># File lib/rack/static.rb, line 111</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">overwrite_file_path</span>(<span class="ruby-identifier">path</span>) <span class="ruby-ivar">@urls</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Hash</span>) <span class="ruby-operator">&&</span> <span class="ruby-ivar">@urls</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-identifier">path</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">add_index_root?</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-route_file" class="method-detail "> <div class="method-heading"> <span class="method-name">route_file</span><span class="method-args">(path)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="route_file-source"> <pre><span class="ruby-comment"># File lib/rack/static.rb, line 115</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">route_file</span>(<span class="ruby-identifier">path</span>) <span class="ruby-ivar">@urls</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Array</span>) <span class="ruby-operator">&&</span> <span class="ruby-ivar">@urls</span>.<span class="ruby-identifier">any?</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">url</span><span class="ruby-operator">|</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">url</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</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>