Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add keep-alive support

  • Loading branch information...
commit 36ecf00b200be4e508060bd2d01ca9688460a9be 1 parent e4dc65f
@byroot byroot authored
View
38 Gemfile.lock
@@ -0,0 +1,38 @@
+PATH
+ remote: .
+ specs:
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
+ net-http-persistent
+ netrc (~> 0.7.7)
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ addressable (2.3.3)
+ crack (0.3.2)
+ diff-lcs (1.2.1)
+ mime-types (1.21)
+ net-http-persistent (2.8)
+ netrc (0.7.7)
+ rake (10.0.3)
+ rspec (2.13.0)
+ rspec-core (~> 2.13.0)
+ rspec-expectations (~> 2.13.0)
+ rspec-mocks (~> 2.13.0)
+ rspec-core (2.13.1)
+ rspec-expectations (2.13.0)
+ diff-lcs (>= 1.1.3, < 2.0)
+ rspec-mocks (2.13.0)
+ webmock (1.11.0)
+ addressable (>= 2.2.7)
+ crack (>= 0.3.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rake
+ rest-client!
+ rspec (>= 2.0)
+ webmock (>= 0.9.1)
View
3  lib/restclient.rb
@@ -1,6 +1,7 @@
require 'uri'
require 'zlib'
require 'stringio'
+require 'net/http/persistent'
begin
require 'net/https'
@@ -93,7 +94,7 @@ def self.options(url, headers={}, &block)
end
class << self
- attr_accessor :proxy
+ attr_accessor :proxy, :persistent
end
# Setup the log for RestClient calls.
View
31 lib/restclient/request.rb
@@ -98,6 +98,16 @@ def make_headers user_headers
headers
end
+ def open_connection(host, port)
+ if RestClient.persistent
+ proxy = nil
+ proxy = URI.parse(RestClient.proxy) if RestClient.proxy
+ Net::HTTP::Persistent.new('rest-client', proxy)
+ else
+ net_http_class.new(host, port)
+ end
+ end
+
def net_http_class
if RestClient.proxy
proxy_uri = URI.parse(RestClient.proxy)
@@ -146,8 +156,8 @@ def process_payload(p=nil, parent_key=nil)
def transmit uri, req, payload, & block
setup_credentials req
- net = net_http_class.new(uri.host, uri.port)
- net.use_ssl = uri.is_a?(URI::HTTPS)
+ net = open_connection(uri.host, uri.port)
+ net.use_ssl = uri.is_a?(URI::HTTPS) if net.respond_to?(:use_ssl=)
net.ssl_version = @ssl_version
err_msg = nil
if (@verify_ssl == false) || (@verify_ssl == OpenSSL::SSL::VERIFY_NONE)
@@ -178,14 +188,25 @@ def transmit uri, req, payload, & block
log_request
- net.start do |http|
+ if RestClient.persistent
+ req.body = payload.to_s if payload
if @block_response
- http.request(req, payload ? payload.to_s : nil, & @block_response)
+ net.request(uri, req)
else
- res = http.request(req, payload ? payload.to_s : nil) { |http_response| fetch_body(http_response) }
+ res = net.request(uri, req) { |http_response| fetch_body(http_response) }
log_response res
process_result res, & block
end
+ else
+ net.start do |http|
+ if @block_response
+ http.request(req, payload ? payload.to_s : nil, & @block_response)
+ else
+ res = http.request(req, payload ? payload.to_s : nil) { |http_response| fetch_body(http_response) }
+ log_response res
+ process_result res, & block
+ end
+ end
end
rescue OpenSSL::SSL::SSLError => e
if err_msg
View
1  rest-client.gemspec
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
s.summary = 'Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.'
s.add_runtime_dependency(%q<mime-types>, [">= 1.16"])
+ s.add_runtime_dependency(%q<net-http-persistent>)
s.add_development_dependency(%q<webmock>, [">= 0.9.1"])
s.add_development_dependency(%q<rspec>, [">= 2.0"])
s.add_dependency(%q<netrc>, ["~> 0.7.7"])
Please sign in to comment.
Something went wrong with that request. Please try again.