Skip to content

Instantly share code, notes, and snippets.

@ssig33
Created July 11, 2014 07:30
Show Gist options
  • Save ssig33/1cd00269ed2517365a18 to your computer and use it in GitHub Desktop.
Save ssig33/1cd00269ed2517365a18 to your computer and use it in GitHub Desktop.
content-type をリダイレクト考慮して取得
def head_and_track_redirect(uri, limit = 10)
raise if limit == 0
h = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
h.use_ssl = true
h.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
h.start{|http|
path = uri.path
path += "?#{uri.query}" unless uri.query.blank?
response = http.head(path)
case response
when Net::HTTPSuccess
return response
when Net::HTTPRedirection
head_and_track_redirect URI.parse(response['location']), limit-1
end
}
end
def check_mime_type(url)
uri = URI.parse url
response = head_and_track_redirect(uri)
return response['content-type']
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment