rack-try_static2 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +7 -0
- data/lib/rack/try_static.rb +39 -0
- data/lib/rack/try_static/version.rb +5 -0
- data/rack-try_static.gemspec +25 -0
- data/spec/rack/documents/existing.html +1 -0
- data/spec/rack/documents/index.htm +1 -0
- data/spec/rack/documents/index.html +1 -0
- data/spec/rack/documents/test +1 -0
- data/spec/rack/try_static_spec.rb +54 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1d33181f338cb5f5b0fd4f8a3649adb101e98e2e
|
4
|
+
data.tar.gz: 3a95ae7a68077f88204e003fe5c221a37f2818e6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d150db8f3f2ffe768d5e37c56487219708d23059f91aeed273689a4c15ed84f632b0ce023f8b6e65bd9e5ab8c04dad28c49e0fb0fd0d18b465207dff27abb2f
|
7
|
+
data.tar.gz: 3cb6490206ea58e5791634be287035e71a1af0ff2da3bda18978344482bd9d4f00a34a35f55719d868c852e615e55f50bdbfd149f2c233c87ab6895f91133611
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Caleb Buxton
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Rack::TryStatic, Recontributed Rack Middleware
|
2
|
+
|
3
|
+
This package includes one add-on component for Rack, a Ruby web server interface:
|
4
|
+
|
5
|
+
* Rack::TryStatic - Tries to match a request to a static file.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'rack-try_static2'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rack-try_static2
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
The Rack::TryStatic middleware delegates requests to Rack::Static middleware
|
24
|
+
trying to match a static file
|
25
|
+
|
26
|
+
Examples
|
27
|
+
|
28
|
+
use Rack::TryStatic,
|
29
|
+
:root => "public", # static files root dir
|
30
|
+
:urls => %w[/], # match all requests
|
31
|
+
:try => ['.html', 'index.html', '/index.html'] # try these postfixes sequentially
|
32
|
+
|
33
|
+
uses same options as Rack::Static with extra :try option which is an array
|
34
|
+
of postfixes to find desired file
|
35
|
+
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Add failing tests
|
42
|
+
4. Commit your changes (`git commit -am 'Adding tests for some feature'`)
|
43
|
+
5. Make your tests pass
|
44
|
+
6. Commit your changes (`git commit -am 'Implements some feature'`)
|
45
|
+
7. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
8. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "rack/try_static/version"
|
2
|
+
require "rack/static"
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
|
6
|
+
# The Rack::TryStatic middleware delegates requests to Rack::Static middleware
|
7
|
+
# trying to match a static file
|
8
|
+
#
|
9
|
+
# Examples
|
10
|
+
#
|
11
|
+
# use Rack::TryStatic,
|
12
|
+
# :root => "public", # static files root dir
|
13
|
+
# :urls => %w[/], # match all requests
|
14
|
+
# :try => ['.html', 'index.html', '/index.html'] # try these postfixes sequentially
|
15
|
+
#
|
16
|
+
# uses same options as Rack::Static with extra :try option which is an array
|
17
|
+
# of postfixes to find desired file
|
18
|
+
|
19
|
+
class TryStatic
|
20
|
+
|
21
|
+
def initialize(app, options)
|
22
|
+
@app = app
|
23
|
+
@try = ['', *options[:try]]
|
24
|
+
@static = ::Rack::Static.new(
|
25
|
+
lambda { |_| [404, {}, []] },
|
26
|
+
options)
|
27
|
+
end
|
28
|
+
|
29
|
+
def call(env)
|
30
|
+
orig_path = env['PATH_INFO']
|
31
|
+
found = nil
|
32
|
+
@try.each do |path|
|
33
|
+
resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path}))
|
34
|
+
break if 404 != resp[0] && found = resp
|
35
|
+
end
|
36
|
+
found or @app.call(env.merge!('PATH_INFO' => orig_path))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/try_static/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rack-try_static2"
|
8
|
+
spec.version = Rack::TryStatic::VERSION
|
9
|
+
spec.authors = ["Caleb Buxton"]
|
10
|
+
spec.email = ["me@cpb.ca"]
|
11
|
+
spec.description = %q{The Recontributed Rack Middleware}
|
12
|
+
spec.summary = %q{The Recontributed Rack Middleware}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "rack", "~> 1.5"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.1"
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
existing.html
|
@@ -0,0 +1 @@
|
|
1
|
+
index.htm
|
@@ -0,0 +1 @@
|
|
1
|
+
index.html
|
@@ -0,0 +1 @@
|
|
1
|
+
nocache
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'rack/try_static'
|
3
|
+
require 'rack/mock'
|
4
|
+
|
5
|
+
def build_options(opts)
|
6
|
+
{
|
7
|
+
:urls => %w[/],
|
8
|
+
:root => ::File.expand_path(::File.dirname(__FILE__)),
|
9
|
+
}.merge(opts)
|
10
|
+
end
|
11
|
+
|
12
|
+
def request(options = {})
|
13
|
+
@request =
|
14
|
+
Rack::MockRequest.new(
|
15
|
+
Rack::TryStatic.new(
|
16
|
+
lambda { |_| [200, {}, ["Hello World"]]},
|
17
|
+
options))
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Rack::TryStatic do
|
21
|
+
context 'when file cannot be found' do
|
22
|
+
it 'should call call app' do
|
23
|
+
res = request(build_options(:try => ['html'])).get('/documents')
|
24
|
+
res.should be_ok
|
25
|
+
res.body.should == "Hello World"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when file can be found' do
|
30
|
+
it 'should serve first found' do
|
31
|
+
res = request(build_options(:try => ['.html', '/index.html', '/index.htm'])).get('/documents')
|
32
|
+
res.should be_ok
|
33
|
+
res.body.strip.should == "index.html"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when path_info maps directly to file' do
|
38
|
+
it 'should serve existing' do
|
39
|
+
res = request(build_options(:try => ['/index.html'])).get('/documents/existing.html')
|
40
|
+
res.should be_ok
|
41
|
+
res.body.strip.should == "existing.html"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when sharing options' do
|
46
|
+
it 'should not mutate given options' do
|
47
|
+
org_options = build_options :try => ['/index.html']
|
48
|
+
given_options = org_options.dup
|
49
|
+
request(given_options).get('/documents').should be_ok
|
50
|
+
request(given_options).get('/documents').should be_ok
|
51
|
+
given_options.should == org_options
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-try_static2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Caleb Buxton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.14'
|
69
|
+
description: The Recontributed Rack Middleware
|
70
|
+
email:
|
71
|
+
- me@cpb.ca
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/rack/try_static.rb
|
82
|
+
- lib/rack/try_static/version.rb
|
83
|
+
- rack-try_static.gemspec
|
84
|
+
- spec/rack/documents/existing.html
|
85
|
+
- spec/rack/documents/index.htm
|
86
|
+
- spec/rack/documents/index.html
|
87
|
+
- spec/rack/documents/test
|
88
|
+
- spec/rack/try_static_spec.rb
|
89
|
+
homepage: ''
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.0.3
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: The Recontributed Rack Middleware
|
113
|
+
test_files:
|
114
|
+
- spec/rack/documents/existing.html
|
115
|
+
- spec/rack/documents/index.htm
|
116
|
+
- spec/rack/documents/index.html
|
117
|
+
- spec/rack/documents/test
|
118
|
+
- spec/rack/try_static_spec.rb
|