Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support extra stuff in the Content-Type header via Mime registration #36996

Merged
merged 1 commit into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions actionpack/lib/action_dispatch/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def each(&block)
SET_COOKIE = "Set-Cookie"
LOCATION = "Location"
NO_CONTENT_CODES = [100, 101, 102, 204, 205, 304]
CONTENT_TYPE_PARSER = /\A(?<type>[^;\s]+)?(?:.*;\s*charset=(?<quote>"?)(?<charset>[^;\s]+)\k<quote>)?/ # :nodoc:
CONTENT_TYPE_PARSER = /\A(?<type>[^;\s]+)?\s*(?:;\s*(?<extra>.+))?(?:;\s*charset=(?<quote>"?)(?<charset>[^;\s]+)\k<quote>)/ # :nodoc:

cattr_accessor :default_charset, default: "utf-8"
cattr_accessor :default_headers
Expand Down Expand Up @@ -419,14 +419,14 @@ def cookies
end

private
ContentTypeHeader = Struct.new :mime_type, :charset
NullContentTypeHeader = ContentTypeHeader.new nil, nil
ContentTypeHeader = Struct.new :mime_type, :extra, :charset
NullContentTypeHeader = ContentTypeHeader.new nil, nil, nil
carlosantoniodasilva marked this conversation as resolved.
Show resolved Hide resolved

def parse_content_type(content_type)
if content_type && match = CONTENT_TYPE_PARSER.match(content_type)
ContentTypeHeader.new(match[:type], match[:charset])
ContentTypeHeader.new(match[:type], match[:extra], match[:charset])
else
NullContentTypeHeader
ContentTypeHeader.new(content_type, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change required for GitHub?
Looks like this change is not tested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. I think we can remove the change (you're right, it seems not tested)

end
end

Expand Down
15 changes: 15 additions & 0 deletions actionpack/test/controller/mime/respond_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class RespondToController < ActionController::Base
end
}

def my_html_fragment
respond_to do |type|
type.html_fragment { render body: "neat" }
end
end

def html_xml_or_rss
respond_to do |type|
type.html { render body: "HTML" }
Expand Down Expand Up @@ -321,13 +327,22 @@ def setup
Mime::Type.register_alias("text/html", :iphone)
Mime::Type.register("text/x-mobile", :mobile)
Mime::Type.register("application/fancy-xml", :fancy_xml)
Mime::Type.register("text/html; fragment", :html_fragment)
end

def teardown
super
Mime::Type.unregister(:iphone)
Mime::Type.unregister(:mobile)
Mime::Type.unregister(:fancy_xml)
Mime::Type.unregister(:html_fragment)
end

def test_html_fragment
@request.accept = "text/html; fragment"
get :my_html_fragment
assert_equal "text/html; fragment; charset=utf-8", @response.headers["Content-Type"]
assert_equal "neat", @response.body
end

def test_html
Expand Down