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/#[email protected]_s}/#{operation}"} 
#{operation}
+          %a{:href => 
url_for("/api/docs/#[email protected]_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/#[email protected]_s}/#{op.name}"} 
#{op.name}
+            %a{:href => 
url_for("/api/docs/#[email protected]_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/#[email protected]}", :name => 
"#[email protected]}"}
+  %collection{:url => url_for("/api/docs/#[email protected]}"), :name => 
"#[email protected]}"}
     %description #[email protected]}
     %operations
       - @operations.keys.sort_by { |k| k.to_s }.each do |operation|
-        %operation{:url => "/api/#[email protected]_s}", :name => 
"#{operation}", :href => "#...@operations[operation].path}", :method => 
"#...@operations[operation].method}"}
+        %operation{:url => url_for("/api/#[email protected]_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/#[email protected]_s}"} 
#[email protected]_s.titlecase}
+  %a{:href => url_for("/api/docs/#[email protected]_s}")} 
#[email protected]_s.titlecase}
   #{'::'}
   #[email protected]}
 
@@ -9,7 +9,7 @@
 %h3
   URL:
   %u
-    = "/api/#[email protected]_s}/#[email protected]_s}"
+    = url_for("/api/#[email protected]_s}/#[email protected]_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/#[email protected]_s}", :name => 
"#[email protected]_s}", :href => "#[email protected]}", :method => 
"#[email protected]}"}
+  %operation{:url => url_for("/api/docs/#[email protected]_s}"), :name => 
"#[email protected]_s}", :href => url_for("#[email protected]}"), :method 
=> "#[email protected]}"}
     %description #[email protected]}
     - @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

Reply via email to