vast-sinatra-xsendfile 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Vasily Polovnyov
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ #Sinatra Extension: XSendFile
2
+
3
+ `sinatra-xsendfile` extension provides `x_send_file` helper method for sending files faster
4
+
5
+ ##XSendFile
6
+ * in [lighttpd](http://redmine.lighttpd.net/projects/1/wiki/X-LIGHTTPD-send-file);
7
+ * in [apache](http://tn123.ath.cx/mod_xsendfile/);
8
+ * in [nginx](http://wiki.nginx.org/NginxXSendfile).
9
+
10
+
11
+ ##Example
12
+ require 'rubygems'
13
+ require 'sinatra'
14
+ gem 'vast-sinatra-xsendfile'
15
+ require 'sinatra/x_send_file'
16
+
17
+ configure :production do
18
+ Sinatra::XSendFile.replace_send_file! #replaces sinatra's send_file with x_send_file
19
+ set :xsf_header, 'X-Accel-Redirect' #setting default(X-SendFile) header (nginx)
20
+ end
21
+
22
+ get '/' do
23
+ x_send_file(__FILE__)
24
+ end
25
+
26
+ get '/nginx' do
27
+ x_send_file(__FILE__, :header => 'X-LIGHTTPD-send-file')
28
+ end
29
+
30
+ get '/sendfile' do
31
+ send_file(__FILE__) #will work as x_send_file in production
32
+ end
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake/testtask'
3
+ require 'rake/gempackagetask'
4
+ require 'rubygems/specification'
5
+
6
+ GEM = "sinatra-xsendfile"
7
+ GEM_VERSION = "0.1.1"
8
+ AUTHOR = "Vasily Polovnyov"
9
+ EMAIL = "vasily@polovnyov.ru"
10
+ HOMEPAGE = "http://github.com/vast/sinatra-xsendfile"
11
+ SUMMARY = "X-SendFile helpers for Sinatra"
12
+
13
+ spec = Gem::Specification.new do |s|
14
+ s.name = GEM
15
+ s.version = GEM_VERSION
16
+ s.has_rdoc = false
17
+ s.summary = SUMMARY
18
+ s.description = s.summary
19
+ s.author = AUTHOR
20
+ s.email = EMAIL
21
+ s.homepage = HOMEPAGE
22
+
23
+ s.add_dependency 'sinatra', '>=0.9.1'
24
+ s.add_dependency 'rack', '>=0.9.1'
25
+
26
+ s.add_development_dependency 'rack-test', '>=0.3.0'
27
+
28
+ s.files = ["LICENSE", "README.md", "Rakefile"] + Dir.glob('lib/**/*')
29
+ s.test_files = Dir.glob('test/*')
30
+ end
31
+
32
+ task :default => :test
33
+
34
+ Rake::GemPackageTask.new(spec) do |pkg|
35
+ pkg.gem_spec = spec
36
+ end
37
+
38
+ Rake::TestTask.new do |t|
39
+ t.libs << "test"
40
+ t.test_files = FileList['test/*_test.rb']
41
+ t.verbose = true
42
+ end
43
+
44
+ desc "create a gemspec file"
45
+ task :make_spec do
46
+ File.open("#{GEM}.gemspec", "w") do |file|
47
+ file.puts spec.to_ruby
48
+ end
49
+ end
@@ -0,0 +1,37 @@
1
+ require 'sinatra/base'
2
+ module Sinatra
3
+
4
+ module XSendFile
5
+ def x_send_file(path, opts = {})
6
+ stat = File.stat(path)
7
+
8
+ content_type media_type(opts[:type]) ||
9
+ media_type(File.extname(path)) ||
10
+ response['Content-Type'] ||
11
+ 'application/octet-stream'
12
+
13
+ if opts[:disposition] == 'attachment' || opts[:filename]
14
+ attachment opts[:filename] || path
15
+ elsif opts[:disposition] == 'inline'
16
+ response['Content-Disposition'] = 'inline'
17
+ end
18
+
19
+ header_key = opts[:header] || (Sinatra::Application.respond_to?(:xsf_header) && Sinatra::Application.xsf_header) ||
20
+ 'X-SendFile'
21
+ path = File.expand_path(path).gsub(Sinatra::Application.public, '') if header_key == 'X-Accel-Redirect'
22
+
23
+ response[header_key] = path
24
+
25
+ halt
26
+ rescue Errno::ENOENT
27
+ not_found
28
+ end
29
+
30
+ def self.replace_send_file!
31
+ Sinatra::Helpers.send(:alias_method, :old_send_file, :send_file)
32
+ Sinatra::Helpers.module_eval("def send_file(path, opts={}); x_send_file(path, opts); end;")
33
+ end
34
+ end
35
+
36
+ helpers XSendFile
37
+ end
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'lib/sinatra/x_send_file'
4
+
5
+
6
+ set :xsf_header, 'x-test-send-file'
7
+
8
+ Sinatra::XSendFile.replace_send_file!
9
+
10
+ get '/' do
11
+ x_send_file(__FILE__)
12
+ end
13
+
14
+ get '/nginx' do
15
+ x_send_file(__FILE__, :header => 'X-Accel-Redirect')
16
+ end
17
+
18
+ get '/lighty1.4' do
19
+ x_send_file(__FILE__, :header => 'X-LIGHTTPD-send-file')
20
+ end
21
+
22
+ get '/xsendfile' do
23
+ x_send_file(__FILE__, :header => 'X-SendFile')
24
+ end
25
+
26
+ get '/sendfile' do
27
+ send_file(__FILE__)
28
+ end
@@ -0,0 +1,38 @@
1
+ require 'sinatra_app'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+
5
+ set :environment, :test
6
+
7
+ class SinatraXSendFileTest < Test::Unit::TestCase
8
+ include Rack::Test::Methods
9
+
10
+ def app
11
+ Sinatra::Application
12
+ end
13
+
14
+ def test_x_send_file
15
+ get '/xsendfile'
16
+ assert_not_nil last_response.headers['X-SendFile']
17
+ end
18
+
19
+ def test_nginx_x_accel_redirect
20
+ get '/nginx'
21
+ assert_not_nil last_response.headers['X-Accel-Redirect']
22
+ end
23
+
24
+ def test_lighty1_4_send_file
25
+ get '/lighty1.4'
26
+ assert_not_nil last_response.headers['X-LIGHTTPD-send-file']
27
+ end
28
+
29
+ def test_header_global_override
30
+ get '/'
31
+ assert_not_nil last_response.headers['x-test-send-file']
32
+ end
33
+
34
+ def test_sendfile_overriding
35
+ get '/sendfile'
36
+ assert_not_nil last_response.headers['x-test-send-file']
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vast-sinatra-xsendfile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Vasily Polovnyov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-15 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rack
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.1
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rack-test
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.3.0
44
+ version:
45
+ description: X-SendFile helpers for Sinatra
46
+ email: vasily@polovnyov.ru
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files: []
52
+
53
+ files:
54
+ - LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - lib/sinatra
58
+ - lib/sinatra/x_send_file.rb
59
+ - test/sinatra_x_send_file_test.rb
60
+ - test/sinatra_app.rb
61
+ has_rdoc: false
62
+ homepage: http://github.com/vast/sinatra-xsendfile
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.2.0
84
+ signing_key:
85
+ specification_version: 2
86
+ summary: X-SendFile helpers for Sinatra
87
+ test_files:
88
+ - test/sinatra_x_send_file_test.rb
89
+ - test/sinatra_app.rb