Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
Added parsing of arbitrary media type parameters.
Browse files Browse the repository at this point in the history
Based on rails#4918.

Related to rails#4127.

Conflicts:
	actionpack/test/dispatch/mime_type_test.rb
  • Loading branch information
steveklabnik authored and tamird committed Jun 22, 2014
1 parent 4ab14c0 commit 965358e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions actionpack/lib/action_dispatch/http/mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def ==(item)
class << self

TRAILING_STAR_REGEXP = /(text|application)\/\*/
Q_SEPARATOR_REGEXP = /;\s*q=/
PARAMETER_SEPARATOR_REGEXP = /;\s*\w+="?\w+"?/

def lookup(string)
LOOKUP[string]
Expand All @@ -109,7 +109,7 @@ def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], s

def parse(accept_header)
if accept_header !~ /,/
accept_header = accept_header.split(Q_SEPARATOR_REGEXP).first
accept_header = accept_header.split(PARAMETER_SEPARATOR_REGEXP).first
if accept_header =~ TRAILING_STAR_REGEXP
parse_data_with_trailing_star($1)
else
Expand All @@ -119,7 +119,7 @@ def parse(accept_header)
# keep track of creation order to keep the subsequent sort stable
list, index = [], 0
accept_header.split(/,/).each do |header|
params, q = header.split(Q_SEPARATOR_REGEXP)
params, q = header.split(PARAMETER_SEPARATOR_REGEXP)
if params.present?
params.strip!

Expand Down
6 changes: 6 additions & 0 deletions actionpack/test/dispatch/mime_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class MimeTypeTest < ActiveSupport::TestCase
assert_equal expect, Mime::Type.parse(accept)
end

test "parse arbitarry media type parameters" do
accept = 'multipart/form-data; boundary="simple boundary"'
expect = [Mime::MULTIPART_FORM]
assert_equal expect, Mime::Type.parse(accept)
end

# Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP)
test "parse broken acceptlines" do
accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5"
Expand Down

0 comments on commit 965358e

Please sign in to comment.