~singpolyma/dhall-ruby

e4aeafb66074b9d15d90b13bf7d6d3f6590ffb0d — Stephen Paul Weber 2 years ago 8e01cc2
Follow redirects + timeout helper
2 files changed, 23 insertions(+), 22 deletions(-)

M lib/dhall/resolve.rb
M lib/dhall/util.rb
M lib/dhall/resolve.rb => lib/dhall/resolve.rb +11 -22
@@ 41,15 41,7 @@ module Dhall
					source.headers.to_a.map { |h|
						(h.fetch("header") { h.fetch("mapKey") }).to_s
					}.join(",")
				r = Net::HTTP.start(
					uri.hostname,
					uri.port,
					use_ssl:       uri.scheme == "https",
					open_timeout:  timeout,
					ssl_timeout:   timeout,
					read_timeout:  timeout,
					write_timeout: timeout
				) { |http| http.request(req) }
				r = Util.net_http_req_with_timeout(uri, req, timeout: timeout)

				raise ImportFailedException, source if r.code != "200"
				unless r["Access-Control-Allow-Origin"] == parent_origin ||


@@ 65,20 57,17 @@ module Dhall
					PreflightCORS.call(source, parent_origin)
					timeout = source.deadline.timeout
					uri = source.uri
					req = Net::HTTP::Get.new(uri)
					source.headers.each do |header|
						req[(header.fetch("header") { header.fetch("mapKey") }).to_s] =
							(header.fetch("value") { header.fetch("mapValue") }).to_s
					r = loop do
						req = Net::HTTP::Get.new(uri)
						source.headers.each do |header|
							req[(header.fetch("header") { header.fetch("mapKey") }).to_s] =
								(header.fetch("value") { header.fetch("mapValue") }).to_s
						end
						r = Util.net_http_req_with_timeout(uri, req, timeout: timeout)

						break r unless ["301", "302", "303", "307", "308"].include?(r.code)
						uri = URI(r["Location"])
					end
					r = Net::HTTP.start(
						uri.hostname,
						uri.port,
						use_ssl:       uri.scheme == "https",
						open_timeout:  timeout,
						ssl_timeout:   timeout,
						read_timeout:  timeout,
						write_timeout: timeout
					) { |http| http.request(req) }

					raise ImportFailedException, source if r.code != "200"
					r.body

M lib/dhall/util.rb => lib/dhall/util.rb +12 -0
@@ 200,5 200,17 @@ module Dhall
		def self.uri_escape(s)
			::URI.encode_www_form_component(s).gsub("+", "%20")
		end

		def self.net_http_req_with_timeout(uri, req, timeout:)
			Net::HTTP.start(
				uri.hostname,
				uri.port,
				use_ssl:       uri.scheme == "https",
				open_timeout:  timeout,
				ssl_timeout:   timeout,
				read_timeout:  timeout,
				write_timeout: timeout
			) { |http| http.request(req) }
		end
	end
end