Skip to content

Commit

Permalink
replace proxy method in Mojo::Message::Request with an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 21, 2015
1 parent 6a0abf1 commit 75fe9eb
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 52 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,6 +1,10 @@

6.25 2015-10-21
- Deprecated boolean and string arguments of Mojo::Message::Request::proxy in
favor of Mojo::Message::Request::via_proxy.
- Replaced proxy method in Mojo::Message::Request with an attribute.
- Moved all bundled files into "resources" directories.
- Added via_proxy attribute to Mojo::Message::Request.
- Improved Mojo::DOM::CSS to support selectors with leading and trailing
whitespace.
- Improved rendering of built-in templates to show actual template names in
Expand Down
57 changes: 36 additions & 21 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -2,13 +2,14 @@ package Mojo::Message::Request;
use Mojo::Base 'Mojo::Message';

use Mojo::Cookie::Request;
use Mojo::Util qw(b64_encode b64_decode);
use Mojo::Util qw(b64_encode b64_decode deprecated);
use Mojo::URL;

has env => sub { {} };
has method => 'GET';
has url => sub { Mojo::URL->new };
has 'reverse_proxy';
has url => sub { Mojo::URL->new };
has via_proxy => 1;

sub clone {
my $self = shift;
Expand Down Expand Up @@ -75,8 +76,8 @@ sub fix_headers {
}

# Basic proxy authentication
return $self unless my $proxy = $self->proxy;
return $self unless my $info = $proxy->userinfo;
return $self unless $self->via_proxy && (my $proxy = $self->proxy);
return $self unless my $info = $proxy->userinfo;
$headers->proxy_authorization('Basic ' . b64_encode($info, ''))
unless $headers->proxy_authorization;
return $self;
Expand Down Expand Up @@ -146,10 +147,21 @@ sub parse {
return $self;
}

# DEPRECATED in Clinking Beer Mugs!
sub proxy {
my $self = shift;

return $self->{proxy} unless @_;
$self->{proxy} = !$_[0] || ref $_[0] ? shift : Mojo::URL->new(shift);

deprecated 'Calling Mojo::Message::Request::proxy with a boolean argument is'
. ' DEPRECATED in favor of Mojo::Message::Request::via_proxy'
and return $self->via_proxy(0)
unless $_[0];
deprecated
'Calling Mojo::Message::Request::proxy with a string argument is DEPRECATED'
unless ref $_[0];

$self->{proxy} = ref $_[0] ? shift : Mojo::URL->new(shift);
return $self;
}

Expand Down Expand Up @@ -241,7 +253,7 @@ sub _start_line {
}

# Proxy
elsif ($self->proxy && $url->protocol ne 'https') {
elsif ($self->proxy && $self->via_proxy && $url->protocol ne 'https') {
$path = $url->clone->userinfo(undef) unless $self->is_handshake;
}

Expand Down Expand Up @@ -315,6 +327,20 @@ Direct access to the C<CGI> or C<PSGI> environment hash if available.
HTTP request method, defaults to C<GET>.
=head2 proxy
my $url = $req->proxy;
$req = $req->proxy(Mojo::URL->new('http://127.0.0.1:3000'));
Proxy URL for request.
=head2 reverse_proxy
my $bool = $req->reverse_proxy;
$req = $req->reverse_proxy($bool);
Request has been performed through a reverse proxy.
=head2 url
my $url = $req->url;
Expand All @@ -327,12 +353,12 @@ HTTP request URL, defaults to a L<Mojo::URL> object.
my $host = $req->url->to_abs->host;
my $path = $req->url->to_abs->path;
=head2 reverse_proxy
=head2 via_proxy
my $bool = $req->reverse_proxy;
$req = $req->reverse_proxy($bool);
my $bool = $req->via_proxy;
$req = $req->via_proxy($bool);
Request has been performed through a reverse proxy.
Request can be performed through a proxy server.
=head1 METHODS
Expand Down Expand Up @@ -439,17 +465,6 @@ default.
Parse HTTP request chunks or environment hash.
=head2 proxy
my $proxy = $req->proxy;
$req = $req->proxy('http://foo:bar@127.0.0.1:3000');
$req = $req->proxy(Mojo::URL->new('http://127.0.0.1:3000'));
Proxy URL for request.
# Disable proxy
$req->proxy(0);
=head2 query_params
my $params = $req->query_params;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -100,7 +100,7 @@ sub _connect {
($proto, @options{qw(address port)}) = $t->endpoint($tx);
my $req = $tx->req;
my $userinfo = $req->proxy->userinfo;
$req->proxy(0);
$req->via_proxy(0);
@options{qw(socks_user socks_pass)} = split ':', $userinfo if $userinfo;
}

Expand Down Expand Up @@ -143,7 +143,7 @@ sub _connect_proxy {
if $tx->error || !$tx->res->is_status_class(200) || !$tx->keep_alive;

# Start real transaction
$old->req->proxy(0);
$old->req->via_proxy(0);
my $id = $tx->connection;
return $self->_start($nb, $old->connection($id), $cb)
unless $tx->req->url->protocol eq 'https';
Expand Down
8 changes: 5 additions & 3 deletions lib/Mojo/UserAgent/Proxy.pm
@@ -1,6 +1,8 @@
package Mojo::UserAgent::Proxy;
use Mojo::Base -base;

use Mojo::URL;

has [qw(http https not)];

sub detect {
Expand All @@ -20,16 +22,16 @@ sub prepare {
$self->detect if $ENV{MOJO_PROXY};
my $req = $tx->req;
my $url = $req->url;
return if !$self->is_needed($url->host) || defined $req->proxy;
return unless $req->via_proxy && $self->is_needed($url->host);

# HTTP proxy
my $proto = $url->protocol;
my $http = $self->http;
$req->proxy($http) if $http && $proto eq 'http';
$req->proxy(Mojo::URL->new($http)) if $http && $proto eq 'http';

# HTTPS proxy
my $https = $self->https;
$req->proxy($https) if $https && $proto eq 'https';
$req->proxy(Mojo::URL->new($https)) if $https && $proto eq 'https';
}

1;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -32,7 +32,7 @@ sub endpoint {
my $socks;
if (my $proxy = $req->proxy) { $socks = $proxy->protocol eq 'socks' }
return $self->_proxy($tx, $proto, $host, $port)
if $proto eq 'http' && !$req->is_handshake && !$socks;
if $proto eq 'http' && $req->via_proxy && !$req->is_handshake && !$socks;

return $proto, $host, $port;
}
Expand All @@ -47,7 +47,7 @@ sub proxy_connect {
return undef if uc $req->method eq 'CONNECT';

# No proxy
return undef unless my $proxy = $req->proxy;
return undef unless $req->via_proxy && (my $proxy = $req->proxy);
return undef if $proxy->protocol eq 'socks';

# WebSocket and/or HTTPS
Expand Down
29 changes: 23 additions & 6 deletions t/mojo/request.t
Expand Up @@ -8,6 +8,7 @@ use Mojo::Content::Single;
use Mojo::Content::MultiPart;
use Mojo::Cookie::Request;
use Mojo::Message::Request;
use Mojo::URL;
use Mojo::Util 'encode';

# Parse HTTP 1.1 message with huge "Cookie" header exceeding all limits
Expand Down Expand Up @@ -1307,7 +1308,7 @@ $req->headers->connection('Upgrade');
$req->headers->sec_websocket_accept('abcdef=');
$req->headers->sec_websocket_protocol('sample');
$req->headers->upgrade('websocket');
$req->proxy('http://127.0.0.2:8080');
$req->proxy(Mojo::URL->new('http://127.0.0.2:8080'));
$req = Mojo::Message::Request->new->parse($req->to_string);
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
Expand All @@ -1332,7 +1333,7 @@ $req->method('GET');
$req->url->parse('http://127.0.0.1/foo/bar');
$req->headers->expect('100-continue');
$req->body("Hello World!\n");
$req->proxy('http://127.0.0.2:8080');
$req->proxy(Mojo::URL->new('http://127.0.0.2:8080'));
$req = Mojo::Message::Request->new->parse($req->to_string);
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
Expand All @@ -1344,13 +1345,29 @@ is $req->headers->host, '127.0.0.1', 'right "Host" value';
is $req->headers->content_length, '13', 'right "Content-Length" value';
is $req->body, "Hello World!\n", 'right content';

# Build full HTTP 1.1 proxy request (proxy disabled)
$req = Mojo::Message::Request->new;
$req->method('GET');
$req->url->parse('http://127.0.0.1/foo/bar');
$req->body("Hello World!\n");
$req->via_proxy(0)->proxy(Mojo::URL->new('http://127.0.0.2:8080'));
$req = Mojo::Message::Request->new->parse($req->to_string);
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
is $req->version, '1.1', 'right version';
is $req->url, '/foo/bar', 'right URL';
is $req->url->to_abs, 'http://127.0.0.1/foo/bar', 'right absolute URL';
is $req->headers->host, '127.0.0.1', 'right "Host" value';
is $req->headers->content_length, '13', 'right "Content-Length" value';
is $req->body, "Hello World!\n", 'right content';

# Build full HTTP 1.1 proxy request (HTTPS)
$req = Mojo::Message::Request->new;
$req->method('GET');
$req->url->parse('https://127.0.0.1/foo/bar');
$req->headers->expect('100-continue');
$req->body("Hello World!\n");
$req->proxy('http://127.0.0.2:8080');
$req->proxy(Mojo::URL->new('http://127.0.0.2:8080'));
$req = Mojo::Message::Request->new->parse($req->to_string);
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
Expand All @@ -1368,7 +1385,7 @@ $req->method('GET');
$req->url->parse('http://Aladdin:open%20sesame@127.0.0.1/foo/bar');
$req->headers->expect('100-continue');
$req->body("Hello World!\n");
$req->proxy('http://Aladdin:open%20sesame@127.0.0.2:8080');
$req->proxy(Mojo::URL->new('http://Aladdin:open%20sesame@127.0.0.2:8080'));
$req = Mojo::Message::Request->new->parse($req->to_string);
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
Expand All @@ -1391,7 +1408,7 @@ $req->method('GET');
$req->url->parse('http://Aladdin:open%20sesame@127.0.0.1/foo/bar');
$req->headers->expect('100-continue');
$req->body("Hello World!\n");
$req->proxy('http://Aladdin:open%20sesame@127.0.0.2:8080');
$req->proxy(Mojo::URL->new('http://Aladdin:open%20sesame@127.0.0.2:8080'));
$clone = $req->clone;
$req = Mojo::Message::Request->new->parse($req->to_string);
ok $req->is_finished, 'request is finished';
Expand Down Expand Up @@ -1428,7 +1445,7 @@ is $clone->body, "Hello World!\n", 'right content';
$req = Mojo::Message::Request->new;
$req->method('CONNECT');
$req->url->parse('http://Aladdin:open%20sesame@bücher.ch:3000/foo/bar');
$req->proxy('http://Aladdin:open%20sesame@127.0.0.2:8080');
$req->proxy(Mojo::URL->new('http://Aladdin:open%20sesame@127.0.0.2:8080'));
$req = Mojo::Message::Request->new->parse($req->to_string);
ok $req->is_finished, 'request is finished';
is $req->method, 'CONNECT', 'right method';
Expand Down

0 comments on commit 75fe9eb

Please sign in to comment.