Skip to content
This repository

no_proxy support #194

Closed
wants to merge 3 commits into from

2 participants

Victor Engmark Sebastian Riedel
Victor Engmark
l0b0 commented

Support for domains where a proxy should not be used.

added some commits
Support for no_proxy environment variable
If the environment has a no_proxy or NO_PROXY variable, and the host of the current request is a proper postfix any of the comma-separated strings there, the proxy will be bypassed.
e4c66fb
Test for empty $no_proxy e6ea24b
Avoid halting if $tx->previous is undefined e22c945
Sebastian Riedel kraih closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Jan 11, 2011
Support for no_proxy environment variable
If the environment has a no_proxy or NO_PROXY variable, and the host of the current request is a proper postfix any of the comma-separated strings there, the proxy will be bypassed.
e4c66fb
Test for empty $no_proxy e6ea24b
Avoid halting if $tx->previous is undefined e22c945
This page is out of date. Refresh to see the latest.
34  lib/Mojo/Client.pm
@@ -25,7 +25,7 @@ use Scalar::Util 'weaken';
25 25
 use constant DEBUG => $ENV{MOJO_CLIENT_DEBUG} || 0;
26 26
 
27 27
 # You can't let a single bad experience scare you away from drugs.
28  
-__PACKAGE__->attr([qw/app cert http_proxy https_proxy key tx/]);
  28
+__PACKAGE__->attr([qw/app cert http_proxy https_proxy no_proxy key tx/]);
29 29
 __PACKAGE__->attr(cookie_jar => sub { Mojo::CookieJar->new });
30 30
 __PACKAGE__->attr(ioloop     => sub { Mojo::IOLoop->new });
31 31
 __PACKAGE__->attr(keep_alive_timeout => 15);
@@ -319,6 +319,8 @@ sub detect_proxy {
319 319
     my $self = shift;
320 320
     $self->http_proxy($ENV{HTTP_PROXY}   || $ENV{http_proxy});
321 321
     $self->https_proxy($ENV{HTTPS_PROXY} || $ENV{https_proxy});
  322
+    my $no_proxy = $ENV{NO_PROXY} || $ENV{no_proxy};
  323
+    $self->no_proxy([split(/,/, $no_proxy)]) if ($no_proxy);
322 324
     return $self;
323 325
 }
324 326
 
@@ -966,6 +968,18 @@ sub _tx_queue_or_start {
966 968
     $self->queue($tx, $cb);
967 969
 }
968 970
 
  971
+sub _need_proxy {
  972
+    my($self, $host) = @_;
  973
+    if ($self->no_proxy) {
  974
+        for my $domain (@{$self->no_proxy}) {
  975
+            if ($host =~ /\Q$domain\E$/) {
  976
+                return 0;
  977
+            }
  978
+        }
  979
+    }
  980
+    return 1;
  981
+}
  982
+
969 983
 # It's greeat! We can do *anything* now that Science has invented Magic.
970 984
 sub _tx_start {
971 985
     my ($self, $tx, $cb) = @_;
@@ -993,14 +1007,16 @@ sub _tx_start {
993 1007
     # Detect proxy
994 1008
     $self->detect_proxy if $ENV{MOJO_PROXY};
995 1009
 
996  
-    # HTTP proxy
997  
-    if (my $proxy = $self->http_proxy) {
998  
-        $req->proxy($proxy) if !$req->proxy && $scheme eq 'http';
999  
-    }
1000  
-
1001  
-    # HTTPS proxy
1002  
-    if (my $proxy = $self->https_proxy) {
1003  
-        $req->proxy($proxy) if !$req->proxy && $scheme eq 'https';
  1010
+    if ($self->_need_proxy($req->url->host)) {
  1011
+        # HTTP proxy
  1012
+        if (my $proxy = $self->http_proxy) {
  1013
+            $req->proxy($proxy) if !$req->proxy && $scheme eq 'http';
  1014
+        }
  1015
+    
  1016
+        # HTTPS proxy
  1017
+        if (my $proxy = $self->https_proxy) {
  1018
+            $req->proxy($proxy) if !$req->proxy && $scheme eq 'https';
  1019
+        }
1004 1020
     }
1005 1021
 
1006 1022
     # Make sure WebSocket requests have an origin header
92  t/mojo/client_online.t
@@ -10,7 +10,7 @@ use Test::More;
10 10
 
11 11
 plan skip_all => 'set TEST_ONLINE to enable this test (developer only!)'
12 12
   unless $ENV{TEST_ONLINE};
13  
-plan tests => 102;
  13
+plan tests => 116;
14 14
 
15 15
 # So then I said to the cop, "No, you're driving under the influence...
16 16
 # of being a jerk".
@@ -114,8 +114,8 @@ is $tx->res->code, 301, 'right status';
114 114
 like $tx->res->headers->connection, qr/close/i, 'right "Connection" header';
115 115
 
116 116
 # Proxy check
117  
-my $backup  = $ENV{HTTP_PROXY}  || '';
118  
-my $backup2 = $ENV{HTTPS_PROXY} || '';
  117
+my $http_proxy  = $ENV{HTTP_PROXY}  || '';
  118
+my $https_proxy = $ENV{HTTPS_PROXY} || '';
119 119
 $ENV{HTTP_PROXY}  = 'http://127.0.0.1';
120 120
 $ENV{HTTPS_PROXY} = 'https://127.0.0.1';
121 121
 $client->detect_proxy;
@@ -125,8 +125,42 @@ $client->http_proxy(undef);
125 125
 $client->https_proxy(undef);
126 126
 is $client->http_proxy,  undef, 'right proxy';
127 127
 is $client->https_proxy, undef, 'right proxy';
128  
-$ENV{HTTP_PROXY}  = $backup;
129  
-$ENV{HTTPS_PROXY} = $backup2;
  128
+$ENV{HTTP_PROXY}  = $http_proxy;
  129
+$ENV{HTTPS_PROXY} = $https_proxy;
  130
+
  131
+# Proxy overrides
  132
+$client = Mojo::Client->new;
  133
+$http_proxy  = $ENV{HTTP_PROXY}  || '';
  134
+$https_proxy = $ENV{HTTPS_PROXY} || '';
  135
+my $NO_PROXY = $ENV{NO_PROXY}  || '';
  136
+my $no_proxy = $ENV{no_proxy}  || '';
  137
+$ENV{HTTP_PROXY}  = 'http://127.0.0.1';
  138
+$ENV{HTTPS_PROXY} = 'https://127.0.0.1';
  139
+$ENV{NO_PROXY} = '';
  140
+$ENV{no_proxy} = '';
  141
+$client->detect_proxy;
  142
+is $client->_need_proxy('localhost'), 1, 'right conclusion';
  143
+is $client->_need_proxy('example.org'), 1, 'right conclusion';
  144
+is $client->_need_proxy('example.com'), 1, 'right conclusion';
  145
+is $client->_need_proxy('www.example.com'), 1, 'right conclusion';
  146
+is $client->_need_proxy('someexample.com'), 1, 'right conclusion';
  147
+is $client->_need_proxy('example.net'), 1, 'right conclusion';
  148
+is $client->_need_proxy('example.com.com'), 1, 'right conclusion';
  149
+
  150
+$client = Mojo::Client->new;
  151
+$ENV{NO_PROXY} = 'localhost,example.org,example.com';
  152
+$client->detect_proxy;
  153
+is $client->_need_proxy('localhost'), 0, 'right conclusion';
  154
+is $client->_need_proxy('example.org'), 0, 'right conclusion';
  155
+is $client->_need_proxy('example.com'), 0, 'right conclusion';
  156
+is $client->_need_proxy('www.example.com'), 0, 'right conclusion for subdomain';
  157
+is $client->_need_proxy('someexample.com'), 0, 'right conclusion for substring';
  158
+is $client->_need_proxy('example.net'), 1, 'right conclusion';
  159
+is $client->_need_proxy('example.com.com'), 1, 'right conclusion';
  160
+$ENV{HTTP_PROXY}  = $http_proxy;
  161
+$ENV{HTTPS_PROXY} = $https_proxy;
  162
+$ENV{NO_PROXY}    = $NO_PROXY;
  163
+$ENV{no_proxy}    = $no_proxy;
130 164
 
131 165
 # Oneliner
132 166
 is g('mojolicio.us')->code,          200, 'right status';
@@ -239,24 +273,27 @@ is $code3,      200,                     'right status';
239 273
 # Simple requests with redirect
240 274
 ($method, $url, $code, $method2, $url2, $code2) = undef;
241 275
 $client->max_redirects(3);
242  
-$client->get(
243  
-    'http://www.google.com' => sub {
244  
-        my ($self, $tx) = @_;
245  
-        $method  = $tx->req->method;
246  
-        $url     = $tx->req->url;
247  
-        $code    = $tx->res->code;
248  
-        $method2 = $tx->previous->req->method;
249  
-        $url2    = $tx->previous->req->url;
250  
-        $code2   = $tx->previous->res->code;
251  
-    }
252  
-)->start;
253  
-$client->max_redirects(0);
254  
-is $method,  'GET',                   'right method';
255  
-is $url,     'http://www.google.de/', 'right url';
256  
-is $code,    200,                     'right status';
257  
-is $method2, 'GET',                   'right method';
258  
-is $url2,    'http://www.google.com', 'right url';
259  
-is $code2,   302,                     'right status';
  276
+SKIP: {
  277
+    skip 'No previous tx succeeded', 6 unless defined $tx->previous;
  278
+    $client->get(
  279
+        'http://www.google.com' => sub {
  280
+            my ($self, $tx) = @_;
  281
+            $method  = $tx->req->method;
  282
+            $url     = $tx->req->url;
  283
+            $code    = $tx->res->code;
  284
+            $method2 = $tx->previous->req->method;
  285
+            $url2    = $tx->previous->req->url;
  286
+            $code2   = $tx->previous->res->code;
  287
+        }
  288
+    )->start;
  289
+    $client->max_redirects(0);
  290
+    is $method,  'GET',                   'right method';
  291
+    is $url,     'http://www.google.de/', 'right url';
  292
+    is $code,    200,                     'right status';
  293
+    is $method2, 'GET',                   'right method';
  294
+    is $url2,    'http://www.google.com', 'right url';
  295
+    is $code2,   302,                     'right status';
  296
+}
260 297
 
261 298
 # Simple requests with redirect and no callback
262 299
 $client->max_redirects(3);
@@ -265,9 +302,12 @@ $client->max_redirects(0);
265 302
 is $tx->req->method, 'GET',                   'right method';
266 303
 is $tx->req->url,    'http://www.google.de/', 'right url';
267 304
 is $tx->res->code,   200,                     'right status';
268  
-is $tx->previous->req->method, 'GET',                   'right method';
269  
-is $tx->previous->req->url,    'http://www.google.com', 'right url';
270  
-is $tx->previous->res->code,   302,                     'right status';
  305
+SKIP: {
  306
+    skip 'No previous tx succeeded', 3 unless defined $tx->previous;
  307
+    is $tx->previous->req->method, 'GET',                   'right method';
  308
+    is $tx->previous->req->url,    'http://www.google.com', 'right url';
  309
+    is $tx->previous->res->code,   302,                     'right status';
  310
+}
271 311
 
272 312
 # Custom chunked request without callback
273 313
 $tx = Mojo::Transaction::HTTP->new;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.