class HTTPI::Adapter::EmHttpRequest
are supported by em-httprequest but not HTTPI.
* multi-request
* pipelining, and
* keepalive,
* file body streaming,
* response streaming,
* automatic redirect following,
* SOCKS5 proxying,
and are therefore not supported. In particular,
In addition, some features of em-httprequest are not represented in HTTPI
are supported by HTTPI but not em-httprequest.
* password-protected certificate keys
* digest authentication, and
* NTLM authentication,
* certificate verification modes other than “none” and “peer,”
* CA files,
the em-httprequest library, not all features are supported. In particular,
An HTTPI adapter for ‘EventMachine::HttpRequest`. Due to limitations of
def _request
def _request options = client_options setup_http_auth(options) if @request.auth.http? if @request.auth.ssl? raise NotSupportedError, "EM-HTTP-Request does not support SSL client auth" end if @request.on_body raise NotSupportedError, "EM-HTTP-Request does not support response streaming" end start_time = Time.now respond_with yield(options), start_time end
def build_request_url(url)
def build_request_url(url) "%s://%s:%s%s" % [url.scheme, url.host, url.port, url.path] end
def cert_directory
def cert_directory @cert_directory ||= "/tmp" end
def client_options
def client_options { :query => @request.url.query, :head => @request.headers.to_hash, :body => @request.body } end
def connection_options
def connection_options options = { :connect_timeout => @request.open_timeout, :inactivity_timeout => @request.read_timeout } options[:proxy] = proxy_options if @request.proxy options end
def convert_headers(headers)
converts the name to camel case, where words are separated by a dash.
Takes any header names with an underscore as a word separator and
def convert_headers(headers) return headers unless headers.keys.any? { |k| k =~ /_/ } result = {} headers.each do |k, v| words = k.split("_") key = words.map { |w| w.downcase.capitalize }.join("-") result[key] = v end result end
def initialize(request)
def initialize(request) @request = request @client = EventMachine::HttpRequest.new build_request_url(request.url), connection_options end
def proxy_options
def proxy_options { :host => @request.proxy.host, :port => @request.proxy.port, :authorization => [@request.proxy.user, @request.proxy.password] } end
def request(method)
- See: HTTPI.request -
def request(method) _request { |options| @client.send method, options } end
def respond_with(http, start_time)
def respond_with(http, start_time) raise TimeoutError, "EM-HTTP-Request connection timed out: #{Time.now - start_time} sec" if http.response_header.status.zero? Response.new http.response_header.status, convert_headers(http.response_header), http.response end
def setup_http_auth(options)
def setup_http_auth(options) unless @request.auth.type == :basic raise NotSupportedError, "EM-HTTP-Request does only support HTTP basic auth" end options[:head] ||= {} options[:head][:authorization] = @request.auth.credentials end