1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/soap/mimemessage.rb

241 lines
4.9 KiB
Ruby
Raw Normal View History

# SOAP4R - MIME Message implementation.
# Copyright (C) 2002 Jamie Herre.
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
# redistribute it and/or modify it under the same terms of Ruby's license;
# either the dual license version in 2003, or any later version.
require 'soap/attachment'
module SOAP
# Classes for MIME message handling. Should be put somewhere else!
# Tried using the 'tmail' module but found that I needed something
# lighter in weight.
class MIMEMessage
class MIMEMessageError < StandardError; end
MultipartContentType = 'multipart/\w+'
class Header
attr_accessor :str, :key, :root
def initialize
@attrs = {}
end
def [](key)
@attrs[key]
end
def []=(key, value)
@attrs[key] = value
end
def to_s
@key + ": " + @str
end
end
class Headers < Hash
def self.parse(str)
new.parse(str)
end
def parse(str)
header_cache = nil
str.each do |line|
case line
when /^\A[^\: \t]+:\s*.+$/
parse_line(header_cache) if header_cache
header_cache = line.sub(/\r?\n\z/, '')
when /^\A\s+(.*)$/
# a continuous line at the beginning line crashes here.
header_cache << line
else
raise RuntimeError.new("unexpected header: #{line.inspect}")
end
end
parse_line(header_cache) if header_cache
self
end
def parse_line(line)
if /^\A([^\: \t]+):\s*(.+)\z/ =~ line
header = parse_rhs($2.strip)
header.key = $1.strip
self[header.key.downcase] = header
else
raise RuntimeError.new("unexpected header line: #{line.inspect}")
end
end
def parse_rhs(str)
a = str.split(/;+\s+/)
header = Header.new
header.str = str
header.root = a.shift
a.each do |pair|
if pair =~ /(\w+)\s*=\s*"?([^"]+)"?/
header[$1.downcase] = $2
else
raise RuntimeError.new("unexpected header component: #{pair.inspect}")
end
end
header
end
def add(key, value)
if key != nil and value != nil
header = parse_rhs(value)
header.key = key
self[key.downcase] = header
end
end
def to_s
self.values.collect { |hdr|
hdr.to_s
}.join("\r\n")
end
end
class Part
attr_accessor :headers, :body
def initialize
@headers = Headers.new
@headers.add("Content-Transfer-Encoding", "8bit")
@body = nil
* lib/{soap,wsdl,xsd}, test/{soap,wsdl,xsd}: imported soap4r/1.5.5. #nnn is a ticket number at http://dev.ctor.org/soap4r * SOAP * allow to configure an envelope namespace of SOAP request. (#124) TemporaryNamespace = 'http://www.w3.org/2003/05/soap-envelope' @client.options["soap.envelope.requestnamespace"] = TemporaryNamespace @client.options["soap.envelope.responsenamespace"] = TemporaryNamespace @client.do_proc(...) * let SOAP request XML indent space configuable. see "soap.envelope.no_indent" option. (#130) * let external CES configuable. ex. client["soap.mapping.external_ces"] = 'SJIS'. $KCODE is used by default. (#133) external CES ::= CES used in Ruby object of client and server internal CES ::= CES used in SOAP/OM * add iso-8859-1 external CES support. (#106) * fixed illegal 'qualified' handling of elements. it caused ASP.NET inteoperability problem. (#144) * added 'soap.envelope.use_numeric_character_reference' (boolean) option to let query XML use numeric character reference in XML, not plain UTF-8 character. !GoogleSearch server seems to not allow plain UTF-8 character since 2005-08-15 update. (#147) * SOAP::Header::SimpleHeader (de)serialization throws an exception on !SimpleHeader.on_(in|out)bound when header is a String. so we could not use a simple single element headerItem. fixed. thanks to emil. (#129) * out parameter of rpc operation did not work. (#132) * follow HTTP redirect only if using http-access2. (#125) (#145) * add a workaround for importing an WSDL whose path begins with drive letter. (#115) * WSDL * SOAP Data which is defined as a simpletype was not mapped correctly to Ruby obj when using wsdl2ruby.rb generated classdef file. (#123) * rpc/literal support. (#118) * re-implemented local element qualify/unqualify control. handles elementFormDefault and form in WSDL. (#119) * Array of an element which has simpleType causes a crash. (#128) * prarmeterOrder may not contain return part so it can be shorter than parts size. Thanks to Hugh. (#139) * Samples * added !BasicAuth client sample. (#117) * added Base64 client/server sample. * added Flickr SOAP interface client sample. (#122) * added !SalesForce client sample. (#135) * updated Thawte CA certificate for !GoogleAdWords sample. * updated a client script with the newer version made by Johan. thanks! * shortened long file names. (#120) * fixed typo in authheader sample. (#129) * updated deprecated method usage. (#138) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9169 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-15 10:33:53 -04:00
@contentid = nil
end
def self.parse(str)
new.parse(str)
end
def parse(str)
headers, body = str.split(/\r\n\r\n/s)
if headers != nil and body != nil
@headers = Headers.parse(headers)
@body = body.sub(/\r\n\z/, '')
else
raise RuntimeError.new("unexpected part: #{str.inspect}")
end
self
end
def contentid
if @contentid == nil and @headers.key?('content-id')
@contentid = @headers['content-id'].str
@contentid = $1 if @contentid =~ /^<(.+)>$/
end
@contentid
end
alias content body
def to_s
@headers.to_s + "\r\n\r\n" + @body
end
end
def initialize
@parts = []
@headers = Headers.new
* lib/{soap,wsdl,xsd}, test/{soap,wsdl,xsd}: imported soap4r/1.5.5. #nnn is a ticket number at http://dev.ctor.org/soap4r * SOAP * allow to configure an envelope namespace of SOAP request. (#124) TemporaryNamespace = 'http://www.w3.org/2003/05/soap-envelope' @client.options["soap.envelope.requestnamespace"] = TemporaryNamespace @client.options["soap.envelope.responsenamespace"] = TemporaryNamespace @client.do_proc(...) * let SOAP request XML indent space configuable. see "soap.envelope.no_indent" option. (#130) * let external CES configuable. ex. client["soap.mapping.external_ces"] = 'SJIS'. $KCODE is used by default. (#133) external CES ::= CES used in Ruby object of client and server internal CES ::= CES used in SOAP/OM * add iso-8859-1 external CES support. (#106) * fixed illegal 'qualified' handling of elements. it caused ASP.NET inteoperability problem. (#144) * added 'soap.envelope.use_numeric_character_reference' (boolean) option to let query XML use numeric character reference in XML, not plain UTF-8 character. !GoogleSearch server seems to not allow plain UTF-8 character since 2005-08-15 update. (#147) * SOAP::Header::SimpleHeader (de)serialization throws an exception on !SimpleHeader.on_(in|out)bound when header is a String. so we could not use a simple single element headerItem. fixed. thanks to emil. (#129) * out parameter of rpc operation did not work. (#132) * follow HTTP redirect only if using http-access2. (#125) (#145) * add a workaround for importing an WSDL whose path begins with drive letter. (#115) * WSDL * SOAP Data which is defined as a simpletype was not mapped correctly to Ruby obj when using wsdl2ruby.rb generated classdef file. (#123) * rpc/literal support. (#118) * re-implemented local element qualify/unqualify control. handles elementFormDefault and form in WSDL. (#119) * Array of an element which has simpleType causes a crash. (#128) * prarmeterOrder may not contain return part so it can be shorter than parts size. Thanks to Hugh. (#139) * Samples * added !BasicAuth client sample. (#117) * added Base64 client/server sample. * added Flickr SOAP interface client sample. (#122) * added !SalesForce client sample. (#135) * updated Thawte CA certificate for !GoogleAdWords sample. * updated a client script with the newer version made by Johan. thanks! * shortened long file names. (#120) * fixed typo in authheader sample. (#129) * updated deprecated method usage. (#138) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9169 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-15 10:33:53 -04:00
@root = nil
end
def self.parse(head, str)
new.parse(head, str)
end
attr_reader :parts, :headers
def close
@headers.add(
"Content-Type",
"multipart/related; type=\"text/xml\"; boundary=\"#{boundary}\"; start=\"#{@parts[0].contentid}\""
)
end
def parse(head, str)
@headers = Headers.parse(head + "\r\n" + "From: jfh\r\n")
boundary = @headers['content-type']['boundary']
if boundary != nil
parts = str.split(/--#{Regexp.quote(boundary)}\s*(?:\r\n|--\r\n)/)
part = parts.shift # preamble must be ignored.
@parts = parts.collect { |part| Part.parse(part) }
else
@parts = [Part.parse(str)]
end
if @parts.length < 1
raise MIMEMessageError.new("This message contains no valid parts!")
end
self
end
def root
if @root == nil
start = @headers['content-type']['start']
@root = (start && @parts.find { |prt| prt.contentid == start }) ||
@parts[0]
end
@root
end
def boundary
if @boundary == nil
@boundary = "----=Part_" + __id__.to_s + rand.to_s
end
@boundary
end
def add_part(content)
part = Part.new
part.headers.add("Content-Type",
* lib/{soap,wsdl,xsd}, test/{soap,wsdl,xsd}: imported soap4r/1.5.5. #nnn is a ticket number at http://dev.ctor.org/soap4r * SOAP * allow to configure an envelope namespace of SOAP request. (#124) TemporaryNamespace = 'http://www.w3.org/2003/05/soap-envelope' @client.options["soap.envelope.requestnamespace"] = TemporaryNamespace @client.options["soap.envelope.responsenamespace"] = TemporaryNamespace @client.do_proc(...) * let SOAP request XML indent space configuable. see "soap.envelope.no_indent" option. (#130) * let external CES configuable. ex. client["soap.mapping.external_ces"] = 'SJIS'. $KCODE is used by default. (#133) external CES ::= CES used in Ruby object of client and server internal CES ::= CES used in SOAP/OM * add iso-8859-1 external CES support. (#106) * fixed illegal 'qualified' handling of elements. it caused ASP.NET inteoperability problem. (#144) * added 'soap.envelope.use_numeric_character_reference' (boolean) option to let query XML use numeric character reference in XML, not plain UTF-8 character. !GoogleSearch server seems to not allow plain UTF-8 character since 2005-08-15 update. (#147) * SOAP::Header::SimpleHeader (de)serialization throws an exception on !SimpleHeader.on_(in|out)bound when header is a String. so we could not use a simple single element headerItem. fixed. thanks to emil. (#129) * out parameter of rpc operation did not work. (#132) * follow HTTP redirect only if using http-access2. (#125) (#145) * add a workaround for importing an WSDL whose path begins with drive letter. (#115) * WSDL * SOAP Data which is defined as a simpletype was not mapped correctly to Ruby obj when using wsdl2ruby.rb generated classdef file. (#123) * rpc/literal support. (#118) * re-implemented local element qualify/unqualify control. handles elementFormDefault and form in WSDL. (#119) * Array of an element which has simpleType causes a crash. (#128) * prarmeterOrder may not contain return part so it can be shorter than parts size. Thanks to Hugh. (#139) * Samples * added !BasicAuth client sample. (#117) * added Base64 client/server sample. * added Flickr SOAP interface client sample. (#122) * added !SalesForce client sample. (#135) * updated Thawte CA certificate for !GoogleAdWords sample. * updated a client script with the newer version made by Johan. thanks! * shortened long file names. (#120) * fixed typo in authheader sample. (#129) * updated deprecated method usage. (#138) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9169 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-15 10:33:53 -04:00
"text/xml; charset=" + XSD::Charset.xml_encoding_label)
part.headers.add("Content-ID", Attachment.contentid(part))
part.body = content
@parts.unshift(part)
end
def add_attachment(attach)
part = Part.new
part.headers.add("Content-Type", attach.contenttype)
part.headers.add("Content-ID", attach.mime_contentid)
part.body = attach.content
@parts.unshift(part)
end
def has_parts?
(@parts.length > 0)
end
def headers_str
@headers.to_s
end
def content_str
str = ''
@parts.each do |prt|
str << "--" + boundary + "\r\n"
str << prt.to_s + "\r\n"
end
str << '--' + boundary + "--\r\n"
str
end
def to_s
str = headers_str + "\r\n\r\n" + conent_str
end
end
end