Created
July 11, 2014 07:30
-
-
Save ssig33/1cd00269ed2517365a18 to your computer and use it in GitHub Desktop.
content-type をリダイレクト考慮して取得
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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