Skip to content

Commit

Permalink
Raise Rack::Multipart::EmptyContentError instead of EOFError for parsing
Browse files Browse the repository at this point in the history
This makes it simpler to catch this specific error.

Fixes rack#1603
  • Loading branch information
jeremyevans committed Jul 14, 2020
1 parent a12c4a8 commit efbc243
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. For info on
- Removed antiquated handlers: FCGI, LSWS, SCGI, Thin. ([#1658](https://github.com/rack/rack/pull/1658), [@ioquatix](https://github.com/ioquatix))
- Removed options from `Rack::Builder.parse_file` and `Rack::Builder.load_file`. ([#1663](https://github.com/rack/rack/pull/1663), [@ioquatix](https://github.com/ioquatix))
- HMAC argument for `Rack::Session::Cookie` doesn't accept a class constant anymore, but only a string recognized by OpenSSL (e.g. `"SHA256"`) or compatible instance (e.g. `OpenSSL::Digest.new("SHA256")`) ([#1676](https://github.com/rack/rack/pull/1676), [@bdewater](https://github.com/bdewater))
- Attempting to parse a multipart response with an empty body now raises Rack::Multipart::EmptyContentError. ([#1603](https://github.com/rack/rack/issues/1603), [@jeremyevans](https://github.com/jeremyevans))

### Fixed

Expand Down
6 changes: 5 additions & 1 deletion lib/rack/multipart/parser.rb
Expand Up @@ -6,6 +6,10 @@ module Rack
module Multipart
class MultipartPartLimitError < Errno::EMFILE; end

# Use specific error class when parsing multipart request
# that ends early.
class EmptyContentError < ::EOFError; end

class Parser
(require_relative '../core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4'

Expand Down Expand Up @@ -356,7 +360,7 @@ def tag_multipart_encoding(filename, content_type, name, body)

def handle_empty_content!(content)
if content.nil? || content.empty?
raise EOFError
raise EmptyContentError
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/spec_multipart.rb
Expand Up @@ -151,7 +151,7 @@ def rd.rewind; end
env = Rack::MockRequest.env_for '/', fixture
lambda {
Rack::Multipart.parse_multipart(env)
}.must_raise EOFError
}.must_raise Rack::Multipart::EmptyContentError
rd.close

err = thr.value
Expand Down

0 comments on commit efbc243

Please sign in to comment.