관리-도구
편집 파일: Utils.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>module Rack::Utils - 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="module"> <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"> <!-- Method Quickref --> <div id="method-list-section" class="nav-section"> <h3>Methods</h3> <ul class="link-list" role="directory"> <li ><a href="#method-c-key_space_limit">::key_space_limit</a> <li ><a href="#method-c-key_space_limit-3D">::key_space_limit=</a> <li ><a href="#method-c-param_depth_limit">::param_depth_limit</a> <li ><a href="#method-c-param_depth_limit-3D">::param_depth_limit=</a> <li ><a href="#method-i-add_cookie_to_header">#add_cookie_to_header</a> <li ><a href="#method-i-add_remove_cookie_to_header">#add_remove_cookie_to_header</a> <li ><a href="#method-i-best_q_match">#best_q_match</a> <li ><a href="#method-i-build_nested_query">#build_nested_query</a> <li ><a href="#method-i-build_query">#build_query</a> <li ><a href="#method-i-byte_ranges">#byte_ranges</a> <li ><a href="#method-i-clean_path_info">#clean_path_info</a> <li ><a href="#method-i-clock_time">#clock_time</a> <li ><a href="#method-i-delete_cookie_header-21">#delete_cookie_header!</a> <li ><a href="#method-i-escape">#escape</a> <li ><a href="#method-i-escape_html">#escape_html</a> <li ><a href="#method-i-escape_path">#escape_path</a> <li ><a href="#method-i-get_byte_ranges">#get_byte_ranges</a> <li ><a href="#method-i-make_delete_cookie_header">#make_delete_cookie_header</a> <li ><a href="#method-i-parse_cookies">#parse_cookies</a> <li ><a href="#method-i-parse_cookies_header">#parse_cookies_header</a> <li ><a href="#method-i-parse_nested_query">#parse_nested_query</a> <li ><a href="#method-i-parse_query">#parse_query</a> <li ><a href="#method-i-q_values">#q_values</a> <li ><a href="#method-i-rfc2109">#rfc2109</a> <li ><a href="#method-i-rfc2822">#rfc2822</a> <li ><a href="#method-i-secure_compare">#secure_compare</a> <li ><a href="#method-i-select_best_encoding">#select_best_encoding</a> <li ><a href="#method-i-set_cookie_header-21">#set_cookie_header!</a> <li ><a href="#method-i-status_code">#status_code</a> <li ><a href="#method-i-unescape">#unescape</a> <li ><a href="#method-i-unescape_path">#unescape_path</a> <li ><a href="#method-i-valid_path-3F">#valid_path?</a> </ul> </div> </div> </nav> <main role="main" aria-labelledby="module-Rack::Utils"> <h1 id="module-Rack::Utils" class="module"> module Rack::Utils </h1> <section class="description"> <p><a href="Utils.html"><code>Rack::Utils</code></a> contains a grab-bag of useful methods for writing web applications adopted from all kinds of Ruby libraries.</p> </section> <section id="5Buntitled-5D" class="documentation-section"> <section class="constants-list"> <header> <h3>Constants</h3> </header> <dl> <dt id="COMMON_SEP">COMMON_SEP <dd> <dt id="DEFAULT_SEP">DEFAULT_SEP <dd> <dt id="ESCAPE_HTML">ESCAPE_HTML <dd> <dt id="ESCAPE_HTML_PATTERN">ESCAPE_HTML_PATTERN <dd> <dt id="HTTP_STATUS_CODES">HTTP_STATUS_CODES <dd><p>Every standard HTTP code mapped to the appropriate message. Generated with:</p> <pre>curl -s https://www.iana.org/assignments/http-status-codes/http-status-codes-1.csv | \ ruby -ne 'm = /^(\d{3}),(?!Unassigned|\(Unused\))([^,]+)/.match($_) and \ puts "#{m[1]} => \x27#{m[2].strip}\x27,"'</pre> <dt id="InvalidParameterError">InvalidParameterError <dd> <dt id="KeySpaceConstrainedParams">KeySpaceConstrainedParams <dd> <dt id="NULL_BYTE">NULL_BYTE <dd> <dt id="PATH_SEPS">PATH_SEPS <dd> <dt id="ParameterTypeError">ParameterTypeError <dd> <dt id="RFC2396_PARSER">RFC2396_PARSER <dd> <dt id="RFC2822_DAY_NAME">RFC2822_DAY_NAME <dd> <dt id="RFC2822_MONTH_NAME">RFC2822_MONTH_NAME <dd> <dt id="STATUS_WITH_NO_ENTITY_BODY">STATUS_WITH_NO_ENTITY_BODY <dd><p>Responses with HTTP status codes that should not have an entity body</p> <dt id="SYMBOL_TO_STATUS_CODE">SYMBOL_TO_STATUS_CODE <dd> </dl> </section> <section class="attribute-method-details" class="method-section"> <header> <h3>Attributes</h3> </header> <div id="attribute-c-default_query_parser" class="method-detail"> <div class="method-heading attribute-method-heading"> <span class="method-name">default_query_parser</span><span class="attribute-access-type">[RW]</span> </div> <div class="method-description"> </div> </div> <div id="attribute-c-multipart_file_limit" class="method-detail"> <div class="method-heading attribute-method-heading"> <span class="method-name">multipart_file_limit</span><span class="attribute-access-type">[RW]</span> </div> <div class="method-description"> </div> </div> <div id="attribute-c-multipart_part_limit" class="method-detail"> <div class="method-heading attribute-method-heading"> <span class="method-name">multipart_part_limit</span><span class="attribute-access-type">[RW]</span> </div> <div class="method-description"> </div> </div> <div id="attribute-c-multipart_part_limit-3D" class="method-detail"> <div class="method-heading attribute-method-heading"> <span class="method-name">multipart_part_limit=</span><span class="attribute-access-type">[RW]</span> </div> <div class="method-description"> </div> </div> <div id="attribute-c-multipart_total_part_limit" class="method-detail"> <div class="method-heading attribute-method-heading"> <span class="method-name">multipart_total_part_limit</span><span class="attribute-access-type">[RW]</span> </div> <div class="method-description"> </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-key_space_limit" class="method-detail "> <div class="method-heading"> <span class="method-name">key_space_limit</span><span class="method-args">()</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="key_space_limit-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 88</span> <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">key_space_limit</span> <span class="ruby-identifier">default_query_parser</span>.<span class="ruby-identifier">key_space_limit</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-c-key_space_limit-3D" class="method-detail "> <div class="method-heading"> <span class="method-name">key_space_limit=</span><span class="method-args">(v)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="key_space_limit-3D-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 92</span> <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">key_space_limit=</span>(<span class="ruby-identifier">v</span>) <span class="ruby-keyword">self</span>.<span class="ruby-identifier">default_query_parser</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">default_query_parser</span>.<span class="ruby-identifier">new_space_limit</span>(<span class="ruby-identifier">v</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-c-param_depth_limit" class="method-detail "> <div class="method-heading"> <span class="method-name">param_depth_limit</span><span class="method-args">()</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="param_depth_limit-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 80</span> <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">param_depth_limit</span> <span class="ruby-identifier">default_query_parser</span>.<span class="ruby-identifier">param_depth_limit</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-c-param_depth_limit-3D" class="method-detail "> <div class="method-heading"> <span class="method-name">param_depth_limit=</span><span class="method-args">(v)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="param_depth_limit-3D-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 84</span> <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">param_depth_limit=</span>(<span class="ruby-identifier">v</span>) <span class="ruby-keyword">self</span>.<span class="ruby-identifier">default_query_parser</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">default_query_parser</span>.<span class="ruby-identifier">new_depth_limit</span>(<span class="ruby-identifier">v</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_cookie_to_header" class="method-detail "> <div class="method-heading"> <span class="method-name">add_cookie_to_header</span><span class="method-args">(header, key, value)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="add_cookie_to_header-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 237</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_cookie_to_header</span>(<span class="ruby-identifier">header</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>) <span class="ruby-keyword">case</span> <span class="ruby-identifier">value</span> <span class="ruby-keyword">when</span> <span class="ruby-constant">Hash</span> <span class="ruby-identifier">domain</span> = <span class="ruby-node">"; domain=#{value[:domain]}"</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>[<span class="ruby-value">:domain</span>] <span class="ruby-identifier">path</span> = <span class="ruby-node">"; path=#{value[:path]}"</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>[<span class="ruby-value">:path</span>] <span class="ruby-identifier">max_age</span> = <span class="ruby-node">"; max-age=#{value[:max_age]}"</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>[<span class="ruby-value">:max_age</span>] <span class="ruby-identifier">expires</span> = <span class="ruby-node">"; expires=#{value[:expires].httpdate}"</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>[<span class="ruby-value">:expires</span>] <span class="ruby-identifier">secure</span> = <span class="ruby-string">"; secure"</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>[<span class="ruby-value">:secure</span>] <span class="ruby-identifier">httponly</span> = <span class="ruby-string">"; HttpOnly"</span> <span class="ruby-keyword">if</span> (<span class="ruby-identifier">value</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-value">:httponly</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">value</span>[<span class="ruby-value">:httponly</span>] <span class="ruby-operator">:</span> <span class="ruby-identifier">value</span>[<span class="ruby-value">:http_only</span>]) <span class="ruby-identifier">same_site</span> = <span class="ruby-keyword">case</span> <span class="ruby-identifier">value</span>[<span class="ruby-value">:same_site</span>] <span class="ruby-keyword">when</span> <span class="ruby-keyword">false</span>, <span class="ruby-keyword">nil</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">when</span> <span class="ruby-value">:none</span>, <span class="ruby-string">'None'</span>, <span class="ruby-value">:None</span> <span class="ruby-string">'; SameSite=None'</span> <span class="ruby-keyword">when</span> <span class="ruby-value">:lax</span>, <span class="ruby-string">'Lax'</span>, <span class="ruby-value">:Lax</span> <span class="ruby-string">'; SameSite=Lax'</span> <span class="ruby-keyword">when</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">:strict</span>, <span class="ruby-string">'Strict'</span>, <span class="ruby-value">:Strict</span> <span class="ruby-string">'; SameSite=Strict'</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">"Invalid SameSite value: #{value[:same_site].inspect}"</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">value</span> = <span class="ruby-identifier">value</span>[<span class="ruby-value">:value</span>] <span class="ruby-keyword">end</span> <span class="ruby-identifier">value</span> = [<span class="ruby-identifier">value</span>] <span class="ruby-keyword">unless</span> <span class="ruby-constant">Array</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">value</span> <span class="ruby-identifier">cookie</span> = <span class="ruby-node">"#{escape(key)}=#{value.map { |v| escape v }.join('&')}#{domain}"</span> \ <span class="ruby-node">"#{path}#{max_age}#{expires}#{secure}#{httponly}#{same_site}"</span> <span class="ruby-keyword">case</span> <span class="ruby-identifier">header</span> <span class="ruby-keyword">when</span> <span class="ruby-keyword">nil</span>, <span class="ruby-string">''</span> <span class="ruby-identifier">cookie</span> <span class="ruby-keyword">when</span> <span class="ruby-constant">String</span> [<span class="ruby-identifier">header</span>, <span class="ruby-identifier">cookie</span>].<span class="ruby-identifier">join</span>(<span class="ruby-string">"\n"</span>) <span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span> (<span class="ruby-identifier">header</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">cookie</span>]).<span class="ruby-identifier">join</span>(<span class="ruby-string">"\n"</span>) <span class="ruby-keyword">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">"Unrecognized cookie header value. Expected String, Array, or nil, got #{header.inspect}"</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-add_remove_cookie_to_header" class="method-detail "> <div class="method-heading"> <span class="method-name">add_remove_cookie_to_header</span><span class="method-args">(header, key, value = {})</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Adds a cookie that will <strong>remove</strong> a cookie from the client. Hence the strange method name.</p> <div class="method-source-code" id="add_remove_cookie_to_header-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 320</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_remove_cookie_to_header</span>(<span class="ruby-identifier">header</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span> = {}) <span class="ruby-identifier">new_header</span> = <span class="ruby-identifier">make_delete_cookie_header</span>(<span class="ruby-identifier">header</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>) <span class="ruby-identifier">add_cookie_to_header</span>(<span class="ruby-identifier">new_header</span>, <span class="ruby-identifier">key</span>, { <span class="ruby-value">value:</span> <span class="ruby-string">''</span>, <span class="ruby-value">path:</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">domain:</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">max_age:</span> <span class="ruby-string">'0'</span>, <span class="ruby-value">expires:</span> <span class="ruby-constant">Time</span>.<span class="ruby-identifier">at</span>(<span class="ruby-value">0</span>) }.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">value</span>)) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-best_q_match" class="method-detail "> <div class="method-heading"> <span class="method-name">best_q_match</span><span class="method-args">(q_value_header, available_mimes)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Return best accept value to use, based on the algorithm in RFC 2616 Section 14. If there are multiple best matches (same specificity and quality), the value returned is arbitrary.</p> <div class="method-source-code" id="best_q_match-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 159</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">best_q_match</span>(<span class="ruby-identifier">q_value_header</span>, <span class="ruby-identifier">available_mimes</span>) <span class="ruby-identifier">values</span> = <span class="ruby-identifier">q_values</span>(<span class="ruby-identifier">q_value_header</span>) <span class="ruby-identifier">matches</span> = <span class="ruby-identifier">values</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">req_mime</span>, <span class="ruby-identifier">quality</span><span class="ruby-operator">|</span> <span class="ruby-identifier">match</span> = <span class="ruby-identifier">available_mimes</span>.<span class="ruby-identifier">find</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">am</span><span class="ruby-operator">|</span> <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Mime</span>.<span class="ruby-identifier">match?</span>(<span class="ruby-identifier">am</span>, <span class="ruby-identifier">req_mime</span>) } <span class="ruby-keyword">next</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">match</span> [<span class="ruby-identifier">match</span>, <span class="ruby-identifier">quality</span>] <span class="ruby-keyword">end</span>.<span class="ruby-identifier">compact</span>.<span class="ruby-identifier">sort_by</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">match</span>, <span class="ruby-identifier">quality</span><span class="ruby-operator">|</span> (<span class="ruby-identifier">match</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">'/'</span>, <span class="ruby-value">2</span>).<span class="ruby-identifier">count</span>(<span class="ruby-string">'*'</span>) <span class="ruby-operator">*</span> <span class="ruby-value">-10</span>) <span class="ruby-operator">+</span> <span class="ruby-identifier">quality</span> <span class="ruby-keyword">end</span>.<span class="ruby-identifier">last</span> <span class="ruby-identifier">matches</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">matches</span>.<span class="ruby-identifier">first</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-build_nested_query" class="method-detail "> <div class="method-heading"> <span class="method-name">build_nested_query</span><span class="method-args">(value, prefix = nil)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="build_nested_query-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 126</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">build_nested_query</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">prefix</span> = <span class="ruby-keyword">nil</span>) <span class="ruby-keyword">case</span> <span class="ruby-identifier">value</span> <span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">build_nested_query</span>(<span class="ruby-identifier">v</span>, <span class="ruby-node">"#{prefix}[]"</span>) }.<span class="ruby-identifier">join</span>(<span class="ruby-string">"&"</span>) <span class="ruby-keyword">when</span> <span class="ruby-constant">Hash</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">build_nested_query</span>(<span class="ruby-identifier">v</span>, <span class="ruby-identifier">prefix</span> <span class="ruby-operator">?</span> <span class="ruby-node">"#{prefix}[#{escape(k)}]"</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">escape</span>(<span class="ruby-identifier">k</span>)) }.<span class="ruby-identifier">delete_if</span>(<span class="ruby-operator">&</span><span class="ruby-value">:empty?</span>).<span class="ruby-identifier">join</span>(<span class="ruby-string">'&'</span>) <span class="ruby-keyword">when</span> <span class="ruby-keyword">nil</span> <span class="ruby-identifier">prefix</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">"value must be a Hash"</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">prefix</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-node">"#{prefix}=#{escape(value)}"</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-build_query" class="method-detail "> <div class="method-heading"> <span class="method-name">build_query</span><span class="method-args">(params)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="build_query-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 116</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">build_query</span>(<span class="ruby-identifier">params</span>) <span class="ruby-identifier">params</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">v</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Array</span> <span class="ruby-identifier">build_query</span>(<span class="ruby-identifier">v</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">k</span>, <span class="ruby-identifier">x</span>] }) <span class="ruby-keyword">else</span> <span class="ruby-identifier">v</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">escape</span>(<span class="ruby-identifier">k</span>) <span class="ruby-operator">:</span> <span class="ruby-node">"#{escape(k)}=#{escape(v)}"</span> <span class="ruby-keyword">end</span> }.<span class="ruby-identifier">join</span>(<span class="ruby-string">"&"</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-byte_ranges" class="method-detail "> <div class="method-heading"> <span class="method-name">byte_ranges</span><span class="method-args">(env, size)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Parses the “Range:” header, if present, into an array of Range objects. Returns nil if the header is missing or syntactically invalid. Returns an empty array if none of the ranges are satisfiable.</p> <div class="method-source-code" id="byte_ranges-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 352</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">byte_ranges</span>(<span class="ruby-identifier">env</span>, <span class="ruby-identifier">size</span>) <span class="ruby-identifier">warn</span> <span class="ruby-string">"`byte_ranges` is deprecated, please use `get_byte_ranges`"</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">$VERBOSE</span> <span class="ruby-identifier">get_byte_ranges</span> <span class="ruby-identifier">env</span>[<span class="ruby-string">'HTTP_RANGE'</span>], <span class="ruby-identifier">size</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-clean_path_info" class="method-detail "> <div class="method-heading"> <span class="method-name">clean_path_info</span><span class="method-args">(path_info)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="clean_path_info-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 609</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">clean_path_info</span>(<span class="ruby-identifier">path_info</span>) <span class="ruby-identifier">parts</span> = <span class="ruby-identifier">path_info</span>.<span class="ruby-identifier">split</span> <span class="ruby-constant">PATH_SEPS</span> <span class="ruby-identifier">clean</span> = [] <span class="ruby-identifier">parts</span>.<span class="ruby-identifier">each</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-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">part</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">part</span> <span class="ruby-operator">==</span> <span class="ruby-string">'.'</span> <span class="ruby-identifier">part</span> <span class="ruby-operator">==</span> <span class="ruby-string">'..'</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">clean</span>.<span class="ruby-identifier">pop</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">clean</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">part</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">clean_path</span> = <span class="ruby-identifier">clean</span>.<span class="ruby-identifier">join</span>(<span class="ruby-operator">::</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">SEPARATOR</span>) <span class="ruby-identifier">clean_path</span>.<span class="ruby-identifier">prepend</span>(<span class="ruby-string">"/"</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">parts</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">parts</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-identifier">clean_path</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-clock_time" class="method-detail "> <div class="method-heading"> <span class="method-name">clock_time</span><span class="method-args">()</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="clock_time-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 97</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">clock_time</span> <span class="ruby-constant">Process</span>.<span class="ruby-identifier">clock_gettime</span>(<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">CLOCK_MONOTONIC</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-delete_cookie_header-21" class="method-detail "> <div class="method-heading"> <span class="method-name">delete_cookie_header!</span><span class="method-args">(header, key, value = {})</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="delete_cookie_header-21-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 313</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">delete_cookie_header!</span>(<span class="ruby-identifier">header</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span> = {}) <span class="ruby-identifier">header</span>[<span class="ruby-constant">SET_COOKIE</span>] = <span class="ruby-identifier">add_remove_cookie_to_header</span>(<span class="ruby-identifier">header</span>[<span class="ruby-constant">SET_COOKIE</span>], <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>) <span class="ruby-keyword">nil</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-escape" class="method-detail "> <div class="method-heading"> <span class="method-name">escape</span><span class="method-args">(s)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>URI escapes. (CGI style space to +)</p> <div class="method-source-code" id="escape-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 38</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">escape</span>(<span class="ruby-identifier">s</span>) <span class="ruby-constant">URI</span>.<span class="ruby-identifier">encode_www_form_component</span>(<span class="ruby-identifier">s</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-escape_html" class="method-detail "> <div class="method-heading"> <span class="method-name">escape_html</span><span class="method-args">(string)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Escape ampersands, brackets and quotes to their HTML/XML entities.</p> <div class="method-source-code" id="escape_html-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 184</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">escape_html</span>(<span class="ruby-identifier">string</span>) <span class="ruby-identifier">string</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-constant">ESCAPE_HTML_PATTERN</span>){<span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-constant">ESCAPE_HTML</span>[<span class="ruby-identifier">c</span>] } <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-escape_path" class="method-detail "> <div class="method-heading"> <span class="method-name">escape_path</span><span class="method-args">(s)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Like URI escaping, but with %20 instead of +. Strictly speaking this is true URI escaping.</p> <div class="method-source-code" id="escape_path-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 44</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">escape_path</span>(<span class="ruby-identifier">s</span>) <span class="ruby-constant">RFC2396_PARSER</span>.<span class="ruby-identifier">escape</span> <span class="ruby-identifier">s</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-get_byte_ranges" class="method-detail "> <div class="method-heading"> <span class="method-name">get_byte_ranges</span><span class="method-args">(http_range, size)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="get_byte_ranges-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 357</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get_byte_ranges</span>(<span class="ruby-identifier">http_range</span>, <span class="ruby-identifier">size</span>) <span class="ruby-comment"># See <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35></span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">http_range</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">http_range</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/bytes=([^;]+)/</span> <span class="ruby-identifier">ranges</span> = [] <span class="ruby-node">$1</span>.<span class="ruby-identifier">split</span>(<span class="ruby-regexp">/,\s*/</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">range_spec</span><span class="ruby-operator">|</span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">range_spec</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-string">'-'</span>) <span class="ruby-identifier">range</span> = <span class="ruby-identifier">range_spec</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">'-'</span>) <span class="ruby-identifier">r0</span>, <span class="ruby-identifier">r1</span> = <span class="ruby-identifier">range</span>[<span class="ruby-value">0</span>], <span class="ruby-identifier">range</span>[<span class="ruby-value">1</span>] <span class="ruby-keyword">if</span> <span class="ruby-identifier">r0</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">r0</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">r1</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-comment"># suffix-byte-range-spec, represents trailing suffix of file</span> <span class="ruby-identifier">r0</span> = <span class="ruby-identifier">size</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">r1</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-identifier">r0</span> = <span class="ruby-value">0</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">r0</span> <span class="ruby-operator"><</span> <span class="ruby-value">0</span> <span class="ruby-identifier">r1</span> = <span class="ruby-identifier">size</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">r0</span> = <span class="ruby-identifier">r0</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">r1</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-identifier">r1</span> = <span class="ruby-identifier">size</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">r1</span> = <span class="ruby-identifier">r1</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">r1</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">r0</span> <span class="ruby-comment"># backwards range is syntactically invalid</span> <span class="ruby-identifier">r1</span> = <span class="ruby-identifier">size</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">r1</span> <span class="ruby-operator">>=</span> <span class="ruby-identifier">size</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">ranges</span> <span class="ruby-operator"><<</span> (<span class="ruby-identifier">r0</span><span class="ruby-operator">..</span><span class="ruby-identifier">r1</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">r0</span> <span class="ruby-operator"><=</span> <span class="ruby-identifier">r1</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">return</span> [] <span class="ruby-keyword">if</span> <span class="ruby-identifier">ranges</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&</span><span class="ruby-value">:size</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">0</span>, <span class="ruby-value">:+</span>) <span class="ruby-operator">></span> <span class="ruby-identifier">size</span> <span class="ruby-identifier">ranges</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-make_delete_cookie_header" class="method-detail "> <div class="method-heading"> <span class="method-name">make_delete_cookie_header</span><span class="method-args">(header, key, value)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="make_delete_cookie_header-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 283</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">make_delete_cookie_header</span>(<span class="ruby-identifier">header</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>) <span class="ruby-keyword">case</span> <span class="ruby-identifier">header</span> <span class="ruby-keyword">when</span> <span class="ruby-keyword">nil</span>, <span class="ruby-string">''</span> <span class="ruby-identifier">cookies</span> = [] <span class="ruby-keyword">when</span> <span class="ruby-constant">String</span> <span class="ruby-identifier">cookies</span> = <span class="ruby-identifier">header</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">"\n"</span>) <span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span> <span class="ruby-identifier">cookies</span> = <span class="ruby-identifier">header</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">key</span> = <span class="ruby-identifier">escape</span>(<span class="ruby-identifier">key</span>) <span class="ruby-identifier">domain</span> = <span class="ruby-identifier">value</span>[<span class="ruby-value">:domain</span>] <span class="ruby-identifier">path</span> = <span class="ruby-identifier">value</span>[<span class="ruby-value">:path</span>] <span class="ruby-identifier">regexp</span> = <span class="ruby-keyword">if</span> <span class="ruby-identifier">domain</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">path</span> <span class="ruby-regexp">/\A#{key}=.*(?:domain=#{domain}(?:;|$).*path=#{path}(?:;|$)|path=#{path}(?:;|$).*domain=#{domain}(?:;|$))/</span> <span class="ruby-keyword">else</span> <span class="ruby-regexp">/\A#{key}=.*domain=#{domain}(?:;|$)/</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">path</span> <span class="ruby-regexp">/\A#{key}=.*path=#{path}(?:;|$)/</span> <span class="ruby-keyword">else</span> <span class="ruby-regexp">/\A#{key}=/</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">cookies</span>.<span class="ruby-identifier">reject!</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">cookie</span><span class="ruby-operator">|</span> <span class="ruby-identifier">regexp</span>.<span class="ruby-identifier">match?</span> <span class="ruby-identifier">cookie</span> } <span class="ruby-identifier">cookies</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">"\n"</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-parse_cookies" class="method-detail "> <div class="method-heading"> <span class="method-name">parse_cookies</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="parse_cookies-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 220</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parse_cookies</span>(<span class="ruby-identifier">env</span>) <span class="ruby-identifier">parse_cookies_header</span> <span class="ruby-identifier">env</span>[<span class="ruby-constant">HTTP_COOKIE</span>] <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-parse_cookies_header" class="method-detail "> <div class="method-heading"> <span class="method-name">parse_cookies_header</span><span class="method-args">(header)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="parse_cookies_header-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 224</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parse_cookies_header</span>(<span class="ruby-identifier">header</span>) <span class="ruby-comment"># According to RFC 6265:</span> <span class="ruby-comment"># The syntax for cookie headers only supports semicolons</span> <span class="ruby-comment"># User Agent -> Server ==</span> <span class="ruby-comment"># Cookie: SID=31d4d96e407aad42; lang=en-US</span> <span class="ruby-keyword">return</span> {} <span class="ruby-keyword">unless</span> <span class="ruby-identifier">header</span> <span class="ruby-identifier">header</span>.<span class="ruby-identifier">split</span>(<span class="ruby-regexp">/[;] */n</span>).<span class="ruby-identifier">each_with_object</span>({}) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">cookie</span>, <span class="ruby-identifier">cookies</span><span class="ruby-operator">|</span> <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">cookie</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span> = <span class="ruby-identifier">cookie</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">'='</span>, <span class="ruby-value">2</span>) <span class="ruby-identifier">cookies</span>[<span class="ruby-identifier">key</span>] = (<span class="ruby-identifier">unescape</span>(<span class="ruby-identifier">value</span>) <span class="ruby-keyword">rescue</span> <span class="ruby-identifier">value</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">cookies</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-identifier">key</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-parse_nested_query" class="method-detail "> <div class="method-heading"> <span class="method-name">parse_nested_query</span><span class="method-args">(qs, d = nil)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="parse_nested_query-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 112</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parse_nested_query</span>(<span class="ruby-identifier">qs</span>, <span class="ruby-identifier">d</span> = <span class="ruby-keyword">nil</span>) <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Utils</span>.<span class="ruby-identifier">default_query_parser</span>.<span class="ruby-identifier">parse_nested_query</span>(<span class="ruby-identifier">qs</span>, <span class="ruby-identifier">d</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-parse_query" class="method-detail "> <div class="method-heading"> <span class="method-name">parse_query</span><span class="method-args">(qs, d = nil, &unescaper)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="parse_query-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 108</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parse_query</span>(<span class="ruby-identifier">qs</span>, <span class="ruby-identifier">d</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">unescaper</span>) <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Utils</span>.<span class="ruby-identifier">default_query_parser</span>.<span class="ruby-identifier">parse_query</span>(<span class="ruby-identifier">qs</span>, <span class="ruby-identifier">d</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">unescaper</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-q_values" class="method-detail "> <div class="method-heading"> <span class="method-name">q_values</span><span class="method-args">(q_value_header)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="q_values-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 144</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">q_values</span>(<span class="ruby-identifier">q_value_header</span>) <span class="ruby-identifier">q_value_header</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-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">part</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span>, <span class="ruby-identifier">parameters</span> = <span class="ruby-identifier">part</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">';'</span>, <span class="ruby-value">2</span>).<span class="ruby-identifier">map</span>(<span class="ruby-operator">&</span><span class="ruby-value">:strip</span>) <span class="ruby-identifier">quality</span> = <span class="ruby-value">1.0</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">parameters</span> <span class="ruby-operator">&&</span> (<span class="ruby-identifier">md</span> = <span class="ruby-regexp">/\Aq=([\d.]+)/</span>.<span class="ruby-identifier">match</span>(<span class="ruby-identifier">parameters</span>)) <span class="ruby-identifier">quality</span> = <span class="ruby-identifier">md</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_f</span> <span class="ruby-keyword">end</span> [<span class="ruby-identifier">value</span>, <span class="ruby-identifier">quality</span>] <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-rfc2109" class="method-detail "> <div class="method-heading"> <span class="method-name">rfc2109</span><span class="method-args">(time)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Modified version of stdlib time.rb Time#rfc2822 to use '%d-%b-%Y' instead of '% %b %Y'. It assumes that the time is in GMT to comply to the RFC 2109.</p> <p>NOTE: I'm not sure the RFC says it requires GMT, but is ambiguous enough that I'm certain someone implemented only that option. Do not use %a and %b from Time.strptime, it would use localized names for weekday and month.</p> <div class="method-source-code" id="rfc2109-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 343</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">rfc2109</span>(<span class="ruby-identifier">time</span>) <span class="ruby-identifier">wday</span> = <span class="ruby-constant">RFC2822_DAY_NAME</span>[<span class="ruby-identifier">time</span>.<span class="ruby-identifier">wday</span>] <span class="ruby-identifier">mon</span> = <span class="ruby-constant">RFC2822_MONTH_NAME</span>[<span class="ruby-identifier">time</span>.<span class="ruby-identifier">mon</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>] <span class="ruby-identifier">time</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-node">"#{wday}, %d-#{mon}-%Y %H:%M:%S GMT"</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-rfc2822" class="method-detail "> <div class="method-heading"> <span class="method-name">rfc2822</span><span class="method-args">(time)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="rfc2822-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 330</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">rfc2822</span>(<span class="ruby-identifier">time</span>) <span class="ruby-identifier">time</span>.<span class="ruby-identifier">rfc2822</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-secure_compare" class="method-detail "> <div class="method-heading"> <span class="method-name">secure_compare</span><span class="method-args">(a, b)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Constant time string comparison.</p> <p>NOTE: the values compared should be of fixed length, such as strings that have already been processed by HMAC. This should not be used on variable length plaintext strings because it could leak length info via timing attacks.</p> <div class="method-source-code" id="secure_compare-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 395</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">secure_compare</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>) <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">bytesize</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">bytesize</span> <span class="ruby-identifier">l</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">unpack</span>(<span class="ruby-string">"C*"</span>) <span class="ruby-identifier">r</span>, <span class="ruby-identifier">i</span> = <span class="ruby-value">0</span>, <span class="ruby-value">-1</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">each_byte</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">r</span> <span class="ruby-operator">|=</span> <span class="ruby-identifier">v</span> <span class="ruby-operator">^</span> <span class="ruby-identifier">l</span>[<span class="ruby-identifier">i</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>] } <span class="ruby-identifier">r</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-select_best_encoding" class="method-detail "> <div class="method-heading"> <span class="method-name">select_best_encoding</span><span class="method-args">(available_encodings, accept_encoding)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="select_best_encoding-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 188</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">select_best_encoding</span>(<span class="ruby-identifier">available_encodings</span>, <span class="ruby-identifier">accept_encoding</span>) <span class="ruby-comment"># http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html</span> <span class="ruby-identifier">expanded_accept_encoding</span> = [] <span class="ruby-identifier">accept_encoding</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">m</span>, <span class="ruby-identifier">q</span><span class="ruby-operator">|</span> <span class="ruby-identifier">preference</span> = <span class="ruby-identifier">available_encodings</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">m</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">available_encodings</span>.<span class="ruby-identifier">size</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">m</span> <span class="ruby-operator">==</span> <span class="ruby-string">"*"</span> (<span class="ruby-identifier">available_encodings</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">accept_encoding</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&</span><span class="ruby-value">:first</span>)).<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">m2</span><span class="ruby-operator">|</span> <span class="ruby-identifier">expanded_accept_encoding</span> <span class="ruby-operator"><<</span> [<span class="ruby-identifier">m2</span>, <span class="ruby-identifier">q</span>, <span class="ruby-identifier">preference</span>] <span class="ruby-keyword">end</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">expanded_accept_encoding</span> <span class="ruby-operator"><<</span> [<span class="ruby-identifier">m</span>, <span class="ruby-identifier">q</span>, <span class="ruby-identifier">preference</span>] <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-identifier">encoding_candidates</span> = <span class="ruby-identifier">expanded_accept_encoding</span> .<span class="ruby-identifier">sort_by</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">_</span>, <span class="ruby-identifier">q</span>, <span class="ruby-identifier">p</span><span class="ruby-operator">|</span> [<span class="ruby-operator">-</span><span class="ruby-identifier">q</span>, <span class="ruby-identifier">p</span>] } .<span class="ruby-identifier">map!</span>(<span class="ruby-operator">&</span><span class="ruby-value">:first</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">encoding_candidates</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-string">"identity"</span>) <span class="ruby-identifier">encoding_candidates</span>.<span class="ruby-identifier">push</span>(<span class="ruby-string">"identity"</span>) <span class="ruby-keyword">end</span> <span class="ruby-identifier">expanded_accept_encoding</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">m</span>, <span class="ruby-identifier">q</span><span class="ruby-operator">|</span> <span class="ruby-identifier">encoding_candidates</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">m</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">q</span> <span class="ruby-operator">==</span> <span class="ruby-value">0.0</span> <span class="ruby-keyword">end</span> (<span class="ruby-identifier">encoding_candidates</span> <span class="ruby-operator">&</span> <span class="ruby-identifier">available_encodings</span>)[<span class="ruby-value">0</span>] <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-set_cookie_header-21" class="method-detail "> <div class="method-heading"> <span class="method-name">set_cookie_header!</span><span class="method-args">(header, key, value)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="set_cookie_header-21-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 278</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">set_cookie_header!</span>(<span class="ruby-identifier">header</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>) <span class="ruby-identifier">header</span>[<span class="ruby-constant">SET_COOKIE</span>] = <span class="ruby-identifier">add_cookie_to_header</span>(<span class="ruby-identifier">header</span>[<span class="ruby-constant">SET_COOKIE</span>], <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>) <span class="ruby-keyword">nil</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-status_code" class="method-detail "> <div class="method-heading"> <span class="method-name">status_code</span><span class="method-args">(status)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <div class="method-source-code" id="status_code-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 599</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">status_code</span>(<span class="ruby-identifier">status</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">status</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>) <span class="ruby-constant">SYMBOL_TO_STATUS_CODE</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-identifier">status</span>) { <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">"Unrecognized status code #{status.inspect}"</span> } <span class="ruby-keyword">else</span> <span class="ruby-identifier">status</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-unescape" class="method-detail "> <div class="method-heading"> <span class="method-name">unescape</span><span class="method-args">(s, encoding = Encoding::UTF_8)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Unescapes a URI escaped string with <code>encoding</code>. <code>encoding</code> will be the target encoding of the string returned, and it defaults to UTF-8</p> <div class="method-source-code" id="unescape-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 56</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">unescape</span>(<span class="ruby-identifier">s</span>, <span class="ruby-identifier">encoding</span> = <span class="ruby-constant">Encoding</span><span class="ruby-operator">::</span><span class="ruby-constant">UTF_8</span>) <span class="ruby-constant">URI</span>.<span class="ruby-identifier">decode_www_form_component</span>(<span class="ruby-identifier">s</span>, <span class="ruby-identifier">encoding</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-unescape_path" class="method-detail "> <div class="method-heading"> <span class="method-name">unescape_path</span><span class="method-args">(s)</span> <span class="method-click-advice">click to toggle source</span> </div> <div class="method-description"> <p>Unescapes the *<strong>path</strong>* component of a URI. See <a href="Utils.html#method-i-unescape"><code>Rack::Utils.unescape</code></a> for unescaping query parameters or form components.</p> <div class="method-source-code" id="unescape_path-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 50</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">unescape_path</span>(<span class="ruby-identifier">s</span>) <span class="ruby-constant">RFC2396_PARSER</span>.<span class="ruby-identifier">unescape</span> <span class="ruby-identifier">s</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-valid_path-3F" class="method-detail "> <div class="method-heading"> <span class="method-name">valid_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="valid_path-3F-source"> <pre><span class="ruby-comment"># File lib/rack/utils.rb, line 626</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">valid_path?</span>(<span class="ruby-identifier">path</span>) <span class="ruby-identifier">path</span>.<span class="ruby-identifier">valid_encoding?</span> <span class="ruby-operator">&&</span> <span class="ruby-operator">!</span><span class="ruby-identifier">path</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-constant">NULL_BYTE</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>