Module: PatchFinder::Helper

Included in:
Engine::MSU::Google, Engine::MSU::Technet, MSU
Defined in:
lib/patch_finder/core/helper.rb

Defined Under Namespace

Classes: PatchFinderException

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/patch_finder/core/helper.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#download_file(uri, dest_dir) ⇒ void

Note:

When the file is saved, the file name will actually include a timestamp in this format: [original filename]_.[ext] to avoid name collision.

This method returns an undefined value.

Downloads a file to a local directory.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/patch_finder/core/helper.rb', line 102

def download_file(uri, dest_dir)
  begin
    u = URI.parse(uri)
    fname, ext = File.basename(u.path).scan(/(.+)\.(.+)/).flatten
    dest_file = File.join(dest_dir, "#{fname}_#{Time.now.to_i}.#{ext}")
    res = send_http_get_request(uri)
  rescue Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
         Errno::ECONNABORTED, Errno::EPIPE, Net::OpenTimeout,
         Errno::ETIMEDOUT => e
    print_error("#{e.message}: #{uri}")
    return
  end

  save_file(res.body, dest_file)
  print_status("Download completed for #{uri}")
end

#download_files(files, dest_dir) ⇒ Object

Note:

3 clients are used to download all the links.

Downloads multiple files to a local directory.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/patch_finder/core/helper.rb', line 126

def download_files(files, dest_dir)
  pool = PatchFinder::ThreadPool.new(3)

  files.each do |f|
    pool.schedule do
      download_file(f, dest_dir)
    end
  end

  pool.shutdown

  sleep(0.5) until pool.eop?
end

This method returns an undefined value.

Prints an error message.



31
32
33
# File 'lib/patch_finder/core/helper.rb', line 31

def print_error(msg = '')
  $stderr.puts "[ERROR] #{msg}"
end

This method returns an undefined value.

Prints a message.



38
39
40
# File 'lib/patch_finder/core/helper.rb', line 38

def print_line(msg = '')
  $stdout.puts msg
end

This method returns an undefined value.

Prints a status message.



24
25
26
# File 'lib/patch_finder/core/helper.rb', line 24

def print_status(msg = '')
  $stderr.puts "[*] #{msg}"
end

This method returns an undefined value.

Prints a message if verbose is set



13
14
15
# File 'lib/patch_finder/core/helper.rb', line 13

def print_verbose(msg = '')
  $stderr.puts "[*] #{msg}" if self.verbose
end


17
18
19
# File 'lib/patch_finder/core/helper.rb', line 17

def print_verbose_error(msg='')
  print_error(msge) if self.verbose
end

#read_file(file_path) ⇒ String

Returns the content of a file.



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/patch_finder/core/helper.rb', line 81

def read_file(file_path)
  return nil unless File.exist?(file_path)

  buf = ''

  File.open(file_path, 'rb') do |f|
    buf = f.read
  end

  buf
end

#send_http_get_request(uri, ssl = false) ⇒ Net::HTTPResponse

Note:

If the request fails, it will try 3 times before passing/raising an exception.

Sends an HTTP request.



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
# File 'lib/patch_finder/core/helper.rb', line 49

def send_http_get_request(uri, ssl = false)
  attempts = 1
  u = URI.parse(uri)
  res = nil
  ssl = u.scheme == 'https' ? true : false

  begin
    Net::HTTP.start(u.host, u.port, use_ssl: ssl) do |cli|
      req = Net::HTTP::Get.new(normalize_uri(u.request_uri))
      req['Host'] = u.host
      req['Content-Type'] = 'application/x-www-form-urlencoded'
      res = cli.request(req)
    end
  rescue Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
         Errno::ECONNABORTED, Errno::EPIPE, Net::OpenTimeout,
         Errno::ETIMEDOUT => e
    if attempts < 3
      sleep(5)
      attempts += 1
      retry
    else
      raise e
    end
  end

  res
end