Skip to content
This repository was archived by the owner on Dec 2, 2018. It is now read-only.

Commit 0070aed

Browse files
committed
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.
1 parent 2a60afb commit 0070aed

File tree

11 files changed

+144
-3
lines changed

11 files changed

+144
-3
lines changed

README.rdoc

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,38 @@ Rails 3 the hacks where not necessary and therefore removed.
5050

5151
If you still need to use rack-legacy on a Rails 2 app see version 0.1.5.
5252

53+
== Legacy Modules
54+
55+
The above configuration just adds PHP support to your stack. That is
56+
just one of currently three modules available. The current modules are:
57+
58+
Rack::Legacy::Cgi::
59+
Provides support for executing any standard CGI script. It is
60+
important that script has the executable bit set.
61+
Rack::Legacy::Php::
62+
Provides support for executing PHP scripts.
63+
Rack::Legacy::Index::
64+
Provides support for running "index.php", "index.cgi" or "index.html"
65+
when just a directory is specified in the request. You will want to
66+
install this module before the CGI or PHP ones.
67+
68+
An example rackup file that enables all modules (but exception reporting
69+
and static file delivery) is:
70+
71+
require 'rack/showexceptions'
72+
require 'rack-legacy'
73+
74+
use Rack::ShowExceptions
75+
use Rack::Legacy::Index
76+
use Rack::Legacy::Php
77+
use Rack::Legacy::Cgi
78+
run Rack::File.new Dir.getwd
79+
5380
== Pure PHP/CGI
5481

5582
Got a project that is nothing but PHP or CGI? Run `rack_legacy` in the
56-
website root directory and it will start serving the files.
83+
website root directory and it will start serving the files. It just
84+
executes the above example rackup file.
5785

5886
= LICENSE
5987

lib/rack/legacy.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ class ExecutionError < StandardError
1010
end
1111
end
1212

13+
require 'rack/legacy/index'
1314
require 'rack/legacy/cgi'
1415
require 'rack/legacy/php'

lib/rack/legacy/index.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Rack::Legacy::Index
2+
3+
# Will rewrite the request if the request is for a directory and
4+
# one of the index files specified exists.
5+
def initialize app, public_dir, order=['index.php', 'index.cgi', 'index.html']
6+
@app = app
7+
@public_dir = public_dir
8+
@order = order
9+
end
10+
11+
# Check for the dir, files and rewrite if necessary. Note that we
12+
# don't check to ensure the requested path is in the public directory
13+
# (i.e. things like ../ will hack outside it). We rely on the
14+
# middleware actually handling the request to do the necessary
15+
# security check.
16+
def call env
17+
dir = File.join @public_dir, env['PATH_INFO']
18+
rewrite = env['PATH_INFO']
19+
@order.reverse.each do |index|
20+
full_index = File.join dir, index
21+
new_path = File.join env['PATH_INFO'], index
22+
rewrite = new_path if File.exists? full_index
23+
end if File.directory? dir
24+
env['PATH_INFO'] = rewrite
25+
@app.call env
26+
end
27+
28+
end

rack-legacy.gemspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'rack-legacy'
3-
s.version = '0.8.0'
3+
s.version = '0.8.5'
44
s.homepage = 'https://github.com/eric1234/rack-legacy'
55
s.author = 'Eric Anderson'
66
s.email = '[email protected]'
@@ -11,7 +11,6 @@ Gem::Specification.new do |s|
1111
s.add_dependency 'rack-reverse-proxy'
1212
s.add_development_dependency 'rake'
1313
s.add_development_dependency 'httparty'
14-
s.add_development_dependency 'flexmock'
1514
s.add_development_dependency 'nokogiri'
1615
s.add_development_dependency 'mechanize', '>= 2.0'
1716
s.files = Dir['lib/**/*.rb'] + Dir['bin/*'] + Dir['share/*']

share/rack_legacy.ru

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require 'rack/showexceptions'
22
require 'rack-legacy'
33

44
use Rack::ShowExceptions
5+
use Rack::Legacy::Index
56
use Rack::Legacy::Php
67
use Rack::Legacy::Cgi
78
run Rack::File.new Dir.getwd

test/fixtures/dir1/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo 'PHP dir1 index';

test/fixtures/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HTML index

test/fixtures/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo 'PHP index';

test/functional/index_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'minitest/autorun'
2+
require 'mechanize'
3+
4+
class IndexTest < MiniTest::Unit::TestCase
5+
6+
def test_no_path
7+
response = Mechanize.new.get 'http://localhost:4000'
8+
assert_equal 'PHP index', response.body
9+
10+
response = Mechanize.new.get 'http://localhost:4000/'
11+
assert_equal 'PHP index', response.body
12+
end
13+
14+
def test_dir
15+
response = Mechanize.new.get 'http://localhost:4000/dir1'
16+
assert_equal 'PHP dir1 index', response.body
17+
18+
response = Mechanize.new.get 'http://localhost:4000/dir1/'
19+
assert_equal 'PHP dir1 index', response.body
20+
21+
response = Mechanize.new.get 'http://localhost:4000/dir2'
22+
assert_equal 'Endpoint', response.body
23+
24+
response = Mechanize.new.get 'http://localhost:4000/dir2/'
25+
assert_equal 'Endpoint', response.body
26+
end
27+
28+
end

test/test_server.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ::WEBrick::BasicLog; def log(level, data); end end
1616

1717
app = Rack::Builder.app do
1818
use Rack::ShowExceptions
19+
use Rack::Legacy::Index, File.join(File.dirname(__FILE__), 'fixtures')
1920
use Rack::Legacy::Php, File.join(File.dirname(__FILE__), 'fixtures'), 'php', 8180, true
2021
use Rack::Legacy::Cgi, File.join(File.dirname(__FILE__), 'fixtures')
2122

0 commit comments

Comments
 (0)