title = nil
begin
- @bot.httputil.get_response(url) { |response|
- case response
- when Net::HTTPSuccess
- extra = String.new
-
- if response['content-type'] =~ /^text\//
-
- title = String.new
-
- # since the content is 'text/*' and is small enough to
- # be a webpage, retrieve the title from the page
- debug "+ getting #{url.request_uri}"
-
- # we act differently depending on whether we want the first par or not:
- # in the first case we download the initial part and the parse it; in the second
- # case we only download as much as we need to find the title
- if @bot.config['url.first_par']
- partial = response.partial_body(@bot.config['http.info_bytes'])
- first_par = Utils.ircify_first_html_par(partial)
- extra << "\n#{LINK_INFO} #{first_par}" unless first_par.empty?
- title = get_title_from_html(partial)
- if title
- return "title: #{title}#{extra}"
- end
- else
- response.partial_body(@bot.config['http.info_bytes']) { |part|
- title = get_title_from_html(part)
- return "title: #{title}" if title
- }
- end
- # if nothing was found, provide more basic info
- end
+ range = @bot.config['http.info_bytes']
+ response = @bot.httputil.get_response(url, :range => "bytes=0-#{range}")
+ if response.code != "206" && response.code != "200"
+ return "Error getting link (#{response.code} - #{response.message})"
+ end
+ extra = String.new
- debug response.to_hash.inspect
- unless @bot.config['url.titles_only']
- # content doesn't have title, just display info.
- size = response['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2') rescue nil
- size = size ? ", size: #{size} bytes" : ""
- return "type: #{response['content-type']}#{size}#{extra}"
- end
- when Net::HTTPResponse
- return "Error getting link (#{response.code} - #{response.message})"
+ if response['content-type'] =~ /^text\//
+
+ body = response.body.slice(0, range)
+ title = String.new
+
+ # since the content is 'text/*' and is small enough to
+ # be a webpage, retrieve the title from the page
+ debug "+ getting #{url.request_uri}"
+
+ title = get_title_from_html(body)
+ if @bot.config['url.first_par']
+ first_par = Utils.ircify_first_html_par(body, :strip => title)
+ extra << ", #{Bold}text#{Bold}: #{first_par}" unless first_par.empty?
+ return "#{Bold}title#{Bold}: #{title}#{extra}" if title
else
- raise response
+ return "#{Bold}title#{Bold}: #{title}" if title
end
- }
- rescue Object => e
- if e.class <= StandardError
- error e.inspect
- debug e.backtrace.join("\n")
+
+ # if nothing was found, provide more basic info
end
- msg = e.respond_to?(:message) ? e.message : e.to_s
+ debug response.to_hash.inspect
+ unless @bot.config['url.titles_only']
+ # content doesn't have title, just display info.
+ size = response['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2') rescue nil
+ if response.code == '206'
+ if response['content-range'] =~ /bytes\s*[^\/]+\/(\d+)/
+ size = $1.to_s.reverse.scan(/\d{1,3}/).join(',').reverse
+ end
+ end
+ size = size ? ", #{Bold}size#{Bold}: #{size} bytes" : ""
+ return "#{Bold}type#{Bold}: #{response['content-type']}#{size}#{extra}"
+ end
+ rescue Exception => e
+ error e.inspect
+ debug e.backtrace.join("\n")
return "Error connecting to site (#{e.message})"
end
end
begin
title = get_title_for_url urlstr
if title
- m.reply "#{LINK_INFO} #{title}"
+ m.reply "#{LINK_INFO} #{title}", :overlong => :truncate
debug "Title found!"
else
debug "Title not found!"