class Down::Wget::Command

Constants

PIPE_BUFFER_SIZE

Public Class Methods

execute(arguments) click to toggle source
# File lib/down/wget.rb, line 137
def self.execute(arguments)
  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 153
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 202
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
# File lib/down/wget.rb, line 159
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

  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
terminate() click to toggle source
# File lib/down/wget.rb, line 192
def terminate
  begin
    Process.kill("TERM", @status_reaper[:pid])
  rescue Errno::ESRCH
    # process has already terminated
  end

  close
end