add ssl support to net/http and typhoeus

This commit is contained in:
rick 2010-04-27 09:35:17 -04:00
parent 403ea34529
commit 7739157945
5 changed files with 51 additions and 5 deletions

View File

@ -1,11 +1,34 @@
require 'net/http'
begin
require 'net/https'
rescue LoadError
puts "no such file to load -- net/https. Make sure openssl is installed if you want ssl support"
require 'net/http'
end
module Faraday
module Adapter
class NetHttp < Middleware
def call(env)
process_body_for_request(env)
http = Net::HTTP.new(env[:url].host, env[:url].port)
is_ssl = env[:url].scheme == 'https'
http = Net::HTTP.new(env[:url].host, env[:url].port || (is_ssl ? 443 : 80))
if http.use_ssl = is_ssl
ssl = env[:ssl]
if ssl[:verify] == false
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
else
http.verify_mode = ssl[:verify]
end
http.cert = ssl[:client_cert] if ssl[:client_cert]
http.key = ssl[:client_key] if ssl[:client_key]
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
end
req = env[:request]
http.read_timeout = net.open_timeout = req[:timeout] if req[:timeout]
http.open_timeout = req[:open_timeout] if req[:open_timeout]
full_path = full_path_for(env[:url].path, env[:url].query, env[:url].fragment)
http_resp = http.send_request(env[:method].to_s.upcase, full_path, env[:body], env[:request_headers])

View File

@ -20,7 +20,8 @@ module Faraday
req = ::Typhoeus::Request.new env[:url].to_s,
:method => env[:method],
:body => env[:body],
:headers => env[:request_headers]
:headers => env[:request_headers],
:disable_ssl_peer_verification => (env[:ssl][:verify] == false)
req.on_complete do |resp|
env.update \

View File

@ -16,11 +16,13 @@ module Faraday
METHODS_WITH_BODIES = Set.new [:post, :put]
attr_accessor :host, :port, :scheme, :params, :headers, :parallel_manager
attr_reader :path_prefix, :builder
attr_reader :path_prefix, :builder, :options, :ssl
# :url
# :params
# :headers
# :request
# :ssl
def initialize(url = nil, options = {}, &block)
if url.is_a?(Hash)
options = url
@ -28,6 +30,8 @@ module Faraday
end
@headers = HeaderHash.new
@params = {}
@options = options[:request] || {}
@ssl = options[:ssl] || {}
@parallel_manager = options[:parallel]
self.url_prefix = url if url
merge_params @params, options[:params] if options[:params]

View File

@ -54,6 +54,10 @@ module Faraday
# :response_headers - Hash of HTTP headers from the server
# :parallel_manager - sent if the connection is in parallel mode
# :response - the actual response object that stores the rack response
# :request - Hash of options for configuring the request.
# :timeout - open/read timeout Integer in seconds
# :open_timeout - read timeout Integer in seconds
# :ssl - Hash of options for configuring SSL requests.
def to_env_hash(connection, request_method)
env_headers = connection.headers.dup
env_params = connection.params.dup
@ -65,7 +69,9 @@ module Faraday
:url => connection.build_url(path, env_params),
:request_headers => env_headers.update(headers),
:parallel_manager => connection.parallel_manager,
:response => Response.new}
:response => Response.new,
:request => connection.options,
:ssl => connection.ssl}
end
def run(connection, request_method)

View File

@ -3,6 +3,9 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class TestEnv < Faraday::TestCase
def setup
@conn = Faraday::Connection.new :url => 'http://sushi.com/api', :headers => {'Mime-Version' => '1.0'}
@conn.options[:timeout] = 3
@conn.options[:open_timeout] = 5
@conn.ssl[:verify] = false
@input = {
:body => 'abc',
:headers => {'Server' => 'Faraday'}}
@ -30,4 +33,13 @@ class TestEnv < Faraday::TestCase
def test_request_create_stores_body
assert_equal @input[:body], @env[:body]
end
def test_request_create_stores_ssl_options
assert_equal 3, @env[:request][:timeout]
assert_equal 5, @env[:request][:open_timeout]
end
def test_request_create_stores_ssl_options
assert_equal false, @env[:ssl][:verify]
end
end