Class: ::Utils::HttpUtil

Class | ::Utils::HttpUtil
In: | lib/rbot/core/utils/httputil.rb
Parent: | Object

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.

### Methods

cleanup get get_partial get_proxy get_response handle_response head new post proxy_required remove_stale_cache

### Classes and Modules

Class ::Utils::HttpUtil::CachedObject

### Public Class methods

new(bot)

Create the HttpUtil instance, associating it with Bot bot

### Public Instance methods

cleanup()

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

get(uri, options = {}) {|resp| ...}

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

get_partial(uri, nbytes = @bot.config['http.info_bytes'], options = {}) {|resp| ...}

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

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.

get_response(uri_or_s, options = {}) {|resp| ...}

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

handle_response(uri, resp, opts) {|resp| ...}

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.

head(uri, options = {}) {|resp| ...}

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

post(uri, data, options = {}) {|resp| ...}

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

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.

remove_stale_cache()

[Validate]