lib/kanoko/application/convert.rb in kanoko-0.3.2 vs lib/kanoko/application/convert.rb in kanoko-0.3.3
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'sinatra'
require 'net/http'
require 'tempfile'
require 'kanoko'
require 'mime/types'
@@ -55,13 +57,14 @@
env["REQUEST_URI"]
else
"#{request.path}#{request.params.empty? ? "" : "?#{request.query_string}"}"
end
request_params = raw_request_uri.split('/').tap(&:shift)
+ request_headers = request.env.select { |k, v| k.start_with?("HTTP_") }
hash = request_params.shift
unless 0 < request_params.length
- logger.error "invalid url #{request_uri}"
+ logger.error "invalid url #{raw_request_uri}"
return 400
end
list = Kanoko::Application::Convert::Function.list
convert_options = []
@@ -87,13 +90,12 @@
check_path = request_params.map { |i| URI.decode_www_form_component(i) }.join('/')
unless hash == Kanoko.make_hash(*arguments, check_path)
logger.error "hash check failed #{[*arguments, check_path]}"
return 400
end
-
src_path = request_params.join('/')
- res = http_get(URI.parse("#{(request.secure? ? 'https' : 'http')}://#{src_path}"))
+ res = http_get(URI.parse("#{(request.secure? ? 'https' : 'http')}://#{src_path}"), request_headers)
if res.nil?
return 404
end
after_response res
@@ -144,12 +146,20 @@
src_path,
dst_path,
].flatten
end
- def http_get(uri)
+ def http_get(uri, headers)
retries = 2
req = Net::HTTP::Get.new(uri.request_uri)
+ headers.each do |key, value|
+ case key
+ when "HTTP_HOST"
+ next
+ end
+ k = key.sub(/^HTTP_/, '')
+ req[k] = value if !req[k]
+ end
http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = 1
http.use_ssl = true if uri.scheme == 'https'
begin
res = http.start do |http|