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 :
Deflater.html
back
Copy
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>class Rack::Deflater - 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-should_deflate-3F">#should_deflate?</a> </ul> </div> </div> </nav> <main role="main" aria-labelledby="class-Rack::Deflater"> <h1 id="class-Rack::Deflater" class="class"> class Rack::Deflater </h1> <section class="description"> <p>This middleware enables content encoding of http responses, usually for purposes of compression.</p> <p>Currently supported encodings:</p> <ul><li> <p>gzip</p> </li><li> <p>identity (no transformation)</p> </li></ul> <p>This middleware automatically detects when encoding is supported and allowed. For example no encoding is made when a cache directive of 'no-transform' is present, when the response status code is one that doesn't allow an entity body, or when the body is empty.</p> <p>Note that despite the name, <a href="Deflater.html"><code>Deflater</code></a> does not support the <code>deflate</code> encoding.</p> </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"> <p>Creates <a href="Deflater.html"><code>Rack::Deflater</code></a> middleware. Options:</p> <dl class="rdoc-list note-list"><dt>:if <dd> <p>a lambda enabling / disabling deflation based on returned boolean value (e.g <code>use Rack::Deflater, :if => lambda { |*, body| sum=0; body.each { |i| sum += i.length }; sum > 512 }</code>). However, be aware that calling `body.each` inside the block will break cases where `body.each` is not idempotent, such as when it is an <code>IO</code> instance.</p> </dd><dt>:include <dd> <p>a list of content types that should be compressed. By default, all content types are compressed.</p> </dd><dt>:sync <dd> <p>determines if the stream is going to be flushed after every chunk. Flushing after every chunk reduces latency for time-sensitive streaming applications, but hurts compression and throughput. Defaults to <code>true</code>.</p> </dd></dl> <div class="method-source-code" id="new-source"> <pre><span class="ruby-comment"># File lib/rack/deflater.rb, line 36</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">@condition</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>] <span class="ruby-ivar">@compressible_types</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:include</span>] <span class="ruby-ivar">@sync</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:sync</span>, <span class="ruby-keyword">true</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/deflater.rb, line 43</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">status</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-identifier">body</span> = <span class="ruby-ivar">@app</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>) <span class="ruby-identifier">headers</span> = <span class="ruby-constant">Utils</span><span class="ruby-operator">::</span><span class="ruby-constant">HeaderHash</span>[<span class="ruby-identifier">headers</span>] <span class="ruby-keyword">unless</span> <span class="ruby-identifier">should_deflate?</span>(<span class="ruby-identifier">env</span>, <span class="ruby-identifier">status</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-identifier">body</span>) <span class="ruby-keyword">return</span> [<span class="ruby-identifier">status</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-identifier">body</span>] <span class="ruby-keyword">end</span> <span class="ruby-identifier">request</span> = <span class="ruby-constant">Request</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">env</span>) <span class="ruby-identifier">encoding</span> = <span class="ruby-constant">Utils</span>.<span class="ruby-identifier">select_best_encoding</span>(<span class="ruby-node">%w(gzip identity)</span>, <span class="ruby-identifier">request</span>.<span class="ruby-identifier">accept_encoding</span>) <span class="ruby-comment"># Set the Vary HTTP header.</span> <span class="ruby-identifier">vary</span> = <span class="ruby-identifier">headers</span>[<span class="ruby-string">"Vary"</span>].<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">","</span>).<span class="ruby-identifier">map</span>(<span class="ruby-operator">&</span><span class="ruby-value">:strip</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">vary</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-string">"*"</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">vary</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-string">"Accept-Encoding"</span>) <span class="ruby-identifier">headers</span>[<span class="ruby-string">"Vary"</span>] = <span class="ruby-identifier">vary</span>.<span class="ruby-identifier">push</span>(<span class="ruby-string">"Accept-Encoding"</span>).<span class="ruby-identifier">join</span>(<span class="ruby-string">","</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">case</span> <span class="ruby-identifier">encoding</span> <span class="ruby-keyword">when</span> <span class="ruby-string">"gzip"</span> <span class="ruby-identifier">headers</span>[<span class="ruby-string">'Content-Encoding'</span>] = <span class="ruby-string">"gzip"</span> <span class="ruby-identifier">headers</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-constant">CONTENT_LENGTH</span>) <span class="ruby-identifier">mtime</span> = <span class="ruby-identifier">headers</span>[<span class="ruby-string">"Last-Modified"</span>] <span class="ruby-identifier">mtime</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">httpdate</span>(<span class="ruby-identifier">mtime</span>).<span class="ruby-identifier">to_i</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">mtime</span> [<span class="ruby-identifier">status</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-constant">GzipStream</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">body</span>, <span class="ruby-identifier">mtime</span>, <span class="ruby-ivar">@sync</span>)] <span class="ruby-keyword">when</span> <span class="ruby-string">"identity"</span> [<span class="ruby-identifier">status</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-identifier">body</span>] <span class="ruby-keyword">when</span> <span class="ruby-keyword">nil</span> <span class="ruby-identifier">message</span> = <span class="ruby-node">"An acceptable encoding for the requested resource #{request.fullpath} could not be found."</span> <span class="ruby-identifier">bp</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">BodyProxy</span>.<span class="ruby-identifier">new</span>([<span class="ruby-identifier">message</span>]) { <span class="ruby-identifier">body</span>.<span class="ruby-identifier">close</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">body</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:close</span>) } [<span class="ruby-value">406</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">message</span>.<span class="ruby-identifier">length</span>.<span class="ruby-identifier">to_s</span> }, <span class="ruby-identifier">bp</span>] <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> </section> <section id="private-instance-5Buntitled-5D-method-details" class="method-section"> <header> <h3>Private Instance Methods</h3> </header> <div id="method-i-should_deflate-3F" class="method-detail "> <div class="method-heading"> <span class="method-name">should_deflate?</span><span class="method-args">(env, status, headers, body)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Whether the body should be compressed.</p> <div class="method-source-code" id="should_deflate-3F-source"> <pre><span class="ruby-comment"># File lib/rack/deflater.rb, line 122</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">should_deflate?</span>(<span class="ruby-identifier">env</span>, <span class="ruby-identifier">status</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-identifier">body</span>) <span class="ruby-comment"># Skip compressing empty entity body responses and responses with</span> <span class="ruby-comment"># no-transform set.</span> <span class="ruby-keyword">if</span> <span class="ruby-constant">Utils</span><span class="ruby-operator">::</span><span class="ruby-constant">STATUS_WITH_NO_ENTITY_BODY</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-identifier">status</span>.<span class="ruby-identifier">to_i</span>) <span class="ruby-operator">||</span> <span class="ruby-regexp">/\bno-transform\b/</span>.<span class="ruby-identifier">match?</span>(<span class="ruby-identifier">headers</span>[<span class="ruby-string">'Cache-Control'</span>].<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">headers</span>[<span class="ruby-string">'Content-Encoding'</span>]&.<span class="ruby-identifier">!~</span>(<span class="ruby-regexp">/\bidentity\b/</span>) <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">end</span> <span class="ruby-comment"># Skip if @compressible_types are given and does not include request's content type</span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@compressible_types</span> <span class="ruby-operator">&&</span> <span class="ruby-operator">!</span>(<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-string">'Content-Type'</span>) <span class="ruby-operator">&&</span> <span class="ruby-ivar">@compressible_types</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">headers</span>[<span class="ruby-string">'Content-Type'</span>][<span class="ruby-regexp">/[^;]*/</span>])) <span class="ruby-comment"># Skip if @condition lambda is given and evaluates to false</span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@condition</span> <span class="ruby-operator">&&</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@condition</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>, <span class="ruby-identifier">status</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-identifier">body</span>) <span class="ruby-comment"># No point in compressing empty body, also handles usage with</span> <span class="ruby-comment"># Rack::Sendfile.</span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">headers</span>[<span class="ruby-constant">CONTENT_LENGTH</span>] <span class="ruby-operator">==</span> <span class="ruby-string">'0'</span> <span class="ruby-keyword">true</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>