Class: Irc::Utils::HttpUtil

Inherits:
Object show all
Defined in:
/home/apoc/projects/ruby/rbot/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

- (HttpUtil) initialize(bot)

Create the HttpUtil instance, associating it with Bot bot



305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 305

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

- (Object) cleanup

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



321
322
323
324
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 321

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

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

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



647
648
649
650
651
652
653
654
655
656
657
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 647

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

- (Object) get_partial(uri, nbytes = @bot.config['http.info_bytes'], options = {}, &block)

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



705
706
707
708
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 705

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

- (Object) get_proxy(uri, options = {})

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.



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 376

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

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

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' }



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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 531

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)

  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

- (Object) handle_response(uri, resp, opts, &block)

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.



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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 421

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?
      # some servers actually provide unescaped location, e.g.
      # http://ulysses.soup.io/post/60734021/Image%20curve%20ball
      # rediects to something like
      # http://ulysses.soup.io/post/60734021/Image curve ball?sessid=8457b2a3752085cca3fb1d79b9965446
      # causing the URI parser to (obviously) complain. We cannot just
      # escape blindly, as this would make a mess of already-escaped
      # locations, so we only do it if the URI.parse fails
      loc = resp['location']
      escaped = false
      debug "redirect location: #{loc.inspect}"
      begin
        new_loc = URI.join(uri.to_s, loc) rescue URI.parse(loc)
      rescue
        if escaped
          raise $!
        else
          loc = URI.escape(loc)
          escaped = true
          debug "escaped redirect location: #{loc.inspect}"
          retry
        end
      end
      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 "set cookie request for #{resp['set-cookie']}"
        cookie, cookie_flags = (resp['set-cookie']+'; ').split('; ', 2)
        domain = uri.host
        cookie_flags.scan(/(\S+)=(\S+);/) { |key, val|
          if key.intern == :domain
            domain = val
            break
          end
        }
        debug "cookie domain #{domain} / #{new_loc.host}"
        if new_loc.host.rindex(domain) == new_loc.host.length - domain.length
          debug "setting cookie"
          new_opts[:headers] ||= Hash.new
          new_opts[:headers]['Cookie'] = cookie
        else
          debug "cookie is for another domain, ignoring"
        end
      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

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

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



665
666
667
668
669
670
671
672
673
674
675
676
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 665

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

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

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



685
686
687
688
689
690
691
692
693
694
695
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 685

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

- (Object) proxy_required(uri)

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.



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 332

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

- (Object) remove_stale_cache



710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 710

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