1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
require 'set'
module Typhoeus
# This is a Factory for easies to be used in the hydra.
# Before an easy is ready to be added to a multi the
# on_complete callback to be set.
# This is done by this class.
#
# @api private
class EasyFactory
RENAMED_OPTIONS = {
:auth_method => :httpauth,
:connect_timeout => :connecttimeout,
:encoding => :accept_encoding,
:follow_location => :followlocation,
:max_redirects => :maxredirs,
:proxy_type => :proxytype,
:ssl_cacert => :cainfo,
:ssl_capath => :capath,
:ssl_cert => :sslcert,
:ssl_cert_type => :sslcerttype,
:ssl_key => :sslkey,
:ssl_key_password => :keypasswd,
:ssl_key_type => :sslkeytype,
:ssl_version => :sslversion,
}
CHANGED_OPTIONS = {
:disable_ssl_host_verification => :ssl_verifyhost,
:disable_ssl_peer_verification => :ssl_verifypeer,
:proxy_auth_method => :proxyauth,
}
REMOVED_OPTIONS = Set.new([:cache_key_basis, :cache_timeout, :user_agent])
SANITIZE_IGNORE = Set.new([:method, :cache_ttl, :cache])
SANITIZE_TIMEOUT = Set.new([:timeout_ms, :connecttimeout_ms])
# Returns the request provided.
#
# @return [ Typhoeus::Request ]
attr_reader :request
# Returns the hydra provided.
#
# @return [ Typhoeus::Hydra ]
attr_reader :hydra
# Create an easy factory.
#
# @example Create easy factory.
# Typhoeus::Hydra::EasyFactory.new(request, hydra)
#
# @param [ Request ] request The request to build an easy for.
# @param [ Hydra ] hydra The hydra to build an easy for.
def initialize(request, hydra = nil)
@request = request
@hydra = hydra
end
# Return the easy in question.
#
# @example Return easy.
# easy_factory.easy
#
# @return [ Ethon::Easy ] The easy.
def easy
@easy ||= Typhoeus::Pool.get
end
# Fabricated easy.
#
# @example Prepared easy.
# easy_factory.get
#
# @return [ Ethon::Easy ] The easy.
def get
begin
easy.http_request(
request.base_url.to_s,
request.options.fetch(:method, :get),
sanitize(request.options)
)
rescue Ethon::Errors::InvalidOption => e
help = provide_help(e.message.match(/:\s(\w+)/)[1])
raise $!, "#{$!}#{help}", $!.backtrace
end
set_callback
easy
end
private
def sanitize(options)
# set nosignal to true by default
# this improves thread safety and timeout behavior
sanitized = {:nosignal => true}
options.each do |k,v|
s = k.to_sym
next if SANITIZE_IGNORE.include?(s)
if new_option = RENAMED_OPTIONS[k.to_sym]
warn("Deprecated option #{k}. Please use #{new_option} instead.")
sanitized[new_option] = v
# sanitize timeouts
elsif SANITIZE_TIMEOUT.include?(s)
if !v.integer?
warn("Value '#{v}' for option '#{k}' must be integer.")
end
sanitized[k] = v.ceil
else
sanitized[k] = v
end
end
sanitize_timeout!(sanitized, :timeout)
sanitize_timeout!(sanitized, :connecttimeout)
sanitized
end
def sanitize_timeout!(options, timeout)
timeout_ms = :"#{timeout}_ms"
if options[timeout] && options[timeout].round != options[timeout]
if !options[timeout_ms]
options[timeout_ms] = (options[timeout]*1000).ceil
end
options[timeout] = options[timeout].ceil
end
options
end
# Sets on_complete callback on easy in order to be able to
# track progress.
#
# @example Set callback.
# easy_factory.set_callback
#
# @return [ Ethon::Easy ] The easy.
def set_callback
if request.streaming?
response = nil
easy.on_headers do |easy|
response = Response.new(Ethon::Easy::Mirror.from_easy(easy).options)
request.execute_headers_callbacks(response)
end
request.on_body.each do |callback|
easy.on_body do |chunk, easy|
callback.call(chunk, response)
end
end
else
easy.on_headers do |easy|
request.execute_headers_callbacks(Response.new(Ethon::Easy::Mirror.from_easy(easy).options))
end
end
request.on_progress.each do |callback|
easy.on_progress do |dltotal, dlnow, ultotal, ulnow, easy|
callback.call(dltotal, dlnow, ultotal, ulnow, response)
end
end
easy.on_complete do |easy|
request.finish(Response.new(easy.mirror.options))
Typhoeus::Pool.release(easy)
if hydra && !hydra.queued_requests.empty?
hydra.dequeue_many
end
end
end
def provide_help(option)
if new_option = CHANGED_OPTIONS[option.to_sym]
"\nPlease try #{new_option} instead of #{option}." if new_option
elsif REMOVED_OPTIONS.include?(option.to_sym)
"\nThe option #{option} was removed."
end
end
end
end
|