Sha256: 02fcb8895c9142f60e29d720c4e2dfc67393bd07fbf49f0bab00368ea9f49fc0

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

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
      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
      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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
uricp-0.0.15 lib/uricp/strategy/piped_remote_get.rb
uricp-0.0.14 lib/uricp/strategy/piped_remote_get.rb
uricp-0.0.13 lib/uricp/strategy/piped_remote_get.rb
uricp-0.0.12 lib/uricp/strategy/piped_remote_get.rb