diff --git a/lib/ethon/easy.rb b/lib/ethon/easy.rb index 69fcdc1..1320707 100644 --- a/lib/ethon/easy.rb +++ b/lib/ethon/easy.rb @@ -229,10 +229,11 @@ def initialize(options = {}) # @see initialize def set_attributes(options) options.each_pair do |key, value| - unless respond_to?("#{key}=") + method = "#{key}=" + unless respond_to?(method) raise Errors::InvalidOption.new(key) end - method("#{key}=").call(value) + send(method, value) end end @@ -244,7 +245,7 @@ def set_attributes(options) def reset @url = nil @hash = nil - @on_complete = [] + @on_complete = nil Curl.easy_reset(handle) set_callbacks end @@ -278,7 +279,7 @@ def to_hash :response_body => response_body } Easy::Informations::AVAILABLE_INFORMATIONS.keys.each do |info| - @hash[info] = method(info).call + @hash[info] = send(info) end @hash end @@ -296,7 +297,7 @@ def log_inspect :return_code => return_code, :total_time => total_time } - "EASY #{hash.map{|k, v| "#{k}=#{v}"}.flatten.join(' ')}" + "EASY #{hash.map!{|k, v| "#{k}=#{v}"}.flatten.join(' ')}" end end end diff --git a/lib/ethon/easy/informations.rb b/lib/ethon/easy/informations.rb index 7c3a9c9..a282a3a 100644 --- a/lib/ethon/easy/informations.rb +++ b/lib/ethon/easy/informations.rb @@ -66,9 +66,7 @@ module Informations } AVAILABLE_INFORMATIONS.each do |name, type| - define_method(name) do - Curl.method("get_info_#{type}").call(name, handle) - end + eval %Q|def #{name}; Curl.send(:get_info_#{type}, :#{name}, handle); end| end # Returns this curl version supports zlib. diff --git a/lib/ethon/easy/operations.rb b/lib/ethon/easy/operations.rb index 3308462..df2127d 100644 --- a/lib/ethon/easy/operations.rb +++ b/lib/ethon/easy/operations.rb @@ -22,7 +22,7 @@ def handle def perform @return_code = Curl.easy_perform(handle) complete - Ethon.logger.debug("ETHON: performed #{self.log_inspect}") + Ethon.logger.debug { "ETHON: performed #{self.log_inspect}" } @return_code end diff --git a/lib/ethon/easy/options.rb b/lib/ethon/easy/options.rb index f635f44..32743c0 100644 --- a/lib/ethon/easy/options.rb +++ b/lib/ethon/easy/options.rb @@ -522,11 +522,29 @@ def postfieldsize=(value) Curl.set_option(:postfieldsize, value_for(value, :int), handle) end + # Pass a bitmask to control how libcurl acts on redirects after + # POSTs that get a 301, 302 or 303 response back. A parameter + # with bit 0 set (value CURL_REDIR_POST_301) tells the library + # to respect RFC 2616/10.3.2 and not convert POST requests into + # GET requests when following a 301 redirection. Setting bit 1 + # (value CURL_REDIR_POST_302) makes libcurl maintain the request + # method after a 302 redirect whilst setting bit 2 (value + # CURL_REDIR_POST_303) makes libcurl maintain the request method + # after a 303 redirect. The value CURL_REDIR_POST_ALL is a + # convenience define that sets all three bits. + # + # The non-RFC behaviour is ubiquitous in web browsers, so the + # library does the conversion by default to maintain + # consistency. However, a server may require a POST to remain a + # POST after such a redirection. This option is meaningful only + # when setting CURLOPT_FOLLOWLOCATION. (Added in 7.17.1) (This + # option was known as CURLOPT_POST301 up to 7.19.0 as it only + # supported the 301 then) + # + # @example Set postredir option. + # easy.postredir = :post_all # - # @example Set protocols option. - # easy.protocols = :http - # - # @param [ Symbol ] value The value or array of values to set. + # @param [ Symbol ] value The value to set. # # @return [ void ] def postredir=(value) diff --git a/lib/ethon/easy/queryable.rb b/lib/ethon/easy/queryable.rb index 664996c..1d6ccf5 100644 --- a/lib/ethon/easy/queryable.rb +++ b/lib/ethon/easy/queryable.rb @@ -27,12 +27,14 @@ def empty? # # @return [ String ] The string representation. def to_s - query_pairs.map{ |pair| + @to_s ||= query_pairs.map{ |pair| return pair if pair.is_a?(String) - pair.map{ |e| - escape && @easy ? @easy.escape(e.to_s) : e - }.join("=") + if escape && @easy + pair.map{ |e| @easy.escape(e.to_s) }.join("=") + else + pair.join("=") + end }.join('&') end @@ -59,21 +61,7 @@ def build_query_pairs(hash) return [hash] if hash.is_a?(String) pairs = [] - recursive = Proc.new do |h, prefix| - case h - when Hash - h.each_pair do |k,v| - key = prefix == '' ? k : "#{prefix}[#{k}]" - pairs_for(v, key, pairs, recursive) - end - when Array - h.each_with_index do |v, i| - key = "#{prefix}[#{i}]" - pairs_for(v, key, pairs, recursive) - end - end - end - recursive.call(hash, '') + recursively_generate_pairs(hash, nil, pairs) pairs end @@ -97,12 +85,27 @@ def file_info(file) private - def pairs_for(v, key, pairs, recursive) + def recursively_generate_pairs(h, prefix, pairs) + case h + when Hash + h.each_pair do |k,v| + key = prefix.nil? ? k : "#{prefix}[#{k}]" + pairs_for(v, key, pairs) + end + when Array + h.each_with_index do |v, i| + key = "#{prefix}[#{i}]" + pairs_for(v, key, pairs) + end + end + end + + def pairs_for(v, key, pairs) case v when Hash - recursive.call(v, key) + recursively_generate_pairs(v, key, pairs) when Array - recursive.call(v, key) + recursively_generate_pairs(v, key, pairs) when File, Tempfile pairs << [Util.escape_zero_byte(key), file_info(v)] else diff --git a/lib/ethon/easy/response_callbacks.rb b/lib/ethon/easy/response_callbacks.rb index b41f572..f36521e 100644 --- a/lib/ethon/easy/response_callbacks.rb +++ b/lib/ethon/easy/response_callbacks.rb @@ -40,7 +40,7 @@ def on_complete(&block) # request.complete def complete if defined?(@on_complete) - @on_complete.map{ |callback| callback.call(self) } + @on_complete.each{ |callback| callback.call(self) } end end end diff --git a/lib/ethon/multi/operations.rb b/lib/ethon/multi/operations.rb index a562a8b..0acbcf5 100644 --- a/lib/ethon/multi/operations.rb +++ b/lib/ethon/multi/operations.rb @@ -2,6 +2,9 @@ module Ethon class Multi # :nodoc # This module contains logic to run a multi. module Operations + STARTED_MULTI = "ETHON: started MULTI" + PERFORMED_MULTI = "ETHON: performed MULTI" + # Return the multi handle. Inititialize multi handle, # in case it didn't happened already. # @@ -35,7 +38,7 @@ def init_vars # @example Perform multi. # multi.perform def perform - Ethon.logger.debug("ETHON: started MULTI") + Ethon.logger.debug(STARTED_MULTI) while ongoing? run timeout = get_timeout @@ -43,7 +46,7 @@ def perform reset_fds set_fds(timeout) end - Ethon.logger.debug("ETHON: performed MULTI") + Ethon.logger.debug(PERFORMED_MULTI) nil end @@ -140,7 +143,7 @@ def check next if msg[:code] != :done easy = easy_handles.find{ |e| e.handle == msg[:easy_handle] } easy.return_code = msg[:data][:code] - Ethon.logger.debug("ETHON: performed #{easy.log_inspect}") + Ethon.logger.debug { "ETHON: performed #{easy.log_inspect}" } delete(easy) easy.complete end