lib/rufus/verbs/endpoint.rb in rufus-verbs-0.9 vs lib/rufus/verbs/endpoint.rb in rufus-verbs-0.10

- old
+ new

@@ -6,14 +6,14 @@ # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: -# +# # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, @@ -47,25 +47,25 @@ module Verbs USER_AGENT = "Ruby rufus-verbs #{VERSION}" # - # An EndPoint can be used to share common options among a set of + # An EndPoint can be used to share common options among a set of # requests. # # ep = EndPoint.new( - # :host => "restful.server", - # :port => 7080, + # :host => "restful.server", + # :port => 7080, # :resource => "inventory/tools") # # res = ep.get :id => 1 # # still a silver bullet ? # # res = ep.get :id => 0 # # where did the hammer go ? # - # When a request gets prepared, the option values will be looked up + # When a request gets prepared, the option values will be looked up # in (1) its local (request) options, then (2) in the EndPoint options. # class EndPoint include CookieMixin @@ -81,11 +81,11 @@ @opts = opts compute_target @opts - @opts[:http_basic_authentication] = + @opts[:http_basic_authentication] = opts[:http_basic_authentication] || opts[:hba] @opts[:user_agent] ||= USER_AGENT @opts[:proxy] ||= ENV['HTTP_PROXY'] @@ -124,11 +124,11 @@ end # # This is the method called by the module methods verbs. # - # For example, + # For example, # # RufusVerbs.get(args) # # calls # @@ -262,14 +262,14 @@ opts[:query] || opts[:params] ] elsif u u = URI.parse u.to_s unless u.is_a?(URI) - [ u.scheme, - u.host, - u.port, - u.path, + [ u.scheme, + u.host, + u.port, + u.path, query_to_h(u.query) ] else [] end @@ -277,22 +277,22 @@ opts[:scheme] = r[0] || @opts[:scheme] opts[:host] = r[1] || @opts[:host] opts[:port] = r[2] || @opts[:port] opts[:path] = r[3] || @opts[:path] - opts[:query] = - r[4] || + opts[:query] = + r[4] || opts[:params] || opts[:query] || - @opts[:query] || @opts[:params] || + @opts[:query] || @opts[:params] || {} opts.delete :path if opts[:path] == "" - opts[:c_uri] = [ - opts[:scheme], - opts[:host], - opts[:port], + opts[:c_uri] = [ + opts[:scheme], + opts[:host], + opts[:port], opts[:path], opts[:query] ].inspect # # can be used for conditional gets @@ -301,19 +301,19 @@ # # Creates the Net::HTTP request instance. # # If :fake_put is set, will use Net::HTTP::Post - # and make sure the query string contains '_method=put' (or + # and make sure the query string contains '_method=put' (or # '_method=delete'). # # This call will also advertise this rufus-verbs as # 'accepting the gzip encoding' (in case of GET). # def create_request (method, opts) - if (o(opts, :fake_put) and + if (o(opts, :fake_put) and (method == :put or method == :delete)) opts[:query][:_method] = method.to_s method = :post end @@ -360,11 +360,11 @@ o.call req end end # - # In that base class, it's empty. + # In that base class, it's empty. # It's implemented in ConditionalEndPoint. # # Only called for a GET. # def add_conditional_headers (req, opts) @@ -379,35 +379,52 @@ def prepare_http (opts) compute_proxy opts http = Net::HTTP.new( - opts[:host], opts[:port], + opts[:host], opts[:port], opts[:proxy_host], opts[:proxy_port], opts[:proxy_user], opts[:proxy_pass]) + set_timeout http, opts + return http unless opts[:scheme] == 'https' require 'net/https' http.use_ssl = true http.enable_post_connection_check = true http.verify_mode = if o(opts, :ssl_verify_peer) - OpenSSL::SSL::VERIFY_NONE - else OpenSSL::SSL::VERIFY_PEER + else + OpenSSL::SSL::VERIFY_NONE end store = OpenSSL::X509::Store.new store.set_default_paths http.cert_store = store http end # + # Sets both the open_timeout and the read_timeout for the http + # instance + # + def set_timeout (http, opts) + + to = o(opts, :timeout) || o(opts, :to) + to = to.to_i + + return if to == 0 + + http.open_timeout = to + http.read_timeout = to + end + + # # Makes sure the request opts hold the proxy information. # # If the option :proxy is set to false, no proxy will be used. # def compute_proxy (opts) @@ -464,23 +481,23 @@ # def query_to_h (q) return nil unless q - q.split("&").inject({}) do |r, e| + q.split("&").inject({}) do |r, e| s = e.split("=") r[s[0]] = s[1] r end end # - # { "a" => "A", "b" => "B" } -> "a=A&b=B" + # { "a" => "A", "b" => "B" } -> "a=A&b=B" # def h_to_query (h, opts) - h.entries.collect { |k, v| + h.entries.collect { |k, v| unless o(opts, :no_escape) k = URI.escape k.to_s v = URI.escape v.to_s end "#{k}=#{v}" @@ -500,11 +517,11 @@ elsif fd sep = opts[:fd_sep] #|| nil req.set_form_data fd, sep elsif block req.body = block.call req - else + else req.body = "" end end # @@ -542,10 +559,10 @@ opts[:query] = u.query else opts[:path], opts[:query] = location.split "?" end - if (authentication_is_on?(opts) and + if (authentication_is_on?(opts) and [ opts[:scheme], opts[:host] ] != prev_host) raise( "getting redirected to #{location} while " + "authentication is on. Stopping.")