Skip to content

Commit

Permalink
Add keep-alive support
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Mar 22, 2013
1 parent e4dc65f commit 55698c7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
38 changes: 38 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion lib/restclient.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'uri'
require 'zlib'
require 'stringio'
require 'net/http/persistent'

begin
require 'net/https'
Expand Down Expand Up @@ -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.
Expand Down
33 changes: 26 additions & 7 deletions lib/restclient/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -178,13 +188,22 @@ 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) }
log_response res
process_result res, & block
net.request(uri, req) { |http_response| fetch_body(http_response) }
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
Expand Down
1 change: 1 addition & 0 deletions rest-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit 55698c7

Please sign in to comment.