inspec compliance: use multipart upload to support upload on windows (#3369)

Signed-off-by: Christoph Hartmann <chris@lollyrock.com>
This commit is contained in:
Christoph Hartmann 2018-09-07 20:25:31 +02:00 committed by Clinton Wolfe
parent d6110ed162
commit df9036f6e4
3 changed files with 11 additions and 18 deletions

View file

@ -40,4 +40,5 @@ Gem::Specification.new do |spec|
spec.add_dependency 'parslet', '~> 1.5' spec.add_dependency 'parslet', '~> 1.5'
spec.add_dependency 'semverse' spec.add_dependency 'semverse'
spec.add_dependency 'htmlentities' spec.add_dependency 'htmlentities'
spec.add_dependency 'multipart-post'
end end

View file

@ -46,4 +46,5 @@ Gem::Specification.new do |spec|
spec.add_dependency 'parslet', '~> 1.5' spec.add_dependency 'parslet', '~> 1.5'
spec.add_dependency 'semverse' spec.add_dependency 'semverse'
spec.add_dependency 'htmlentities' spec.add_dependency 'htmlentities'
spec.add_dependency 'multipart-post'
end end

View file

@ -3,6 +3,7 @@
# author: Dominik Richter # author: Dominik Richter
require 'net/http' require 'net/http'
require 'net/http/post/multipart'
require 'uri' require 'uri'
module Compliance module Compliance
@ -60,7 +61,7 @@ module Compliance
req.body_stream=File.open(file_path, 'rb') req.body_stream=File.open(file_path, 'rb')
req.add_field('Content-Length', File.size(file_path)) req.add_field('Content-Length', File.size(file_path))
req.add_field('Content-Type', 'application/x-gtar') req.add_field('Content-Type', 'application/x-gzip')
boundary = 'INSPEC-PROFILE-UPLOAD' boundary = 'INSPEC-PROFILE-UPLOAD'
req.add_field('session', boundary) req.add_field('session', boundary)
@ -77,24 +78,14 @@ module Compliance
http.use_ssl = (uri.scheme == 'https') http.use_ssl = (uri.scheme == 'https')
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if insecure http.verify_mode = OpenSSL::SSL::VERIFY_NONE if insecure
req = Net::HTTP::Post.new(uri) File.open(file_path) do |tar|
headers.each do |key, value| req = Net::HTTP::Post::Multipart.new(uri, 'file' => UploadIO.new(tar, 'application/x-gzip', File.basename(file_path)))
req.add_field(key, value) headers.each do |key, value|
req.add_field(key, value)
end
res = http.request(req)
return res
end end
boundry = 'AaB03x'
req.add_field('Content-Type', "multipart/form-data; boundary=#{boundry}")
post_body = []
post_body << "--#{boundry}\r\n"
post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{File.basename(file_path)}\"\r\n"
post_body << "Content-Type: application/x-gtar\r\n\r\n"
post_body << File.read(file_path)
post_body << "\r\n\r\n--#{boundry}--\r\n"
req.body = post_body.join
res=http.request(req)
res
end end
# sends a http requests # sends a http requests