savon 2.18.1 → 3.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/savon/builder.rb CHANGED
@@ -1,64 +1,40 @@
1
1
  # frozen_string_literal: true
2
-
3
- require "savon/addressing"
4
2
  require "savon/header"
5
3
  require "savon/message"
6
- require "savon/effective_options"
7
4
  require "nokogiri"
8
5
  require "builder"
9
6
  require "gyoku"
10
7
 
11
8
  module Savon
12
- # Builds the SOAP request for an operation: the XML envelope, or
13
- # a multipart/related message when the request carries attachments
14
- # (SOAP with Attachments, https://www.w3.org/TR/SOAP-attachments).
15
- #
16
- # It owns the envelope structure, its namespace declarations, and the body,
17
- # and signs the document when a WSSE signature is present. Message-body
18
- # serialization is delegated to {Savon::Message}, the Header element to
19
- # {Savon::Header}, and Hash-to-XML conversion to Gyoku.
20
9
  class Builder
21
10
  attr_reader :multipart
22
11
 
23
12
  SCHEMA_TYPES = {
24
13
  "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema",
25
14
  "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance"
26
- }.freeze
15
+ }
27
16
 
28
17
  SOAP_NAMESPACE = {
29
18
  1 => "http://schemas.xmlsoap.org/soap/envelope/",
30
19
  2 => "http://www.w3.org/2003/05/soap-envelope"
31
- }.freeze
32
-
33
- SOAP_XOP_REQUEST_TYPE = {
34
- 1 => "text/xml",
35
- 2 => "application/soap+xml"
36
- }.freeze
37
-
38
- # @deprecated Use {Savon::Addressing::NAMESPACE} instead. This alias will
39
- # be removed in Savon 3.0.
40
- WSA_NAMESPACE = Addressing::NAMESPACE
41
-
42
- # @param operation_name [Symbol] the SOAP operation being called
43
- # @param wsdl [Wasabi::Document] the parsed WSDL, or an empty document when
44
- # the client was configured without one
45
- # @param globals [Savon::GlobalOptions] client-level options
46
- # @param locals [Savon::LocalOptions] per-request options
20
+ }
21
+
22
+ WSA_NAMESPACE = "http://www.w3.org/2005/08/addressing"
23
+
47
24
  def initialize(operation_name, wsdl, globals, locals)
48
25
  @operation_name = operation_name
49
26
 
50
27
  @wsdl = wsdl
51
28
  @globals = globals
52
29
  @locals = locals
53
- @effective = EffectiveOptions.new(operation_name, wsdl, globals, locals)
54
- @signature = @effective.wsse_signature
30
+ @signature = @locals[:wsse_signature] || @globals[:wsse_signature]
55
31
 
56
32
  @types = convert_type_definitions_to_hash
57
33
  @used_namespaces = convert_type_namespaces_to_hash
58
34
  end
59
35
 
60
36
  def pretty
61
- Nokogiri.XML(to_s).to_xml(indent: 2)
37
+ Nokogiri.XML(to_s).to_xml(:indent => 2)
62
38
  end
63
39
 
64
40
  def build_document
@@ -68,7 +44,7 @@ module Savon
68
44
  if @signature
69
45
  @signature.document = xml_result
70
46
 
71
- 3.times do
47
+ 2.times do
72
48
  @header = nil
73
49
  @signature.document = build_xml
74
50
  end
@@ -85,68 +61,48 @@ module Savon
85
61
  end
86
62
  end
87
63
 
88
- # Returns the XML attributes of the envelope's Header element, provided
89
- # by the header's sections.
90
- #
91
- # @return [Hash{String => String}]
92
64
  def header_attributes
93
- header.attributes
65
+ @globals[:use_wsa_headers] ? { 'xmlns:wsa' => WSA_NAMESPACE } : {}
94
66
  end
95
67
 
96
68
  def body_attributes
97
69
  @body_attributes ||= @signature.nil? ? {} : @signature.body_attributes
98
70
  end
99
71
 
100
- # Returns the request body as a String. When the caller supplies a pre-built
101
- # envelope via the :xml local option it is used verbatim, but it must still
102
- # be wrapped in a multipart message when :attachments are present.
103
- # Otherwise the attachments are silently dropped.
104
72
  def to_s
105
- if @locals.include?(:xml)
106
- xml = @locals[:xml]
107
- @locals[:attachments] ? build_multipart_message(xml) : xml
108
- else
109
- build_document
110
- end
73
+ return @locals[:xml] if @locals.include? :xml
74
+ build_document
111
75
  end
112
76
 
113
77
  private
114
78
 
115
79
  def convert_type_definitions_to_hash
116
- @wsdl.type_definitions.each_with_object({}) do |(path, type), memo|
80
+ @wsdl.type_definitions.inject({}) do |memo, (path, type)|
117
81
  memo[path] = type
82
+ memo
118
83
  end
119
84
  end
120
85
 
121
86
  def convert_type_namespaces_to_hash
122
- @wsdl.type_namespaces.each_with_object({}) do |(path, uri), memo|
87
+ @wsdl.type_namespaces.inject({}) do |memo, (path, uri)|
123
88
  key, value = use_namespace(path, uri)
124
89
  memo[key] = value
90
+ memo
125
91
  end
126
92
  end
127
93
 
128
94
  def use_namespace(path, uri)
129
95
  @internal_namespace_count ||= 0
130
96
 
131
- unless (identifier = namespace_by_uri(uri))
132
- wsdl_identifier = wsdl_namespace_identifier(uri)
133
- # The prefix may already be taken by the target namespace or a user-supplied
134
- # :namespaces override - fall back to ins0, ins1... rather than overwriting it.
135
- wsdl_identifier = nil if wsdl_identifier && namespaces.key?("xmlns:#{wsdl_identifier}")
136
- identifier = wsdl_identifier || "ins#{@internal_namespace_count}"
97
+ unless identifier = namespace_by_uri(uri)
98
+ identifier = "ins#{@internal_namespace_count}"
137
99
  namespaces["xmlns:#{identifier}"] = uri
138
- @internal_namespace_count += 1 unless wsdl_identifier
100
+ @internal_namespace_count += 1
139
101
  end
140
102
 
141
103
  [path, identifier]
142
104
  end
143
105
 
144
- def wsdl_namespace_identifier(uri)
145
- return unless @wsdl.document?
146
-
147
- @wsdl.parser.namespaces.find { |key, value| value == uri && !key.downcase.start_with?("xml") }&.first
148
- end
149
-
150
106
  def namespaces_with_globals
151
107
  namespaces.merge @globals[:namespaces]
152
108
  end
@@ -160,9 +116,21 @@ module Savon
160
116
  @globals[:namespace] || @wsdl.namespace
161
117
 
162
118
  # check env_namespace
163
- namespaces["xmlns#{env_namespace && env_namespace != '' ? ":#{env_namespace}" : ''}"] =
119
+ namespaces["xmlns#{env_namespace && env_namespace != "" ? ":#{env_namespace}" : ''}"] =
164
120
  SOAP_NAMESPACE[@globals[:soap_version]]
165
121
 
122
+ if @wsdl&.document
123
+ @wsdl.parser.namespaces.each do |identifier, path|
124
+ next if identifier == 'xmlns' # Do not include xmlns namespace as this causes issues for some servers (https://github.com/savonrb/savon/issues/986)
125
+
126
+ prefixed_identifier = "xmlns:#{identifier}"
127
+
128
+ next if namespaces.key?(prefixed_identifier)
129
+
130
+ namespaces[prefixed_identifier] = path
131
+ end
132
+ end
133
+
166
134
  namespaces
167
135
  end
168
136
  end
@@ -172,14 +140,13 @@ module Savon
172
140
  end
173
141
 
174
142
  def header
175
- @header ||= Header.new(@globals, @effective)
143
+ @header ||= Header.new(@globals, @locals)
176
144
  end
177
145
 
178
146
  def namespaced_message_tag
179
147
  tag_name = message_tag
180
- return [tag_name] if @wsdl.document? && @wsdl.soap_input(@operation_name.to_sym).is_a?(Hash)
181
-
182
- if namespace_identifier.nil?
148
+ return [tag_name] if @wsdl.document? and @wsdl.soap_input(@operation_name.to_sym).is_a?(Hash)
149
+ if namespace_identifier == nil
183
150
  [tag_name, message_attributes]
184
151
  elsif @used_namespaces[[tag_name.to_s]]
185
152
  [@used_namespaces[[tag_name.to_s]], tag_name, message_attributes]
@@ -197,9 +164,8 @@ module Savon
197
164
  message_tag = serialized_message_tag[1]
198
165
  @wsdl.soap_input(@operation_name.to_sym)[message_tag].each_pair do |message, type|
199
166
  break if @locals[:message].nil?
200
-
201
167
  message_locals = @locals[:message][StringUtils.snakecase(message).to_sym]
202
- message_content = Message.new(type.last, namespace_identifier, @types, @used_namespaces, message_locals, :unqualified, @globals[:convert_request_keys_to], @globals[:unwrap]).to_s
168
+ message_content = Message.new(message_tag, namespace_identifier, @types, @used_namespaces, message_locals, :unqualified, @globals[:convert_request_keys_to], @globals[:unwrap]).to_s
203
169
  messages += "<#{message} xsi:type=\"#{type.join(':')}\">#{message_content}</#{message}>"
204
170
  end
205
171
  messages
@@ -211,7 +177,7 @@ module Savon
211
177
  message_tag = wsdl_tag_name.keys.first if wsdl_tag_name.is_a?(Hash)
212
178
  message_tag ||= @locals[:message_tag]
213
179
  message_tag ||= wsdl_tag_name
214
- message_tag ||= Gyoku.xml_tag(@operation_name, key_converter: @globals[:convert_request_keys_to])
180
+ message_tag ||= Gyoku.xml_tag(@operation_name, :key_converter => @globals[:convert_request_keys_to])
215
181
 
216
182
  message_tag.to_sym
217
183
  end
@@ -221,7 +187,7 @@ module Savon
221
187
  end
222
188
 
223
189
  def body_message
224
- if @wsdl.document? && @wsdl.soap_input(@operation_name.to_sym).is_a?(Hash)
190
+ if @wsdl.document? and @wsdl.soap_input(@operation_name.to_sym).is_a?(Hash)
225
191
  serialized_messages
226
192
  else
227
193
  message.to_s
@@ -247,12 +213,15 @@ module Savon
247
213
  end
248
214
 
249
215
  def namespace_by_uri(uri)
250
- namespaces.key(uri)&.delete_prefix("xmlns:")
216
+ namespaces.each do |candidate_identifier, candidate_uri|
217
+ return candidate_identifier.gsub(/^xmlns:/, '') if candidate_uri == uri
218
+ end
219
+ nil
251
220
  end
252
221
 
253
222
  def builder
254
223
  builder = ::Builder::XmlMarkup.new
255
- builder.instruct!(:xml, encoding: @globals[:encoding])
224
+ builder.instruct!(:xml, :encoding => @globals[:encoding])
256
225
  builder
257
226
  end
258
227
 
@@ -285,41 +254,25 @@ module Savon
285
254
 
286
255
  # the mail.body.encoded algorithm reorders the parts, default order is [ "text/plain", "text/enriched", "text/html" ]
287
256
  # should redefine the sort order, because the soap request xml should be the first
288
- multipart_message.body.set_sort_order(
289
- @locals[:mtom] ? ["application/xop+xml", "text/xml"] : ["text/xml"]
290
- )
257
+ multipart_message.body.set_sort_order [ "text/xml" ]
291
258
 
292
259
  multipart_message.body.encoded(multipart_message.content_transfer_encoding)
293
260
  end
294
261
 
295
262
  def init_multipart_message(message_xml)
296
263
  multipart_message = Mail.new
297
-
298
- # MTOM differs from general SOAP attachments:
299
- # 1. binary encoding
300
- # 2. application/xop+xml mime type
301
- if @locals[:mtom]
302
- type = "application/xop+xml; charset=#{@globals[:encoding]}; " \
303
- "type=\"#{SOAP_XOP_REQUEST_TYPE[@globals[:soap_version]]}\""
304
-
305
- multipart_message.transport_encoding = 'binary'
306
- message_xml = message_xml.dup.force_encoding('BINARY')
307
- else
308
- type = 'text/xml'
309
- end
310
-
311
264
  xml_part = Mail::Part.new do
312
- content_type type
265
+ content_type 'text/xml'
313
266
  body message_xml
314
267
  # in Content-Type the start parameter is recommended (RFC 2387)
315
268
  content_id '<soap-request-body@soap>'
316
269
  end
317
270
  multipart_message.add_part xml_part
318
271
 
319
- # request.headers["Content-Type"] = "multipart/related; boundary=\"#{multipart_message.body.boundary}\"; type=\"text/xml\"; start=\"#{xml_part.content_id}\""
272
+ #request.headers["Content-Type"] = "multipart/related; boundary=\"#{multipart_message.body.boundary}\"; type=\"text/xml\"; start=\"#{xml_part.content_id}\""
320
273
  @multipart = {
321
274
  multipart_boundary: multipart_message.body.boundary,
322
- start: xml_part.content_id
275
+ start: xml_part.content_id,
323
276
  }
324
277
 
325
278
  multipart_message
@@ -329,43 +282,19 @@ module Savon
329
282
  if @locals[:attachments].is_a? Hash
330
283
  # hash example: { 'att1' => '/path/to/att1', 'att2' => '/path/to/att2' }
331
284
  @locals[:attachments].each do |identifier, attachment|
332
- explicit = attachment.is_a?(Hash) && attachment.key?(:content_id)
333
- identifier = attachment[:content_id] if explicit
334
- validate_content_id!(identifier, explicit: explicit)
335
285
  add_attachment_to_multipart_message(multipart_message, attachment, identifier)
336
286
  end
337
287
  elsif @locals[:attachments].is_a? Array
338
288
  # array example: [ '/path/to/att1', '/path/to/att2' ]
339
289
  # array example: [ { filename: 'att1.xml', content: '<x/>' }, { filename: 'att2.xml', content: '<y/>' } ]
340
290
  @locals[:attachments].each do |attachment|
341
- explicit = attachment.is_a?(Hash) && attachment.key?(:content_id)
342
- identifier = attachment.is_a?(String) ? File.basename(attachment) : (attachment[:content_id] || attachment[:filename])
343
- validate_content_id!(identifier, explicit: explicit)
344
- add_attachment_to_multipart_message(multipart_message, attachment, identifier)
291
+ add_attachment_to_multipart_message(multipart_message, attachment, attachment.is_a?(String) ? File.basename(attachment) : attachment[:filename])
345
292
  end
346
293
  end
347
294
  end
348
295
 
349
- # Content-IDs are matched against xop:Include hrefs, and the mail library cuts the header at the first
350
- # space, so an ID with non-ASCII characters, spaces or header punctuation silently stops matching.
351
- # Fail fast when the caller chose the ID or the request is MTOM; for plain attachments, whose default
352
- # filename IDs predate this check, warn instead so current users do not break.
353
- def validate_content_id!(identifier, explicit:)
354
- id = identifier.to_s
355
- return if !id.empty? && id.ascii_only? && id !~ /[\s()<>,;:"\[\]]/
356
-
357
- message = "Invalid Content-ID #{id.inspect}: use ASCII without spaces or ()<>,;:\"[]. " \
358
- "Set content_id: on the attachment to choose a valid ID."
359
- raise ArgumentError, message if explicit || @locals[:mtom]
360
-
361
- warn "#{message} The filename is used as-is.", uplevel: 1
362
- end
363
-
364
296
  def add_attachment_to_multipart_message(multipart_message, attachment, identifier)
365
- # :content_id only selects the Content-ID header; the mail part itself
366
- # takes its filename from :filename, so it must not reach add_file.
367
- file = attachment.is_a?(Hash) ? attachment.reject { |key, _| key == :content_id } : attachment
368
- multipart_message.add_file file.clone
297
+ multipart_message.add_file attachment.clone
369
298
  multipart_message.parts.last.content_id = multipart_message.parts.last.content_location = identifier.to_s
370
299
  end
371
300
  end
data/lib/savon/client.rb CHANGED
@@ -1,26 +1,19 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  require "savon/operation"
4
- require "savon/transport/httpi"
5
- require "savon/transport/faraday"
3
+ require "savon/request"
6
4
  require "savon/options"
7
5
  require "savon/block_interface"
8
6
  require "wasabi"
9
7
 
10
8
  module Savon
11
- # The main entry point for Savon.
12
- #
13
- # Holds global configuration, owns the WSDL document, and dispatches
14
- # named operations. A single Client instance is typically shared across
15
- # multiple calls to the same service.
16
9
  class Client
10
+
17
11
  def initialize(globals = {}, &block)
18
- unless globals.is_a? Hash
12
+ unless globals.kind_of? Hash
19
13
  raise_version1_initialize_error! globals
20
14
  end
21
15
 
22
16
  set_globals(globals, block)
23
- @globals.validate_transport!
24
17
 
25
18
  unless wsdl_or_endpoint_and_namespace_specified?
26
19
  raise_initialization_error!
@@ -31,25 +24,13 @@ module Savon
31
24
 
32
25
  attr_reader :globals, :wsdl
33
26
 
34
- # Returns the memoized Faraday::Connection for this client.
35
- # Callers use this to configure middleware, SSL, auth, timeouts, and any
36
- # other transport-level concern before making calls.
37
- # Raises ArgumentError if transport is not :faraday.
38
- def faraday
39
- unless @globals[:transport] == :faraday
40
- raise ArgumentError, "client.faraday is only available when transport: :faraday is set"
41
- end
42
-
43
- @faraday ||= ::Faraday.new
44
- end
45
-
46
27
  def operations
47
28
  raise_missing_wsdl_error! unless @wsdl.document?
48
29
  @wsdl.soap_actions
49
30
  end
50
31
 
51
32
  def operation(operation_name)
52
- Operation.create(operation_name, @wsdl, @globals, build_transport)
33
+ Operation.create(operation_name, @wsdl, @globals)
53
34
  end
54
35
 
55
36
  def call(operation_name, locals = {}, &block)
@@ -67,15 +48,6 @@ module Savon
67
48
 
68
49
  private
69
50
 
70
- # Builds the transport for a single operation.
71
- def build_transport
72
- if @globals[:transport] == :faraday
73
- Transport::Faraday.new(faraday, @globals)
74
- else
75
- Transport::HTTPI.new(@globals)
76
- end
77
- end
78
-
79
51
  def set_globals(globals, block)
80
52
  globals = GlobalOptions.new(globals)
81
53
  BlockInterface.new(globals).evaluate(block) if block
@@ -86,16 +58,12 @@ module Savon
86
58
  def build_wsdl_document
87
59
  @wsdl = Wasabi::Document.new
88
60
 
89
- @wsdl.document = @globals[:wsdl] if @globals.include? :wsdl
90
- @wsdl.endpoint = @globals[:endpoint] if @globals.include? :endpoint
91
- @wsdl.namespace = @globals[:namespace] if @globals.include? :namespace
61
+ @wsdl.document = @globals[:wsdl] if @globals.include? :wsdl
62
+ @wsdl.endpoint = @globals[:endpoint] if @globals.include? :endpoint
63
+ @wsdl.namespace = @globals[:namespace] if @globals.include? :namespace
64
+ @wsdl.adapter = @globals[:adapter] if @globals.include? :adapter
92
65
 
93
- if @globals[:transport] == :faraday
94
- @wsdl.request = faraday
95
- else
96
- @wsdl.adapter = @globals[:adapter] if @globals.include? :adapter
97
- @wsdl.request = Transport::HTTPI.new(@globals).wsdl_request
98
- end
66
+ @wsdl.request = WSDLRequest.new(@globals).build
99
67
  end
100
68
 
101
69
  def wsdl_or_endpoint_and_namespace_specified?
@@ -104,9 +72,9 @@ module Savon
104
72
 
105
73
  def raise_version1_initialize_error!(object)
106
74
  raise InitializationError,
107
- "Some code tries to initialize Savon with the #{object.inspect} (#{object.class}) \n" \
108
- "Savon 2 expects a Hash of options for creating a new client and executing requests.\n" \
109
- "Please read the updated documentation for version 2: http://savonrb.com/version2.html"
75
+ "Some code tries to initialize Savon with the #{object.inspect} (#{object.class}) \n" \
76
+ "Savon 2 expects a Hash of options for creating a new client and executing requests.\n" \
77
+ "Please read the updated documentation for version 2: http://savonrb.com/version2.html"
110
78
  end
111
79
 
112
80
  def raise_initialization_error!
@@ -120,5 +88,6 @@ module Savon
120
88
  def raise_missing_wsdl_error!
121
89
  raise "Unable to inspect the service without a WSDL document."
122
90
  end
91
+
123
92
  end
124
93
  end
data/lib/savon/header.rb CHANGED
@@ -1,136 +1,89 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  require "akami"
4
3
  require "gyoku"
5
- require "savon/addressing"
4
+ require "securerandom"
6
5
 
7
6
  module Savon
8
- # Assembles the SOAP +Header+ element of a request envelope and renders it to
9
- # XML.
10
- #
11
- # A SOAP Header carries a request's out-of-band metadata. This object is
12
- # responsible for every header block Savon can emit, concatenated in this
13
- # order:
14
- #
15
- # 1. application headers supplied by the caller (the +soap_header+ option),
16
- # 2. the WS-Addressing headers, rendered by {Savon::Addressing},
17
- # 3. the WS-Security header with UsernameToken credentials, a +wsu:Timestamp+
18
- # and an XML Signature (OASIS WSS SOAP Message Security 1.1).
19
- #
20
- # WS-Security markup is delegated to Akami and Hash-to-XML conversion to Gyoku.
21
7
  class Header
22
- # @param globals [Savon::GlobalOptions] client-level options, read for the
23
- # Gyoku key converter (+:convert_request_keys_to+) and the WS-Addressing
24
- # toggle (+:use_wsa_headers+)
25
- # @param effective [Savon::EffectiveOptions] resolves the WSSE, soap_header,
26
- # soap_action and endpoint values for the request
27
- def initialize(globals, effective)
28
- @gyoku_options = { key_converter: globals[:convert_request_keys_to] }
29
-
30
- @wsse_auth = effective.wsse_auth
31
- @wsse_timestamp = effective.wsse_timestamp
32
- @wsse_signature = effective.wsse_signature
33
- @soap_header = effective.soap_header
34
-
35
- @addressing = build_addressing(globals, effective)
36
- @header = build
37
- end
38
8
 
39
- attr_reader :gyoku_options, :wsse_auth, :wsse_timestamp, :wsse_signature
9
+ def initialize(globals, locals)
10
+ @gyoku_options = { :key_converter => globals[:convert_request_keys_to] }
11
+
12
+ @wsse_auth = locals[:wsse_auth].nil? ? globals[:wsse_auth] : locals[:wsse_auth]
13
+ @wsse_timestamp = locals[:wsse_timestamp].nil? ? globals[:wsse_timestamp] : locals[:wsse_timestamp]
14
+ @wsse_signature = locals[:wsse_signature].nil? ? globals[:wsse_signature] : locals[:wsse_signature]
40
15
 
41
- # Returns the XML attributes of the enclosing Header element. Only the
42
- # WS-Addressing section declares one, the +xmlns:wsa+ prefix.
43
- #
44
- # @return [Hash{String => String}]
45
- def attributes
46
- @addressing.namespace_attributes
16
+ @global_header = globals[:soap_header]
17
+ @local_header = locals[:soap_header]
18
+
19
+ @globals = globals
20
+ @locals = locals
21
+
22
+ @header = build
47
23
  end
48
24
 
49
- # @return [Boolean] whether the rendered header is empty, i.e. there is no
50
- # header content to include in the envelope
25
+ attr_reader :local_header, :global_header, :gyoku_options,
26
+ :wsse_auth, :wsse_timestamp, :wsse_signature
27
+
51
28
  def empty?
52
29
  @header.empty?
53
30
  end
54
31
 
55
- # Returns the rendered SOAP header XML. The header is built once during
56
- # construction and cached, so this is a cheap, repeatable read.
57
- #
58
- # @return [String] the header XML (may be empty)
59
32
  def to_s
60
33
  @header
61
34
  end
62
35
 
63
36
  private
64
37
 
65
- # Constructs the WS-Addressing section from the resolved request values.
66
- # Resolving the endpoint raises without a WSDL document and without a
67
- # global +:endpoint+, so action and endpoint are only resolved when the
68
- # +:use_wsa_headers+ global asks for the headers.
69
- #
70
- # @return [Savon::Addressing]
71
- def build_addressing(globals, effective)
72
- return Addressing.new(enabled: false) unless globals[:use_wsa_headers]
73
-
74
- Addressing.new(
75
- enabled: true,
76
- action: effective.soap_action,
77
- to: effective.endpoint
78
- )
79
- end
80
-
81
- # Concatenates the three header sections in document order: caller content,
82
- # WS-Addressing, then WSSE security.
83
- #
84
- # @return [String]
85
38
  def build
86
- build_header + @addressing.to_xml + build_wsse_header
39
+ build_header + build_wsa_header + build_wsse_header
87
40
  end
88
41
 
89
- # Renders the resolved soap_header. {Savon::EffectiveOptions#soap_header}
90
- # merges the global and local values, so there is nothing to combine here.
91
- # Hash-to-XML conversion only. Strings pass through.
92
- #
93
- # @return [String]
94
42
  def build_header
95
- convert_to_xml(@soap_header)
43
+ header =
44
+ if global_header.kind_of?(Hash) && local_header.kind_of?(Hash)
45
+ global_header.merge(local_header)
46
+ elsif local_header
47
+ local_header
48
+ else
49
+ global_header
50
+ end
51
+
52
+ convert_to_xml(header)
96
53
  end
97
54
 
98
- # Builds the WS-Security header via Akami, or an empty string when no WSSE
99
- # content (credentials, timestamp or signature) applies.
100
- #
101
- # @return [String]
102
55
  def build_wsse_header
103
56
  wsse_header = akami
104
57
  wsse_header.respond_to?(:to_xml) ? wsse_header.to_xml : ""
105
58
  end
106
59
 
107
- # Renders a header value to XML. A Hash goes through Gyoku (honouring the
108
- # configured key converter), anything else is coerced with +#to_s+.
109
- #
110
- # @param hash_or_string [Hash, #to_s] the header content
111
- # @return [String]
60
+ def build_wsa_header
61
+ return '' unless @globals[:use_wsa_headers]
62
+ convert_to_xml({
63
+ 'wsa:Action' => @locals[:soap_action],
64
+ 'wsa:To' => @globals[:endpoint],
65
+ 'wsa:MessageID' => "urn:uuid:#{SecureRandom.uuid}"
66
+ })
67
+ end
68
+
112
69
  def convert_to_xml(hash_or_string)
113
- if hash_or_string.is_a? Hash
70
+ if hash_or_string.kind_of? Hash
114
71
  Gyoku.xml(hash_or_string, gyoku_options)
115
72
  else
116
73
  hash_or_string.to_s
117
74
  end
118
75
  end
119
76
 
120
- # Configures an Akami WSSE object from the resolved WSSE options. Sets the
121
- # UsernameToken credentials, enables the wsu:Timestamp, and attaches the XML
122
- # Signature when a document has been assigned to it.
123
- #
124
- # @return [Akami::WSSE] configured, not yet serialized
125
77
  def akami
126
78
  wsse = Akami.wsse
127
79
  wsse.credentials(*wsse_auth) if wsse_auth
128
80
  wsse.timestamp = wsse_timestamp if wsse_timestamp
129
- if wsse_signature&.have_document?
81
+ if wsse_signature && wsse_signature.have_document?
130
82
  wsse.signature = wsse_signature
131
83
  end
132
84
 
133
85
  wsse
134
86
  end
87
+
135
88
  end
136
89
  end
@@ -2,26 +2,26 @@
2
2
 
3
3
  module Savon
4
4
  class HTTPError < Error
5
+
5
6
  def self.present?(http)
6
- http.error?
7
+ !http.success?
7
8
  end
8
9
 
9
- def initialize(transport_response)
10
- @transport_response = transport_response
10
+ def initialize(http)
11
+ @http = http
11
12
  end
12
13
 
13
- def http
14
- @transport_response
15
- end
14
+ attr_reader :http
16
15
 
17
16
  def to_s
18
- String.new("HTTP error (#{http.code})").tap do |str_error|
19
- str_error << ": #{http.body}" unless http.body.empty?
17
+ String.new("HTTP error (#{@http.status})").tap do |str_error|
18
+ str_error << ": #{@http.body}" unless @http.body.empty?
20
19
  end
21
20
  end
22
21
 
23
22
  def to_hash
24
- { code: http.code, headers: http.headers, body: http.body }
23
+ { :code => @http.status, :headers => @http.headers, :body => @http.body }
25
24
  end
25
+
26
26
  end
27
27
  end