Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion lib/lotus/action/mime.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rack/utils'
require 'lotus/utils'
require 'lotus/utils/kernel'

module Lotus
Expand Down Expand Up @@ -415,7 +417,7 @@ def accept
# @api private
def accepts
unless accept == DEFAULT_ACCEPT
::Rack::Utils.best_q_match(accept, ::Rack::Mime::MIME_TYPES.values)
best_q_match(accept, ::Rack::Mime::MIME_TYPES.values)
end
end

Expand Down Expand Up @@ -446,6 +448,34 @@ def content_type_with_charset
"#{content_type}; charset=#{charset}"
end

# Patched version of <tt>Rack::Utils.best_q_match</tt>.
#
# @since x.x.x
# @api private
#
# @see http://www.rubydoc.info/gems/rack/Rack/Utils#best_q_match-class_method
# @see https://github.com/rack/rack/pull/659
# @see https://github.com/lotus/controller/issues/59
# @see https://github.com/lotus/controller/issues/104
def best_q_match(q_value_header, available_mimes)
values = ::Rack::Utils.q_values(q_value_header)

values = values.map do |req_mime, quality|
match = available_mimes.find { |am| ::Rack::Mime.match?(am, req_mime) }
next unless match
[match, quality]
end.compact

# See https://github.com/lotus/controller/issues/59
# See https://github.com/lotus/controller/issues/104
values = values.reverse unless Lotus::Utils.jruby?

value = values.sort_by do |match, quality|
(match.split('/', 2).count('*') * -10) + quality
end.last

value.first if value
end
end
end
end
1 change: 0 additions & 1 deletion lib/lotus/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'lotus/action'
require 'lotus/controller/configuration'
require 'lotus/controller/version'
require 'rack-patch'

module Lotus
# A set of logically grouped actions
Expand Down
28 changes: 0 additions & 28 deletions lib/rack-patch.rb

This file was deleted.

10 changes: 10 additions & 0 deletions test/action/format_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def call(params)
status.must_equal 200
end

# Bug
# See https://github.com/lotus/controller/issues/104
it "accepts 'text/html, application/xhtml+xml, image/jxr, */*' and returns :html" do
status, headers, _ = @action.call({ 'HTTP_ACCEPT' => 'text/html, application/xhtml+xml, image/jxr, */*' })

@action.format.must_equal :html
headers['Content-Type'].must_equal 'text/html; charset=utf-8'
status.must_equal 200
end

mime_types = ['application/octet-stream', 'text/html']
Rack::Mime::MIME_TYPES.each do |format, mime_type|
next if mime_types.include?(mime_type)
Expand Down