You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2011/08/16 17:53:41 UTC

svn commit: r1158338 - /incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb

Author: mfojtik
Date: Tue Aug 16 15:53:41 2011
New Revision: 1158338

URL: http://svn.apache.org/viewvc?rev=1158338&view=rev
Log:
* lib/sinatra/rack_matrix_params.rb: rewrite paths for all reqs

We didn't rewrite PATH_INFO, REQUEST_PATH and REQUEST_URI for POST
requests, which made dispatching fail if matrix parameters were present.

Signed-off-by: David Lutterkort <lu...@redhat.com>

Modified:
    incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb

Modified: incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb?rev=1158338&r1=1158337&r2=1158338&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb (original)
+++ incubator/deltacloud/trunk/server/lib/sinatra/rack_matrix_params.rb Tue Aug 16 15:53:41 2011
@@ -62,25 +62,42 @@ module Rack
         end
       end
 
-      # If request method is POST, simply include matrix params in form_hash
-      env['rack.request.form_hash'].merge!(matrix_params) if env['rack.request.form_hash']
+      # Two things need to happen to make matrix params work:
+      #     (1) the parameters need to be appended to the 'normal' params
+      #         for the request. 'Normal' really depends on the content
+      #         type of the request, which does not seem accessible from
+      #         Middleware, so we use the existence of
+      #         rack.request.form_hash in the environment to distinguish
+      #         between basic and application/x-www-form-urlencoded
+      #         requests
+      #     (2) the parameters need to be stripped from the appropriate
+      #         path related env variables, so that request dispatching
+      #         does not trip over them
 
-      # For other methods it's a way complicated ;-)
-      if env['REQUEST_METHOD']!='POST' and not matrix_params.keys.empty?
-        if env['REQUEST_PATH'] == '/'
-          env['REQUEST_URI'] = env['REQUEST_PATH']
-          env['REQUEST_PATH'] = env['PATH_INFO']
+      # (1) Rewrite current path by stripping all matrix params from it
+      if env['REQUEST_PATH'] == '/'
+        env['REQUEST_URI'] = env['REQUEST_PATH']
+        env['REQUEST_PATH'] = env['PATH_INFO']
+      end
+      env['REQUEST_PATH'] = env['REQUEST_PATH'].gsub(/;([^\/]*)/, '').gsub(/\?(.*)$/, '')
+      env['PATH_INFO'] = env['REQUEST_PATH']
+
+      # (2) Append the matrix params to the 'normal' request params
+      # FIXME: Make this work for multipart/form-data
+      if env['rack.request.form_hash']
+        # application/x-www-form-urlencoded, most likely a POST
+        env['rack.request.form_hash'].merge!(matrix_params)
+      else
+        # For other methods it's more complicated
+        if env['REQUEST_METHOD']!='POST' and not matrix_params.keys.empty?
+          env['QUERY_STRING'].gsub!(/;([^\/]*)/, '')
+          new_params = matrix_params.collect do |component, params|
+            params.collect { |k,v| "#{component}[#{k}]=#{CGI::escape(v.to_s)}" }
+          end.flatten
+          # Add matrix params as a regular GET params
+          env['QUERY_STRING'] += '&' if not env['QUERY_STRING'].empty?
+          env['QUERY_STRING'] += "#{new_params.join('&')}"
         end
-        # Rewrite current path and query string and strip all matrix params from it
-        env['REQUEST_PATH'] = env['REQUEST_PATH'].gsub(/;([^\/]*)/, '').gsub(/\?(.*)$/, '')
-        env['PATH_INFO'] = env['REQUEST_PATH']
-        env['QUERY_STRING'].gsub!(/;([^\/]*)/, '')
-        new_params = matrix_params.collect do |component, params|
-          params.collect { |k,v| "#{component}[#{k}]=#{CGI::escape(v.to_s)}" }
-        end.flatten
-        # Add matrix params as a regular GET params
-        env['QUERY_STRING'] += '&' if not env['QUERY_STRING'].empty?
-        env['QUERY_STRING'] += "#{new_params.join('&')}"
       end
       @app.call(env)
     end