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.
@@ -1,68 +1,54 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  require "savon/options"
4
- require "savon/effective_options"
5
3
  require "savon/block_interface"
4
+ require "savon/request"
6
5
  require "savon/builder"
7
6
  require "savon/response"
7
+ require "savon/request_logger"
8
8
  require "savon/http_error"
9
- require "savon/transport/httpi"
10
- require "savon/transport/faraday"
11
9
  require "mail"
10
+ require 'faraday/gzip'
11
+
12
12
 
13
13
  module Savon
14
- # Represents a single named SOAP operation.
15
- #
16
- # Bridges the SOAP layer (envelope building, action headers, multipart) and the
17
- # transport layer (execution, logging). Knows nothing about transport internals
18
- # such as proxy, SSL, or auth.
19
14
  class Operation
20
- # SOAP Content-Type values indexed by SOAP version.
21
- # SOAP 1.1 §6 (HTTP binding), SOAP 1.2 Part 2 §7.1.4 (HTTP media type)
22
- CONTENT_TYPE = {
23
- 1 => "text/xml;charset=%s",
24
- 2 => "application/soap+xml;charset=%s"
25
- }.freeze
26
-
27
- # Maps SOAP version to the base MIME type used in multipart requests.
28
- # RFC 2387 §3.1 (multipart/related Content-Type parameter)
15
+
29
16
  SOAP_REQUEST_TYPE = {
30
17
  1 => "text/xml",
31
18
  2 => "application/soap+xml"
32
- }.freeze
33
- SOAP_REQUEST_TYPE_MTOM = "application/xop+xml"
19
+ }
34
20
 
35
- def self.create(operation_name, wsdl, globals, transport)
21
+ def self.create(operation_name, wsdl, globals)
36
22
  if wsdl.document?
37
23
  ensure_name_is_symbol! operation_name
38
- ensure_exists! operation_name, wsdl, transport
24
+ ensure_exists! operation_name, wsdl
39
25
  end
40
26
 
41
- new(operation_name, wsdl, globals, transport)
27
+ new(operation_name, wsdl, globals)
42
28
  end
43
29
 
44
- # Verifies the operation is provided by the WSDL.
45
- def self.ensure_exists!(operation_name, wsdl, transport)
30
+ def self.ensure_exists!(operation_name, wsdl)
46
31
  unless wsdl.soap_actions.include? operation_name
47
32
  raise UnknownOperationError, "Unable to find SOAP operation: #{operation_name.inspect}\n" \
48
33
  "Operations provided by your service: #{wsdl.soap_actions.inspect}"
49
34
  end
50
35
  rescue Wasabi::Resolver::HTTPError => e
51
- raise HTTPError, transport.normalize_response(e.response)
36
+ raise HTTPError.new(e.response)
52
37
  end
53
38
 
54
39
  def self.ensure_name_is_symbol!(operation_name)
55
- return if operation_name.is_a? Symbol
56
-
57
- raise ArgumentError, "Expected the first parameter (the name of the operation to call) to be a symbol\n" \
58
- "Actual: #{operation_name.inspect} (#{operation_name.class})"
40
+ unless operation_name.kind_of? Symbol
41
+ raise ArgumentError, "Expected the first parameter (the name of the operation to call) to be a symbol\n" \
42
+ "Actual: #{operation_name.inspect} (#{operation_name.class})"
43
+ end
59
44
  end
60
45
 
61
- def initialize(name, wsdl, globals, transport)
62
- @name = name
63
- @wsdl = wsdl
64
- @globals = globals
65
- @transport = transport
46
+ def initialize(name, wsdl, globals)
47
+ @name = name
48
+ @wsdl = wsdl
49
+ @globals = globals
50
+
51
+ @logger = RequestLogger.new(globals)
66
52
  end
67
53
 
68
54
  def build(locals = {}, &block)
@@ -70,45 +56,32 @@ module Savon
70
56
  Builder.new(@name, @wsdl, @globals, @locals)
71
57
  end
72
58
 
73
- # Executes the SOAP operation and returns a Savon::Response.
74
- #
75
- # Observer short-circuit: if any registered observer returns a
76
- # Transport::Response (or legacy HTTPI::Response), the HTTP call
77
- # is skipped and that response is used directly.
78
59
  def call(locals = {}, &block)
79
60
  builder = build(locals, &block)
80
- observer_response = Savon.notify_observers(@name, builder, @globals, @locals)
81
-
82
- transport_response =
83
- if observer_response.nil?
84
- body = builder.to_s
85
- headers = soap_headers(builder)
86
- @transport.post(endpoint.to_s, headers, body, @locals)
87
- else
88
- normalize_observer_response(observer_response)
89
- end
90
61
 
91
- Response.new(transport_response, @globals, @locals)
62
+ response = Savon.notify_observers(@name, builder, @globals, @locals)
63
+ response ||= call_with_logging build_connection(builder)
64
+
65
+ raise_expected_faraday_response! unless response.kind_of?(Faraday::Response)
66
+
67
+ create_response(response)
92
68
  end
93
69
 
94
- # Builds and returns the HTTPI::Request that would be sent for this
95
- # operation, without executing it. Useful for inspection and debugging.
96
- # Not supported with transport: :faraday.
97
70
  def request(locals = {}, &block)
98
- if @globals[:transport] == :faraday
99
- raise ArgumentError, "#request returns an HTTPI::Request and is not supported " \
100
- "with transport: :faraday. Use client.faraday to configure " \
101
- "the connection"
102
- end
103
-
104
71
  builder = build(locals, &block)
105
- # Build the body before soap_headers - see #call.
106
- body = builder.to_s
107
- @transport.to_httpi_request(endpoint.to_s, soap_headers(builder), body, @locals)
72
+ connection = build_connection(builder)
73
+ connection.build_request(:post) do |req|
74
+ req.url(@globals[:endpoint])
75
+ req.body = @locals[:body]
76
+ end
108
77
  end
109
78
 
110
79
  private
111
80
 
81
+ def create_response(response)
82
+ Response.new(response, @globals, @locals)
83
+ end
84
+
112
85
  def set_locals(locals, block)
113
86
  locals = LocalOptions.new(locals)
114
87
  BlockInterface.new(locals).evaluate(block) if block
@@ -116,76 +89,75 @@ module Savon
116
89
  @locals = locals
117
90
  end
118
91
 
119
- # The effective options view for the current request. Built on demand
120
- # instead of being cached, so it always reflects the current per-request
121
- # options.
122
- def effective_options
123
- EffectiveOptions.new(@name, @wsdl, @globals, @locals)
92
+ def call_with_logging(connection)
93
+ ntlm_auth = handle_ntlm(connection) if @globals.include?(:ntlm)
94
+ @logger.log_response(connection.post(@globals[:endpoint]) { |request|
95
+ request.body = @locals[:body]
96
+ request.headers['Authorization'] = "NTLM #{auth.encode64}" if ntlm_auth
97
+ @logger.log_request(request)
98
+ })
99
+ end
100
+
101
+ def handle_ntlm(connection)
102
+ ntlm_message = Net::NTLM::Message
103
+ response = connection.get(@globals[:endpoint]) do |request|
104
+ request.headers['Authorization'] = 'NTLM ' + ntlm_message::Type1.new.encode64
105
+ end
106
+ challenge = response.headers['www-authenticate'][/(?:NTLM|Negotiate) (.*)$/, 1]
107
+ message = ntlm_message::Type2.decode64(challenge)
108
+ message.response([:user, :password, :domain].zip(@globals[:ntlm]).to_h)
124
109
  end
125
110
 
126
- # Assembles the SOAP-level request headers for the given builder.
127
- #
128
- # Our responsibility regardless of transport:
129
- # * Content-Type (SOAP 1.1 §6 / SOAP 1.2 Part 2 §7.1.4)
130
- # * SOAPAction (SOAP 1.1 §6.1.1)
131
- # * Multipart Content-Type (RFC 2387), MIME-Version (RFC 2045 §4), Accept-Encoding (RFC 9110 §12.5.3)
132
- def soap_headers(builder)
133
- headers = {}
134
-
135
- if builder.multipart
136
- # RFC 2387 §3 (multipart/related) - SOAP envelope is the root body part
137
- content_type = ["multipart/related"]
138
- if @locals[:mtom]
139
- content_type << "type=\"#{SOAP_REQUEST_TYPE_MTOM}\""
140
- content_type << "start-info=\"#{SOAP_REQUEST_TYPE[@globals[:soap_version]]}\""
141
- else
142
- content_type << "type=\"#{SOAP_REQUEST_TYPE[@globals[:soap_version]]}\""
111
+ def build_connection(builder)
112
+ @globals[:endpoint] ||= endpoint
113
+ @locals[:soap_action] ||= soap_action
114
+ @locals[:body] = builder.to_s
115
+ @connection = SOAPRequest.new(@globals).build(
116
+ :soap_action => soap_action,
117
+ :cookies => @locals[:cookies],
118
+ :headers => @locals[:headers]
119
+ ) do |connection|
120
+ if builder.multipart
121
+ connection.request :gzip
122
+ connection.headers["Content-Type"] = %W[multipart/related
123
+ type="#{SOAP_REQUEST_TYPE[@globals[:soap_version]]}",
124
+ start="#{builder.multipart[:start]}",
125
+ boundary="#{builder.multipart[:multipart_boundary]}"].join("; ")
126
+ connection.headers["MIME-Version"] = "1.0"
143
127
  end
144
128
 
145
- headers["Content-Type"] = [
146
- *content_type,
147
- "start=\"#{builder.multipart[:start]}\"",
148
- "boundary=\"#{builder.multipart[:multipart_boundary]}\""
149
- ].join("; ")
150
- headers["MIME-Version"] = "1.0"
151
- headers["Accept-Encoding"] = "gzip,deflate"
152
- else
153
- headers["Content-Type"] = CONTENT_TYPE[@globals[:soap_version]] % @globals[:encoding]
129
+ connection.headers["Content-Length"] = @locals[:body].bytesize.to_s
154
130
  end
155
131
 
156
- action = soap_action
157
- headers["SOAPAction"] = %("#{action}") if action
158
132
 
159
- headers
160
133
  end
161
134
 
162
- # The resolved SOAPAction of the request, used for the HTTP header.
163
- # {Savon::EffectiveOptions#soap_action} owns the precedence rules.
164
135
  def soap_action
165
- effective_options.soap_action
136
+ # soap_action explicitly set to something falsy
137
+ return if @locals.include?(:soap_action) && !@locals[:soap_action]
138
+
139
+ # get the soap_action from local options
140
+ @locals[:soap_action] ||
141
+ # with no local option, but a wsdl, ask it for the soap_action
142
+ @wsdl.document? && @wsdl.soap_action(@name.to_sym) ||
143
+ # if there is no soap_action up to this point, fallback to a simple default
144
+ Gyoku.xml_tag(@name, :key_converter => @globals[:convert_request_keys_to])
166
145
  end
167
146
 
168
- # The resolved endpoint URL the request is sent to.
169
- # {Savon::EffectiveOptions#endpoint} owns the precedence rules.
170
147
  def endpoint
171
- effective_options.endpoint
172
- end
173
-
174
- # Normalizes an observer return value into a Transport::Response.
175
- #
176
- # Accepts Transport::Response directly (current contract), wraps
177
- # HTTPI::Response with a deprecation warning (legacy observer support),
178
- # and raises on anything else.
179
- def normalize_observer_response(observer_response)
180
- return observer_response if observer_response.is_a?(Transport::Response)
181
-
182
- if observer_response.is_a?(HTTPI::Response)
183
- warn "Observers returning HTTPI::Response is deprecated - return Savon::Transport::Response instead.", uplevel: 1
184
- return Transport::Response.from_httpi(observer_response)
148
+ @globals[:endpoint] || @wsdl.endpoint.tap do |url|
149
+ if @globals[:host]
150
+ host_url = URI.parse(@globals[:host])
151
+ url.host = host_url.host
152
+ url.port = host_url.port
153
+ end
185
154
  end
155
+ end
186
156
 
187
- raise Error, "Observers need to return a Savon::Transport::Response " \
188
- "to mock the request or nil to execute the request."
157
+ def raise_expected_faraday_response!
158
+ raise Error, "Observers need to return a Faraday::Response to mock " \
159
+ "the request or nil to execute the request."
189
160
  end
161
+
190
162
  end
191
163
  end