class Down::Wget::Command

Handles executing the wget command.

Constants

PIPE_BUFFER_SIZE

Public Class Methods

execute(arguments) click to toggle source
# File lib/down/wget.rb, line 148
def self.execute(arguments)
  # posix-spawn gem has better performance, so we use it if it's available
  if defined?(POSIX::Spawn)
    pid, stdin_pipe, stdout_pipe, stderr_pipe = POSIX::Spawn.popen4(*arguments)
    status_reaper = Process.detach(pid)
  else
    stdin_pipe, stdout_pipe, stderr_pipe, status_reaper = Open3.popen3(*arguments)
  end

  stdin_pipe.close
  [stdout_pipe, stderr_pipe].each(&:binmode)

  new(stdout_pipe, stderr_pipe, status_reaper)
rescue Errno::ENOENT
  raise Down::Error, "wget is not installed"
end
new(stdout_pipe, stderr_pipe, status_reaper) click to toggle source
# File lib/down/wget.rb, line 165
def initialize(stdout_pipe, stderr_pipe, status_reaper)
  @status_reaper = status_reaper
  @stdout_pipe   = stdout_pipe
  @stderr_pipe   = stderr_pipe
end

Public Instance Methods

close() click to toggle source
# File lib/down/wget.rb, line 198
def close
  @stdout_pipe.close unless @stdout_pipe.closed?
  @stderr_pipe.close unless @stderr_pipe.closed?
end
output() { |readpartial(PIPE_BUFFER_SIZE) until eof?| ... } click to toggle source

Yields chunks of stdout. At the end handles the exit status.

# File lib/down/wget.rb, line 172
def output
  # Keep emptying the stderr buffer, to allow the subprocess to send more
  # than 64KB if it wants to.
  stderr_reader = Thread.new { @stderr_pipe.read }

  yield @stdout_pipe.readpartial(PIPE_BUFFER_SIZE) until @stdout_pipe.eof?

  status = @status_reaper.value
  stderr = stderr_reader.value

  close

  handle_status(status, stderr)
end
terminate() click to toggle source
# File lib/down/wget.rb, line 187
def terminate
  begin
    Process.kill("TERM", @status_reaper[:pid])
    Process.waitpid(@status_reaper[:pid])
  rescue Errno::ESRCH
    # process has already terminated
  end

  close
end

Private Instance Methods

handle_status(status, stderr) click to toggle source

Translates nonzero wget exit statuses into exceptions.

# File lib/down/wget.rb, line 206
def handle_status(status, stderr)
  case status.exitstatus
  when 0  # No problems occurred
    # success
  when 1, # Generic error code
       2, # Parse error---for instance, when parsing command-line options, the .wgetrc or .netrc...
       3  # File I/O error
    raise Down::Error, stderr
  when 4  # Network failure
    raise Down::TimeoutError, stderr if stderr.include?("timed out")
    raise Down::ConnectionError, stderr
  when 5  # SSL verification failure
    raise Down::SSLError, stderr
  when 6  # Username/password authentication failure
    raise Down::ClientError, stderr
  when 7  # Protocol errors
    raise Down::Error, stderr
  when 8  # Server issued an error response
    raise Down::TooManyRedirects, stderr if stderr.include?("redirections exceeded")
    raise Down::ResponseError, stderr
  end
end