Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 4b137326f38b5adf1f9ffd3dbbb48a7b > files > 975

deltacloud-core-doc-0.4.0-4.fc15.noarch.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>call (Rack::MatrixParams)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File lib/sinatra/rack_matrix_params.rb, line 40</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>)
      <span class="ruby-comment cmt"># Copy PATH_INFO to REQUEST_URI if Rack::Test</span>
      <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_URI'</span>] = <span class="ruby-identifier">env</span>[<span class="ruby-value str">'PATH_INFO'</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">env</span>[<span class="ruby-value str">'rack.test'</span>]
      <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_PATH'</span>] = <span class="ruby-identifier">env</span>[<span class="ruby-value str">'PATH_INFO'</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">env</span>[<span class="ruby-value str">'rack.test'</span>]

      <span class="ruby-comment cmt"># Split URI to components and then extract ;var=value pairs</span>
      <span class="ruby-identifier">uri_components</span> = <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_URI'</span>].<span class="ruby-identifier">split</span>(<span class="ruby-value str">'/'</span>)
      <span class="ruby-identifier">matrix_params</span> = {}
      <span class="ruby-identifier">uri_components</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">component</span><span class="ruby-operator">|</span>
        <span class="ruby-identifier">sub_components</span>, <span class="ruby-identifier">value</span> = <span class="ruby-identifier">component</span>.<span class="ruby-identifier">split</span>(<span class="ruby-regexp re">/\;(\w+)\=/</span>), <span class="ruby-keyword kw">nil</span>
        <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">sub_components</span>.<span class="ruby-identifier">first</span>  <span class="ruby-comment cmt"># Skip subcomponent if it's empty (usually /)</span>
        <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">param</span>=<span class="ruby-identifier">sub_components</span>.<span class="ruby-identifier">pop</span> <span class="ruby-keyword kw">do</span>
          <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>
            <span class="ruby-identifier">matrix_params</span>[<span class="ruby-identifier">sub_components</span>.<span class="ruby-identifier">first</span>] <span class="ruby-operator">||=</span> {}
            <span class="ruby-identifier">matrix_params</span>[<span class="ruby-identifier">sub_components</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">merge!</span>(
                                                       <span class="ruby-identifier">param</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">value</span>
                                                       )
            <span class="ruby-identifier">value</span>=<span class="ruby-keyword kw">nil</span>
            <span class="ruby-keyword kw">next</span>
          <span class="ruby-keyword kw">else</span>
            <span class="ruby-identifier">value</span> = <span class="ruby-identifier">param</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/\?.*$/</span>, <span class="ruby-value str">''</span>)
          <span class="ruby-keyword kw">end</span>
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">end</span>

      <span class="ruby-comment cmt"># Two things need to happen to make matrix params work:</span>
      <span class="ruby-comment cmt">#     (1) the parameters need to be appended to the 'normal' params</span>
      <span class="ruby-comment cmt">#         for the request. 'Normal' really depends on the content</span>
      <span class="ruby-comment cmt">#         type of the request, which does not seem accessible from</span>
      <span class="ruby-comment cmt">#         Middleware, so we use the existence of</span>
      <span class="ruby-comment cmt">#         rack.request.form_hash in the environment to distinguish</span>
      <span class="ruby-comment cmt">#         between basic and application/x-www-form-urlencoded</span>
      <span class="ruby-comment cmt">#         requests</span>
      <span class="ruby-comment cmt">#     (2) the parameters need to be stripped from the appropriate</span>
      <span class="ruby-comment cmt">#         path related env variables, so that request dispatching</span>
      <span class="ruby-comment cmt">#         does not trip over them</span>

      <span class="ruby-comment cmt"># (1) Rewrite current path by stripping all matrix params from it</span>
      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_PATH'</span>] <span class="ruby-operator">==</span> <span class="ruby-value str">'/'</span>
        <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_URI'</span>] = <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_PATH'</span>]
        <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_PATH'</span>] = <span class="ruby-identifier">env</span>[<span class="ruby-value str">'PATH_INFO'</span>]
      <span class="ruby-keyword kw">end</span>
      <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_PATH'</span>] = <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_PATH'</span>].<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/;([^\/]*)/</span>, <span class="ruby-value str">''</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/\?(.*)$/</span>, <span class="ruby-value str">''</span>)
      <span class="ruby-identifier">env</span>[<span class="ruby-value str">'PATH_INFO'</span>] = <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_PATH'</span>]

      <span class="ruby-comment cmt"># (2) Append the matrix params to the 'normal' request params</span>
      <span class="ruby-comment cmt"># FIXME: Make this work for multipart/form-data</span>
      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">env</span>[<span class="ruby-value str">'rack.request.form_hash'</span>]
        <span class="ruby-comment cmt"># application/x-www-form-urlencoded, most likely a POST</span>
        <span class="ruby-identifier">env</span>[<span class="ruby-value str">'rack.request.form_hash'</span>].<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">matrix_params</span>)
      <span class="ruby-keyword kw">else</span>
        <span class="ruby-comment cmt"># For other methods it's more complicated</span>
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">env</span>[<span class="ruby-value str">'REQUEST_METHOD'</span>]<span class="ruby-operator">!=</span><span class="ruby-value str">'POST'</span> <span class="ruby-keyword kw">and</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">matrix_params</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">empty?</span>
          <span class="ruby-identifier">env</span>[<span class="ruby-value str">'QUERY_STRING'</span>].<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp re">/;([^\/]*)/</span>, <span class="ruby-value str">''</span>)
          <span class="ruby-identifier">new_params</span> = <span class="ruby-identifier">matrix_params</span>.<span class="ruby-identifier">collect</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">component</span>, <span class="ruby-identifier">params</span><span class="ruby-operator">|</span>
            <span class="ruby-identifier">params</span>.<span class="ruby-identifier">collect</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-node">&quot;#{component}[#{k}]=#{CGI::escape(v.to_s)}&quot;</span> }
          <span class="ruby-keyword kw">end</span>.<span class="ruby-identifier">flatten</span>
          <span class="ruby-comment cmt"># Add matrix params as a regular GET params</span>
          <span class="ruby-identifier">env</span>[<span class="ruby-value str">'QUERY_STRING'</span>] <span class="ruby-operator">+=</span> <span class="ruby-value str">'&amp;'</span> <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">env</span>[<span class="ruby-value str">'QUERY_STRING'</span>].<span class="ruby-identifier">empty?</span>
          <span class="ruby-identifier">env</span>[<span class="ruby-value str">'QUERY_STRING'</span>] <span class="ruby-operator">+=</span> <span class="ruby-node">&quot;#{new_params.join('&amp;')}&quot;</span>
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">end</span>
      <span class="ruby-ivar">@app</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>)
    <span class="ruby-keyword kw">end</span></pre>
</body>
</html>