require"net/http"unlessdefined?(Net::HTTP)require"net/http/post/multipart"require"uri"unlessdefined?(URI)moduleInspecPluginsmoduleCompliance# implements a simple http abstraction on top of Net::HTTPclassHTTP# generic get requiresdefself.get(url,headers=nil,insecure)uri=_parse_url(url)req=Net::HTTP::Get.new(uri.path)headers&.eachdo|key,value|req.add_field(key,value)endsend_request(uri,req,insecure)end# generic post requestdefself.post(url,token,insecure,basic_auth=false)# form requesturi=_parse_url(url)req=Net::HTTP::Post.new(uri.path)ifbasic_authreq.basic_authtoken,""elsereq["Authorization"]="Bearer #{token}"endreq.form_data={}send_request(uri,req,insecure)enddefself.post_with_headers(url,headers,body,insecure)uri=_parse_url(url)req=Net::HTTP::Post.new(uri.path)req.body=bodyunlessbody.nil?headers&.eachdo|key,value|req.add_field(key,value)endsend_request(uri,req,insecure)end# post a filedefself.post_file(url,headers,file_path,insecure)uri=_parse_url(url)raise"Unable to parse URL: #{url}"ifuri.nil?||uri.host.nil?http=Net::HTTP.new(uri.host,uri.port)# set connection flagshttp.use_ssl=(uri.scheme=="https")http.verify_mode=OpenSSL::SSL::VERIFY_NONEifinsecurereq=Net::HTTP::Post.new(uri.path)headers.eachdo|key,value|req.add_field(key,value)endreq.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)resenddefself.post_multipart_file(url,headers,file_path,insecure)uri=_parse_url(url)raise"Unable to parse URL: #{url}"ifuri.nil?||uri.host.nil?http=Net::HTTP.new(uri.host,uri.port)# set connection flagshttp.use_ssl=(uri.scheme=="https")http.verify_mode=OpenSSL::SSL::VERIFY_NONEifinsecureFile.open(file_path)do|tar|req=Net::HTTP::Post::Multipart.new(uri,"file"=>UploadIO.new(tar,"application/x-gzip",File.basename(file_path)))headers.eachdo|key,value|req.add_field(key,value)endres=http.request(req)returnresendend# sends a http requestsdefself.send_request(uri,req,insecure)opts={use_ssl: uri.scheme=="https",}opts[:verify_mode]=OpenSSL::SSL::VERIFY_NONEifinsecureraise"Unable to parse URI: #{uri}"ifuri.nil?||uri.host.nil?res=Net::HTTP.start(uri.host,uri.port,opts)do|http|http.request(req)endresrescueOpenSSL::SSL::SSLError=>eraiseeunlesse.message.include?"certificate verify failed"puts"Error: Failed to connect to #{uri}."puts"If the server uses a self-signed certificate, please re-run the login command with the --insecure option."exit1enddefself._parse_url(url)url="https://#{url}"ifURI.parse(url).scheme.nil?URI.parse(url)endendendend