Class: Irc::Utils::HttpUtil

Inherits:
Object show all
Defined in:
lib/rbot/core/utils/httputil.rb

Overview

class for making http requests easier (mainly for plugins to use) this class can check the bot proxy configuration to determine if a proxy needs to be used, which includes support for per-url proxy configuration.

Defined Under Namespace

Classes: CachedObject

Instance Method Summary collapse

Constructor Details

#initialize(bot) ⇒ HttpUtil

Create the HttpUtil instance, associating it with Bot bot



291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/rbot/core/utils/httputil.rb', line 291

def initialize(bot)
  @bot = bot
  @cache = Hash.new
  @headers = {
    'Accept-Charset' => 'utf-8;q=1.0, *;q=0.8',
    'Accept-Encoding' => 'gzip;q=1, deflate;q=1, identity;q=0.8, *;q=0.2',
    'User-Agent' =>
      "rbot http util #{$version} (#{Irc::Bot::SOURCE_URL})"
  }
  debug "starting http cache cleanup timer"
  @timer = @bot.timer.add(300) {
    self.remove_stale_cache unless @bot.config['http.no_expire_cache']
  }
end

Instance Method Details

#cleanupObject

Clean up on HttpUtil unloading, by stopping the cache cleanup timer.



307
308
309
310
# File 'lib/rbot/core/utils/httputil.rb', line 307

def cleanup
  debug 'stopping http cache cleanup timer'
  @bot.timer.remove(@timer)
end

#get(uri, options = {}, &block) ⇒ Object

uri

uri to query (URI object or String)

Simple GET request, returns (if possible) response body following redirs and caching if requested, yielding the actual response(s) to the optional block. See get_response for details on the supported options



601
602
603
604
605
606
607
608
609
610
611
# File 'lib/rbot/core/utils/httputil.rb', line 601

def get(uri, options = {}, &block) # :yields: resp
  begin
    resp = get_response(uri, options, &block)
    raise "http error: #{resp}" unless Net::HTTPOK === resp ||
      Net::HTTPPartialContent === resp
    return resp.body
  rescue Exception => e
    error e
  end
  return nil
end

#get_partial(uri, nbytes = , options = {}, &block) ⇒ Object

uri

uri to query (URI object or String)

nbytes

number of bytes to get

Partial GET request, returns (if possible) the first nbytes bytes of the response body, following redirs and caching if requested, yielding the actual response(s) to the optional block. See get_response for details on the supported options



659
660
661
662
# File 'lib/rbot/core/utils/httputil.rb', line 659

def get_partial(uri, nbytes = @bot.config['http.info_bytes'], options = {}, &block) # :yields: resp
  opts = {:range => "bytes=0-#{nbytes}"}.merge(options)
  return get(uri, opts, &block)
end

#get_proxy(uri, options = {}) ⇒ Object

uri

URI to create a proxy for

Return a net/http Proxy object, configured for proxying based on the bot’s proxy configuration. See proxy_required for more details on this.



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/rbot/core/utils/httputil.rb', line 362

def get_proxy(uri, options = {})
  opts = {
    :read_timeout => @bot.config["http.read_timeout"],
    :open_timeout => @bot.config["http.open_timeout"]
  }.merge(options)

  proxy = nil
  proxy_host = nil
  proxy_port = nil
  proxy_user = nil
  proxy_pass = nil

  if @bot.config["http.use_proxy"]
    if (ENV['http_proxy'])
      proxy = URI.parse ENV['http_proxy'] rescue nil
    end
    if (@bot.config["http.proxy_uri"])
      proxy = URI.parse @bot.config["http.proxy_uri"] rescue nil
    end
    if proxy
      debug "proxy is set to #{proxy.host} port #{proxy.port}"
      if proxy_required(uri)
        proxy_host = proxy.host
        proxy_port = proxy.port
        proxy_user = @bot.config["http.proxy_user"]
        proxy_pass = @bot.config["http.proxy_pass"]
      end
    end
  end

  h = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_pass)
  h.use_ssl = true if uri.scheme == "https"

  h.read_timeout = opts[:read_timeout]
  h.open_timeout = opts[:open_timeout]
  return h
end

#get_response(uri_or_s, options = {}, &block) ⇒ Object

uri

uri to query (URI object or String)

Generic http transaction method. It will return a Net::HTTPResponse object or raise an exception

If a block is given, it will yield the response (see :yield option)

Currently supported options:

method

request method [:get (default), :post or :head]

open_timeout

open timeout for the proxy

read_timeout

read timeout for the proxy

cache

should we cache results?

yield

if :final [default], calls the block for the response object; if :all, call the block for all intermediate redirects, too

max_redir

how many redirects to follow before raising the exception if -1, don’t follow redirects, just return them

range

make a ranged request (usually GET). accepts a string for HTTP/1.1 “Range:” header (i.e. “bytes=0-1000”)

body

request body (usually for POST requests)

headers

additional headers to be set for the request. Its value must be a Hash in the form { ‘Header’ => ‘value’ }



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/rbot/core/utils/httputil.rb', line 483

def get_response(uri_or_s, options = {}, &block) # :yields: resp
  uri = uri_or_s.kind_of?(URI) ? uri_or_s : URI.parse(uri_or_s.to_s)
  unless URI::HTTP === uri
    if uri.scheme
      raise "#{uri.scheme.inspect} URI scheme is not supported"
    else
      raise "don't know what to do with #{uri.to_s.inspect}"
    end
  end

  opts = {
    :max_redir => @bot.config['http.max_redir'],
    :yield => :final,
    :cache => true,
    :method => :GET
  }.merge(options)

  resp = nil

  req_class = case opts[:method].to_s.downcase.intern
              when :head, :"net::http::head"
                opts[:max_redir] = -1
                Net::HTTP::Head
              when :get, :"net::http::get"
                Net::HTTP::Get
              when :post, :"net::http::post"
                opts[:cache] = false
                opts[:body] or raise 'post request w/o a body?'
                warning "refusing to cache POST request" if options[:cache]
                Net::HTTP::Post
              else
                warning "unsupported method #{opts[:method]}, doing GET"
                Net::HTTP::Get
              end

  if req_class != Net::HTTP::Get && opts[:range]
    warning "can't request ranges for #{req_class}"
    opts.delete(:range)
  end

  cache_key = "#{opts[:range]}|#{req_class}|#{uri.to_s}"

  if req_class != Net::HTTP::Get && req_class != Net::HTTP::Head
    if opts[:cache]
      warning "can't cache #{req_class.inspect} requests, working w/o cache"
      opts[:cache] = false
    end
  end

  debug "get_response(#{uri}, #{opts.inspect})"

  cached = @cache[cache_key]

  if opts[:cache] && cached
    debug "got cached"
    if !cached.expired?
      debug "using cached"
      cached.use
      return handle_response(uri, cached.response, opts, &block)
    end
  end

  headers = @headers.dup.merge(opts[:headers] || {})
  headers['Range'] = opts[:range] if opts[:range]
  headers['Authorization'] = opts[:auth_head] if opts[:auth_head]

  if opts[:cache] && cached && (req_class == Net::HTTP::Get)
    cached.setup_headers headers
  end

  req = req_class.new(uri.request_uri, headers)
  if uri.user && uri.password
    req.basic_auth(uri.user, uri.password)
    opts[:auth_head] = req['Authorization']
  end
  req.body = opts[:body] if req_class == Net::HTTP::Post
  debug "prepared request: #{req.to_hash.inspect}"

  begin
    get_proxy(uri, opts).start do |http|
      http.request(req) do |resp|
        resp['x-rbot-location'] = uri.to_s
        if Net::HTTPNotModified === resp
          debug "not modified"
          begin
            cached.revalidate(resp)
          rescue Exception => e
            error e
          end
          debug "reusing cached"
          resp = cached.response
        elsif Net::HTTPServerError === resp || Net::HTTPClientError === resp
          debug "http error, deleting cached obj" if cached
          @cache.delete(cache_key)
        end

        begin
          return handle_response(uri, resp, opts, &block)
        ensure
          if cached = CachedObject.maybe_new(resp) rescue nil
            debug "storing to cache"
            @cache[cache_key] = cached
          end
        end
      end
    end
  rescue Exception => e
    error e
    raise e.message
  end
end

#handle_response(uri, resp, opts, &block) ⇒ Object

Internal method used to hanlde response resp received when making a request for URI uri.

It follows redirects, optionally yielding them if option :yield is :all.

Also yields and returns the final resp.



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/rbot/core/utils/httputil.rb', line 407

def handle_response(uri, resp, opts, &block) # :yields: resp
  if Net::HTTPRedirection === resp && opts[:max_redir] >= 0
    if resp.key?('location')
      raise 'Too many redirections' if opts[:max_redir] <= 0
      yield resp if opts[:yield] == :all && block_given?
      loc = resp['location']
      new_loc = URI.join(uri.to_s, loc) rescue URI.parse(loc)
      new_opts = opts.dup
      new_opts[:max_redir] -= 1
      case opts[:method].to_s.downcase.intern
      when :post, :"net::http::post"
        new_opts[:method] = :get
      end
      if resp['set-cookie']
        debug "setting cookie #{resp['set-cookie']}"
        new_opts[:headers] ||= Hash.new
        new_opts[:headers]['Cookie'] = resp['set-cookie']
      end
      debug "following the redirect to #{new_loc}"
      return get_response(new_loc, new_opts, &block)
    else
      warning ":| redirect w/o location?"
    end
  end
  class << resp
    undef_method :body
    alias :body :cooked_body
  end
  unless resp['content-type']
    debug "No content type, guessing"
    resp['content-type'] =
      case resp['x-rbot-location']
      when /.html?$/i
        'text/html'
      when /.xml$/i
        'application/xml'
      when /.xhtml$/i
        'application/xml+xhtml'
      when /.(gif|png|jpe?g|jp2|tiff?)$/i
        "image/#{$1.sub(/^jpg$/,'jpeg').sub(/^tif$/,'tiff')}"
      else
        'application/octetstream'
      end
  end
  if block_given?
    yield(resp)
  else
    # Net::HTTP wants us to read the whole body here
    resp.raw_body
  end
  return resp
end

#head(uri, options = {}, &block) ⇒ Object

uri

uri to query (URI object or String)

Simple HEAD request, returns (if possible) response head following redirs and caching if requested, yielding the actual response(s) to the optional block. See get_response for details on the supported options



619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/rbot/core/utils/httputil.rb', line 619

def head(uri, options = {}, &block) # :yields: resp
  opts = {:method => :head}.merge(options)
  begin
    resp = get_response(uri, opts, &block)
    # raise "http error #{resp}" if Net::HTTPClientError === resp ||
    #   Net::HTTPServerError == resp
    return resp
  rescue Exception => e
    error e
  end
  return nil
end

#post(uri, data, options = {}, &block) ⇒ Object

uri

uri to query (URI object or String)

data

body of the POST

Simple POST request, returns (if possible) response following redirs and caching if requested, yielding the response(s) to the optional block. See get_response for details on the supported options



639
640
641
642
643
644
645
646
647
648
649
# File 'lib/rbot/core/utils/httputil.rb', line 639

def post(uri, data, options = {}, &block) # :yields: resp
  opts = {:method => :post, :body => data, :cache => false}.merge(options)
  begin
    resp = get_response(uri, opts, &block)
    raise 'http error' unless Net::HTTPOK === resp or Net::HTTPCreated === resp
    return resp
  rescue Exception => e
    error e
  end
  return nil
end

#proxy_required(uri) ⇒ Object

This method checks if a proxy is required to access uri, by looking at the values of config values http.proxy_include and http.proxy_exclude.

Each of these config values, if set, should be a Regexp the server name and IP address should be checked against.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/rbot/core/utils/httputil.rb', line 318

def proxy_required(uri)
  use_proxy = true
  if @bot.config["http.proxy_exclude"].empty? && @bot.config["http.proxy_include"].empty?
    return use_proxy
  end

  list = [uri.host]
  begin
    list.concat Resolv.getaddresses(uri.host)
  rescue StandardError => err
    warning "couldn't resolve host uri.host"
  end

  unless @bot.config["http.proxy_exclude"].empty?
    re = @bot.config["http.proxy_exclude"].collect{|r| Regexp.new(r)}
    re.each do |r|
      list.each do |item|
        if r.match(item)
          use_proxy = false
          break
        end
      end
    end
  end
  unless @bot.config["http.proxy_include"].empty?
    re = @bot.config["http.proxy_include"].collect{|r| Regexp.new(r)}
    re.each do |r|
      list.each do |item|
        if r.match(item)
          use_proxy = true
          break
        end
      end
    end
  end
  debug "using proxy for uri #{uri}?: #{use_proxy}"
  return use_proxy
end

#remove_stale_cacheObject



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/rbot/core/utils/httputil.rb', line 664

def remove_stale_cache
  debug "Removing stale cache"
  now = Time.new
  max_last = @bot.config['http.expire_time'] * 60
  max_first = @bot.config['http.max_cache_time'] * 60
  debug "#{@cache.size} pages before"
  begin
    @cache.reject! { |k, val|
      (now - val.last_used > max_last) || (now - val.first_used > max_first)
    }
  rescue => e
    error "Failed to remove stale cache: #{e.pretty_inspect}"
  end
  debug "#{@cache.size} pages after"
end