1 | # SOAP4R - HTTP config loader.
|
---|
2 | # Copyright (C) 2005 NAKAMURA, Hiroshi <[email protected]>.
|
---|
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 | options.add_hook("connect_timeout") do |key, value|
|
---|
44 | client.connect_timeout = value
|
---|
45 | end
|
---|
46 | options.add_hook("send_timeout") do |key, value|
|
---|
47 | client.send_timeout = value
|
---|
48 | end
|
---|
49 | options.add_hook("receive_timeout") do |key, value|
|
---|
50 | client.receive_timeout = value
|
---|
51 | end
|
---|
52 | end
|
---|
53 |
|
---|
54 | def set_basic_auth(client, basic_auth)
|
---|
55 | basic_auth.values.each do |url, userid, passwd|
|
---|
56 | client.set_basic_auth(url, userid, passwd)
|
---|
57 | end
|
---|
58 | end
|
---|
59 |
|
---|
60 | def set_ssl_config(client, ssl_config)
|
---|
61 | ssl_config.each do |key, value|
|
---|
62 | cfg = client.ssl_config
|
---|
63 | if cfg.nil?
|
---|
64 | raise NotImplementedError.new("SSL not supported")
|
---|
65 | end
|
---|
66 | case key
|
---|
67 | when 'client_cert'
|
---|
68 | cfg.client_cert = cert_from_file(value)
|
---|
69 | when 'client_key'
|
---|
70 | cfg.client_key = key_from_file(value)
|
---|
71 | when 'client_ca'
|
---|
72 | cfg.client_ca = value
|
---|
73 | when 'ca_path'
|
---|
74 | cfg.set_trust_ca(value)
|
---|
75 | when 'ca_file'
|
---|
76 | cfg.set_trust_ca(value)
|
---|
77 | when 'crl'
|
---|
78 | cfg.set_crl(value)
|
---|
79 | when 'verify_mode'
|
---|
80 | cfg.verify_mode = ssl_config_int(value)
|
---|
81 | when 'verify_depth'
|
---|
82 | cfg.verify_depth = ssl_config_int(value)
|
---|
83 | when 'options'
|
---|
84 | cfg.options = value
|
---|
85 | when 'ciphers'
|
---|
86 | cfg.ciphers = value
|
---|
87 | when 'verify_callback'
|
---|
88 | cfg.verify_callback = value
|
---|
89 | when 'cert_store'
|
---|
90 | cfg.cert_store = value
|
---|
91 | else
|
---|
92 | raise ArgumentError.new("unknown ssl_config property #{key}")
|
---|
93 | end
|
---|
94 | end
|
---|
95 | end
|
---|
96 |
|
---|
97 | def ssl_config_int(value)
|
---|
98 | if value.nil? or value.to_s.empty?
|
---|
99 | nil
|
---|
100 | else
|
---|
101 | begin
|
---|
102 | Integer(value)
|
---|
103 | rescue ArgumentError
|
---|
104 | ::SOAP::Property::Util.const_from_name(value.to_s)
|
---|
105 | end
|
---|
106 | end
|
---|
107 | end
|
---|
108 |
|
---|
109 | def cert_from_file(filename)
|
---|
110 | OpenSSL::X509::Certificate.new(File.open(filename) { |f| f.read })
|
---|
111 | end
|
---|
112 |
|
---|
113 | def key_from_file(filename)
|
---|
114 | OpenSSL::PKey::RSA.new(File.open(filename) { |f| f.read })
|
---|
115 | end
|
---|
116 | end
|
---|
117 |
|
---|
118 |
|
---|
119 | end
|
---|