Method: InspecPlugins::Compliance::HTTP.post_file

Defined in:
lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb

.post_file(url, headers, file_path, insecure) ⇒ Object

post a file



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/http.rb', line 47

def self.post_file(url, headers, file_path, insecure)
  uri = _parse_url(url)
  raise "Unable to parse URL: #{url}" if uri.nil? || uri.host.nil?
  http = Net::HTTP.new(uri.host, uri.port)

  # set connection flags
  http.use_ssl = (uri.scheme == 'https')
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if insecure

  req = Net::HTTP::Post.new(uri.path)
  headers.each do |key, value|
    req.add_field(key, value)
  end

  req.body_stream=File.open(file_path, 'rb')
  req.add_field('Content-Length', File.size(file_path))
  req.add_field('Content-Type', 'application/x-gzip')

  boundary = 'INSPEC-PROFILE-UPLOAD'
  req.add_field('session', boundary)
  res=http.request(req)
  res
end