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

214 lines
11 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>Class: Rack::URLMap</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>Class</strong></td>
<td class="class-name-in-header">Rack::URLMap</td>
</tr>
<tr class="top-aligned-row">
<td><strong>In:</strong></td>
<td>
<a href="../../files/lib/rack/urlmap_rb.html">
lib/rack/urlmap.rb
</a>
<br />
</td>
</tr>
<tr class="top-aligned-row">
<td><strong>Parent:</strong></td>
<td>
Object
</td>
</tr>
</table>
</div>
<!-- banner header -->
<div id="bodyContent">
<div id="contextContent">
<div id="description">
<p>
<a href="URLMap.html">Rack::URLMap</a> takes a hash mapping urls or paths
to apps, and dispatches accordingly. Support for HTTP/1.1 host names exists
if the URLs start with <tt>http://</tt> or <tt>https://</tt>.
</p>
<p>
<a href="URLMap.html">URLMap</a> modifies the SCRIPT_NAME and PATH_INFO
such that the part relevant for dispatch is in the SCRIPT_NAME, and the
rest in the PATH_INFO. This should be taken care of when you need to
reconstruct the URL in order to create links.
</p>
<p>
<a href="URLMap.html">URLMap</a> dispatches in such a way that the longest
paths are tried first, since they are most specific.
</p>
</div>
</div>
<div id="method-list">
<h3 class="section-bar">Methods</h3>
<div class="name-list">
<a href="#M000077">call</a>&nbsp;&nbsp;
<a href="#M000076">new</a>&nbsp;&nbsp;
</div>
</div>
</div>
<!-- if includes -->
<div id="section">
<!-- if method_list -->
<div id="methods">
<h3 class="section-bar">Public Class methods</h3>
<div id="method-M000076" class="method-detail">
<a name="M000076"></a>
<div class="method-heading">
<a href="#M000076" class="method-signature">
<span class="method-name">new</span><span class="method-args">(map)</span>
</a>
</div>
<div class="method-description">
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000076-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000076-source">
<pre>
<span class="ruby-comment cmt"># File lib/rack/urlmap.rb, line 15</span>
15: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">map</span>)
16: <span class="ruby-ivar">@mapping</span> = <span class="ruby-identifier">map</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">location</span>, <span class="ruby-identifier">app</span><span class="ruby-operator">|</span>
17: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">location</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">%r{\Ahttps?://(.*?)(/.*)}</span>
18: <span class="ruby-identifier">host</span>, <span class="ruby-identifier">location</span> = <span class="ruby-identifier">$1</span>, <span class="ruby-identifier">$2</span>
19: <span class="ruby-keyword kw">else</span>
20: <span class="ruby-identifier">host</span> = <span class="ruby-keyword kw">nil</span>
21: <span class="ruby-keyword kw">end</span>
22:
23: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">location</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-value">?/</span>
24: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">&quot;paths need to start with /&quot;</span>
25: <span class="ruby-keyword kw">end</span>
26: <span class="ruby-identifier">location</span> = <span class="ruby-identifier">location</span>.<span class="ruby-identifier">chomp</span>(<span class="ruby-value str">'/'</span>)
27:
28: [<span class="ruby-identifier">host</span>, <span class="ruby-identifier">location</span>, <span class="ruby-identifier">app</span>]
29: }.<span class="ruby-identifier">sort_by</span> { <span class="ruby-operator">|</span>(<span class="ruby-identifier">h</span>, <span class="ruby-identifier">l</span>, <span class="ruby-identifier">a</span>)<span class="ruby-operator">|</span> [<span class="ruby-operator">-</span><span class="ruby-identifier">l</span>.<span class="ruby-identifier">size</span>, <span class="ruby-identifier">h</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">size</span>] } <span class="ruby-comment cmt"># Longest path first</span>
30: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
<h3 class="section-bar">Public Instance methods</h3>
<div id="method-M000077" class="method-detail">
<a name="M000077"></a>
<div class="method-heading">
<a href="#M000077" class="method-signature">
<span class="method-name">call</span><span class="method-args">(env)</span>
</a>
</div>
<div class="method-description">
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000077-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000077-source">
<pre>
<span class="ruby-comment cmt"># File lib/rack/urlmap.rb, line 32</span>
32: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>)
33: <span class="ruby-identifier">path</span> = <span class="ruby-identifier">env</span>[<span class="ruby-value str">&quot;PATH_INFO&quot;</span>].<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">squeeze</span>(<span class="ruby-value str">&quot;/&quot;</span>)
34: <span class="ruby-identifier">hHost</span>, <span class="ruby-identifier">sName</span>, <span class="ruby-identifier">sPort</span> = <span class="ruby-identifier">env</span>.<span class="ruby-identifier">values_at</span>(<span class="ruby-value str">'HTTP_HOST'</span>,<span class="ruby-value str">'SERVER_NAME'</span>,<span class="ruby-value str">'SERVER_PORT'</span>)
35: <span class="ruby-ivar">@mapping</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">host</span>, <span class="ruby-identifier">location</span>, <span class="ruby-identifier">app</span><span class="ruby-operator">|</span>
36: <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">hHost</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">host</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">sName</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">host</span> \
37: <span class="ruby-operator">||</span> (<span class="ruby-identifier">host</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">hHost</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">sName</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">hHost</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">sName</span><span class="ruby-operator">+</span><span class="ruby-value str">':'</span><span class="ruby-operator">+</span><span class="ruby-identifier">sPort</span>)))
38: <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">location</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">path</span>[<span class="ruby-value">0</span>, <span class="ruby-identifier">location</span>.<span class="ruby-identifier">size</span>]
39: <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">path</span>[<span class="ruby-identifier">location</span>.<span class="ruby-identifier">size</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">path</span>[<span class="ruby-identifier">location</span>.<span class="ruby-identifier">size</span>] <span class="ruby-operator">==</span> <span class="ruby-value">?/</span>
40: <span class="ruby-identifier">env</span>[<span class="ruby-value str">&quot;SCRIPT_NAME&quot;</span>] <span class="ruby-operator">+=</span> <span class="ruby-identifier">location</span>
41: <span class="ruby-identifier">env</span>[<span class="ruby-value str">&quot;PATH_INFO&quot;</span>] = <span class="ruby-identifier">path</span>[<span class="ruby-identifier">location</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>]
42: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">app</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>)
43: }
44: [<span class="ruby-value">404</span>, {<span class="ruby-value str">&quot;Content-Type&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;text/plain&quot;</span>}, [<span class="ruby-node">&quot;Not Found: #{path}&quot;</span>]]
45: <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>