Welcome to the "trac"-ing site of soap4r!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

root/trunk/lib/soap/httpconfigloader.rb

Revision 1869, 3.6 kB (checked in by nahi, 1 year ago)
  • added 'protocol.http.auth' option to set authentication information for both BasicAuth? and DigestAuth?. with the latest http-access2, now soap4r client can connect to DigestAuth? server. closes #345.
Line 
1 # SOAP4R - HTTP config loader.
2 # Copyright (C) 2000-2007  NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
3
4 # This program is copyrighted free software by NAKAMURA, Hiroshi.  You can
5 # redistribute it and/or modify it under the same terms of Ruby's license;
6 # either the dual license version in 2003, or any later version.
7
8
9 require 'soap/property'
10
11
12 module SOAP
13
14
15 module HTTPConfigLoader
16 module_function
17
18   def set_options(client, options)
19     client.proxy = options["proxy"]
20     options.add_hook("proxy") do |key, value|
21       client.proxy = value
22     end
23     client.no_proxy = options["no_proxy"]
24     options.add_hook("no_proxy") do |key, value|
25       client.no_proxy = value
26     end
27     if client.respond_to?(:protocol_version=)
28       client.protocol_version = options["protocol_version"]
29       options.add_hook("protocol_version") do |key, value|
30         client.protocol_version = value
31       end
32     end
33     ssl_config = options["ssl_config"] ||= ::SOAP::Property.new
34     set_ssl_config(client, ssl_config)
35     ssl_config.add_hook(true) do |key, value|
36       set_ssl_config(client, ssl_config)
37     end
38     basic_auth = options["basic_auth"] ||= ::SOAP::Property.new
39     set_basic_auth(client, basic_auth)
40     basic_auth.add_hook do |key, value|
41       set_basic_auth(client, basic_auth)
42     end
43     auth = options["auth"] ||= ::SOAP::Property.new
44     set_auth(client, auth)
45     auth.add_hook do |key, value|
46       set_auth(client, auth)
47     end
48     options.add_hook("connect_timeout") do |key, value|
49       client.connect_timeout = value
50     end
51     options.add_hook("send_timeout") do |key, value|
52       client.send_timeout = value
53     end
54     options.add_hook("receive_timeout") do |key, value|
55       client.receive_timeout = value
56     end
57   end
58
59   def set_basic_auth(client, basic_auth)
60     basic_auth.values.each do |ele|
61       client.set_basic_auth(*authele_to_triplets(ele))
62     end
63   end
64
65   def set_auth(client, auth)
66     auth.values.each do |ele|
67       client.set_auth(*authele_to_triplets(ele))
68     end
69   end
70
71   def authele_to_triplets(ele)
72     if ele.is_a?(::Array)
73       url, userid, passwd = ele
74     else
75       url, userid, passwd = ele[:url], ele[:userid], ele[:password]
76     end
77     return url, userid, passwd
78   end
79
80   def set_ssl_config(client, ssl_config)
81     ssl_config.each do |key, value|
82       cfg = client.ssl_config
83       if cfg.nil?
84         raise NotImplementedError.new("SSL not supported")
85       end
86       case key
87       when 'client_cert'
88         cfg.client_cert = cert_from_file(value)
89       when 'client_key'
90         cfg.client_key = key_from_file(value)
91       when 'client_ca'
92         cfg.client_ca = value
93       when 'ca_path'
94         cfg.set_trust_ca(value)
95       when 'ca_file'
96         cfg.set_trust_ca(value)
97       when 'crl'
98         cfg.set_crl(value)
99       when 'verify_mode'
100         cfg.verify_mode = ssl_config_int(value)
101       when 'verify_depth'
102         cfg.verify_depth = ssl_config_int(value)
103       when 'options'
104         cfg.options = value
105       when 'ciphers'
106         cfg.ciphers = value
107       when 'verify_callback'
108         cfg.verify_callback = value
109       when 'cert_store'
110         cfg.cert_store = value
111       else
112         raise ArgumentError.new("unknown ssl_config property #{key}")
113       end
114     end
115   end
116
117   def ssl_config_int(value)
118     if value.nil? or value.to_s.empty?
119       nil
120     else
121       begin
122         Integer(value)
123       rescue ArgumentError
124         ::SOAP::Property::Util.const_from_name(value.to_s)
125       end
126     end
127   end
128
129   def cert_from_file(filename)
130     OpenSSL::X509::Certificate.new(File.open(filename) { |f| f.read })
131   end
132
133   def key_from_file(filename)
134     OpenSSL::PKey::RSA.new(File.open(filename) { |f| f.read })
135   end
136 end
137
138
139 end
Note: See TracBrowser for help on using the browser.