- head = http.request_head(url.path)
- case head
- when Net::HTTPRedirection then
- # call self recursively if this is a redirect
- redirect_to = head['location']
- puts "+ redirect location: #{redirect_to}"
- url = URI.join url.to_s, redirect_to
- puts "+ whee, redirecting to #{url.to_s}!"
- title = get_title_for_url(url.to_s)
- when Net::HTTPSuccess then
- if head['content-type'] =~ /^text\//
- # content is 'text/*'
- # retrieve the title from the page
- puts "+ getting #{url.path}"
- response = http.request_get(url.request_uri)
- return get_title_from_html(response.body)
+
+ http.request_get(url.path, "User-Agent" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)") { |response|
+
+ case response
+ when Net::HTTPRedirection, Net::HTTPMovedPermanently then
+ # call self recursively if this is a redirect
+ redirect_to = response['location'] || './'
+ debug "+ redirect location: #{redirect_to.inspect}"
+ url = URI.join url.to_s, redirect_to
+ debug "+ whee, redirecting to #{url.to_s}!"
+ return get_title_for_url(url.to_s, depth-1)
+ when Net::HTTPSuccess then
+ if response['content-type'] =~ /^text\//
+ # since the content is 'text/*' and is small enough to
+ # be a webpage, retrieve the title from the page
+ debug "+ getting #{url.request_uri}"
+ data = read_data_from_response(response, 50000)
+ return get_title_from_html(data)
+ else
+ # content doesn't have title, just display info.
+ size = response['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
+ return "[Link Info] type: #{response['content-type']}#{size ? ", size: #{size} bytes" : ""}"
+ end
+ when Net::HTTPClientError then
+ return "[Link Info] Error getting link (#{response.code} - #{response.message})"
+ when Net::HTTPServerError then
+ return "[Link Info] Error getting link (#{response.code} - #{response.message})"