/tools/Ruby/lib/ruby/1.8/soap/netHttpClient.rb

http://github.com/agross/netopenspace · Ruby · 190 lines · 152 code · 29 blank · 9 comment · 20 complexity · 1cb4ec7198d11ce326c6d62add9e2ac8 MD5 · raw file

  1. # SOAP4R - net/http wrapper
  2. # Copyright (C) 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
  3. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
  4. # redistribute it and/or modify it under the same terms of Ruby's license;
  5. # either the dual license version in 2003, or any later version.
  6. require 'net/http'
  7. module SOAP
  8. class NetHttpClient
  9. SSLEnabled = begin
  10. require 'net/https'
  11. true
  12. rescue LoadError
  13. false
  14. end
  15. attr_reader :proxy
  16. attr_accessor :no_proxy
  17. attr_accessor :debug_dev
  18. attr_accessor :ssl_config # ignored for now.
  19. attr_accessor :protocol_version # ignored for now.
  20. attr_accessor :connect_timeout
  21. attr_accessor :send_timeout # ignored for now.
  22. attr_accessor :receive_timeout
  23. def initialize(proxy = nil, agent = nil)
  24. @proxy = proxy ? URI.parse(proxy) : nil
  25. @agent = agent
  26. @debug_dev = nil
  27. @session_manager = SessionManager.new
  28. @no_proxy = @ssl_config = @protocol_version = nil
  29. @connect_timeout = @send_timeout = @receive_timeout = nil
  30. end
  31. def test_loopback_response
  32. raise NotImplementedError.new("not supported for now")
  33. end
  34. def proxy=(proxy)
  35. if proxy.nil?
  36. @proxy = nil
  37. else
  38. if proxy.is_a?(URI)
  39. @proxy = proxy
  40. else
  41. @proxy = URI.parse(proxy)
  42. end
  43. if @proxy.scheme == nil or @proxy.scheme.downcase != 'http' or
  44. @proxy.host == nil or @proxy.port == nil
  45. raise ArgumentError.new("unsupported proxy `#{proxy}'")
  46. end
  47. end
  48. reset_all
  49. @proxy
  50. end
  51. def set_basic_auth(uri, user_id, passwd)
  52. # net/http does not handle url.
  53. @basic_auth = [user_id, passwd]
  54. raise NotImplementedError.new("basic_auth is not supported under soap4r + net/http.")
  55. end
  56. def set_cookie_store(filename)
  57. raise NotImplementedError.new
  58. end
  59. def save_cookie_store(filename)
  60. raise NotImplementedError.new
  61. end
  62. def reset(url)
  63. # no persistent connection. ignored.
  64. end
  65. def reset_all
  66. # no persistent connection. ignored.
  67. end
  68. def post(url, req_body, header = {})
  69. unless url.is_a?(URI)
  70. url = URI.parse(url)
  71. end
  72. extra = header.dup
  73. extra['User-Agent'] = @agent if @agent
  74. res = start(url) { |http|
  75. http.post(url.request_uri, req_body, extra)
  76. }
  77. Response.new(res)
  78. end
  79. def get_content(url, header = {})
  80. unless url.is_a?(URI)
  81. url = URI.parse(url)
  82. end
  83. extra = header.dup
  84. extra['User-Agent'] = @agent if @agent
  85. res = start(url) { |http|
  86. http.get(url.request_uri, extra)
  87. }
  88. res.body
  89. end
  90. private
  91. def start(url)
  92. http = create_connection(url)
  93. response = nil
  94. http.start { |worker|
  95. response = yield(worker)
  96. worker.finish
  97. }
  98. @debug_dev << response.body if @debug_dev
  99. response
  100. end
  101. def create_connection(url)
  102. proxy_host = proxy_port = nil
  103. unless no_proxy?(url)
  104. proxy_host = @proxy.host
  105. proxy_port = @proxy.port
  106. end
  107. http = Net::HTTP::Proxy(proxy_host, proxy_port).new(url.host, url.port)
  108. if http.respond_to?(:set_debug_output)
  109. http.set_debug_output(@debug_dev)
  110. end
  111. http.open_timeout = @connect_timeout if @connect_timeout
  112. http.read_timeout = @receive_timeout if @receive_timeout
  113. case url
  114. when URI::HTTPS
  115. if SSLEnabled
  116. http.use_ssl = true
  117. else
  118. raise RuntimeError.new("Cannot connect to #{url} (OpenSSL is not installed.)")
  119. end
  120. when URI::HTTP
  121. # OK
  122. else
  123. raise RuntimeError.new("Cannot connect to #{url} (Not HTTP.)")
  124. end
  125. http
  126. end
  127. NO_PROXY_HOSTS = ['localhost']
  128. def no_proxy?(uri)
  129. if !@proxy or NO_PROXY_HOSTS.include?(uri.host)
  130. return true
  131. end
  132. if @no_proxy
  133. @no_proxy.scan(/([^:,]*)(?::(\d+))?/) do |host, port|
  134. if /(\A|\.)#{Regexp.quote(host)}\z/i =~ uri.host &&
  135. (!port || uri.port == port.to_i)
  136. return true
  137. end
  138. end
  139. else
  140. false
  141. end
  142. end
  143. class SessionManager
  144. attr_accessor :connect_timeout
  145. attr_accessor :send_timeout
  146. attr_accessor :receive_timeout
  147. end
  148. class Response
  149. attr_reader :content
  150. attr_reader :status
  151. attr_reader :reason
  152. attr_reader :contenttype
  153. def initialize(res)
  154. @status = res.code.to_i
  155. @reason = res.message
  156. @contenttype = res['content-type']
  157. @content = res.body
  158. end
  159. end
  160. end
  161. end