Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
From 42356914489814e2c8a89a7cfc94a8cc822211de Mon Sep 17 00:00:00 2001
From: Brian Lopez <brianmario@Brians-MacBookPro-HomeRun.local>
Date: Tue, 19 Jan 2010 23:48:49 -0800
Subject: [PATCH] re-enable streaming params parsing and update the backend parsers to better conform to the rack spec
---
.../action_dispatch/middleware/params_parser.rb | 6 ++--
activesupport/lib/active_support/xml_mini/jdom.rb | 14 ++++-------
.../lib/active_support/xml_mini/libxml.rb | 8 +-----
.../lib/active_support/xml_mini/libxmlsax.rb | 21 +++++-----------
.../lib/active_support/xml_mini/nokogiri.rb | 12 ++-------
.../lib/active_support/xml_mini/nokogirisax.rb | 14 +++--------
activesupport/lib/active_support/xml_mini/rexml.rb | 12 ++-------
activesupport/test/abstract_unit.rb | 25 ++++++++++++++++++++
activesupport/test/json/decoding_test.rb | 12 +++++++++
9 files changed, 63 insertions(+), 61 deletions(-)
diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb
index 522982e..534390d 100644
--- a/actionpack/lib/action_dispatch/middleware/params_parser.rb
+++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb
@@ -35,14 +35,14 @@ module ActionDispatch
when Proc
strategy.call(request.raw_post)
when :xml_simple, :xml_node
- request.body.size == 0 ? {} : Hash.from_xml(request.raw_post).with_indifferent_access
+ request.body.size == 0 ? {} : Hash.from_xml(request.body).with_indifferent_access
when :yaml
- YAML.load(request.raw_post)
+ YAML.load(request.body)
when :json
if request.body.size == 0
{}
else
- data = ActiveSupport::JSON.decode(request.raw_post)
+ data = ActiveSupport::JSON.decode(request.body)
data = {:_json => data} unless data.is_a?(Hash)
data.with_indifferent_access
end
diff --git a/activesupport/lib/active_support/xml_mini/jdom.rb b/activesupport/lib/active_support/xml_mini/jdom.rb
index 48c1cb3..fe6fb31 100644
--- a/activesupport/lib/active_support/xml_mini/jdom.rb
+++ b/activesupport/lib/active_support/xml_mini/jdom.rb
@@ -34,15 +34,11 @@ module ActiveSupport
data = data.read
end
- if data.blank?
- {}
- else
- @dbf = DocumentBuilderFactory.new_instance
- xml_string_reader = StringReader.new(data)
- xml_input_source = InputSource.new(xml_string_reader)
- doc = @dbf.new_document_builder.parse(xml_input_source)
- merge_element!({}, doc.document_element)
- end
+ @dbf = DocumentBuilderFactory.new_instance
+ xml_string_reader = StringReader.new(data)
+ xml_input_source = InputSource.new(xml_string_reader)
+ doc = @dbf.new_document_builder.parse(xml_input_source)
+ merge_element!({}, doc.document_element)
end
private
diff --git a/activesupport/lib/active_support/xml_mini/libxml.rb b/activesupport/lib/active_support/xml_mini/libxml.rb
index 9cf1873..afb421d 100644
--- a/activesupport/lib/active_support/xml_mini/libxml.rb
+++ b/activesupport/lib/active_support/xml_mini/libxml.rb
@@ -15,13 +15,7 @@ module ActiveSupport
data = StringIO.new(data || '')
end
- char = data.getc
- if char.nil?
- {}
- else
- data.ungetc(char)
- LibXML::XML::Parser.io(data).parse.to_hash
- end
+ LibXML::XML::Parser.io(data).parse.to_hash
end
end
diff --git a/activesupport/lib/active_support/xml_mini/libxmlsax.rb b/activesupport/lib/active_support/xml_mini/libxmlsax.rb
index d7b2f4c..7afcac6 100644
--- a/activesupport/lib/active_support/xml_mini/libxmlsax.rb
+++ b/activesupport/lib/active_support/xml_mini/libxmlsax.rb
@@ -65,20 +65,13 @@ module ActiveSupport
data = StringIO.new(data || '')
end
- char = data.getc
- if char.nil?
- {}
- else
- data.ungetc(char)
-
- LibXML::XML::Error.set_handler(&LibXML::XML::Error::QUIET_HANDLER)
- parser = LibXML::XML::SaxParser.io(data)
- document = self.document_class.new
-
- parser.callbacks = document
- parser.parse
- document.hash
- end
+ LibXML::XML::Error.set_handler(&LibXML::XML::Error::QUIET_HANDLER)
+ parser = LibXML::XML::SaxParser.io(data)
+ document = self.document_class.new
+
+ parser.callbacks = document
+ parser.parse
+ document.hash
end
end
end
\ No newline at end of file
diff --git a/activesupport/lib/active_support/xml_mini/nokogiri.rb b/activesupport/lib/active_support/xml_mini/nokogiri.rb
index eb61a7f..b445f50 100644
--- a/activesupport/lib/active_support/xml_mini/nokogiri.rb
+++ b/activesupport/lib/active_support/xml_mini/nokogiri.rb
@@ -14,15 +14,9 @@ module ActiveSupport
data = StringIO.new(data || '')
end
- char = data.getc
- if char.nil?
- {}
- else
- data.ungetc(char)
- doc = Nokogiri::XML(data)
- raise doc.errors.first if doc.errors.length > 0
- doc.to_hash
- end
+ doc = Nokogiri::XML(data)
+ raise doc.errors.first if doc.errors.length > 0
+ doc.to_hash
end
module Conversions #:nodoc:
diff --git a/activesupport/lib/active_support/xml_mini/nokogirisax.rb b/activesupport/lib/active_support/xml_mini/nokogirisax.rb
index d538a91..8c4a8c2 100644
--- a/activesupport/lib/active_support/xml_mini/nokogirisax.rb
+++ b/activesupport/lib/active_support/xml_mini/nokogirisax.rb
@@ -67,16 +67,10 @@ module ActiveSupport
data = StringIO.new(data || '')
end
- char = data.getc
- if char.nil?
- {}
- else
- data.ungetc(char)
- document = self.document_class.new
- parser = Nokogiri::XML::SAX::Parser.new(document)
- parser.parse(data)
- document.hash
- end
+ document = self.document_class.new
+ parser = Nokogiri::XML::SAX::Parser.new(document)
+ parser.parse(data)
+ document.hash
end
end
end
\ No newline at end of file
diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb
index 3db48ce..dcaa422 100644
--- a/activesupport/lib/active_support/xml_mini/rexml.rb
+++ b/activesupport/lib/active_support/xml_mini/rexml.rb
@@ -20,15 +20,9 @@ module ActiveSupport
data = StringIO.new(data || '')
end
- char = data.getc
- if char.nil?
- {}
- else
- data.ungetc(char)
- silence_warnings { require 'rexml/document' } unless defined?(REXML::Document)
- doc = REXML::Document.new(data)
- merge_element!({}, doc.root)
- end
+ silence_warnings { require 'rexml/document' } unless defined?(REXML::Document)
+ doc = REXML::Document.new(data)
+ merge_element!({}, doc.root)
end
private
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb
index d91e041..124d5dc 100644
--- a/activesupport/test/abstract_unit.rb
+++ b/activesupport/test/abstract_unit.rb
@@ -46,3 +46,28 @@ ActiveSupport::Deprecation.debug = true
if RUBY_VERSION < '1.9'
$KCODE = 'UTF8'
end
+
+# Wrap another IO with a Rack-compatible mock for testing compliance.
+class TestRackIO
+ def initialize(str)
+ @json = StringIO.new(str)
+ end
+
+ def gets
+ @json.gets
+ end
+
+ def read(num=nil)
+ @json.read(num)
+ end
+
+ def each(&block)
+ @json.each { |s|
+ yield s
+ }
+ end
+
+ def close
+ @json.close
+ end
+end
diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb
index 8fcb16a..d0cd7dd 100644
--- a/activesupport/test/json/decoding_test.rb
+++ b/activesupport/test/json/decoding_test.rb
@@ -63,6 +63,18 @@ class TestJSONDecoding < ActiveSupport::TestCase
end
end
end
+
+ test "json decodes from a rack-compatible IO with the #{backend} backend" do
+ json = TestRackIO.new('{"a":1}')
+ expected = {"a" => 1}
+ ActiveSupport::JSON.with_backend backend do
+ begin
+ assert_equal expected, ActiveSupport::JSON.decode(json)
+ rescue ArgumentError,
+ assert(false, "backend #{backend} attempted to call a Rack-compatible IO method with invalid parameters")
+ end
+ end
+ end
end
if backends.include?("JSONGem")
--
1.6.6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment