instiki/vendor/plugins/rack/doc/classes/Rack/Utils.html

377 lines
21 KiB
HTML
Raw Normal View History

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Module: Rack::Utils</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
<script type="text/javascript">
// <![CDATA[
function popupCode( url ) {
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
}
function toggleCode( id ) {
if ( document.getElementById )
elem = document.getElementById( id );
else if ( document.all )
elem = eval( "document.all." + id );
else
return false;
elemStyle = elem.style;
if ( elemStyle.display != "block" ) {
elemStyle.display = "block"
} else {
elemStyle.display = "none"
}
return true;
}
// Make codeblocks hidden by default
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
// ]]>
</script>
</head>
<body>
<div id="classHeader">
<table class="header-table">
<tr class="top-aligned-row">
<td><strong>Module</strong></td>
<td class="class-name-in-header">Rack::Utils</td>
</tr>
<tr class="top-aligned-row">
<td><strong>In:</strong></td>
<td>
<a href="../../files/lib/rack/utils_rb.html">
lib/rack/utils.rb
</a>
<br />
</td>
</tr>
</table>
</div>
<!-- banner header -->
<div id="bodyContent">
<div id="contextContent">
<div id="description">
<p>
<a href="Utils.html">Rack::Utils</a> contains a grab-bag of useful methods
for writing web applications adopted from all kinds of Ruby libraries.
</p>
</div>
</div>
<div id="method-list">
<h3 class="section-bar">Methods</h3>
<div class="name-list">
<a href="#M000025">build_query</a>&nbsp;&nbsp;
<a href="#M000022">escape</a>&nbsp;&nbsp;
<a href="#M000026">escape_html</a>&nbsp;&nbsp;
<a href="#M000024">parse_query</a>&nbsp;&nbsp;
<a href="#M000027">select_best_encoding</a>&nbsp;&nbsp;
<a href="#M000023">unescape</a>&nbsp;&nbsp;
</div>
</div>
</div>
<!-- if includes -->
<div id="section">
<div id="class-list">
<h3 class="section-bar">Classes and Modules</h3>
Module <a href="Utils/Multipart.html" class="link">Rack::Utils::Multipart</a><br />
Class <a href="Utils/Context.html" class="link">Rack::Utils::Context</a><br />
Class <a href="Utils/HeaderHash.html" class="link">Rack::Utils::HeaderHash</a><br />
</div>
<div id="constants-list">
<h3 class="section-bar">Constants</h3>
<div class="name-list">
<table summary="Constants">
<tr class="top-aligned-row context-row">
<td class="context-item-name">HTTP_STATUS_CODES</td>
<td>=</td>
<td class="context-item-value">{ 100 =&gt; 'Continue', 101 =&gt; 'Switching Protocols', 200 =&gt; 'OK', 201 =&gt; 'Created', 202 =&gt; 'Accepted', 203 =&gt; 'Non-Authoritative Information', 204 =&gt; 'No Content', 205 =&gt; 'Reset Content', 206 =&gt; 'Partial Content', 300 =&gt; 'Multiple Choices', 301 =&gt; 'Moved Permanently', 302 =&gt; 'Found', 303 =&gt; 'See Other', 304 =&gt; 'Not Modified', 305 =&gt; 'Use Proxy', 307 =&gt; 'Temporary Redirect', 400 =&gt; 'Bad Request', 401 =&gt; 'Unauthorized', 402 =&gt; 'Payment Required', 403 =&gt; 'Forbidden', 404 =&gt; 'Not Found', 405 =&gt; 'Method Not Allowed', 406 =&gt; 'Not Acceptable', 407 =&gt; 'Proxy Authentication Required', 408 =&gt; 'Request Timeout', 409 =&gt; 'Conflict', 410 =&gt; 'Gone', 411 =&gt; 'Length Required', 412 =&gt; 'Precondition Failed', 413 =&gt; 'Request Entity Too Large', 414 =&gt; 'Request-URI Too Large', 415 =&gt; 'Unsupported Media Type', 416 =&gt; 'Requested Range Not Satisfiable', 417 =&gt; 'Expectation Failed', 500 =&gt; 'Internal Server Error', 501 =&gt; 'Not Implemented', 502 =&gt; 'Bad Gateway', 503 =&gt; 'Service Unavailable', 504 =&gt; 'Gateway Timeout', 505 =&gt; 'HTTP Version Not Supported'</td>
<td width="3em">&nbsp;</td>
<td class="context-item-desc">
Every standard HTTP code mapped to the appropriate message. Stolen from
Mongrel.
</td>
</tr>
<tr class="top-aligned-row context-row">
<td class="context-item-name">STATUS_WITH_NO_ENTITY_BODY</td>
<td>=</td>
<td class="context-item-value">Set.new((100..199).to_a &lt;&lt; 204 &lt;&lt; 304)</td>
<td width="3em">&nbsp;</td>
<td class="context-item-desc">
Responses with HTTP status codes that should not have an entity body
</td>
</tr>
</table>
</div>
</div>
<!-- if method_list -->
<div id="methods">
<h3 class="section-bar">Public Instance methods</h3>
<div id="method-M000025" class="method-detail">
<a name="M000025"></a>
<div class="method-heading">
<a href="#M000025" class="method-signature">
<span class="method-name">build_query</span><span class="method-args">(params)</span>
</a>
</div>
<div class="method-description">
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000025-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000025-source">
<pre>
<span class="ruby-comment cmt"># File lib/rack/utils.rb, line 54</span>
54: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">build_query</span>(<span class="ruby-identifier">params</span>)
55: <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>
56: <span class="ruby-keyword kw">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>
57: <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>] })
58: <span class="ruby-keyword kw">else</span>
59: <span class="ruby-identifier">escape</span>(<span class="ruby-identifier">k</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;=&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">escape</span>(<span class="ruby-identifier">v</span>)
60: <span class="ruby-keyword kw">end</span>
61: }.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;&amp;&quot;</span>)
62: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div id="method-M000022" class="method-detail">
<a name="M000022"></a>
<div class="method-heading">
<a href="#M000022" class="method-signature">
<span class="method-name">escape</span><span class="method-args">(s)</span>
</a>
</div>
<div class="method-description">
<p>
Performs URI escaping so that you can construct proper query strings
faster. Use this rather than the cgi.rb version since it&#8216;s faster.
(Stolen from Camping).
</p>
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000022-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000022-source">
<pre>
<span class="ruby-comment cmt"># File lib/rack/utils.rb, line 12</span>
12: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">escape</span>(<span class="ruby-identifier">s</span>)
13: <span class="ruby-identifier">s</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/([^ a-zA-Z0-9_.-]+)/n</span>) {
14: <span class="ruby-value str">'%'</span><span class="ruby-operator">+</span><span class="ruby-identifier">$1</span>.<span class="ruby-identifier">unpack</span>(<span class="ruby-value str">'H2'</span><span class="ruby-operator">*</span><span class="ruby-identifier">$1</span>.<span class="ruby-identifier">size</span>).<span class="ruby-identifier">join</span>(<span class="ruby-value str">'%'</span>).<span class="ruby-identifier">upcase</span>
15: }.<span class="ruby-identifier">tr</span>(<span class="ruby-value str">' '</span>, <span class="ruby-value str">'+'</span>)
16: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div id="method-M000026" class="method-detail">
<a name="M000026"></a>
<div class="method-heading">
<a href="#M000026" class="method-signature">
<span class="method-name">escape_html</span><span class="method-args">(string)</span>
</a>
</div>
<div class="method-description">
<p>
Escape ampersands, brackets and quotes to their HTML/XML entities.
</p>
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000026-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000026-source">
<pre>
<span class="ruby-comment cmt"># File lib/rack/utils.rb, line 66</span>
66: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">escape_html</span>(<span class="ruby-identifier">string</span>)
67: <span class="ruby-identifier">string</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;&amp;&quot;</span>, <span class="ruby-value str">&quot;&amp;amp;&quot;</span>).
68: <span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;&lt;&quot;</span>, <span class="ruby-value str">&quot;&amp;lt;&quot;</span>).
69: <span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;&gt;&quot;</span>, <span class="ruby-value str">&quot;&amp;gt;&quot;</span>).
70: <span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;'&quot;</span>, <span class="ruby-value str">&quot;&amp;#39;&quot;</span>).
71: <span class="ruby-identifier">gsub</span>(<span class="ruby-value str">'&quot;'</span>, <span class="ruby-value str">&quot;&amp;quot;&quot;</span>)
72: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div id="method-M000024" class="method-detail">
<a name="M000024"></a>
<div class="method-heading">
<a href="#M000024" class="method-signature">
<span class="method-name">parse_query</span><span class="method-args">(qs, d = '&amp;;')</span>
</a>
</div>
<div class="method-description">
<p>
Stolen from Mongrel, with some small modifications: Parses a query string
by breaking it up at the &#8217;&amp;&#8217; and &#8217;;&#8217;
characters. You can also use this to parse cookies by changing the
characters used in the second parameter (which defaults to
&#8217;&amp;;&#8217;).
</p>
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000024-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000024-source">
<pre>
<span class="ruby-comment cmt"># File lib/rack/utils.rb, line 33</span>
33: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">parse_query</span>(<span class="ruby-identifier">qs</span>, <span class="ruby-identifier">d</span> = <span class="ruby-value str">'&amp;;'</span>)
34: <span class="ruby-identifier">params</span> = {}
35:
36: (<span class="ruby-identifier">qs</span> <span class="ruby-operator">||</span> <span class="ruby-value str">''</span>).<span class="ruby-identifier">split</span>(<span class="ruby-node">/[#{d}] */n</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">p</span><span class="ruby-operator">|</span>
37: <span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span> = <span class="ruby-identifier">unescape</span>(<span class="ruby-identifier">p</span>).<span class="ruby-identifier">split</span>(<span class="ruby-value str">'='</span>, <span class="ruby-value">2</span>)
38:
39: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cur</span> = <span class="ruby-identifier">params</span>[<span class="ruby-identifier">k</span>]
40: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cur</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Array</span>
41: <span class="ruby-identifier">params</span>[<span class="ruby-identifier">k</span>] <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">v</span>
42: <span class="ruby-keyword kw">else</span>
43: <span class="ruby-identifier">params</span>[<span class="ruby-identifier">k</span>] = [<span class="ruby-identifier">cur</span>, <span class="ruby-identifier">v</span>]
44: <span class="ruby-keyword kw">end</span>
45: <span class="ruby-keyword kw">else</span>
46: <span class="ruby-identifier">params</span>[<span class="ruby-identifier">k</span>] = <span class="ruby-identifier">v</span>
47: <span class="ruby-keyword kw">end</span>
48: <span class="ruby-keyword kw">end</span>
49:
50: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">params</span>
51: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div id="method-M000027" class="method-detail">
<a name="M000027"></a>
<div class="method-heading">
<a href="#M000027" class="method-signature">
<span class="method-name">select_best_encoding</span><span class="method-args">(available_encodings, accept_encoding)</span>
</a>
</div>
<div class="method-description">
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000027-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000027-source">
<pre>
<span class="ruby-comment cmt"># File lib/rack/utils.rb, line 75</span>
75: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">select_best_encoding</span>(<span class="ruby-identifier">available_encodings</span>, <span class="ruby-identifier">accept_encoding</span>)
76: <span class="ruby-comment cmt"># http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html</span>
77:
78: <span class="ruby-identifier">expanded_accept_encoding</span> =
79: <span class="ruby-identifier">accept_encoding</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">m</span>, <span class="ruby-identifier">q</span><span class="ruby-operator">|</span>
80: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">m</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;*&quot;</span>
81: (<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-identifier">m2</span>, <span class="ruby-identifier">_</span><span class="ruby-operator">|</span> <span class="ruby-identifier">m2</span> }).<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">m2</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">m2</span>, <span class="ruby-identifier">q</span>] }
82: <span class="ruby-keyword kw">else</span>
83: [[<span class="ruby-identifier">m</span>, <span class="ruby-identifier">q</span>]]
84: <span class="ruby-keyword kw">end</span>
85: }.<span class="ruby-identifier">inject</span>([]) { <span class="ruby-operator">|</span><span class="ruby-identifier">mem</span>, <span class="ruby-identifier">list</span><span class="ruby-operator">|</span>
86: <span class="ruby-identifier">mem</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">list</span>
87: }
88:
89: <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-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">q</span> }.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">m</span>, <span class="ruby-identifier">_</span><span class="ruby-operator">|</span> <span class="ruby-identifier">m</span> }
90:
91: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">encoding_candidates</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-value str">&quot;identity&quot;</span>)
92: <span class="ruby-identifier">encoding_candidates</span>.<span class="ruby-identifier">push</span>(<span class="ruby-value str">&quot;identity&quot;</span>)
93: <span class="ruby-keyword kw">end</span>
94:
95: <span class="ruby-identifier">expanded_accept_encoding</span>.<span class="ruby-identifier">find_all</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">m</span>, <span class="ruby-identifier">q</span><span class="ruby-operator">|</span>
96: <span class="ruby-identifier">q</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span><span class="ruby-value">.0</span>
97: }.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">m</span>, <span class="ruby-identifier">_</span><span class="ruby-operator">|</span>
98: <span class="ruby-identifier">encoding_candidates</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">m</span>)
99: }
100:
101: <span class="ruby-keyword kw">return</span> (<span class="ruby-identifier">encoding_candidates</span> <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">available_encodings</span>)[<span class="ruby-value">0</span>]
102: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<div id="method-M000023" class="method-detail">
<a name="M000023"></a>
<div class="method-heading">
<a href="#M000023" class="method-signature">
<span class="method-name">unescape</span><span class="method-args">(s)</span>
</a>
</div>
<div class="method-description">
<p>
Unescapes a URI escaped string. (Stolen from Camping).
</p>
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000023-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000023-source">
<pre>
<span class="ruby-comment cmt"># File lib/rack/utils.rb, line 20</span>
20: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">unescape</span>(<span class="ruby-identifier">s</span>)
21: <span class="ruby-identifier">s</span>.<span class="ruby-identifier">tr</span>(<span class="ruby-value str">'+'</span>, <span class="ruby-value str">' '</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/((?:%[0-9a-fA-F]{2})+)/n</span>){
22: [<span class="ruby-identifier">$1</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value str">'%'</span>)].<span class="ruby-identifier">pack</span>(<span class="ruby-value str">'H*'</span>)
23: }
24: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="validator-badges">
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>
</body>
</html>