Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed .htaccess to allow dispatch.* to be called from a sub-directo…

…ry as part of the push with Action Pack to make Rails work on non-vhost setups #826 [Nicholas Seckar/Tobias Luetke] Fixed routing and helpers to make Rails work on non-vhost setups #826 [Nicholas Seckar/Tobias Luetke]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@945 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit a61360688cd0e1f43f523866384d0d0796a4ea73 1 parent ee8d110
David Heinemeier Hansson dhh authored
2  actionpack/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Fixed routing and helpers to make Rails work on non-vhost setups #826 [Nicholas Seckar/Tobias Luetke]
+
* Added a much improved Flash module that allows for finer-grained control on expiration and allows you to flash the current action #839 [Caio Chassot]. Example of flash.now:
class SomethingController < ApplicationController
15 actionpack/lib/action_controller/request.rb
View
@@ -84,9 +84,20 @@ def protocol
def ssl?
protocol == 'https://'
end
-
+
+ # returns the interpreted path to requested resource after
+ # all the installation directory of this application was taken into account
def path
- request_uri ? request_uri.split('?').first : ''
+ path = request_uri ? request_uri.split('?').first : ''
+
+ # cut off the part of the url which leads to the installation directory of this app
+ path[relative_url_root.length..-1]
+ end
+
+ # returns the path minus the web server relative
+ # installation directory
+ def relative_url_root
+ File.dirname(env["SCRIPT_NAME"].to_s).gsub /(^\.$|^\/$)/, ''
end
def port
7 actionpack/lib/action_controller/url_rewriter.rb
View
@@ -2,13 +2,13 @@ module ActionController
# Rewrites URLs for Base.redirect_to and Base.url_for in the controller.
class UrlRewriter #:nodoc:
- RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol]
+ RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :application_prefix]
def initialize(request, parameters)
@request, @parameters = request, parameters
@rewritten_path = @request.path ? @request.path.dup : ""
end
- def rewrite(options = {})
+ def rewrite(options = {})
rewrite_url(rewrite_path(options), options)
end
@@ -20,11 +20,12 @@ def to_str
private
def rewrite_url(path, options)
+
rewritten_url = ""
rewritten_url << (options[:protocol] || @request.protocol) unless options[:only_path]
rewritten_url << (options[:host] || @request.host_with_port) unless options[:only_path]
- rewritten_url << options[:application_prefix] if options[:application_prefix]
+ rewritten_url << (options[:application_prefix] || @request.relative_url_root)
rewritten_url << path
rewritten_url << "##{options[:anchor]}" if options[:anchor]
26 actionpack/lib/action_view/helpers/asset_tag_helper.rb
View
@@ -33,8 +33,7 @@ def auto_discovery_link_tag(type = :rss, options = {})
# <script language="JavaScript" type="text/javascript" src="/elsewhere/cools.js"></script>
def javascript_include_tag(*sources)
sources.collect { |source|
- source = "/javascripts/#{source}" unless source.include?("/")
- source = "#{source}.js" unless source.include?(".")
+ source = compute_public_path(source, 'javascripts', 'js')
content_tag("script", "", "language" => "JavaScript", "type" => "text/javascript", "src" => source)
}.join("\n")
end
@@ -49,8 +48,7 @@ def javascript_include_tag(*sources)
# <link href="/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />
def stylesheet_link_tag(*sources)
sources.collect { |source|
- source = "/stylesheets/#{source}" unless source.include?("/")
- source = "#{source}.css" unless source.include?(".")
+ source = compute_public_path(source, 'stylesheets', 'css')
tag("link", "rel" => "Stylesheet", "type" => "text/css", "media" => "screen", "href" => source)
}.join("\n")
end
@@ -64,13 +62,11 @@ def stylesheet_link_tag(*sources)
# * full path, like "/my_images/image.gif"
# * file name, like "rss.gif", that gets expanded to "/images/rss.gif"
# * file name without extension, like "logo", that gets expanded to "/images/logo.png"
- def image_tag(src, options = {})
+ def image_tag(source, options = {})
options.symbolize_keys
-
- options.update({ :src => src.include?("/") ? src : "/images/#{src}" })
- options[:src] += ".png" unless options[:src].include?(".")
-
- options[:alt] ||= src.split("/").last.split(".").first.capitalize
+
+ options[:src] = compute_public_path(source, 'images', 'png')
+ options[:alt] ||= source.split("/").last.split(".").first.capitalize
if options[:size]
options[:width], options[:height] = options[:size].split("x")
@@ -79,6 +75,16 @@ def image_tag(src, options = {})
tag("img", options)
end
+
+ private
+
+ def compute_public_path(source, dir, ext)
+ source = "/#{dir}/#{source}" unless source.include?("/")
+ source = "#{source}.#{ext}" unless source.include?(".")
+ source = "#{@request.relative_url_root}#{source}"
+ source
+ end
+
end
end
end
34 actionpack/test/controller/request_test.rb
View
@@ -32,6 +32,23 @@ def test_port_string
assert_equal ":8080", @request.port_string
end
+ def test_relative_url_root
+ @request.env['SCRIPT_NAME'] = nil
+ assert_equal "", @request.relative_url_root
+
+ @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
+ assert_equal "", @request.relative_url_root
+
+ @request.env['SCRIPT_NAME'] = "/myapp.rb"
+ assert_equal "", @request.relative_url_root
+
+ @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
+ assert_equal "/hieraki", @request.relative_url_root
+
+ @request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
+ assert_equal "/collaboration/hieraki", @request.relative_url_root
+ end
+
def test_request_uri
@request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
@@ -52,7 +69,24 @@ def test_request_uri
@request.set_REQUEST_URI "/?m=b"
assert_equal "/?m=b", @request.request_uri
assert_equal "/", @request.path
+
+ @request.set_REQUEST_URI "/"
+ @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
+ assert_equal "/", @request.request_uri
+ assert_equal "/", @request.path
+
+ @request.set_REQUEST_URI "/hieraki/"
+ @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
+ assert_equal "/hieraki/", @request.request_uri
+ assert_equal "/", @request.path
+
+ @request.set_REQUEST_URI "/collaboration/hieraki/books/edit/2"
+ @request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
+ assert_equal "/collaboration/hieraki/books/edit/2", @request.request_uri
+ assert_equal "/books/edit/2", @request.path
+
end
+
def test_host_with_port
@request.env['HTTP_HOST'] = "rubyonrails.org:8080"
87 actionpack/test/template/asset_tag_helper_test.rb
View
@@ -8,11 +8,19 @@ class AssetTagHelperTest < Test::Unit::TestCase
def setup
@controller = Class.new do
+
def url_for(options, *parameters_for_method_reference)
"http://www.example.com"
end
- end
- @controller = @controller.new
+
+ end.new
+
+ @request = Class.new do
+ def relative_url_root
+ ""
+ end
+ end.new
+
end
AutoDiscoveryToTag = {
@@ -31,6 +39,74 @@ def url_for(options, *parameters_for_method_reference)
%(stylesheet_link_tag("random.styles", "/css/stylish")) => %(<link href="/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />\n<link href="/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />)
}
+ ImageLinkToTag = {
+ %(image_tag("xml")) => %(<img alt="Xml" src="/images/xml.png" />),
+ %(image_tag("rss", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/images/rss.png" />),
+ %(image_tag("gold", :size => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
+ }
+
+ def test_auto_discovery
+ AutoDiscoveryToTag.each { |method, tag| assert_equal(tag, eval(method)) }
+ end
+
+ def test_javascript_include
+ JavascriptIncludeToTag.each { |method, tag| assert_equal(tag, eval(method)) }
+ end
+
+ def test_style_link
+ StyleLinkToTag.each { |method, tag| assert_equal(tag, eval(method)) }
+ end
+
+ def test_image_tag
+ ImageLinkToTag.each { |method, tag| assert_equal(tag, eval(method)) }
+ end
+
+end
+
+class AssetTagHelperNonVhostTest < Test::Unit::TestCase
+ include ActionView::Helpers::TagHelper
+ include ActionView::Helpers::UrlHelper
+ include ActionView::Helpers::AssetTagHelper
+
+ def setup
+ @controller = Class.new do
+
+ def url_for(options, *parameters_for_method_reference)
+ "http://www.example.com/calloboration/hieraki"
+ end
+
+ end.new
+
+ @request = Class.new do
+ def relative_url_root
+ "/calloboration/hieraki"
+ end
+ end.new
+
+ end
+
+ AutoDiscoveryToTag = {
+ %(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.example.com/calloboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />),
+ %(auto_discovery_link_tag(:atom)) => %(<link href="http://www.example.com/calloboration/hieraki" rel="alternate" title="ATOM" type="application/atom+xml" />),
+ %(auto_discovery_link_tag) => %(<link href="http://www.example.com/calloboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />),
+ }
+
+ JavascriptIncludeToTag = {
+ %(javascript_include_tag("xmlhr")) => %(<script language="JavaScript" src="/calloboration/hieraki/javascripts/xmlhr.js" type="text/javascript"></script>),
+ %(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(<script language="JavaScript" src="/calloboration/hieraki/javascripts/common.javascript" type="text/javascript"></script>\n<script language="JavaScript" src="/calloboration/hieraki/elsewhere/cools.js" type="text/javascript"></script>),
+ }
+
+ StyleLinkToTag = {
+ %(stylesheet_link_tag("style")) => %(<link href="/calloboration/hieraki/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />),
+ %(stylesheet_link_tag("random.styles", "/css/stylish")) => %(<link href="/calloboration/hieraki/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />\n<link href="/calloboration/hieraki/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />)
+ }
+
+ ImageLinkToTag = {
+ %(image_tag("xml")) => %(<img alt="Xml" src="/calloboration/hieraki/images/xml.png" />),
+ %(image_tag("rss", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/calloboration/hieraki/images/rss.png" />),
+ %(image_tag("gold", :size => "45x70")) => %(<img alt="Gold" height="70" src="/calloboration/hieraki/images/gold.png" width="45" />),
+ }
+
def test_auto_discovery
AutoDiscoveryToTag.each { |method, tag| assert_equal(tag, eval(method)) }
end
@@ -44,6 +120,11 @@ def test_style_link
end
def test_image_tag
- assert_equal %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />), image_tag("gold", :size => "45x70")
+ assert_equal %(<img alt="Gold" height="70" src="/calloboration/hieraki/images/gold.png" width="45" />), image_tag("gold", :size => "45x70")
+ end
+
+ def test_image_tag
+ ImageLinkToTag.each { |method, tag| assert_equal(tag, eval(method)) }
end
+
end
22 actionpack/test/template/url_helper_test.rb
View
@@ -36,28 +36,6 @@ def test_link_tag_with_javascript_confirm
)
end
- def test_link_image_to
- assert_equal(
- "<a href=\"http://www.example.com\"><img alt=\"Rss\" border=\"0\" height=\"45\" src=\"/images/rss.png\" width=\"30\" /></a>",
- link_image_to("rss", "http://www.example.com", "size" => "30x45", "border" => "0")
- )
-
- assert_equal(
- "<a href=\"http://www.example.com\"><img alt=\"Rss\" border=\"0\" height=\"45\" src=\"/images/rss.png\" width=\"30\" /></a>",
- link_to(image_tag("rss", :size => "30x45", :border => 0), "http://www.example.com")
- )
-
- assert_equal(
- "<a class=\"admin\" href=\"http://www.example.com\"><img alt=\"Feed\" height=\"45\" src=\"/images/rss.gif\" width=\"30\" /></a>",
- link_image_to("rss.gif", "http://www.example.com", "size" => "30x45", "alt" => "Feed", "class" => "admin")
- )
-
- assert_equal link_image_to("rss", "http://www.example.com", "size" => "30x45"),
- link_image_to("rss", "http://www.example.com", :size => "30x45")
- assert_equal link_image_to("rss.gif", "http://www.example.com", "size" => "30x45", "alt" => "Feed", "class" => "admin"),
- link_image_to("rss.gif", "http://www.example.com", :size => "30x45", :alt => "Feed", :class => "admin")
- end
-
def test_link_to_unless
assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")
assert "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")
2  railties/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Changed .htaccess to allow dispatch.* to be called from a sub-directory as part of the push with Action Pack to make Rails work on non-vhost setups #826 [Nicholas Seckar/Tobias Luetke]
+
* Added script/runner which can be used to run code inside the environment by eval'ing the first parameter. Examples:
./script/runner 'ReminderService.deliver'
15 railties/configs/apache.conf
View
@@ -4,11 +4,22 @@ AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI
# Redirect all requests not available on the filesystem to Rails
+# By default the cgi dispatcher is used which is very slow
+#
+# For better performance replace the dispatcher with the fastcgi one
+#
+# Example:
+# RewriteRule ^(.*)$ dispatch.fcgi?$1 [QSA,L]
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
-RewriteRule ^(.*)$ /dispatch.cgi?$1 [QSA,L]
+RewriteRule ^(.*)$ dispatch.cgi?$1 [QSA,L]
# In case Rails experiences terminal errors
-ErrorDocument 500 /500.html
+# Instead of displaying this message you can supply a file here which will be rendered instead
+#
+# Example:
+# ErrorDocument 500 /500.html
+
+ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
Please sign in to comment.
Something went wrong with that request. Please try again.