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.
data/README.md ADDED
@@ -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
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'rake/testtask'
3
+ require 'rake/gempackagetask'
4
+
5
+ task :default => :test
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gemspec|
10
+ gemspec.name = "sinatra-xsendfile"
11
+ gemspec.version = "0.1.1"
12
+ gemspec.summary = gemspec.description = "X-SendFile helpers for Sinatra"
13
+ gemspec.email = "vasily@polovnyov.ru"
14
+ gemspec.homepage = "http://github.com/vast/sinatra-xsendfile"
15
+ gemspec.authors = ["Vasily Polovnyov"]
16
+
17
+ gemspec.add_dependency 'sinatra', '>=0.9.1'
18
+ gemspec.add_dependency 'rack', '>=0.9.1'
19
+
20
+ gemspec.add_development_dependency 'rack-test', '>=0.3.0'
21
+
22
+ gemspec.test_files = Dir.glob('test/*')
23
+ gemspec.files = ["LICENSE", "README.md", "Rakefile"] + Dir.glob('lib/**/*') + gemspec.test_files
24
+
25
+ end
26
+ rescue LoadError
27
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
28
+ end
29
+
30
+ Rake::TestTask.new do |t|
31
+ t.libs << "test"
32
+ t.test_files = FileList['test/*_test.rb']
33
+ t.verbose = true
34
+ 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,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 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-11-18 00:00:00 +03: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
+ - LICENSE
53
+ - README.md
54
+ files:
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/sinatra/x_send_file.rb
59
+ - test/sinatra_app.rb
60
+ - test/sinatra_x_send_file_test.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/vast/sinatra-xsendfile
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.3.5
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: X-SendFile helpers for Sinatra
89
+ test_files:
90
+ - test/sinatra_x_send_file_test.rb
91
+ - test/sinatra_app.rb