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 :
Events.html
back
Copy
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>class Rack::Events - 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-make_request">#make_request</a> <li ><a href="#method-i-make_response">#make_response</a> <li ><a href="#method-i-on_commit">#on_commit</a> <li ><a href="#method-i-on_error">#on_error</a> <li ><a href="#method-i-on_finish">#on_finish</a> <li ><a href="#method-i-on_start">#on_start</a> </ul> </div> </div> </nav> <main role="main" aria-labelledby="class-Rack::Events"> <h1 id="class-Rack::Events" class="class"> class Rack::Events </h1> <section class="description"> <pre>This middleware provides hooks to certain places in the request /</pre> <p>response lifecycle. This is so that middleware that don't need to filter the response data can safely leave it alone and not have to send messages down the traditional “rack stack”.</p> <p>The events are:</p> <ul><li> <p><a href="Events.html#method-i-on_start"><code>on_start</code></a>(request, response)</p> <p>This event is sent at the start of the request, before the next middleware in the chain is called. This method is called with a request object, and a response object. Right now, the response object is always nil, but in the future it may actually be a real response object.</p> </li><li> <p><a href="Events.html#method-i-on_commit"><code>on_commit</code></a>(request, response)</p> <p>The response has been committed. The application has returned, but the response has not been sent to the webserver yet. This method is always called with a request object and the response object. The response object is constructed from the rack triple that the application returned. Changes may still be made to the response object at this point.</p> </li><li> <p>on_send(request, response)</p> <p>The webserver has started iterating over the response body and presumably has started sending data over the wire. This method is always called with a request object and the response object. The response object is constructed from the rack triple that the application returned. Changes SHOULD NOT be made to the response object as the webserver has already started sending data. Any mutations will likely result in an exception.</p> </li><li> <p><a href="Events.html#method-i-on_finish"><code>on_finish</code></a>(request, response)</p> <p>The webserver has closed the response, and all data has been written to the response socket. The request and response object should both be read-only at this point. The body MAY NOT be available on the response object as it may have been flushed to the socket.</p> </li><li> <p><a href="Events.html#method-i-on_error"><code>on_error</code></a>(request, response, error)</p> <p>An exception has occurred in the application or an `on_commit` event. This method will get the request, the response (if available) and the exception that was raised.</p> </li></ul> <p>## Order</p> <p>`on_start` is called on the handlers in the order that they were passed to the constructor. `on_commit`, on_send`, `on_finish`, and `on_error` are called in the reverse order. `on_finish` handlers are called inside an `ensure` block, so they are guaranteed to be called even if something raises an exception. If something raises an exception in a `on_finish` method, then nothing is guaranteed.</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, handlers)</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/events.rb, line 102</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">handlers</span>) <span class="ruby-ivar">@app</span> = <span class="ruby-identifier">app</span> <span class="ruby-ivar">@handlers</span> = <span class="ruby-identifier">handlers</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/events.rb, line 107</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">request</span> = <span class="ruby-identifier">make_request</span> <span class="ruby-identifier">env</span> <span class="ruby-identifier">on_start</span> <span class="ruby-identifier">request</span>, <span class="ruby-keyword">nil</span> <span class="ruby-keyword">begin</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">request</span>.<span class="ruby-identifier">env</span> <span class="ruby-identifier">response</span> = <span class="ruby-identifier">make_response</span> <span class="ruby-identifier">status</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-identifier">body</span> <span class="ruby-identifier">on_commit</span> <span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span> <span class="ruby-keyword">rescue</span> <span class="ruby-constant">StandardError</span> <span class="ruby-operator">=></span> <span class="ruby-identifier">e</span> <span class="ruby-identifier">on_error</span> <span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span>, <span class="ruby-identifier">e</span> <span class="ruby-identifier">on_finish</span> <span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span> <span class="ruby-identifier">raise</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">body</span> = <span class="ruby-constant">EventedBodyProxy</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">body</span>, <span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span>, <span class="ruby-ivar">@handlers</span>) <span class="ruby-keyword">do</span> <span class="ruby-identifier">on_finish</span> <span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span> <span class="ruby-keyword">end</span> [<span class="ruby-identifier">response</span>.<span class="ruby-identifier">status</span>, <span class="ruby-identifier">response</span>.<span class="ruby-identifier">headers</span>, <span class="ruby-identifier">body</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-make_request" class="method-detail "> <div class="method-heading"> <span class="method-name">make_request</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="make_request-source"> <pre><span class="ruby-comment"># File lib/rack/events.rb, line 145</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">make_request</span>(<span class="ruby-identifier">env</span>) <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Request</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">env</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-make_response" class="method-detail "> <div class="method-heading"> <span class="method-name">make_response</span><span class="method-args">(status, headers, body)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="make_response-source"> <pre><span class="ruby-comment"># File lib/rack/events.rb, line 149</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">make_response</span>(<span class="ruby-identifier">status</span>, <span class="ruby-identifier">headers</span>, <span class="ruby-identifier">body</span>) <span class="ruby-constant">BufferedResponse</span>.<span class="ruby-identifier">new</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></pre> </div> </div> </div> <div id="method-i-on_commit" class="method-detail "> <div class="method-heading"> <span class="method-name">on_commit</span><span class="method-args">(request, response)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="on_commit-source"> <pre><span class="ruby-comment"># File lib/rack/events.rb, line 133</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">on_commit</span>(<span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span>) <span class="ruby-ivar">@handlers</span>.<span class="ruby-identifier">reverse_each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">handler</span><span class="ruby-operator">|</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">on_commit</span> <span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span> } <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-on_error" class="method-detail "> <div class="method-heading"> <span class="method-name">on_error</span><span class="method-args">(request, response, e)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="on_error-source"> <pre><span class="ruby-comment"># File lib/rack/events.rb, line 129</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">on_error</span>(<span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span>, <span class="ruby-identifier">e</span>) <span class="ruby-ivar">@handlers</span>.<span class="ruby-identifier">reverse_each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">handler</span><span class="ruby-operator">|</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">on_error</span> <span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span>, <span class="ruby-identifier">e</span> } <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-on_finish" class="method-detail "> <div class="method-heading"> <span class="method-name">on_finish</span><span class="method-args">(request, response)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="on_finish-source"> <pre><span class="ruby-comment"># File lib/rack/events.rb, line 141</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">on_finish</span>(<span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span>) <span class="ruby-ivar">@handlers</span>.<span class="ruby-identifier">reverse_each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">handler</span><span class="ruby-operator">|</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">on_finish</span> <span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span> } <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-on_start" class="method-detail "> <div class="method-heading"> <span class="method-name">on_start</span><span class="method-args">(request, response)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="on_start-source"> <pre><span class="ruby-comment"># File lib/rack/events.rb, line 137</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">on_start</span>(<span class="ruby-identifier">request</span>, <span class="ruby-identifier">response</span>) <span class="ruby-ivar">@handlers</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">handler</span><span class="ruby-operator">|</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">on_start</span> <span class="ruby-identifier">request</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>