incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Browning <bbrow...@redhat.com>
Subject [PATCH] Generate correct URLs when not running at the root context.
Date Mon, 26 Jul 2010 18:59:37 GMT
This patch fixes the URLs generated by deltacloud-core when it
is proxied or deployed via Passenger or TorqueBox at a non-root
context.

It mainly consists of using url_for for all URLs and improving the
url_for logic to handle the cases where it gets applied multiple
times to the same url. This happens in a few places indirectly
because link_to also calls url_for on the passed-in URL.
---
 .../lib/deltacloud/helpers/application_helper.rb   |    6 +++---
 server/lib/sinatra/url_for.rb                      |    8 +++++++-
 server/server.rb                                   |    2 +-
 server/views/api/show.html.haml                    |    2 +-
 server/views/docs/collection.html.haml             |    4 ++--
 server/views/docs/collection.xml.haml              |    4 ++--
 server/views/docs/index.html.haml                  |    2 +-
 server/views/docs/index.xml.haml                   |    2 +-
 server/views/docs/operation.html.haml              |    4 ++--
 server/views/docs/operation.xml.haml               |    2 +-
 server/views/layout.html.haml                      |    3 ++-
 11 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/server/lib/deltacloud/helpers/application_helper.rb b/server/lib/deltacloud/helpers/application_helper.rb
index 94396d2..9c6d000 100644
--- a/server/lib/deltacloud/helpers/application_helper.rb
+++ b/server/lib/deltacloud/helpers/application_helper.rb
@@ -20,8 +20,8 @@
 module ApplicationHelper
 
   def bread_crumb
-    s = "<ul class='breadcrumb'><li class='first'><a href='/'>&#948</a></li>"
-    url = request.path.split('?')  #remove extra query string parameters
+    s = "<ul class='breadcrumb'><li class='first'><a href='#{url_for('/')}'>&#948</a></li>"
+    url = request.path_info.split('?')  #remove extra query string parameters
     levels = url[0].split('/') #break up url into different levels
     levels.each_with_index do |level, index|
       unless level.blank?
@@ -30,7 +30,7 @@ module ApplicationHelper
           s += "<li class='subsequent'>#{level.gsub(/_/, ' ')}</li>\n" unless
level.to_i > 0
         else
             link = levels.slice(0, index+1).join("/")
-            s += "<li class='subsequent'><a href=\"#{link}\">#{level.gsub(/_/,
' ')}</a></li>\n"
+            s += "<li class='subsequent'><a href=\"#{url_for(link)}\">#{level.gsub(/_/,
' ')}</a></li>\n"
         end
       end
     end
diff --git a/server/lib/sinatra/url_for.rb b/server/lib/sinatra/url_for.rb
index faed5b1..db305c2 100644
--- a/server/lib/sinatra/url_for.rb
+++ b/server/lib/sinatra/url_for.rb
@@ -30,7 +30,13 @@ module Sinatra
       else
         raise TypeError, "Unknown url_for mode #{mode}"
       end
-      "#{base}#{url_fragment}"
+      # Don't add the base fragment if url_for gets called more than once
+      # per url or the url_fragment passed in is an absolute url
+      if url_fragment.match(/^#{base}/) or url_fragment.match(/^http/)
+        url_fragment
+      else
+        "#{base}#{url_fragment}"
+      end
     end
 
     def root_url
diff --git a/server/server.rb b/server/server.rb
index 2516d3e..16c38a0 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -38,7 +38,7 @@ error Deltacloud::BackendError do
 end
 
 # Redirect to /api
-get '/' do redirect '/api'; end
+get '/' do redirect url_for('/api'); end
 
 get '/api\/?' do
     @version = 0.1
diff --git a/server/views/api/show.html.haml b/server/views/api/show.html.haml
index 455e919..0077972 100644
--- a/server/views/api/show.html.haml
+++ b/server/views/api/show.html.haml
@@ -12,4 +12,4 @@
             = op
   %li
     %strong
-      %a{:href => "/api/docs"} Documentation (v#{@version})
+      %a{:href => url_for("/api/docs")} Documentation (v#{@version})
diff --git a/server/views/docs/collection.html.haml b/server/views/docs/collection.html.haml
index 97b735f..505cbde 100644
--- a/server/views/docs/collection.html.haml
+++ b/server/views/docs/collection.html.haml
@@ -16,7 +16,7 @@
     - @operations.keys.sort_by { |k| k.to_s }.each do |operation|
       %tr
         %td{:style => "width:15em"}
-          %a{:href => "/api/docs/#{@collection.name.to_s}/#{operation}"} #{operation}
+          %a{:href => url_for("/api/docs/#{@collection.name.to_s}/#{operation}")} #{operation}
         %td{:style => "width:10em"} #{@operations[operation].description}
 
 %h3 Features:
@@ -34,4 +34,4 @@
         %td= feature.description
         %td
           - feature.operations.each do |op|
-            %a{:href => "/api/docs/#{@collection.name.to_s}/#{op.name}"} #{op.name}
+            %a{:href => url_for("/api/docs/#{@collection.name.to_s}/#{op.name}")} #{op.name}
diff --git a/server/views/docs/collection.xml.haml b/server/views/docs/collection.xml.haml
index df25091..a82d4e1 100644
--- a/server/views/docs/collection.xml.haml
+++ b/server/views/docs/collection.xml.haml
@@ -1,9 +1,9 @@
 %docs{:status => "unsupported"}
-  %collection{:url => "/api/docs/#{@collection.name}", :name => "#{@collection.name}"}
+  %collection{:url => url_for("/api/docs/#{@collection.name}"), :name => "#{@collection.name}"}
     %description #{@collection.description}
     %operations
       - @operations.keys.sort_by { |k| k.to_s }.each do |operation|
-        %operation{:url => "/api/#{@collection.name.to_s}", :name => "#{operation}",
:href => "#{@operations[operation].path}", :method => "#{@operations[operation].method}"}
+        %operation{:url => url_for("/api/#{@collection.name.to_s}"), :name => "#{operation}",
:href => url_for("#{@operations[operation].path}"), :method => "#{@operations[operation].method}"}
           %description #{@operations[operation].description}
           - @operations[operation].each_param do |param|
             %parameter{:name => "#{param.name}", :type => "#{param.type}"}
diff --git a/server/views/docs/index.html.haml b/server/views/docs/index.html.haml
index ae21e70..66d56e7 100644
--- a/server/views/docs/index.html.haml
+++ b/server/views/docs/index.html.haml
@@ -11,5 +11,5 @@
     - collections.keys.sort_by { |k| k.to_s }.each do |collection|
       %tr
         %td{:style => "width:15em"}
-          %a{:href => "/api/docs/#{collection}"} #{collection}
+          %a{:href => url_for("/api/docs/#{collection}")} #{collection}
         %td{:style => "width:10em"} #{collections[collection].description}
diff --git a/server/views/docs/index.xml.haml b/server/views/docs/index.xml.haml
index 2210c8c..4edcf9a 100644
--- a/server/views/docs/index.xml.haml
+++ b/server/views/docs/index.xml.haml
@@ -1,5 +1,5 @@
 %docs{:status => "unsupported"}
   - collections.keys.sort_by { |k| k.to_s }.each do |collection|
-    %collection{:url => "/api/docs/#{collection}"}
+    %collection{:url => url_for("/api/docs/#{collection}")}
       %name #{collection}
       %description  #{collections[collection].description}
diff --git a/server/views/docs/operation.html.haml b/server/views/docs/operation.html.haml
index ec6374b..79bb6bb 100644
--- a/server/views/docs/operation.html.haml
+++ b/server/views/docs/operation.html.haml
@@ -1,5 +1,5 @@
 %h2
-  %a{:href => "/api/docs/#{@collection.name.to_s}"} #{@collection.name.to_s.titlecase}
+  %a{:href => url_for("/api/docs/#{@collection.name.to_s}")} #{@collection.name.to_s.titlecase}
   #{'::'}
   #{@operation.name}
 
@@ -9,7 +9,7 @@
 %h3
   URL:
   %u
-    = "/api/#{@collection.name.to_s}/#{@operation.name.to_s}"
+    = url_for("/api/#{@collection.name.to_s}/#{@operation.name.to_s}")
 %br
 %h3 Parameters:
 
diff --git a/server/views/docs/operation.xml.haml b/server/views/docs/operation.xml.haml
index ca4687a..189ef47 100644
--- a/server/views/docs/operation.xml.haml
+++ b/server/views/docs/operation.xml.haml
@@ -1,5 +1,5 @@
 %docs{:status => "unsupported"}
-  %operation{:url => "/api/docs/#{@collection.name.to_s}", :name => "#{@operation.name.to_s}",
:href => "#{@operation.path}", :method => "#{@operation.method}"}
+  %operation{:url => url_for("/api/docs/#{@collection.name.to_s}"), :name => "#{@operation.name.to_s}",
:href => url_for("#{@operation.path}"), :method => "#{@operation.method}"}
     %description #{@operation.description}
     - @operation.each_param do |param|
       %parameter{:name => "#{param.name}", :type => "#{param.type}"}
diff --git a/server/views/layout.html.haml b/server/views/layout.html.haml
index 0f736fb..c46b15b 100644
--- a/server/views/layout.html.haml
+++ b/server/views/layout.html.haml
@@ -22,4 +22,5 @@
           Driver: #{DRIVER}
         #copyright
           Copyright 2009, 2010
-          #{link_to 'Red Hat', 'http://redhat.com'} and individual contributors.
+          %a{:href => 'http://redhat.com'} Red Hat
+          and individual contributors.
-- 
1.7.0.4


Mime
View raw message