Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add generic support for index rewriting.

Traditional webserver allow a directory to be requested and it then routes
the request to files like "index.php", "index.html", "index.cgi", etc.
We once had limited support for this on the PHP module. This was removed so
we could add support for any index file.
  • Loading branch information...
commit 0070aed421470a058ea5c861037b9451e87b183e 1 parent 2a60afb
@eric1234 authored
View
30 README.rdoc
@@ -50,10 +50,38 @@ Rails 3 the hacks where not necessary and therefore removed.
If you still need to use rack-legacy on a Rails 2 app see version 0.1.5.
+== Legacy Modules
+
+The above configuration just adds PHP support to your stack. That is
+just one of currently three modules available. The current modules are:
+
+ Rack::Legacy::Cgi::
+ Provides support for executing any standard CGI script. It is
+ important that script has the executable bit set.
+ Rack::Legacy::Php::
+ Provides support for executing PHP scripts.
+ Rack::Legacy::Index::
+ Provides support for running "index.php", "index.cgi" or "index.html"
+ when just a directory is specified in the request. You will want to
+ install this module before the CGI or PHP ones.
+
+An example rackup file that enables all modules (but exception reporting
+and static file delivery) is:
+
+ require 'rack/showexceptions'
+ require 'rack-legacy'
+
+ use Rack::ShowExceptions
+ use Rack::Legacy::Index
+ use Rack::Legacy::Php
+ use Rack::Legacy::Cgi
+ run Rack::File.new Dir.getwd
+
== Pure PHP/CGI
Got a project that is nothing but PHP or CGI? Run `rack_legacy` in the
-website root directory and it will start serving the files.
+website root directory and it will start serving the files. It just
+executes the above example rackup file.
= LICENSE
View
1  lib/rack/legacy.rb
@@ -10,5 +10,6 @@ class ExecutionError < StandardError
end
end
+require 'rack/legacy/index'
require 'rack/legacy/cgi'
require 'rack/legacy/php'
View
28 lib/rack/legacy/index.rb
@@ -0,0 +1,28 @@
+class Rack::Legacy::Index
+
+ # Will rewrite the request if the request is for a directory and
+ # one of the index files specified exists.
+ def initialize app, public_dir, order=['index.php', 'index.cgi', 'index.html']
+ @app = app
+ @public_dir = public_dir
+ @order = order
+ end
+
+ # Check for the dir, files and rewrite if necessary. Note that we
+ # don't check to ensure the requested path is in the public directory
+ # (i.e. things like ../ will hack outside it). We rely on the
+ # middleware actually handling the request to do the necessary
+ # security check.
+ def call env
+ dir = File.join @public_dir, env['PATH_INFO']
+ rewrite = env['PATH_INFO']
+ @order.reverse.each do |index|
+ full_index = File.join dir, index
+ new_path = File.join env['PATH_INFO'], index
+ rewrite = new_path if File.exists? full_index
+ end if File.directory? dir
+ env['PATH_INFO'] = rewrite
+ @app.call env
+ end
+
+end
View
3  rack-legacy.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'rack-legacy'
- s.version = '0.8.0'
+ s.version = '0.8.5'
s.homepage = 'https://github.com/eric1234/rack-legacy'
s.author = 'Eric Anderson'
s.email = 'eric@pixelwareinc.com'
@@ -11,7 +11,6 @@ Gem::Specification.new do |s|
s.add_dependency 'rack-reverse-proxy'
s.add_development_dependency 'rake'
s.add_development_dependency 'httparty'
- s.add_development_dependency 'flexmock'
s.add_development_dependency 'nokogiri'
s.add_development_dependency 'mechanize', '>= 2.0'
s.files = Dir['lib/**/*.rb'] + Dir['bin/*'] + Dir['share/*']
View
1  share/rack_legacy.ru
@@ -2,6 +2,7 @@ require 'rack/showexceptions'
require 'rack-legacy'
use Rack::ShowExceptions
+use Rack::Legacy::Index
use Rack::Legacy::Php
use Rack::Legacy::Cgi
run Rack::File.new Dir.getwd
View
1  test/fixtures/dir1/index.php
@@ -0,0 +1 @@
+<?php echo 'PHP dir1 index';
View
1  test/fixtures/index.html
@@ -0,0 +1 @@
+HTML index
View
1  test/fixtures/index.php
@@ -0,0 +1 @@
+<?php echo 'PHP index';
View
28 test/functional/index_test.rb
@@ -0,0 +1,28 @@
+require 'minitest/autorun'
+require 'mechanize'
+
+class IndexTest < MiniTest::Unit::TestCase
+
+ def test_no_path
+ response = Mechanize.new.get 'http://localhost:4000'
+ assert_equal 'PHP index', response.body
+
+ response = Mechanize.new.get 'http://localhost:4000/'
+ assert_equal 'PHP index', response.body
+ end
+
+ def test_dir
+ response = Mechanize.new.get 'http://localhost:4000/dir1'
+ assert_equal 'PHP dir1 index', response.body
+
+ response = Mechanize.new.get 'http://localhost:4000/dir1/'
+ assert_equal 'PHP dir1 index', response.body
+
+ response = Mechanize.new.get 'http://localhost:4000/dir2'
+ assert_equal 'Endpoint', response.body
+
+ response = Mechanize.new.get 'http://localhost:4000/dir2/'
+ assert_equal 'Endpoint', response.body
+ end
+
+end
View
1  test/test_server.rb
@@ -16,6 +16,7 @@ class ::WEBrick::BasicLog; def log(level, data); end end
app = Rack::Builder.app do
use Rack::ShowExceptions
+ use Rack::Legacy::Index, File.join(File.dirname(__FILE__), 'fixtures')
use Rack::Legacy::Php, File.join(File.dirname(__FILE__), 'fixtures'), 'php', 8180, true
use Rack::Legacy::Cgi, File.join(File.dirname(__FILE__), 'fixtures')
View
52 test/unit/index_test.rb
@@ -0,0 +1,52 @@
+require 'minitest/autorun'
+require 'rack/legacy'
+require 'rack/legacy/index'
+
+class IndexTest < MiniTest::Unit::TestCase
+
+ def test_no_path
+ response = app.call 'PATH_INFO' => ''
+ assert_equal 200, response.first
+ assert_equal ['/index.php'], response.last
+
+ response = app.call 'PATH_INFO' => '/'
+ assert_equal 200, response.first
+ assert_equal ['/index.php'], response.last
+ end
+
+ def test_dir
+ response = app.call 'PATH_INFO' => '/dir1'
+ assert_equal 200, response.first
+ assert_equal ['/dir1/index.php'], response.last
+
+ response = app.call 'PATH_INFO' => '/dir1/'
+ assert_equal 200, response.first
+ assert_equal ['/dir1/index.php'], response.last
+
+ response = app.call 'PATH_INFO' => '/dir2'
+ assert_equal 200, response.first
+ assert_equal ['/dir2'], response.last
+
+ response = app.call 'PATH_INFO' => '/dir2/'
+ assert_equal 200, response.first
+ assert_equal ['/dir2/'], response.last
+ end
+
+ def test_fallback
+ app = Rack::Legacy::Index.new \
+ proc {|env| [200, {'Content-Type' => 'text/html'}, [env['PATH_INFO']]]},
+ File.join(File.dirname(__FILE__), '../fixtures'), ['index.pl', 'index.html']
+
+ response = app.call 'PATH_INFO' => '', 'REQUEST_METHOD' => 'GET'
+ assert_equal 200, response.first
+ assert_equal ['/index.html'], response.last
+ end
+
+ private
+
+ def app
+ @app ||= Rack::Legacy::Index.new \
+ proc {|env| [200, {'Content-Type' => 'text/html'}, [env['PATH_INFO']]]},
+ File.join(File.dirname(__FILE__), '../fixtures')
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.