rack-raw-upload 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rack/raw_upload.rb +32 -13
  2. data/test/raw_upload_test.rb +18 -10
  3. metadata +82 -55
@@ -3,7 +3,7 @@ require 'tmpdir' # Needed in 1.8.7 to access Dir::tmpdir
3
3
  module Rack
4
4
  class RawUpload
5
5
 
6
- VERSION = '1.0.6'
6
+ VERSION = '1.0.7'
7
7
 
8
8
  def initialize(app, opts = {})
9
9
  @app = app
@@ -29,19 +29,15 @@ module Rack
29
29
  private
30
30
 
31
31
  def convert_and_pass_on(env)
32
- tempfile = Tempfile.new('raw-upload.', @tmpdir)
33
- if (RUBY_VERSION.split('.').map{|e| e.to_i} <=> [1, 9]) > 0
34
- # 1.8.7: if the 'original' tempfile has no open file-handler,
35
- # the garbage collector will unlink this file.
36
- # in this case, only the path to the 'original' tempfile is used
37
- # and the physical file will be deleted, if the gc runs.
38
- tempfile2 = relink_file(tempfile)
39
- tempfile.close
40
- tempfile = tempfile2
32
+ if env['rack.input'].kind_of?(Tempfile)
33
+ env['rack.input'].extend(EqlFix)
34
+ tempfile = env['rack.input']
35
+ else
36
+ tempfile = create_tempfile
37
+ tempfile << env['rack.input'].read
38
+ tempfile.flush
39
+ tempfile.rewind
41
40
  end
42
- tempfile << env['rack.input'].read
43
- tempfile.flush
44
- tempfile.rewind
45
41
  fake_file = {
46
42
  :filename => env['HTTP_X_FILE_NAME'],
47
43
  :type => env['CONTENT_TYPE'],
@@ -92,6 +88,18 @@ module Rack
92
88
  end
93
89
  end
94
90
 
91
+ def create_tempfile
92
+ tempfile = Tempfile.new('raw-upload.', @tmpdir)
93
+
94
+ # If the GC runs, it may unlink the tempfile.
95
+ # To avoid this, I create another version of it
96
+ # (a hard link to the same file). If the original
97
+ # is unlinked, we'll still have this other link.
98
+ ret = relink_file(tempfile)
99
+ tempfile.close
100
+ ret
101
+ end
102
+
95
103
  def relink_file(file)
96
104
  new_name = file.path + random_string
97
105
  ::File.link(file.path, new_name)
@@ -107,4 +115,15 @@ module Rack
107
115
  (0...8).map{65.+(rand(25)).chr}.join
108
116
  end
109
117
  end
118
+
119
+
120
+ module EqlFix
121
+ def eql_with_fix?(o)
122
+ self.object_id.eql?(o.object_id) || self.eql_without_fix?(o)
123
+ end
124
+
125
+ alias_method :eql_without_fix?, :eql?
126
+ alias_method :eql?, :eql_with_fix?
127
+ end
128
+
110
129
  end
@@ -54,7 +54,7 @@ class RawUploadTest < Test::Unit::TestCase
54
54
  end
55
55
 
56
56
  should "work with Content-Type 'application/octet-stream'" do
57
- upload('CONTENT_TYPE' => 'application/octet-stream')
57
+ upload
58
58
  assert_file_uploaded_as 'application/octet-stream'
59
59
  end
60
60
 
@@ -69,7 +69,7 @@ class RawUploadTest < Test::Unit::TestCase
69
69
  end
70
70
 
71
71
  should "not work with Content-Type 'multipart/form-data'" do
72
- post('CONTENT_TYPE' => 'multipart/form-data')
72
+ post
73
73
  assert_successful_non_upload
74
74
  end
75
75
 
@@ -79,6 +79,14 @@ class RawUploadTest < Test::Unit::TestCase
79
79
  post('CONTENT_TYPE' => 'multipart/form-data; stuff')
80
80
  assert_successful_non_upload
81
81
  end
82
+
83
+ should "work when the input is a Tempfile" do
84
+ tempfile = Tempfile.new('rack-raw-upload-test-')
85
+ tempfile << @file.read
86
+ tempfile.rewind
87
+ upload('rack.input' => tempfile)
88
+ assert_file_uploaded
89
+ end
82
90
 
83
91
  should "be forced to perform a file upload if `X-File-Upload: true`" do
84
92
  upload('CONTENT_TYPE' => 'multipart/form-data', 'HTTP_X_FILE_UPLOAD' => 'true')
@@ -90,8 +98,8 @@ class RawUploadTest < Test::Unit::TestCase
90
98
  assert_successful_non_upload
91
99
  end
92
100
 
93
- should "be compatible to rails 1.8.7 and tempfile must exist after garbage collection" do
94
- upload('CONTENT_TYPE' => 'application/octet-stream')
101
+ should "ensure the uploaded file exists after garbage collection (Ruby 1.9)" do
102
+ upload
95
103
  received = last_request.POST["file"]
96
104
  GC.start
97
105
  assert File.exists?(received[:tempfile].path)
@@ -99,7 +107,7 @@ class RawUploadTest < Test::Unit::TestCase
99
107
 
100
108
  context "with X-File-Upload: smart" do
101
109
  should "perform a file upload if appropriate" do
102
- post('CONTENT_TYPE' => 'multipart/form-data', 'HTTP_X_FILE_UPLOAD' => 'smart')
110
+ post('HTTP_X_FILE_UPLOAD' => 'smart')
103
111
  assert_successful_non_upload
104
112
  end
105
113
 
@@ -130,7 +138,7 @@ class RawUploadTest < Test::Unit::TestCase
130
138
  end
131
139
 
132
140
  should "stay put when `X-File-Upload: smart` and the request is not an upload" do
133
- post('CONTENT_TYPE' => 'multipart/form-data', 'HTTP_X_FILE_UPLOAD' => 'smart')
141
+ post('HTTP_X_FILE_UPLOAD' => 'smart')
134
142
  assert_successful_non_upload
135
143
  end
136
144
  end
@@ -206,7 +214,7 @@ class RawUploadTest < Test::Unit::TestCase
206
214
 
207
215
  def assert_file_uploaded
208
216
  file = File.open(@path)
209
- received = last_request.POST["file"]
217
+ assert received = last_request.POST["file"], "No file received"
210
218
  assert_files_equal file, received[:tempfile]
211
219
  assert last_response.ok?
212
220
  end
@@ -225,8 +233,8 @@ class RawUploadTest < Test::Unit::TestCase
225
233
  end
226
234
 
227
235
  def assert_files_equal(f1, f2)
228
- f1.seek(0)
229
- f2.seek(0)
230
- assert_equal Digest::MD5.hexdigest(f1.read), Digest::MD5.hexdigest(f2.read)
236
+ expected = Digest::MD5.hexdigest(IO.read(f1.path))
237
+ actual = Digest::MD5.hexdigest(IO.read(f2.path))
238
+ assert_equal expected, actual
231
239
  end
232
240
  end
metadata CHANGED
@@ -1,70 +1,88 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rack-raw-upload
3
- version: !ruby/object:Gem::Version
4
- version: 1.0.6
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
5
  prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 7
10
+ version: 1.0.7
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Pablo Brasero
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2011-07-30 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2011-08-20 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: json
16
- requirement: &2157245860 !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
17
24
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
22
32
  type: :runtime
23
- prerelease: false
24
- version_requirements: *2157245860
25
- - !ruby/object:Gem::Dependency
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
26
35
  name: rake
27
- requirement: &2157245420 !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
28
38
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
33
46
  type: :development
34
- prerelease: false
35
- version_requirements: *2157245420
36
- - !ruby/object:Gem::Dependency
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
37
49
  name: rack-test
38
- requirement: &2157245000 !ruby/object:Gem::Requirement
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
39
52
  none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
44
60
  type: :development
45
- prerelease: false
46
- version_requirements: *2157245000
47
- - !ruby/object:Gem::Dependency
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
48
63
  name: shoulda
49
- requirement: &2157271180 !ruby/object:Gem::Requirement
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
50
66
  none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
55
74
  type: :development
56
- prerelease: false
57
- version_requirements: *2157271180
58
- description: Middleware that converts files uploaded with mimetype application/octet-stream
59
- into normal form input, so Rack applications can read these as normal, rather than
60
- as raw input.
75
+ version_requirements: *id004
76
+ description: Middleware that converts files uploaded with mimetype application/octet-stream into normal form input, so Rack applications can read these as normal, rather than as raw input.
61
77
  email: pablobm@gmail.com
62
78
  executables: []
79
+
63
80
  extensions: []
64
- extra_rdoc_files:
81
+
82
+ extra_rdoc_files:
65
83
  - LICENSE
66
84
  - README.md
67
- files:
85
+ files:
68
86
  - lib/rack/raw_upload.rb
69
87
  - lib/rack-raw-upload.rb
70
88
  - test/raw_upload_test.rb
@@ -74,29 +92,38 @@ files:
74
92
  - Gemfile.lock
75
93
  homepage: https://github.com/newbamboo/rack-raw-upload
76
94
  licenses: []
95
+
77
96
  post_install_message:
78
- rdoc_options:
97
+ rdoc_options:
79
98
  - --charset=UTF-8
80
99
  - --main
81
100
  - README.rdoc
82
- require_paths:
101
+ require_paths:
83
102
  - lib
84
- required_ruby_version: !ruby/object:Gem::Requirement
103
+ required_ruby_version: !ruby/object:Gem::Requirement
85
104
  none: false
86
- requirements:
87
- - - ! '>='
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
113
  none: false
92
- requirements:
93
- - - ! '>='
94
- - !ruby/object:Gem::Version
95
- version: '0'
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
96
121
  requirements: []
122
+
97
123
  rubyforge_project:
98
124
  rubygems_version: 1.8.6
99
125
  signing_key:
100
126
  specification_version: 3
101
127
  summary: Rack Raw Upload middleware
102
128
  test_files: []
129
+