rack 2.0.1 → 2.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +795 -0
  3. data/CONTRIBUTING.md +136 -0
  4. data/{COPYING → MIT-LICENSE} +4 -2
  5. data/README.rdoc +188 -145
  6. data/Rakefile +37 -23
  7. data/{SPEC → SPEC.rdoc} +46 -17
  8. data/bin/rackup +1 -0
  9. data/example/lobster.ru +2 -0
  10. data/example/protectedlobster.rb +3 -1
  11. data/example/protectedlobster.ru +2 -0
  12. data/lib/rack/auth/abstract/handler.rb +3 -1
  13. data/lib/rack/auth/abstract/request.rb +1 -1
  14. data/lib/rack/auth/basic.rb +6 -4
  15. data/lib/rack/auth/digest/md5.rb +13 -11
  16. data/lib/rack/auth/digest/nonce.rb +5 -3
  17. data/lib/rack/auth/digest/params.rb +4 -2
  18. data/lib/rack/auth/digest/request.rb +5 -3
  19. data/lib/rack/body_proxy.rb +15 -14
  20. data/lib/rack/builder.rb +116 -23
  21. data/lib/rack/cascade.rb +28 -12
  22. data/lib/rack/chunked.rb +68 -20
  23. data/lib/rack/common_logger.rb +37 -25
  24. data/lib/rack/conditional_get.rb +20 -16
  25. data/lib/rack/config.rb +2 -0
  26. data/lib/rack/content_length.rb +8 -7
  27. data/lib/rack/content_type.rb +5 -4
  28. data/lib/rack/core_ext/regexp.rb +14 -0
  29. data/lib/rack/deflater.rb +60 -70
  30. data/lib/rack/directory.rb +84 -64
  31. data/lib/rack/etag.rb +8 -5
  32. data/lib/rack/events.rb +19 -20
  33. data/lib/rack/file.rb +4 -173
  34. data/lib/rack/files.rb +218 -0
  35. data/lib/rack/handler/cgi.rb +2 -3
  36. data/lib/rack/handler/fastcgi.rb +4 -4
  37. data/lib/rack/handler/lsws.rb +3 -3
  38. data/lib/rack/handler/scgi.rb +9 -8
  39. data/lib/rack/handler/thin.rb +3 -3
  40. data/lib/rack/handler/webrick.rb +19 -10
  41. data/lib/rack/handler.rb +7 -2
  42. data/lib/rack/head.rb +1 -1
  43. data/lib/rack/lint.rb +221 -186
  44. data/lib/rack/lobster.rb +10 -10
  45. data/lib/rack/lock.rb +14 -4
  46. data/lib/rack/logger.rb +2 -0
  47. data/lib/rack/media_type.rb +23 -8
  48. data/lib/rack/method_override.rb +13 -4
  49. data/lib/rack/mime.rb +9 -1
  50. data/lib/rack/mock.rb +135 -29
  51. data/lib/rack/multipart/generator.rb +17 -13
  52. data/lib/rack/multipart/parser.rb +85 -68
  53. data/lib/rack/multipart/uploaded_file.rb +15 -7
  54. data/lib/rack/multipart.rb +6 -5
  55. data/lib/rack/null_logger.rb +2 -0
  56. data/lib/rack/query_parser.rb +108 -36
  57. data/lib/rack/recursive.rb +7 -5
  58. data/lib/rack/reloader.rb +8 -4
  59. data/lib/rack/request.rb +232 -60
  60. data/lib/rack/response.rb +127 -44
  61. data/lib/rack/rewindable_input.rb +4 -3
  62. data/lib/rack/runtime.rb +6 -4
  63. data/lib/rack/sendfile.rb +14 -10
  64. data/lib/rack/server.rb +97 -25
  65. data/lib/rack/session/abstract/id.rb +113 -25
  66. data/lib/rack/session/cookie.rb +22 -14
  67. data/lib/rack/session/memcache.rb +4 -87
  68. data/lib/rack/session/pool.rb +24 -10
  69. data/lib/rack/show_exceptions.rb +22 -18
  70. data/lib/rack/show_status.rb +9 -9
  71. data/lib/rack/static.rb +25 -12
  72. data/lib/rack/tempfile_reaper.rb +1 -1
  73. data/lib/rack/urlmap.rb +13 -7
  74. data/lib/rack/utils.rb +135 -123
  75. data/lib/rack/version.rb +29 -0
  76. data/lib/rack.rb +67 -73
  77. data/rack.gemspec +40 -29
  78. metadata +25 -184
  79. data/HISTORY.md +0 -505
  80. data/test/builder/an_underscore_app.rb +0 -5
  81. data/test/builder/anything.rb +0 -5
  82. data/test/builder/comment.ru +0 -4
  83. data/test/builder/end.ru +0 -5
  84. data/test/builder/line.ru +0 -1
  85. data/test/builder/options.ru +0 -2
  86. data/test/cgi/assets/folder/test.js +0 -1
  87. data/test/cgi/assets/fonts/font.eot +0 -1
  88. data/test/cgi/assets/images/image.png +0 -1
  89. data/test/cgi/assets/index.html +0 -1
  90. data/test/cgi/assets/javascripts/app.js +0 -1
  91. data/test/cgi/assets/stylesheets/app.css +0 -1
  92. data/test/cgi/lighttpd.conf +0 -26
  93. data/test/cgi/rackup_stub.rb +0 -6
  94. data/test/cgi/sample_rackup.ru +0 -5
  95. data/test/cgi/test +0 -9
  96. data/test/cgi/test+directory/test+file +0 -1
  97. data/test/cgi/test.fcgi +0 -9
  98. data/test/cgi/test.gz +0 -0
  99. data/test/cgi/test.ru +0 -5
  100. data/test/gemloader.rb +0 -10
  101. data/test/helper.rb +0 -34
  102. data/test/multipart/bad_robots +0 -259
  103. data/test/multipart/binary +0 -0
  104. data/test/multipart/content_type_and_no_filename +0 -6
  105. data/test/multipart/empty +0 -10
  106. data/test/multipart/fail_16384_nofile +0 -814
  107. data/test/multipart/file1.txt +0 -1
  108. data/test/multipart/filename_and_modification_param +0 -7
  109. data/test/multipart/filename_and_no_name +0 -6
  110. data/test/multipart/filename_with_encoded_words +0 -7
  111. data/test/multipart/filename_with_escaped_quotes +0 -6
  112. data/test/multipart/filename_with_escaped_quotes_and_modification_param +0 -7
  113. data/test/multipart/filename_with_percent_escaped_quotes +0 -6
  114. data/test/multipart/filename_with_single_quote +0 -7
  115. data/test/multipart/filename_with_unescaped_percentages +0 -6
  116. data/test/multipart/filename_with_unescaped_percentages2 +0 -6
  117. data/test/multipart/filename_with_unescaped_percentages3 +0 -6
  118. data/test/multipart/filename_with_unescaped_quotes +0 -6
  119. data/test/multipart/ie +0 -6
  120. data/test/multipart/invalid_character +0 -6
  121. data/test/multipart/mixed_files +0 -21
  122. data/test/multipart/nested +0 -10
  123. data/test/multipart/none +0 -9
  124. data/test/multipart/quoted +0 -15
  125. data/test/multipart/rack-logo.png +0 -0
  126. data/test/multipart/semicolon +0 -6
  127. data/test/multipart/text +0 -15
  128. data/test/multipart/three_files_three_fields +0 -31
  129. data/test/multipart/unity3d_wwwform +0 -11
  130. data/test/multipart/webkit +0 -32
  131. data/test/rackup/config.ru +0 -31
  132. data/test/registering_handler/rack/handler/registering_myself.rb +0 -8
  133. data/test/spec_auth_basic.rb +0 -89
  134. data/test/spec_auth_digest.rb +0 -260
  135. data/test/spec_body_proxy.rb +0 -85
  136. data/test/spec_builder.rb +0 -233
  137. data/test/spec_cascade.rb +0 -63
  138. data/test/spec_cgi.rb +0 -84
  139. data/test/spec_chunked.rb +0 -103
  140. data/test/spec_common_logger.rb +0 -95
  141. data/test/spec_conditional_get.rb +0 -103
  142. data/test/spec_config.rb +0 -23
  143. data/test/spec_content_length.rb +0 -86
  144. data/test/spec_content_type.rb +0 -46
  145. data/test/spec_deflater.rb +0 -365
  146. data/test/spec_directory.rb +0 -148
  147. data/test/spec_etag.rb +0 -108
  148. data/test/spec_events.rb +0 -133
  149. data/test/spec_fastcgi.rb +0 -85
  150. data/test/spec_file.rb +0 -251
  151. data/test/spec_handler.rb +0 -57
  152. data/test/spec_head.rb +0 -46
  153. data/test/spec_lint.rb +0 -515
  154. data/test/spec_lobster.rb +0 -59
  155. data/test/spec_lock.rb +0 -194
  156. data/test/spec_logger.rb +0 -24
  157. data/test/spec_media_type.rb +0 -42
  158. data/test/spec_method_override.rb +0 -83
  159. data/test/spec_mime.rb +0 -51
  160. data/test/spec_mock.rb +0 -342
  161. data/test/spec_multipart.rb +0 -716
  162. data/test/spec_null_logger.rb +0 -21
  163. data/test/spec_recursive.rb +0 -75
  164. data/test/spec_request.rb +0 -1393
  165. data/test/spec_response.rb +0 -510
  166. data/test/spec_rewindable_input.rb +0 -128
  167. data/test/spec_runtime.rb +0 -50
  168. data/test/spec_sendfile.rb +0 -125
  169. data/test/spec_server.rb +0 -193
  170. data/test/spec_session_abstract_id.rb +0 -31
  171. data/test/spec_session_abstract_session_hash.rb +0 -28
  172. data/test/spec_session_cookie.rb +0 -442
  173. data/test/spec_session_memcache.rb +0 -320
  174. data/test/spec_session_pool.rb +0 -210
  175. data/test/spec_show_exceptions.rb +0 -80
  176. data/test/spec_show_status.rb +0 -104
  177. data/test/spec_static.rb +0 -184
  178. data/test/spec_tempfile_reaper.rb +0 -64
  179. data/test/spec_thin.rb +0 -96
  180. data/test/spec_urlmap.rb +0 -237
  181. data/test/spec_utils.rb +0 -742
  182. data/test/spec_version.rb +0 -11
  183. data/test/spec_webrick.rb +0 -208
  184. data/test/static/another/index.html +0 -1
  185. data/test/static/foo.html +0 -1
  186. data/test/static/index.html +0 -1
  187. data/test/testrequest.rb +0 -78
  188. data/test/unregistered_handler/rack/handler/unregistered.rb +0 -7
  189. data/test/unregistered_handler/rack/handler/unregistered_long_one.rb +0 -7
@@ -1,716 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'minitest/autorun'
4
- require 'rack'
5
- require 'rack/multipart'
6
- require 'rack/multipart/parser'
7
- require 'rack/utils'
8
- require 'rack/mock'
9
-
10
- describe Rack::Multipart do
11
- def multipart_fixture(name, boundary = "AaB03x")
12
- file = multipart_file(name)
13
- data = File.open(file, 'rb') { |io| io.read }
14
-
15
- type = %(multipart/form-data; boundary=#{boundary})
16
- length = data.bytesize
17
-
18
- { "CONTENT_TYPE" => type,
19
- "CONTENT_LENGTH" => length.to_s,
20
- :input => StringIO.new(data) }
21
- end
22
-
23
- def multipart_file(name)
24
- File.join(File.dirname(__FILE__), "multipart", name.to_s)
25
- end
26
-
27
- it "return nil if content type is not multipart" do
28
- env = Rack::MockRequest.env_for("/",
29
- "CONTENT_TYPE" => 'application/x-www-form-urlencoded')
30
- Rack::Multipart.parse_multipart(env).must_equal nil
31
- end
32
-
33
- it "parse multipart content when content type present but filename is not" do
34
- env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_no_filename))
35
- params = Rack::Multipart.parse_multipart(env)
36
- params["text"].must_equal "contents"
37
- end
38
-
39
- it "set US_ASCII encoding based on charset" do
40
- env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_no_filename))
41
- params = Rack::Multipart.parse_multipart(env)
42
- params["text"].encoding.must_equal Encoding::US_ASCII
43
-
44
- # I'm not 100% sure if making the param name encoding match the
45
- # Content-Type charset is the right thing to do. We should revisit this.
46
- params.keys.each do |key|
47
- key.encoding.must_equal Encoding::US_ASCII
48
- end
49
- end
50
-
51
- it "set BINARY encoding on things without content type" do
52
- env = Rack::MockRequest.env_for("/", multipart_fixture(:none))
53
- params = Rack::Multipart.parse_multipart(env)
54
- params["submit-name"].encoding.must_equal Encoding::UTF_8
55
- end
56
-
57
- it "set UTF8 encoding on names of things without content type" do
58
- env = Rack::MockRequest.env_for("/", multipart_fixture(:none))
59
- params = Rack::Multipart.parse_multipart(env)
60
- params.keys.each do |key|
61
- key.encoding.must_equal Encoding::UTF_8
62
- end
63
- end
64
-
65
- it "default text to UTF8" do
66
- env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
67
- params = Rack::Multipart.parse_multipart(env)
68
- params['submit-name'].encoding.must_equal Encoding::UTF_8
69
- params['submit-name-with-content'].encoding.must_equal Encoding::UTF_8
70
- params.keys.each do |key|
71
- key.encoding.must_equal Encoding::UTF_8
72
- end
73
- end
74
-
75
- it "handles quoted encodings" do
76
- # See #905
77
- env = Rack::MockRequest.env_for("/", multipart_fixture(:unity3d_wwwform))
78
- params = Rack::Multipart.parse_multipart(env)
79
- params['user_sid'].encoding.must_equal Encoding::UTF_8
80
- end
81
-
82
- it "raise RangeError if the key space is exhausted" do
83
- env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_no_filename))
84
-
85
- old, Rack::Utils.key_space_limit = Rack::Utils.key_space_limit, 1
86
- begin
87
- lambda { Rack::Multipart.parse_multipart(env) }.must_raise(RangeError)
88
- ensure
89
- Rack::Utils.key_space_limit = old
90
- end
91
- end
92
-
93
- it "parse multipart form webkit style" do
94
- env = Rack::MockRequest.env_for '/', multipart_fixture(:webkit)
95
- env['CONTENT_TYPE'] = "multipart/form-data; boundary=----WebKitFormBoundaryWLHCs9qmcJJoyjKR"
96
- params = Rack::Multipart.parse_multipart(env)
97
- params['profile']['bio'].must_include 'hello'
98
- params['profile'].keys.must_include 'public_email'
99
- end
100
-
101
- it "reject insanely long boundaries" do
102
- # using a pipe since a tempfile can use up too much space
103
- rd, wr = IO.pipe
104
-
105
- # we only call rewind once at start, so make sure it succeeds
106
- # and doesn't hit ESPIPE
107
- def rd.rewind; end
108
- wr.sync = true
109
-
110
- # mock out length to make this pipe look like a Tempfile
111
- def rd.length
112
- 1024 * 1024 * 8
113
- end
114
-
115
- # write to a pipe in a background thread, this will write a lot
116
- # unless Rack (properly) shuts down the read end
117
- thr = Thread.new do
118
- begin
119
- wr.write("--AaB03x")
120
-
121
- # make the initial boundary a few gigs long
122
- longer = "0123456789" * 1024 * 1024
123
- (1024 * 1024).times { wr.write(longer) }
124
-
125
- wr.write("\r\n")
126
- wr.write('Content-Disposition: form-data; name="a"; filename="a.txt"')
127
- wr.write("\r\n")
128
- wr.write("Content-Type: text/plain\r\n")
129
- wr.write("\r\na")
130
- wr.write("--AaB03x--\r\n")
131
- wr.close
132
- rescue => err # this is EPIPE if Rack shuts us down
133
- err
134
- end
135
- end
136
-
137
- fixture = {
138
- "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
139
- "CONTENT_LENGTH" => rd.length.to_s,
140
- :input => rd,
141
- }
142
-
143
- env = Rack::MockRequest.env_for '/', fixture
144
- lambda {
145
- Rack::Multipart.parse_multipart(env)
146
- }.must_raise EOFError
147
- rd.close
148
-
149
- err = thr.value
150
- err.must_be_instance_of Errno::EPIPE
151
- wr.close
152
- end
153
-
154
- it 'raises an EOF error on content-length mistmatch' do
155
- env = Rack::MockRequest.env_for("/", multipart_fixture(:empty))
156
- env['rack.input'] = StringIO.new
157
- assert_raises(EOFError) do
158
- Rack::Multipart.parse_multipart(env)
159
- end
160
- end
161
-
162
- it "parse multipart upload with text file" do
163
- env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
164
- params = Rack::Multipart.parse_multipart(env)
165
- params["submit-name"].must_equal "Larry"
166
- params["submit-name-with-content"].must_equal "Berry"
167
- params["files"][:type].must_equal "text/plain"
168
- params["files"][:filename].must_equal "file1.txt"
169
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
170
- "name=\"files\"; filename=\"file1.txt\"\r\n" +
171
- "Content-Type: text/plain\r\n"
172
- params["files"][:name].must_equal "files"
173
- params["files"][:tempfile].read.must_equal "contents"
174
- end
175
-
176
- it "accept the params hash class to use for multipart parsing" do
177
- c = Class.new(Rack::QueryParser::Params) do
178
- def initialize(*)
179
- super
180
- @params = Hash.new{|h,k| h[k.to_s] if k.is_a?(Symbol)}
181
- end
182
- end
183
- query_parser = Rack::QueryParser.new c, 65536, 100
184
- env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
185
- params = Rack::Multipart.parse_multipart(env, query_parser)
186
- params[:files][:type].must_equal "text/plain"
187
- end
188
-
189
- it "preserve extension in the created tempfile" do
190
- env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
191
- params = Rack::Multipart.parse_multipart(env)
192
- File.extname(params["files"][:tempfile].path).must_equal ".txt"
193
- end
194
-
195
- it "parse multipart upload with text file with no name field" do
196
- env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_and_no_name))
197
- params = Rack::Multipart.parse_multipart(env)
198
- params["file1.txt"][:type].must_equal "text/plain"
199
- params["file1.txt"][:filename].must_equal "file1.txt"
200
- params["file1.txt"][:head].must_equal "Content-Disposition: form-data; " +
201
- "filename=\"file1.txt\"\r\n" +
202
- "Content-Type: text/plain\r\n"
203
- params["file1.txt"][:name].must_equal "file1.txt"
204
- params["file1.txt"][:tempfile].read.must_equal "contents"
205
- end
206
-
207
- it "parse multipart upload file using custom tempfile class" do
208
- env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
209
- my_tempfile = ""
210
- env['rack.multipart.tempfile_factory'] = lambda { |filename, content_type| my_tempfile }
211
- params = Rack::Multipart.parse_multipart(env)
212
- params["files"][:tempfile].object_id.must_equal my_tempfile.object_id
213
- my_tempfile.must_equal "contents"
214
- end
215
-
216
- it "parse multipart upload with nested parameters" do
217
- env = Rack::MockRequest.env_for("/", multipart_fixture(:nested))
218
- params = Rack::Multipart.parse_multipart(env)
219
- params["foo"]["submit-name"].must_equal "Larry"
220
- params["foo"]["files"][:type].must_equal "text/plain"
221
- params["foo"]["files"][:filename].must_equal "file1.txt"
222
- params["foo"]["files"][:head].must_equal "Content-Disposition: form-data; " +
223
- "name=\"foo[files]\"; filename=\"file1.txt\"\r\n" +
224
- "Content-Type: text/plain\r\n"
225
- params["foo"]["files"][:name].must_equal "foo[files]"
226
- params["foo"]["files"][:tempfile].read.must_equal "contents"
227
- end
228
-
229
- it "parse multipart upload with binary file" do
230
- env = Rack::MockRequest.env_for("/", multipart_fixture(:binary))
231
- params = Rack::Multipart.parse_multipart(env)
232
- params["submit-name"].must_equal "Larry"
233
-
234
- params["files"][:type].must_equal "image/png"
235
- params["files"][:filename].must_equal "rack-logo.png"
236
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
237
- "name=\"files\"; filename=\"rack-logo.png\"\r\n" +
238
- "Content-Type: image/png\r\n"
239
- params["files"][:name].must_equal "files"
240
- params["files"][:tempfile].read.length.must_equal 26473
241
- end
242
-
243
- it "parse multipart upload with empty file" do
244
- env = Rack::MockRequest.env_for("/", multipart_fixture(:empty))
245
- params = Rack::Multipart.parse_multipart(env)
246
- params["submit-name"].must_equal "Larry"
247
- params["files"][:type].must_equal "text/plain"
248
- params["files"][:filename].must_equal "file1.txt"
249
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
250
- "name=\"files\"; filename=\"file1.txt\"\r\n" +
251
- "Content-Type: text/plain\r\n"
252
- params["files"][:name].must_equal "files"
253
- params["files"][:tempfile].read.must_equal ""
254
- end
255
-
256
- it "parse multipart upload with filename with semicolons" do
257
- env = Rack::MockRequest.env_for("/", multipart_fixture(:semicolon))
258
- params = Rack::Multipart.parse_multipart(env)
259
- params["files"][:type].must_equal "text/plain"
260
- params["files"][:filename].must_equal "fi;le1.txt"
261
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
262
- "name=\"files\"; filename=\"fi;le1.txt\"\r\n" +
263
- "Content-Type: text/plain\r\n"
264
- params["files"][:name].must_equal "files"
265
- params["files"][:tempfile].read.must_equal "contents"
266
- end
267
-
268
- it "parse multipart upload with quoted boundary" do
269
- env = Rack::MockRequest.env_for("/", multipart_fixture(:quoted, %("AaB:03x")))
270
- params = Rack::Multipart.parse_multipart(env)
271
- params["submit-name"].must_equal "Larry"
272
- params["submit-name-with-content"].must_equal "Berry"
273
- params["files"][:type].must_equal "text/plain"
274
- params["files"][:filename].must_equal "file1.txt"
275
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
276
- "name=\"files\"; filename=\"file1.txt\"\r\n" +
277
- "Content-Type: text/plain\r\n"
278
- params["files"][:name].must_equal "files"
279
- params["files"][:tempfile].read.must_equal "contents"
280
- end
281
-
282
- it "parse multipart upload with filename with invalid characters" do
283
- env = Rack::MockRequest.env_for("/", multipart_fixture(:invalid_character))
284
- params = Rack::Multipart.parse_multipart(env)
285
- params["files"][:type].must_equal "text/plain"
286
- params["files"][:filename].must_match(/invalid/)
287
- head = "Content-Disposition: form-data; " +
288
- "name=\"files\"; filename=\"invalid\xC3.txt\"\r\n" +
289
- "Content-Type: text/plain\r\n"
290
- head = head.force_encoding(Encoding::ASCII_8BIT)
291
- params["files"][:head].must_equal head
292
- params["files"][:name].must_equal "files"
293
- params["files"][:tempfile].read.must_equal "contents"
294
- end
295
-
296
- it "parse multipart form with an encoded word filename" do
297
- env = Rack::MockRequest.env_for '/', multipart_fixture(:filename_with_encoded_words)
298
- params = Rack::Multipart.parse_multipart(env)
299
- params["files"][:filename].must_equal "файл"
300
- end
301
-
302
- it "parse multipart form with a single quote in the filename" do
303
- env = Rack::MockRequest.env_for '/', multipart_fixture(:filename_with_single_quote)
304
- params = Rack::Multipart.parse_multipart(env)
305
- params["files"][:filename].must_equal "bob's flowers.jpg"
306
- end
307
-
308
- it "not include file params if no file was selected" do
309
- env = Rack::MockRequest.env_for("/", multipart_fixture(:none))
310
- params = Rack::Multipart.parse_multipart(env)
311
- params["submit-name"].must_equal "Larry"
312
- params["files"].must_equal nil
313
- params.keys.wont_include "files"
314
- end
315
-
316
- it "parse multipart/mixed" do
317
- env = Rack::MockRequest.env_for("/", multipart_fixture(:mixed_files))
318
- params = Rack::Multipart.parse_multipart(env)
319
- params["foo"].must_equal "bar"
320
- params["files"].must_be_instance_of String
321
- params["files"].size.must_equal 252
322
- end
323
-
324
- it "parse IE multipart upload and clean up filename" do
325
- env = Rack::MockRequest.env_for("/", multipart_fixture(:ie))
326
- params = Rack::Multipart.parse_multipart(env)
327
- params["files"][:type].must_equal "text/plain"
328
- params["files"][:filename].must_equal "file1.txt"
329
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
330
- "name=\"files\"; " +
331
- 'filename="C:\Documents and Settings\Administrator\Desktop\file1.txt"' +
332
- "\r\nContent-Type: text/plain\r\n"
333
- params["files"][:name].must_equal "files"
334
- params["files"][:tempfile].read.must_equal "contents"
335
- end
336
-
337
- it "parse filename and modification param" do
338
- env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_and_modification_param))
339
- params = Rack::Multipart.parse_multipart(env)
340
- params["files"][:type].must_equal "image/jpeg"
341
- params["files"][:filename].must_equal "genome.jpeg"
342
- params["files"][:head].must_equal "Content-Type: image/jpeg\r\n" +
343
- "Content-Disposition: attachment; " +
344
- "name=\"files\"; " +
345
- "filename=genome.jpeg; " +
346
- "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";\r\n" +
347
- "Content-Description: a complete map of the human genome\r\n"
348
- params["files"][:name].must_equal "files"
349
- params["files"][:tempfile].read.must_equal "contents"
350
- end
351
-
352
- it "parse filename with escaped quotes" do
353
- env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_escaped_quotes))
354
- params = Rack::Multipart.parse_multipart(env)
355
- params["files"][:type].must_equal "application/octet-stream"
356
- params["files"][:filename].must_equal "escape \"quotes"
357
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
358
- "name=\"files\"; " +
359
- "filename=\"escape \\\"quotes\"\r\n" +
360
- "Content-Type: application/octet-stream\r\n"
361
- params["files"][:name].must_equal "files"
362
- params["files"][:tempfile].read.must_equal "contents"
363
- end
364
-
365
- it "parse filename with percent escaped quotes" do
366
- env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_percent_escaped_quotes))
367
- params = Rack::Multipart.parse_multipart(env)
368
- params["files"][:type].must_equal "application/octet-stream"
369
- params["files"][:filename].must_equal "escape \"quotes"
370
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
371
- "name=\"files\"; " +
372
- "filename=\"escape %22quotes\"\r\n" +
373
- "Content-Type: application/octet-stream\r\n"
374
- params["files"][:name].must_equal "files"
375
- params["files"][:tempfile].read.must_equal "contents"
376
- end
377
-
378
- it "parse filename with unescaped quotes" do
379
- env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_quotes))
380
- params = Rack::Multipart.parse_multipart(env)
381
- params["files"][:type].must_equal "application/octet-stream"
382
- params["files"][:filename].must_equal "escape \"quotes"
383
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
384
- "name=\"files\"; " +
385
- "filename=\"escape \"quotes\"\r\n" +
386
- "Content-Type: application/octet-stream\r\n"
387
- params["files"][:name].must_equal "files"
388
- params["files"][:tempfile].read.must_equal "contents"
389
- end
390
-
391
- it "parse filename with escaped quotes and modification param" do
392
- env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_escaped_quotes_and_modification_param))
393
- params = Rack::Multipart.parse_multipart(env)
394
- params["files"][:type].must_equal "image/jpeg"
395
- params["files"][:filename].must_equal "\"human\" genome.jpeg"
396
- params["files"][:head].must_equal "Content-Type: image/jpeg\r\n" +
397
- "Content-Disposition: attachment; " +
398
- "name=\"files\"; " +
399
- "filename=\"\"human\" genome.jpeg\"; " +
400
- "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";\r\n" +
401
- "Content-Description: a complete map of the human genome\r\n"
402
- params["files"][:name].must_equal "files"
403
- params["files"][:tempfile].read.must_equal "contents"
404
- end
405
-
406
- it "parse filename with unescaped percentage characters" do
407
- env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_percentages, "----WebKitFormBoundary2NHc7OhsgU68l3Al"))
408
- params = Rack::Multipart.parse_multipart(env)
409
- files = params["document"]["attachment"]
410
- files[:type].must_equal "image/jpeg"
411
- files[:filename].must_equal "100% of a photo.jpeg"
412
- files[:head].must_equal <<-MULTIPART
413
- Content-Disposition: form-data; name="document[attachment]"; filename="100% of a photo.jpeg"\r
414
- Content-Type: image/jpeg\r
415
- MULTIPART
416
-
417
- files[:name].must_equal "document[attachment]"
418
- files[:tempfile].read.must_equal "contents"
419
- end
420
-
421
- it "parse filename with unescaped percentage characters that look like partial hex escapes" do
422
- env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_percentages2, "----WebKitFormBoundary2NHc7OhsgU68l3Al"))
423
- params = Rack::Multipart.parse_multipart(env)
424
- files = params["document"]["attachment"]
425
- files[:type].must_equal "image/jpeg"
426
- files[:filename].must_equal "100%a"
427
- files[:head].must_equal <<-MULTIPART
428
- Content-Disposition: form-data; name="document[attachment]"; filename="100%a"\r
429
- Content-Type: image/jpeg\r
430
- MULTIPART
431
-
432
- files[:name].must_equal "document[attachment]"
433
- files[:tempfile].read.must_equal "contents"
434
- end
435
-
436
- it "parse filename with unescaped percentage characters that look like partial hex escapes" do
437
- env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_percentages3, "----WebKitFormBoundary2NHc7OhsgU68l3Al"))
438
- params = Rack::Multipart.parse_multipart(env)
439
- files = params["document"]["attachment"]
440
- files[:type].must_equal "image/jpeg"
441
- files[:filename].must_equal "100%"
442
- files[:head].must_equal <<-MULTIPART
443
- Content-Disposition: form-data; name="document[attachment]"; filename="100%"\r
444
- Content-Type: image/jpeg\r
445
- MULTIPART
446
-
447
- files[:name].must_equal "document[attachment]"
448
- files[:tempfile].read.must_equal "contents"
449
- end
450
-
451
- it "rewinds input after parsing upload" do
452
- options = multipart_fixture(:text)
453
- input = options[:input]
454
- env = Rack::MockRequest.env_for("/", options)
455
- params = Rack::Multipart.parse_multipart(env)
456
- params["submit-name"].must_equal "Larry"
457
- params["files"][:filename].must_equal "file1.txt"
458
- input.read.length.must_equal 307
459
- end
460
-
461
- it "builds multipart body" do
462
- files = Rack::Multipart::UploadedFile.new(multipart_file("file1.txt"))
463
- data = Rack::Multipart.build_multipart("submit-name" => "Larry", "files" => files)
464
-
465
- options = {
466
- "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
467
- "CONTENT_LENGTH" => data.length.to_s,
468
- :input => StringIO.new(data)
469
- }
470
- env = Rack::MockRequest.env_for("/", options)
471
- params = Rack::Multipart.parse_multipart(env)
472
- params["submit-name"].must_equal "Larry"
473
- params["files"][:filename].must_equal "file1.txt"
474
- params["files"][:tempfile].read.must_equal "contents"
475
- end
476
-
477
- it "builds nested multipart body" do
478
- files = Rack::Multipart::UploadedFile.new(multipart_file("file1.txt"))
479
- data = Rack::Multipart.build_multipart("people" => [{"submit-name" => "Larry", "files" => files}])
480
-
481
- options = {
482
- "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
483
- "CONTENT_LENGTH" => data.length.to_s,
484
- :input => StringIO.new(data)
485
- }
486
- env = Rack::MockRequest.env_for("/", options)
487
- params = Rack::Multipart.parse_multipart(env)
488
- params["people"][0]["submit-name"].must_equal "Larry"
489
- params["people"][0]["files"][:filename].must_equal "file1.txt"
490
- params["people"][0]["files"][:tempfile].read.must_equal "contents"
491
- end
492
-
493
- it "can parse fields that end at the end of the buffer" do
494
- input = File.read(multipart_file("bad_robots"))
495
-
496
- req = Rack::Request.new Rack::MockRequest.env_for("/",
497
- "CONTENT_TYPE" => "multipart/form-data, boundary=1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon",
498
- "CONTENT_LENGTH" => input.size,
499
- :input => input)
500
-
501
- req.POST['file.path'].must_equal "/var/tmp/uploads/4/0001728414"
502
- req.POST['addresses'].wont_equal nil
503
- end
504
-
505
- it "builds complete params with the chunk size of 16384 slicing exactly on boundary" do
506
- begin
507
- previous_limit = Rack::Utils.multipart_part_limit
508
- Rack::Utils.multipart_part_limit = 256
509
-
510
- data = File.open(multipart_file("fail_16384_nofile"), 'rb') { |f| f.read }.gsub(/\n/, "\r\n")
511
- options = {
512
- "CONTENT_TYPE" => "multipart/form-data; boundary=----WebKitFormBoundaryWsY0GnpbI5U7ztzo",
513
- "CONTENT_LENGTH" => data.length.to_s,
514
- :input => StringIO.new(data)
515
- }
516
- env = Rack::MockRequest.env_for("/", options)
517
- params = Rack::Multipart.parse_multipart(env)
518
-
519
- params.wont_equal nil
520
- params.keys.must_include "AAAAAAAAAAAAAAAAAAA"
521
- params["AAAAAAAAAAAAAAAAAAA"].keys.must_include "PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"
522
- params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"].keys.must_include "new"
523
- params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"].keys.must_include "-2"
524
- params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"]["-2"].keys.must_include "ba_unit_id"
525
- params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"]["-2"]["ba_unit_id"].must_equal "1017"
526
- ensure
527
- Rack::Utils.multipart_part_limit = previous_limit
528
- end
529
- end
530
-
531
- it "not reach a multi-part limit" do
532
- begin
533
- previous_limit = Rack::Utils.multipart_part_limit
534
- Rack::Utils.multipart_part_limit = 4
535
-
536
- env = Rack::MockRequest.env_for '/', multipart_fixture(:three_files_three_fields)
537
- params = Rack::Multipart.parse_multipart(env)
538
- params['reply'].must_equal 'yes'
539
- params['to'].must_equal 'people'
540
- params['from'].must_equal 'others'
541
- ensure
542
- Rack::Utils.multipart_part_limit = previous_limit
543
- end
544
- end
545
-
546
- it "reach a multipart limit" do
547
- begin
548
- previous_limit = Rack::Utils.multipart_part_limit
549
- Rack::Utils.multipart_part_limit = 3
550
-
551
- env = Rack::MockRequest.env_for '/', multipart_fixture(:three_files_three_fields)
552
- lambda { Rack::Multipart.parse_multipart(env) }.must_raise Rack::Multipart::MultipartPartLimitError
553
- ensure
554
- Rack::Utils.multipart_part_limit = previous_limit
555
- end
556
- end
557
-
558
- it "return nil if no UploadedFiles were used" do
559
- data = Rack::Multipart.build_multipart("people" => [{"submit-name" => "Larry", "files" => "contents"}])
560
- data.must_equal nil
561
- end
562
-
563
- it "raise ArgumentError if params is not a Hash" do
564
- lambda {
565
- Rack::Multipart.build_multipart("foo=bar")
566
- }.must_raise(ArgumentError).message.must_equal "value must be a Hash"
567
- end
568
-
569
- it "can parse fields with a content type" do
570
- data = <<-EOF
571
- --1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon\r
572
- Content-Disposition: form-data; name="description"\r
573
- Content-Type: text/plain"\r
574
- \r
575
- Very very blue\r
576
- --1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon--\r
577
- EOF
578
- options = {
579
- "CONTENT_TYPE" => "multipart/form-data; boundary=1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon",
580
- "CONTENT_LENGTH" => data.length.to_s,
581
- :input => StringIO.new(data)
582
- }
583
- env = Rack::MockRequest.env_for("/", options)
584
- params = Rack::Multipart.parse_multipart(env)
585
-
586
- params.must_equal "description"=>"Very very blue"
587
- end
588
-
589
- it "parse multipart upload with no content-length header" do
590
- env = Rack::MockRequest.env_for '/', multipart_fixture(:webkit)
591
- env['CONTENT_TYPE'] = "multipart/form-data; boundary=----WebKitFormBoundaryWLHCs9qmcJJoyjKR"
592
- env.delete 'CONTENT_LENGTH'
593
- params = Rack::Multipart.parse_multipart(env)
594
- params['profile']['bio'].must_include 'hello'
595
- end
596
-
597
- it "parse very long unquoted multipart file names" do
598
- data = <<-EOF
599
- --AaB03x\r
600
- Content-Type: text/plain\r
601
- Content-Disposition: attachment; name=file; filename=#{'long' * 100}\r
602
- \r
603
- contents\r
604
- --AaB03x--\r
605
- EOF
606
-
607
- options = {
608
- "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
609
- "CONTENT_LENGTH" => data.length.to_s,
610
- :input => StringIO.new(data)
611
- }
612
- env = Rack::MockRequest.env_for("/", options)
613
- params = Rack::Multipart.parse_multipart(env)
614
-
615
- params["file"][:filename].must_equal 'long' * 100
616
- end
617
-
618
- it "parse unquoted parameter values at end of line" do
619
- data = <<-EOF
620
- --AaB03x\r
621
- Content-Type: text/plain\r
622
- Content-Disposition: attachment; name=inline\r
623
- \r
624
- true\r
625
- --AaB03x--\r
626
- EOF
627
-
628
- options = {
629
- "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
630
- "CONTENT_LENGTH" => data.length.to_s,
631
- :input => StringIO.new(data)
632
- }
633
- env = Rack::MockRequest.env_for("/", options)
634
- params = Rack::Multipart.parse_multipart(env)
635
- params["inline"].must_equal 'true'
636
- end
637
-
638
- it "parse quoted chars in name parameter" do
639
- data = <<-EOF
640
- --AaB03x\r
641
- Content-Type: text/plain\r
642
- Content-Disposition: attachment; name="quoted\\\\chars\\"in\rname"\r
643
- \r
644
- true\r
645
- --AaB03x--\r
646
- EOF
647
-
648
- options = {
649
- "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
650
- "CONTENT_LENGTH" => data.length.to_s,
651
- :input => StringIO.new(data)
652
- }
653
- env = Rack::MockRequest.env_for("/", options)
654
- params = Rack::Multipart.parse_multipart(env)
655
- params["quoted\\chars\"in\rname"].must_equal 'true'
656
- end
657
-
658
- it "support mixed case metadata" do
659
- file = multipart_file(:text)
660
- data = File.open(file, 'rb') { |io| io.read }
661
-
662
- type = "Multipart/Form-Data; Boundary=AaB03x"
663
- length = data.bytesize
664
-
665
- e = { "CONTENT_TYPE" => type,
666
- "CONTENT_LENGTH" => length.to_s,
667
- :input => StringIO.new(data) }
668
-
669
- env = Rack::MockRequest.env_for("/", e)
670
- params = Rack::Multipart.parse_multipart(env)
671
- params["submit-name"].must_equal "Larry"
672
- params["submit-name-with-content"].must_equal "Berry"
673
- params["files"][:type].must_equal "text/plain"
674
- params["files"][:filename].must_equal "file1.txt"
675
- params["files"][:head].must_equal "Content-Disposition: form-data; " +
676
- "name=\"files\"; filename=\"file1.txt\"\r\n" +
677
- "Content-Type: text/plain\r\n"
678
- params["files"][:name].must_equal "files"
679
- params["files"][:tempfile].read.must_equal "contents"
680
- end
681
-
682
- it "fallback to content-type for name" do
683
- rack_logo = File.read(multipart_file("rack-logo.png"))
684
-
685
- data = <<-EOF
686
- --AaB03x\r
687
- Content-Type: text/plain\r
688
- \r
689
- some text\r
690
- --AaB03x\r
691
- \r
692
- \r
693
- some more text (I didn't specify Content-Type)\r
694
- --AaB03x\r
695
- Content-Type: image/png\r
696
- \r
697
- #{rack_logo}\r
698
- --AaB03x--\r
699
- EOF
700
-
701
- options = {
702
- "CONTENT_TYPE" => "multipart/related; boundary=AaB03x",
703
- "CONTENT_LENGTH" => data.bytesize.to_s,
704
- :input => StringIO.new(data)
705
- }
706
- env = Rack::MockRequest.env_for("/", options)
707
- params = Rack::Multipart.parse_multipart(env)
708
-
709
- params["text/plain"].must_equal ["some text", "some more text (I didn't specify Content-Type)"]
710
- params["image/png"].length.must_equal 1
711
-
712
- f = Tempfile.new("rack-logo")
713
- f.write(params["image/png"][0])
714
- f.length.must_equal 26473
715
- end
716
- end