class HTTPI::Adapter::EmHttpRequest
An HTTPI
adapter for ‘EventMachine::HttpRequest`. Due to limitations of the em-httprequest library, not all features are supported. In particular,
-
CA files,
-
certificate verification modes other than “none” and “peer,”
-
NTLM authentication,
-
digest authentication, and
-
password-protected certificate keys
are supported by HTTPI
but not em-httprequest.
In addition, some features of em-httprequest are not represented in HTTPI
and are therefore not supported. In particular,
-
SOCKS5 proxying,
-
automatic redirect following,
-
response streaming,
-
file body streaming,
-
keepalive,
-
pipelining, and
-
multi-request
are supported by em-httprequest but not HTTPI
.
Attributes
Public Class Methods
# File lib/httpi/adapter/em_http.rb, line 34 def initialize(request) @request = request @client = EventMachine::HttpRequest.new build_request_url(request.url), connection_options end
Public Instance Methods
# File lib/httpi/adapter/em_http.rb, line 41 def cert_directory @cert_directory ||= "/tmp" end
Executes arbitrary HTTP
requests. @see HTTPI.request
# File lib/httpi/adapter/em_http.rb, line 49 def request(method) _request { |options| @client.send method, options } end
Private Instance Methods
# File lib/httpi/adapter/em_http.rb, line 55 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
# File lib/httpi/adapter/em_http.rb, line 115 def build_request_url(url) "%s://%s:%s%s" % [url.scheme, url.host, url.port, url.path] end
# File lib/httpi/adapter/em_http.rb, line 83 def client_options { :query => @request.url.query, :head => @request.headers.to_hash, :body => @request.body } end
# File lib/httpi/adapter/em_http.rb, line 71 def connection_options options = {} read_or_write_timeout = @request.read_timeout || @request.write_timeout options[:inactivity_timeout] = read_or_write_timeout if read_or_write_timeout options[:connect_timeout] = @request.open_timeout if @request.open_timeout options[:proxy] = proxy_options if @request.proxy options end
Takes any header names with an underscore as a word separator and converts the name to camel case, where words are separated by a dash.
E.g. CONTENT_TYPE becomes Content-Type.
# File lib/httpi/adapter/em_http.rb, line 123 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
# File lib/httpi/adapter/em_http.rb, line 91 def proxy_options { :host => @request.proxy.host, :port => @request.proxy.port, :authorization => [@request.proxy.user, @request.proxy.password] } end
# File lib/httpi/adapter/em_http.rb, line 108 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
# File lib/httpi/adapter/em_http.rb, line 99 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