lib/savon/request.rb in savon-0.6.6 vs lib/savon/request.rb in savon-0.6.7
- old
+ new
@@ -6,47 +6,63 @@
class Request
# Content-Types by SOAP version.
ContentType = { 1 => "text/xml", 2 => "application/soap+xml" }
- # Defines whether to log HTTP requests.
- @log = true
+ # Whether to log HTTP requests.
+ @@log = true
# The default logger.
- @logger = Logger.new STDOUT
+ @@logger = Logger.new STDOUT
# The default log level.
- @log_level = :debug
+ @@log_level = :debug
- class << self
- # Sets whether to log HTTP requests.
- attr_writer :log
+ # Sets whether to log HTTP requests.
+ def self.log=(log)
+ @@log = log
+ end
- # Returns whether to log HTTP requests.
- def log?
- @log
- end
+ # Returns whether to log HTTP requests.
+ def self.log?
+ @@log
+ end
- # Accessor for the default logger.
- attr_accessor :logger
+ # Sets the logger.
+ def self.logger=(logger)
+ @@logger = logger
+ end
- # Accessor for the default log level.
- attr_accessor :log_level
+ # Returns the logger.
+ def self.logger
+ @@logger
end
- # Expects an endpoint String. Raises an exception in case the given
- # +endpoint+ does not seem to be valid.
- def initialize(endpoint)
- raise ArgumentError, "Invalid endpoint: #{endpoint}" unless
- /^(http|https):\/\// === endpoint
+ # Sets the log level.
+ def self.log_level=(log_level)
+ @@log_level = log_level
+ end
+ # Returns the log level.
+ def self.log_level
+ @@log_level
+ end
+
+ # Expects a SOAP +endpoint+ String. Also accepts an optional Hash of
+ # +options+ for specifying a proxy server and SSL client authentication.
+ def initialize(endpoint, options = {})
@endpoint = URI endpoint
+ @proxy = options[:proxy] ? URI(options[:proxy]) : URI("")
+ @ssl = options[:ssl] if options[:ssl]
end
# Returns the endpoint URI.
attr_reader :endpoint
+ # Returns the proxy URI.
+ attr_reader :proxy
+
# Sets the open timeout for HTTP requests.
def open_timeout=(sec)
http.open_timeout = sec
end
@@ -88,13 +104,23 @@
end
# Returns a Net::HTTP instance.
def http
unless @http
- @http ||= Net::HTTP.new @endpoint.host, @endpoint.port
+ @http ||= Net::HTTP::Proxy(@proxy.host, @proxy.port).new @endpoint.host, @endpoint.port
@http.use_ssl = true if @endpoint.ssl?
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ add_ssl_authentication if @ssl
end
@http
+ end
+
+ # Adds SSL client authentication to the +@http+ instance.
+ def add_ssl_authentication
+ @http.verify_mode = @ssl[:verify] if @ssl[:verify].kind_of? Integer
+ @http.cert = @ssl[:client_cert] if @ssl[:client_cert]
+ @http.key = @ssl[:client_key] if @ssl[:client_key]
+ @http.ca_file = @ssl[:ca_file] if @ssl[:ca_file]
end
# Returns a Hash containing the header for an HTTP request.
def http_header
{ "Content-Type" => ContentType[@soap.version], "SOAPAction" => @soap.action }