11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/databricks_sql/external_link_handler.rb', line 11
def fetch_and_parse(url, timeout: 30, require_https: true, allowed_hosts: nil)
uri = URI.parse(url)
validate_external_uri!(uri, require_https: require_https, allowed_hosts: allowed_hosts)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
http.open_timeout = timeout
http.read_timeout = timeout
response = http.request(Net::HTTP::Get.new(uri.request_uri))
validate_download_response!(response)
parse_body(response.body, response["Content-Type"], url)
rescue Timeout::Error, Net::OpenTimeout, Net::ReadTimeout
raise TimeoutError.new("Timed out downloading external link after #{timeout}s", timeout)
rescue SocketError => e
raise ConnectionError, "Unable to connect to external link host: #{e.message}"
rescue URI::InvalidURIError => e
raise ConfigurationError, "Invalid external link URL: #{e.message}"
end
|