class Inspec::Resources::Http::Worker::Remote

def body

def body
  run_curl
  @body&.strip
end

def curl_command # rubocop:disable Metrics/AbcSize

rubocop:disable Metrics/AbcSize
def curl_command # rubocop:disable Metrics/AbcSize
  cmd = ['curl -i']
  # Use curl's --head option when the method requested is HEAD. Otherwise,
  # the user may experience a timeout when curl does not properly close
  # the connection after the response is received.
  if http_method.casecmp('HEAD') == 0
    cmd << '--head'
  else
    cmd << "-X #{http_method}"
  end
  cmd << "--connect-timeout #{open_timeout}"
  cmd << "--max-time #{open_timeout+read_timeout}"
  cmd << "--user \'#{username}:#{password}\'" unless username.nil? || password.nil?
  cmd << '--insecure' unless ssl_verify?
  cmd << "--data #{Shellwords.shellescape(request_body)}" unless request_body.nil?
  request_headers.each do |k, v|
    cmd << "-H '#{k}: #{v}'"
  end
  if params.nil?
    cmd << "'#{url}'"
  else
    cmd << "'#{url}?#{params.map { |e| e.join('=') }.join('&')}'"
  end
  cmd.join(' ')
end

def initialize(inspec, http_method, url, opts)

def initialize(inspec, http_method, url, opts)
  unless inspec.command('curl').exist?
    raise Inspec::Exceptions::ResourceSkipped,
          'curl is not available on the target machine'
  end
  @ran_curl = false
  @inspec = inspec
  super(http_method, url, opts)
end

def response_headers

def response_headers
  run_curl
  @response_headers
end

def run_curl

def run_curl
  return if @ran_curl
  cmd_result = inspec.command(curl_command)
  response = cmd_result.stdout
  @ran_curl = true
  return if response.nil? || cmd_result.exit_status != 0
  # strip any carriage returns to normalize output
  response.delete!("\r")
  # split the prelude (status line and headers) and the body
  prelude, @body = response.split("\n\n", 2)
  prelude = prelude.lines
  # grab the status off of the first line of the prelude
  status_line = prelude.shift
  @status = status_line.split(' ', 3)[1].to_i
  # parse the rest of the prelude which will be all the HTTP headers
  @response_headers = {}
  prelude.each do |line|
    line.strip!
    key, value = line.split(':', 2)
    @response_headers[key] = value.strip
  end
end

def status

def status
  run_curl
  @status
end