Class: Down::NetHttp

Inherits:
Backend show all
Defined in:
lib/down/net_http.rb

Defined Under Namespace

Modules: DownloadedFile

Instance Method Summary collapse

Methods inherited from Backend

download, open

Constructor Details

#initialize(options = {}) ⇒ NetHttp

Returns a new instance of NetHttp.



14
15
16
17
18
19
20
21
# File 'lib/down/net_http.rb', line 14

def initialize(options = {})
  @options = {
    "User-Agent" => "Down/#{Down::VERSION}",
    max_redirects: 2,
    open_timeout:  30,
    read_timeout:  30,
  }.merge(options)
end

Instance Method Details

#download(url, options = {}) ⇒ Object



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
# File 'lib/down/net_http.rb', line 23

def download(url, options = {})
  options = @options.merge(options)

  max_size            = options.delete(:max_size)
  max_redirects       = options.delete(:max_redirects)
  progress_proc       = options.delete(:progress_proc)
  content_length_proc = options.delete(:content_length_proc)
  destination         = options.delete(:destination)

  open_uri_options = {
    content_length_proc: proc { |size|
      if size && max_size && size > max_size
        raise Down::TooLarge, "file is too large (max is #{max_size/1024/1024}MB)"
      end
      content_length_proc.call(size) if content_length_proc
    },
    progress_proc: proc { |current_size|
      if max_size && current_size > max_size
        raise Down::TooLarge, "file is too large (max is #{max_size/1024/1024}MB)"
      end
      progress_proc.call(current_size) if progress_proc
    },
    redirect: false,
  }

  if options[:proxy]
    proxy    = URI(options.delete(:proxy))
    user     = proxy.user
    password = proxy.password

    if user || password
      proxy.user     = nil
      proxy.password = nil

      open_uri_options[:proxy_http_basic_authentication] = [proxy.to_s, user, password]
    else
      open_uri_options[:proxy] = proxy.to_s
    end
  end

  open_uri_options.merge!(options)

  uri = ensure_uri(url)

  if uri.user || uri.password
    open_uri_options[:http_basic_authentication] ||= [uri.user, uri.password]
    uri.user = nil
    uri.password = nil
  end

  open_uri_file = open_uri(uri, open_uri_options, follows_remaining: max_redirects)

  tempfile = ensure_tempfile(open_uri_file, File.extname(open_uri_file.base_uri.path))
  OpenURI::Meta.init tempfile, open_uri_file # add back open-uri methods
  tempfile.extend Down::NetHttp::DownloadedFile

  download_result(tempfile, destination)
end

#open(url, options = {}) ⇒ Object



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
# File 'lib/down/net_http.rb', line 82

def open(url, options = {})
  options = @options.merge(options)

  uri = ensure_uri(url)

  request = Fiber.new do
    net_http_request(uri, options) do |response|
      Fiber.yield response
    end
  end

  response = request.resume

  response_error!(response) unless response.is_a?(Net::HTTPSuccess)

  Down::ChunkedIO.new(
    chunks:     enum_for(:stream_body, response),
    size:       response["Content-Length"] && response["Content-Length"].to_i,
    encoding:   response.type_params["charset"],
    rewindable: options.fetch(:rewindable, true),
    on_close:   -> { request.resume }, # close HTTP connnection
    data: {
      status:   response.code.to_i,
      headers:  response.each_header.inject({}) { |headers, (downcased_name, value)|
                  name = downcased_name.split("-").map(&:capitalize).join("-")
                  headers.merge!(name => value)
                },
      response: response,
    },
  )
end