require 'open-uri' module Uricp::Strategy class PipedRemoteGet include Uricp::Strategy::Common def appropriate? case from.scheme when 'http', 'https' return proposal unless sequence_complete? else debug "#{self.class.name}: not appropriate" false end end alias command curl_download_to_pipe def proposal @proposed_options = options.dup @proposed_options['from_uri'] = PIPE_URI if conversion_required? @proposed_options['source-format'] = format_peek if @proposed_options['source-format'] == @proposed_options['target-format'] @proposed_options.delete('source-format') @proposed_options.delete('target-format') end end if options['max-cache'] && size_peek.to_i > options['max-cache'].to_i @proposed_options.delete('cache') @proposed_options.delete('cache_name') @proposed_options.delete('max-cache') end self end def format_peek return options['target-format'] || 'raw' if dry_run? options['from_uri'].open(headers) do |u| encoding(u) end rescue OpenURI::HTTPError => e case e.io.status[0] when '416' 'raw' else raise end rescue SocketError => e raise SocketError, options['from_uri'].to_s + ' inaccessible: ' + e.message end def size_peek return options['max-cache'] if dry_run? size_headers = headers size_headers['Range'] = 'bytes=0-0' options['from_uri'].open(headers) do |u| match = %r{bytes\s+(\d+)-(\d+)/(\d+|\*)}i.match(u.meta['content-range']) match && match[3].to_i end rescue OpenURI::HTTPError => e case e.io.status[0] when '416' 0 else raise end rescue SocketError => e raise SocketError, options['from_uri'].to_s + ' inaccessible: ' + e.message end def headers headers = { 'Range' => 'bytes=0-7' } headers['X-Auth-Token'] = options['authenticator'].call if http_authentication? headers end end end