diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2006-07-24 12:32:53 +0000 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2006-07-24 12:32:53 +0000 |
commit | 2a87bed49614f7cc16d92eecb8ddb63f60ab15ce (patch) | |
tree | 53659f957f946428b68e753018fdd3c14fe1ef3e /lib/rbot/httputil.rb | |
parent | bca160cbbf64aeb56fb6729f2ea4d77e5821a31c (diff) |
httputil get/head/get_cached now also accept strings and not just URIs
Diffstat (limited to 'lib/rbot/httputil.rb')
-rw-r--r-- | lib/rbot/httputil.rb | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/lib/rbot/httputil.rb b/lib/rbot/httputil.rb index 19e2a31a..1a43c185 100644 --- a/lib/rbot/httputil.rb +++ b/lib/rbot/httputil.rb @@ -133,7 +133,13 @@ class HttpUtil # simple get request, returns (if possible) response body following redirs # and caching if requested # if a block is given, it yields the urls it gets redirected to - def get(uri, readtimeout=10, opentimeout=5, max_redir=@bot.config["http.max_redir"], cache=false) + def get(uri_or_str, readtimeout=10, opentimeout=5, max_redir=@bot.config["http.max_redir"], cache=false) + if uri_or_str.class <= URI + uri = uri_or_str + else + uri = URI.parse(uri_or_str.to_s) + end + proxy = get_proxy(uri) proxy.open_timeout = opentimeout proxy.read_timeout = readtimeout @@ -181,7 +187,13 @@ class HttpUtil end # just like the above, but only gets the head - def head(uri, readtimeout=10, opentimeout=5, max_redir=@bot.config["http.max_redir"]) + def head(uri_or_str, readtimeout=10, opentimeout=5, max_redir=@bot.config["http.max_redir"]) + if uri_or_str.class <= URI + uri = uri_or_str + else + uri = URI.parse(uri_or_str.to_s) + end + proxy = get_proxy(uri) proxy.open_timeout = opentimeout proxy.read_timeout = readtimeout @@ -215,9 +227,15 @@ class HttpUtil # gets a page from the cache if it's still (assumed to be) valid # TODO remove stale cached pages, except when called with noexpire=true - def get_cached(uri, readtimeout=10, opentimeout=5, + def get_cached(uri_or_str, readtimeout=10, opentimeout=5, max_redir=@bot.config['http.max_redir'], noexpire=@bot.config['http.no_expire_cache']) + if uri_or_str.class <= URI + uri = uri_or_str + else + uri = URI.parse(uri_or_str.to_s) + end + k = uri.to_s if !@cache.key?(k) remove_stale_cache unless noexpire @@ -239,8 +257,8 @@ class HttpUtil h = head(uri, readtimeout, opentimeout, max_redir) if h.key?('last-modified') if Time.httpdate(h['last-modified']) == @cache[k][:last_mod] - if resp.key?('date') - @cache[k][:last_use] = Time.httpdate(resp['date']) + if h.key?('date') + @cache[k][:last_use] = Time.httpdate(h['date']) else @cache[k][:last_use] = now end |