From dfc422b784d3f62daa5a876d65b0ac983c33408a Mon Sep 17 00:00:00 2001 From: Leon Breedt Date: Wed, 13 Jul 2005 06:05:13 +0000 Subject: always send back SOAP responses as UTF-8, we can't guarantee that SOAP4R supports any encoding sent by caller. add documentation describing how to ensure :string types don't get converted into :base64 by SOAP4R when containing non-ASCII chars and $KCODE is not set to a value. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1822 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../action_web_service/protocol/soap_protocol.rb | 31 +++++++++++----------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'actionwebservice/lib/action_web_service') diff --git a/actionwebservice/lib/action_web_service/protocol/soap_protocol.rb b/actionwebservice/lib/action_web_service/protocol/soap_protocol.rb index e6bb5488ab..89f222f908 100644 --- a/actionwebservice/lib/action_web_service/protocol/soap_protocol.rb +++ b/actionwebservice/lib/action_web_service/protocol/soap_protocol.rb @@ -11,7 +11,8 @@ module ActionWebService # :nodoc: end class SoapProtocol < AbstractProtocol # :nodoc: - DefaultEncoding = 'utf-8' + AWSEncoding = 'UTF-8' + XSDEncoding = 'UTF8' attr :marshaler @@ -27,10 +28,9 @@ module ActionWebService # :nodoc: def decode_action_pack_request(action_pack_request) return nil unless soap_action = has_valid_soap_action?(action_pack_request) service_name = action_pack_request.parameters['action'] - charset = parse_charset(action_pack_request.env['HTTP_CONTENT_TYPE']) protocol_options = { :soap_action => soap_action, - :charset => charset + :charset => AWSEncoding } decode_request(action_pack_request.raw_post, service_name, protocol_options) end @@ -42,8 +42,7 @@ module ActionWebService # :nodoc: end def decode_request(raw_request, service_name, protocol_options={}) - charset = protocol_options[:charset] || DefaultEncoding - envelope = SOAP::Processor.unmarshal(raw_request, :charset => charset) + envelope = SOAP::Processor.unmarshal(raw_request, :charset => AWSEncoding) unless envelope raise ProtocolError, "Failed to parse SOAP request message" end @@ -109,8 +108,17 @@ module ActionWebService # :nodoc: end end envelope = create_soap_envelope(response) - charset = protocol_options[:charset] || DefaultEncoding - Response.new(SOAP::Processor.marshal(envelope, :charset => charset), "text/xml; charset=#{charset}", return_value) + + # FIXME: This is not thread-safe, but StringFactory_ in SOAP4R only + # reads target encoding from the XSD::Charset.encoding variable. + # This is required to ensure $KCODE strings are converted + # correctly to UTF-8 for any values of $KCODE. + previous_encoding = XSD::Charset.encoding + XSD::Charset.encoding = XSDEncoding + response_body = SOAP::Processor.marshal(envelope, :charset => AWSEncoding) + XSD::Charset.encoding = previous_encoding + + Response.new(response_body, "text/xml; charset=#{AWSEncoding}", return_value) end def protocol_client(api, protocol_name, endpoint_uri, options={}) @@ -138,15 +146,6 @@ module ActionWebService # :nodoc: soap_action end - def parse_charset(content_type) - return DefaultEncoding if content_type.nil? - if /^text\/xml(?:\s*;\s*charset=([^"]+|"[^"]+"))$/i =~ content_type - $1 - else - DefaultEncoding - end - end - def create_soap_envelope(body) header = SOAP::SOAPHeader.new body = SOAP::SOAPBody.new(body) -- cgit v1.2.3