mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Mime types are separated by a comma, not semicolon, in the Accept header. Also switch all internal configuration of mime types away from strings and over to Mime::Type [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3847 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
3a90c31a56
commit
de37f7df4f
9 changed files with 28 additions and 20 deletions
|
@ -271,7 +271,7 @@ module ActionController #:nodoc:
|
||||||
#
|
#
|
||||||
# Example of doing your own parser for a custom content type:
|
# Example of doing your own parser for a custom content type:
|
||||||
#
|
#
|
||||||
# ActionController::Base.param_parsers['application/atom+xml'] = Proc.new do |data|
|
# ActionController::Base.param_parsers[Mime::Type.lookup('application/atom+xml')] = Proc.new do |data|
|
||||||
# node = REXML::Document.new(post)
|
# node = REXML::Document.new(post)
|
||||||
# { node.root.name => node.root }
|
# { node.root.name => node.root }
|
||||||
# end
|
# end
|
||||||
|
@ -281,10 +281,10 @@ module ActionController #:nodoc:
|
||||||
# in params[:r][:name] for "David" instead of params[:name]. To get the old behavior, you can
|
# in params[:r][:name] for "David" instead of params[:name]. To get the old behavior, you can
|
||||||
# re-register XmlSimple as application/xml handler and enable application/x-yaml like this:
|
# re-register XmlSimple as application/xml handler and enable application/x-yaml like this:
|
||||||
#
|
#
|
||||||
# ActionController::Base.param_parsers['application/xml'] =
|
# ActionController::Base.param_parsers[Mime::XML] =
|
||||||
# Proc.new { |data| XmlSimple.xml_in(data, 'ForceArray' => false) }
|
# Proc.new { |data| XmlSimple.xml_in(data, 'ForceArray' => false) }
|
||||||
# ActionController::Base.param_parsers['application/x-yaml'] = :yaml
|
# ActionController::Base.param_parsers[Mime::YAML] = :yaml
|
||||||
@@param_parsers = { 'application/xml' => :xml_simple }
|
@@param_parsers = { Mime::XML => :xml_simple }
|
||||||
cattr_accessor :param_parsers
|
cattr_accessor :param_parsers
|
||||||
|
|
||||||
# Template root determines the base from which template references will be made. So a call to render("test/template")
|
# Template root determines the base from which template references will be made. So a call to render("test/template")
|
||||||
|
|
|
@ -58,8 +58,8 @@ class CGIMethods #:nodoc:
|
||||||
parsed_params
|
parsed_params
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.parse_formatted_request_parameters(format, raw_post_data)
|
def self.parse_formatted_request_parameters(mime_type, raw_post_data)
|
||||||
params = case strategy = ActionController::Base.param_parsers[format]
|
params = case strategy = ActionController::Base.param_parsers[mime_type]
|
||||||
when Proc
|
when Proc
|
||||||
strategy.call(raw_post_data)
|
strategy.call(raw_post_data)
|
||||||
when :xml_simple
|
when :xml_simple
|
||||||
|
|
|
@ -6,7 +6,7 @@ module ActionController
|
||||||
# For backward compatibility, the post format is extracted from the
|
# For backward compatibility, the post format is extracted from the
|
||||||
# X-Post-Data-Format HTTP header if present.
|
# X-Post-Data-Format HTTP header if present.
|
||||||
def post_format
|
def post_format
|
||||||
case content_type
|
case content_type.to_s
|
||||||
when 'application/xml'
|
when 'application/xml'
|
||||||
:xml
|
:xml
|
||||||
when 'application/x-yaml'
|
when 'application/x-yaml'
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
module Mime
|
module Mime
|
||||||
class Type < String
|
class Type
|
||||||
def self.lookup(string)
|
def self.lookup(string)
|
||||||
LOOKUP[string]
|
LOOKUP[string]
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(string, symbol = nil, synonyms = [])
|
def initialize(string, symbol = nil, synonyms = [])
|
||||||
@symbol, @synonyms = symbol, synonyms
|
@symbol, @synonyms = symbol, synonyms
|
||||||
super(string)
|
@string = string
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
@string
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_sym
|
def to_sym
|
||||||
|
@ -20,6 +24,10 @@ module Mime
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def ==(mime_type)
|
||||||
|
(@synonyms + [ self ]).any? { |synonym| synonym.to_s == mime_type.to_s } if mime_type
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ALL = Type.new "*/*", :all
|
ALL = Type.new "*/*", :all
|
||||||
|
|
|
@ -70,8 +70,8 @@ module ActionController
|
||||||
@accepts = if @env['HTTP_ACCEPT'].to_s.strip.blank?
|
@accepts = if @env['HTTP_ACCEPT'].to_s.strip.blank?
|
||||||
[ content_type, Mime::ALL ]
|
[ content_type, Mime::ALL ]
|
||||||
else
|
else
|
||||||
@env['HTTP_ACCEPT'].split(";").collect! do |mime_type|
|
@env['HTTP_ACCEPT'].split(",").collect! do |mime_type|
|
||||||
Mime::Type.lookup(mime_type.strip)
|
Mime::Type.lookup(mime_type.split(";").first.strip)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -704,7 +704,7 @@ Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
|
||||||
var requestHeaders =
|
var requestHeaders =
|
||||||
['X-Requested-With', 'XMLHttpRequest',
|
['X-Requested-With', 'XMLHttpRequest',
|
||||||
'X-Prototype-Version', Prototype.Version,
|
'X-Prototype-Version', Prototype.Version,
|
||||||
'Accept', 'text/javascript; text/html; text/xml; */*' ];
|
'Accept', 'text/javascript, text/xml, text/html, */*' ];
|
||||||
|
|
||||||
if (this.options.method == 'post') {
|
if (this.options.method == 'post') {
|
||||||
requestHeaders.push('Content-type',
|
requestHeaders.push('Content-type',
|
||||||
|
|
|
@ -111,7 +111,7 @@ class MimeControllerTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_js_or_html
|
def test_js_or_html
|
||||||
@request.env["HTTP_ACCEPT"] = "text/javascript; text/html"
|
@request.env["HTTP_ACCEPT"] = "text/javascript, text/html"
|
||||||
get :js_or_html
|
get :js_or_html
|
||||||
assert_equal 'JS', @response.body
|
assert_equal 'JS', @response.body
|
||||||
|
|
||||||
|
@ -123,7 +123,7 @@ class MimeControllerTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_js_or_anything
|
def test_js_or_anything
|
||||||
@request.env["HTTP_ACCEPT"] = "text/javascript; */*"
|
@request.env["HTTP_ACCEPT"] = "text/javascript, */*"
|
||||||
get :js_or_html
|
get :js_or_html
|
||||||
assert_equal 'JS', @response.body
|
assert_equal 'JS', @response.body
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ class WebServiceTest < Test::Unit::TestCase
|
||||||
def setup
|
def setup
|
||||||
@controller = TestController.new
|
@controller = TestController.new
|
||||||
ActionController::Base.param_parsers.clear
|
ActionController::Base.param_parsers.clear
|
||||||
ActionController::Base.param_parsers['application/xml'] = :xml_node
|
ActionController::Base.param_parsers[Mime::XML] = :xml_node
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_check_parameters
|
def test_check_parameters
|
||||||
|
@ -55,7 +55,7 @@ class WebServiceTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_register_and_use_yaml
|
def test_register_and_use_yaml
|
||||||
ActionController::Base.param_parsers['application/x-yaml'] = Proc.new { |d| YAML.load(d) }
|
ActionController::Base.param_parsers[Mime::YAML] = Proc.new { |d| YAML.load(d) }
|
||||||
process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml)
|
process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml)
|
||||||
assert_equal 'entry', @controller.response.body
|
assert_equal 'entry', @controller.response.body
|
||||||
assert @controller.params.has_key?(:entry)
|
assert @controller.params.has_key?(:entry)
|
||||||
|
@ -63,7 +63,7 @@ class WebServiceTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_register_and_use_yaml_as_symbol
|
def test_register_and_use_yaml_as_symbol
|
||||||
ActionController::Base.param_parsers['application/x-yaml'] = :yaml
|
ActionController::Base.param_parsers[Mime::YAML] = :yaml
|
||||||
process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml)
|
process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml)
|
||||||
assert_equal 'entry', @controller.response.body
|
assert_equal 'entry', @controller.response.body
|
||||||
assert @controller.params.has_key?(:entry)
|
assert @controller.params.has_key?(:entry)
|
||||||
|
@ -71,7 +71,7 @@ class WebServiceTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_register_and_use_xml_simple
|
def test_register_and_use_xml_simple
|
||||||
ActionController::Base.param_parsers['application/xml'] = Proc.new { |data| XmlSimple.xml_in(data, 'ForceArray' => false) }
|
ActionController::Base.param_parsers[Mime::XML] = Proc.new { |data| XmlSimple.xml_in(data, 'ForceArray' => false) }
|
||||||
process('POST', 'application/xml', '<request><summary>content...</summary><title>SimpleXml</title></request>' )
|
process('POST', 'application/xml', '<request><summary>content...</summary><title>SimpleXml</title></request>' )
|
||||||
assert_equal 'summary, title', @controller.response.body
|
assert_equal 'summary, title', @controller.response.body
|
||||||
assert @controller.params.has_key?(:summary)
|
assert @controller.params.has_key?(:summary)
|
||||||
|
@ -82,7 +82,7 @@ class WebServiceTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_deprecated_request_methods
|
def test_deprecated_request_methods
|
||||||
process('POST', 'application/x-yaml')
|
process('POST', 'application/x-yaml')
|
||||||
assert_equal 'application/x-yaml', @controller.request.content_type
|
assert_equal Mime::YAML, @controller.request.content_type
|
||||||
assert_equal true, @controller.request.post?
|
assert_equal true, @controller.request.post?
|
||||||
assert_equal :yaml, @controller.request.post_format
|
assert_equal :yaml, @controller.request.post_format
|
||||||
assert_equal true, @controller.request.yaml_post?
|
assert_equal true, @controller.request.yaml_post?
|
||||||
|
|
2
railties/html/javascripts/prototype.js
vendored
2
railties/html/javascripts/prototype.js
vendored
|
@ -704,7 +704,7 @@ Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
|
||||||
var requestHeaders =
|
var requestHeaders =
|
||||||
['X-Requested-With', 'XMLHttpRequest',
|
['X-Requested-With', 'XMLHttpRequest',
|
||||||
'X-Prototype-Version', Prototype.Version,
|
'X-Prototype-Version', Prototype.Version,
|
||||||
'Accept', 'text/javascript; text/html; text/xml; */*' ];
|
'Accept', 'text/javascript, text/xml, text/html, */*' ];
|
||||||
|
|
||||||
if (this.options.method == 'post') {
|
if (this.options.method == 'post') {
|
||||||
requestHeaders.push('Content-type',
|
requestHeaders.push('Content-type',
|
||||||
|
|
Loading…
Reference in a new issue