lib/link_thumbnailer/processor.rb in link_thumbnailer-2.5.0 vs lib/link_thumbnailer/processor.rb in link_thumbnailer-2.5.1

- old
+ new

@@ -13,18 +13,18 @@ @http = ::Net::HTTP::Persistent.new super(config) end - def call(url = '', redirect_count = 0) + def call(url = '', redirect_count = 0, headers = {}) self.url = url @redirect_count = redirect_count raise ::LinkThumbnailer::RedirectLimit if too_many_redirections? with_valid_url do - set_http_headers + set_http_headers(headers) set_http_options perform_request end end @@ -33,49 +33,62 @@ def with_valid_url raise ::LinkThumbnailer::BadUriFormat unless valid_url_format? yield if block_given? end - def set_http_headers + def set_http_headers(headers = {}) + headers.each { |k, v| http.headers[k] = v } http.headers['User-Agent'] = user_agent http.override_headers['Accept-Encoding'] = 'none' end def set_http_options http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE unless ssl_required? - http.open_timeout = http_timeout + http.open_timeout = http_open_timeout + http.read_timeout = http_read_timeout http.proxy = :ENV end def perform_request - response = http.request(url) + response = http.request(url) + headers = {} + headers['Cookie'] = response['Set-Cookie'] if response['Set-Cookie'].present? + case response when ::Net::HTTPSuccess then response.body when ::Net::HTTPRedirection - call resolve_relative_url(response['location']), redirect_count + 1 + call( + resolve_relative_url(response['location']), + redirect_count + 1, + headers + ) else response.error! end end def resolve_relative_url(location) location.start_with?('http') ? location : build_absolute_url_for(location) end def build_absolute_url_for(relative_url) - URI("#{url.scheme}://#{url.host}#{relative_url}") + ::URI.parse("#{url.scheme}://#{url.host}#{relative_url}") end def redirect_limit config.redirect_limit end def user_agent config.user_agent end - def http_timeout - config.http_timeout + def http_open_timeout + config.http_open_timeout + end + + def http_read_timeout + config.http_read_timeout end def ssl_required? config.verify_ssl end