enable net/http adapter to use gzip for GET

also get rid of `full_path_for`; replaced with `url.request_uri`
This commit is contained in:
Mislav Marohnić 2011-02-28 03:14:51 +01:00
parent 20eee87486
commit 8c5701f8ea
6 changed files with 75 additions and 44 deletions

View File

@ -10,6 +10,7 @@ group :development, :test do
gem 'eventmachine', '~> 0.12'
gem 'em-http-request', '~> 0.3', :require => 'em-http'
gem 'em-synchrony', '~> 0.2', :require => ['em-synchrony', 'em-synchrony/em-http'], :platforms => :ruby_19
gem 'webmock'
# ActiveSupport::JSON will be used in ruby 1.8 and Yajl in 1.9; this is to test against both adapters
gem 'activesupport', '~> 2.3.8', :require => nil, :platforms => :ruby_18
gem 'yajl-ruby', :require => 'yajl', :platforms => :ruby_19

View File

@ -97,17 +97,5 @@ module Faraday
end
end
end
# assume that query and fragment are already encoded properly
def full_path_for(path, query = nil, fragment = nil)
full_path = path.dup
if query && !query.empty?
full_path << "?#{query}"
end
if fragment && !fragment.empty?
full_path << "##{fragment}"
end
full_path
end
end
end

View File

@ -19,8 +19,7 @@ module Faraday
def call(env)
super
full_path = full_path_for(env[:url].path, env[:url].query, env[:url].fragment)
@session.__send__(env[:method], full_path, env[:body], env[:request_headers])
@session.__send__(env[:method], env[:url].request_uri, env[:body], env[:request_headers])
resp = @session.response
env.update \
:status => resp.status,

View File

@ -1,7 +1,7 @@
begin
require 'net/https'
rescue LoadError
puts "no such file to load -- net/https. Make sure openssl is installed if you want ssl support"
warn "Warning: no such file to load -- net/https. Make sure openssl is installed if you want ssl support"
require 'net/http'
end
@ -10,53 +10,62 @@ module Faraday
class NetHttp < Faraday::Adapter
def call(env)
super
url = env[:url]
req = env[:request]
is_ssl = env[:url].scheme == 'https'
http = net_http_class(env).new(url.host, url.port)
http = net_http_class(env).new(env[:url].host, env[:url].port || (is_ssl ? 443 : 80))
if http.use_ssl = is_ssl
if http.use_ssl = url.scheme == 'https'
ssl = env[:ssl]
if ssl[:verify] == false
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
else
http.verify_mode = ssl[:verify]
http.verify_mode = case ssl[:verify]
when false then OpenSSL::SSL::VERIFY_NONE
when true then OpenSSL::SSL::VERIFY_PEER
else 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 = http.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_req = Net::HTTPGenericRequest.new(
if :get != env[:method]
http_request = Net::HTTPGenericRequest.new \
env[:method].to_s.upcase, # request method
(env[:body] ? true : false), # is there data
!!env[:body], # is there data
true, # does net/http love you, true or false?
full_path, # request uri path
env[:request_headers]) # request headers
url.request_uri, # request uri path
env[:request_headers] # request headers
if env[:body].respond_to?(:read)
http_req.body_stream = env[:body]
http_request.body_stream = env[:body]
env[:body] = nil
end
end
http_resp = http.request http_req, env[:body]
begin
http_response = if :get == env[:method]
# prefer `get` to `request` because the former handles gzip (ruby 1.9)
http.get url.request_uri, env[:request_headers]
else
http.request http_request, env[:body]
end
rescue Errno::ECONNREFUSED => e
raise Error::ConnectionFailed.new(e)
end
resp_headers = {}
http_resp.each_header do |key, value|
resp_headers[key] = value
response_headers = {}
http_response.each_header do |key, value|
response_headers[key] = value
end
env.update \
:status => http_resp.code.to_i,
:response_headers => resp_headers,
:body => http_resp.body
:status => http_response.code.to_i,
:response_headers => response_headers,
:body => http_response.body
@app.call env
rescue Errno::ECONNREFUSED => e
raise Error::ConnectionFailed.new(e)
end
def net_http_class(env)

View File

@ -0,0 +1,34 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
require 'webmock/test_unit'
module Adapters
class NetHttpTest < Faraday::TestCase
def setup
@connection = Faraday.new('http://disney.com') do |b|
b.adapter :net_http
end
end
def test_handles_compression_transparently_on_get
stub_request(:get, 'disney.com/hello').with { |request|
accept_encoding = request.headers['Accept-Encoding']
if RUBY_VERSION.index('1.8') == 0
# ruby 1.8 doesn't do any gzip/deflate automatically
accept_encoding == nil
else
# test for a value such as "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
accept_encoding =~ /gzip;.+\bdeflate\b/
end
}
@connection.get('/hello')
end
def test_connect_error_gets_wrapped
stub_request(:get, 'disney.com/hello').to_raise(Errno::ECONNREFUSED)
assert_raise Faraday::Error::ConnectionFailed do
@connection.get('/hello')
end
end
end
end

View File

@ -33,6 +33,6 @@ module Faraday
def test_default
assert true
end
end unless defined? ::MiniTest
end
end