Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Per-request proxy configuration #189

Closed
wants to merge 1 commit into from

10 participants

@danielfm

One of my projects uses a couple of API client gems which are developed on top of rest-client. Everything worked great (thank you btw :-) until I needed to set a proxy for the calls going to one API but not to the others.

Since the proxy is configured via the global setting RestClient.proxy, I figured it would be nice to be able to set the proxy for a specific RestClient::Request (or bypass the globally configured proxy if necessary).

That way, I wouldn't need to resort to "dirty" tricks like this:

old_proxy = RestClient.proxy

begin
  # Change or bypass proxy
  RestClient.proxy = new_proxy

  # Do work
ensure
  RestClient.proxy = old_proxy
end

I added test cases for the new parameter and updated the README accordingly.

Daniel Fernandes Martins Now it's possible to make a request bypassing the global proxy config…
…uration, or use a different proxy when needed.
735017f
@L2G
Owner
L2G commented

Excellent, thanks. There's a tickle in the back of my brain telling me that someone else was working on this, too, but maybe that's a false memory. Anyway, this sounds like a worthy new feature.

@ajahongir

nice feature, in my application I use multithread queries with different proxies so
I was searching a few days ago BUT couldnt find and used another http client.
I cant wait when this request will be released.

@moonmaster9000

went to check on the status of my PR for thread-safe proxy configuration (#191) and discovered this one!

My PR not only adds support for per-RestClient::Request proxies, but also exposes per-request functionality directly in the RestClient module, which is the primary way RestClient is used.

@barmstrong

:+1: we just hit a case where this would be useful

@moonmaster9000
@ajahongir

i couldnt wait anymore and Today I had decided to use #191
it seems work well.

@L2G
Owner

I haven't been looking in on this repo for a while, so I'm trying to bang on it this weekend as much as I can.

@moonmaster9000
@L2G L2G was assigned
@ajahongir

are you guys going to accept this request?

@layerssss

:shipit: +1 for this

@sodabrew

+1 - Is there some blocker on this PR?

@L2G L2G was unassigned by ab
@ab ab self-assigned this
@ab ab modified the milestone: 1.8.0, 1.7.0
@axsuul

Will this ever be merged in?

@tsl0922

+1 for this

@ab ab modified the milestone: 2.0.0, 1.8.0
@ab ab closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 29, 2013
  1. Now it's possible to make a request bypassing the global proxy config…

    Daniel Fernandes Martins authored
    …uration, or use a different proxy when needed.
This page is out of date. Refresh to see the latest.
Showing with 45 additions and 8 deletions.
  1. +1 −0  README.rdoc
  2. +6 −3 lib/restclient/request.rb
  3. +38 −5 spec/request_spec.rb
View
1  README.rdoc
@@ -146,6 +146,7 @@ You can:
* specify ssl parameters
* override cookies
* manually handle the response (so you can operate on the response stream than reading it fully in memory)
+* bypass or use another proxy than the one configured globally (see the Proxy section below for more information)
see the class' rdoc for more information.
View
9 lib/restclient/request.rb
@@ -24,12 +24,13 @@ module RestClient
# * :timeout and :open_timeout passing in -1 will disable the timeout by setting the corresponding net timeout values to nil
# * :ssl_client_cert, :ssl_client_key, :ssl_ca_file
# * :ssl_version specifies the SSL version for the underlying Net::HTTP connection (defaults to 'SSLv3')
+ # * :proxy proxy URL specific for this request, use :none to bypass the global proxy configuration
class Request
attr_reader :method, :url, :headers, :cookies,
:payload, :user, :password, :timeout, :max_redirects,
:open_timeout, :raw_response, :verify_ssl, :ssl_client_cert,
- :ssl_client_key, :ssl_ca_file, :processed_headers, :args,
+ :ssl_client_key, :ssl_ca_file, :processed_headers, :proxy, :args,
:ssl_version
def self.execute(args, & block)
@@ -60,6 +61,7 @@ def initialize args
@tf = nil # If you are a raw request, this is your tempfile
@max_redirects = args[:max_redirects] || 10
@processed_headers = make_headers headers
+ @proxy = args[:proxy]
@args = args
end
@@ -99,8 +101,9 @@ def make_headers user_headers
end
def net_http_class
- if RestClient.proxy
- proxy_uri = URI.parse(RestClient.proxy)
+ proxy = @proxy != :none ? (@proxy || RestClient.proxy) : nil
+ if proxy
+ proxy_uri = URI.parse(proxy)
Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
else
Net::HTTP
View
43 spec/request_spec.rb
@@ -308,14 +308,47 @@
end
end
- describe "proxy" do
- it "creates a proxy class if a proxy url is given" do
- RestClient.stub!(:proxy).and_return("http://example.com/")
+ shared_examples_for "request-specific proxy" do
+ before do
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => 'http://another.proxy.com/')
+ end
+
+ it "uses a different proxy for this request only" do
@request.net_http_class.proxy_class?.should be_true
+ @request.net_http_class.proxy_address.should == 'another.proxy.com'
+ end
+ end
+
+ describe "proxy" do
+ context "with global proxy configuration" do
+ before do
+ RestClient.stub!(:proxy).and_return('http://example.com/')
+ end
+
+ it "creates a proxy class if a proxy url is given" do
+ @request.net_http_class.proxy_class?.should be_true
+ @request.net_http_class.proxy_address.should == 'example.com'
+ end
+
+ describe "bypass global proxy configuration for a specific request" do
+ before do
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => :none)
+ end
+
+ it "creates a non-proxy class" do
+ @request.net_http_class.proxy_class?.should be_false
+ end
+ end
+
+ it_should_behave_like "request-specific proxy"
end
- it "creates a non-proxy class if a proxy url is not given" do
- @request.net_http_class.proxy_class?.should be_false
+ context "without global proxy configuration" do
+ it "creates a non-proxy class if a proxy url is not given" do
+ @request.net_http_class.proxy_class?.should be_false
+ end
+
+ it_should_behave_like "request-specific proxy"
end
end
Something went wrong with that request. Please try again.