rack-raw-upload 1.0.9 → 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -20
- data/lib/rack/raw_upload.rb +7 -2
- data/test/raw_upload_test.rb +5 -0
- metadata +19 -7
data/README.md
CHANGED
@@ -1,21 +1,18 @@
|
|
1
1
|
# Rack Raw Upload middleware
|
2
2
|
|
3
|
-
Rack::RawUpload converts raw
|
3
|
+
Rack::RawUpload converts raw uploads into normal multipart requests, like those in a form. Rack applications can then read these as normal (using `params` for example), rather than from `env['rack.input']` or similar.
|
4
4
|
|
5
|
-
Rack::RawUpload
|
6
|
-
|
7
|
-
* application/x-www-form-urlencoded
|
8
|
-
* multipart/form-data
|
9
|
-
|
10
|
-
Additionally, it can be told explicitly to perform the conversion, using the header `X-File-Upload`. See below for details.
|
5
|
+
Rack::RawUpload processes a request this way when the mimetype **is not** one of the following:
|
11
6
|
|
12
7
|
## Assumptions
|
13
8
|
|
14
|
-
Rack::RawUpload
|
15
|
-
|
16
|
-
1. be POST or PUT requests
|
17
|
-
2. set the mimetype `application/octet-stream`
|
9
|
+
Rack::RawUpload performs this conversion when all these conditions are met:
|
18
10
|
|
11
|
+
1. The request method is POST or PUT
|
12
|
+
2. The mimetype is one of
|
13
|
+
* application/x-www-form-urlencoded
|
14
|
+
* multipart/form-data
|
15
|
+
3. The request includes body data
|
19
16
|
|
20
17
|
## Configuration
|
21
18
|
|
@@ -45,24 +42,24 @@ and then add the middleware in application.rb
|
|
45
42
|
|
46
43
|
config.middleware.use 'Rack::RawUpload'
|
47
44
|
|
48
|
-
## More options
|
49
45
|
|
50
|
-
|
46
|
+
## Usage
|
47
|
+
|
48
|
+
The upload is made into a request argument called `file`. In several popular frameworks, this can be accessed as `params[:file]`. This includes Rails and Sinatra, but may be different in other frameworks.
|
51
49
|
|
52
|
-
Raw uploads, due to their own nature, don't include the name of the file being uploaded. You can work around this limitation by specifying the filename as an HTTP header.
|
53
50
|
|
54
|
-
|
51
|
+
## Optional request headers
|
55
52
|
|
56
|
-
|
53
|
+
Raw uploads, due to their own nature, can't provide additional arguments in the request. This limitation can be worked around using headers.
|
57
54
|
|
58
|
-
|
55
|
+
* `X-File-Name`: specify the name of the uploaded file.
|
56
|
+
* `X-Query-Params`: JSON-formatted hash containing additional arguments. On Rails or Sinatra, you can read these as `params[:name_of_argument]`
|
59
57
|
|
60
|
-
When present, Rack::RawUpload will assume that the header ***`X-Query-Params`*** contains these additional parameters. The values are expected to be in the form of a **JSON** hash.
|
61
58
|
|
62
59
|
## Additional info
|
63
60
|
|
64
|
-
A blog post on
|
61
|
+
A blog post on Ajax uploads. These are raw uploads and can be greatly simplified with this middleware:
|
65
62
|
|
66
63
|
* [http://blog.new-bamboo.co.uk/2010/7/30/html5-powered-ajax-file-uploads](http://blog.new-bamboo.co.uk/2010/7/30/html5-powered-ajax-file-uploads)
|
67
64
|
|
68
|
-
This middleware should work with Ruby 1.8.7, 1.9.2, REE, Rubinius and JRuby. Tests for all these platforms are run on the wonderful [Travis-CI](http://travis-ci.org/) regularly, and the current status of these is: [![Build Status](http://travis-ci.org/newbamboo/rack-raw-upload.png)](http://travis-ci.org/newbamboo/rack-raw-upload)
|
65
|
+
This middleware should work with Ruby 1.8.7, 1.9.2, 1.9.3, REE, Rubinius and JRuby. Tests for all these platforms are run on the wonderful [Travis-CI](http://travis-ci.org/) regularly, and the current status of these is: [![Build Status](http://travis-ci.org/newbamboo/rack-raw-upload.png)](http://travis-ci.org/newbamboo/rack-raw-upload)
|
data/lib/rack/raw_upload.rb
CHANGED
@@ -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
|
+
VERSION = '1.0.10'
|
7
7
|
|
8
8
|
def initialize(app, opts = {})
|
9
9
|
@app = app
|
@@ -75,7 +75,8 @@ module Rack
|
|
75
75
|
def raw_file_upload?(env)
|
76
76
|
upload_path?(env['PATH_INFO']) &&
|
77
77
|
%{POST PUT}.include?(env['REQUEST_METHOD']) &&
|
78
|
-
content_type_of_raw_file?(env['CONTENT_TYPE'])
|
78
|
+
content_type_of_raw_file?(env['CONTENT_TYPE']) &&
|
79
|
+
input_is_present?(env['rack.input'])
|
79
80
|
end
|
80
81
|
|
81
82
|
def literal_path_match?(request_path, candidate)
|
@@ -97,6 +98,10 @@ module Rack
|
|
97
98
|
end
|
98
99
|
end
|
99
100
|
|
101
|
+
def input_is_present?(input)
|
102
|
+
!input.nil? && (!input.respond_to?(:empty?) || !input.empty?)
|
103
|
+
end
|
104
|
+
|
100
105
|
def random_string
|
101
106
|
(0...8).map{65.+(rand(25)).chr}.join
|
102
107
|
end
|
data/test/raw_upload_test.rb
CHANGED
@@ -73,6 +73,11 @@ class RawUploadTest < Test::Unit::TestCase
|
|
73
73
|
assert_successful_non_upload
|
74
74
|
end
|
75
75
|
|
76
|
+
should "not work when there is no input" do
|
77
|
+
upload('rack.input' => '')
|
78
|
+
assert_successful_non_upload
|
79
|
+
end
|
80
|
+
|
76
81
|
# "stuff" should be something like "boundary=----WebKitFormBoundaryeKPeU4p65YgercgO",
|
77
82
|
# but if I do that here, Rack tries to be clever and the test breaks
|
78
83
|
should "not work with Content-Type 'multipart/form-data; stuff'" do
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-raw-upload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 3
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 1
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 1.0.
|
9
|
+
- 10
|
10
|
+
version: 1.0.10
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Pablo Brasero
|
@@ -14,16 +15,17 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2011-
|
18
|
-
default_executable:
|
18
|
+
date: 2011-11-23 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: json
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
24
25
|
requirements:
|
25
26
|
- - ">="
|
26
27
|
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
27
29
|
segments:
|
28
30
|
- 0
|
29
31
|
version: "0"
|
@@ -33,9 +35,11 @@ dependencies:
|
|
33
35
|
name: rake
|
34
36
|
prerelease: false
|
35
37
|
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
36
39
|
requirements:
|
37
40
|
- - ">="
|
38
41
|
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
39
43
|
segments:
|
40
44
|
- 0
|
41
45
|
version: "0"
|
@@ -45,9 +49,11 @@ dependencies:
|
|
45
49
|
name: rack-test
|
46
50
|
prerelease: false
|
47
51
|
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
48
53
|
requirements:
|
49
54
|
- - ">="
|
50
55
|
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
51
57
|
segments:
|
52
58
|
- 0
|
53
59
|
version: "0"
|
@@ -57,9 +63,11 @@ dependencies:
|
|
57
63
|
name: shoulda
|
58
64
|
prerelease: false
|
59
65
|
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
60
67
|
requirements:
|
61
68
|
- - ">="
|
62
69
|
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
63
71
|
segments:
|
64
72
|
- 0
|
65
73
|
version: "0"
|
@@ -101,7 +109,6 @@ files:
|
|
101
109
|
- README.md
|
102
110
|
- Gemfile
|
103
111
|
- Gemfile.lock
|
104
|
-
has_rdoc: true
|
105
112
|
homepage: https://github.com/newbamboo/rack-raw-upload
|
106
113
|
licenses: []
|
107
114
|
|
@@ -113,26 +120,31 @@ rdoc_options:
|
|
113
120
|
require_paths:
|
114
121
|
- lib
|
115
122
|
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
116
124
|
requirements:
|
117
125
|
- - ">="
|
118
126
|
- !ruby/object:Gem::Version
|
127
|
+
hash: 3
|
119
128
|
segments:
|
120
129
|
- 0
|
121
130
|
version: "0"
|
122
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
123
133
|
requirements:
|
124
134
|
- - ">="
|
125
135
|
- !ruby/object:Gem::Version
|
136
|
+
hash: 3
|
126
137
|
segments:
|
127
138
|
- 0
|
128
139
|
version: "0"
|
129
140
|
requirements: []
|
130
141
|
|
131
142
|
rubyforge_project:
|
132
|
-
rubygems_version: 1.
|
143
|
+
rubygems_version: 1.8.10
|
133
144
|
signing_key:
|
134
145
|
specification_version: 3
|
135
146
|
summary: Rack Raw Upload middleware
|
136
147
|
test_files:
|
137
148
|
- test/apps/undefined_conversion_error/app/controllers/test_controller.rb
|
138
149
|
- test/apps/undefined_conversion_error/test/test_helper.rb
|
150
|
+
has_rdoc:
|