Skip to content

Commit

Permalink
Support proxy requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gbarr committed Feb 22, 2012
1 parent 9162ec1 commit 8c5e75b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes.txt
Expand Up @@ -3,6 +3,7 @@ Revision history for LWP-Protocol-AnyEvent-http
1.0.4 2012-mm-dd
- Preserve AnyEvent::HTTP pseudo heads by prefixing with X-AE-
- Fix duplicate headers in response
- Add proxy support

1.0.3 2011-06-17
- Skip tests that will fail due to DNS hijacking.
Expand Down
7 changes: 7 additions & 0 deletions inc/Test/HTTP/log-server
Expand Up @@ -77,6 +77,13 @@ SERVERLOOP: {
return $buf if $count-- > 0;
return undef; # done
});
} elsif ($location =~ m!^/referer$!) {
if (my $referer = $r->headers->{referer}) {
$res = HTTP::Response->new(302);
$res->header('location', $referer);
} else {
$res = HTTP::Response->new(204);
}
} elsif ($location =~ m!^/error/after_headers$!) {
my $count = 2;
$res = HTTP::Response->new(200, "OK", undef, sub {
Expand Down
14 changes: 5 additions & 9 deletions lib/LWP/Protocol/AnyEvent/http.pm
Expand Up @@ -45,8 +45,6 @@ sub _set_response_headers {
sub request {
my ($self, $request, $proxy, $arg, $size, $timeout) = @_;

#TODO Obey $proxy

my $method = $request->method();
my $url = $request->uri();
my %headers; $request->headers()->scan(sub { $headers{$_[0]} = $_[1]; });
Expand All @@ -68,6 +66,11 @@ sub request {
$opts{body} = $$body if defined($body);
$opts{timeout} = $timeout if defined($timeout);

if ($proxy) {
my $proxy_uri = URI->new($proxy);
$opts{proxy} = [$proxy_uri->host, $proxy_uri->port, $proxy_uri->scheme];
}

# Let LWP handle redirects and cookies.
my $guard = http_request(
$method => $url,
Expand Down Expand Up @@ -206,13 +209,6 @@ An alternative to this module. Doesn't help code that uses L<LWP::Simple> or L<L
=back
=head1 KNOWN BUGS
=head2 Ignores proxy settings
I haven't gotten around to implementing proxy support.
=head1 BUGS
Please report any bugs or feature requests to C<bug-LWP-Protocol-AnyEvent-http at rt.cpan.org>,
Expand Down
49 changes: 49 additions & 0 deletions t/07_proxy.t
@@ -0,0 +1,49 @@
#!perl -w
use strict;
use Test::More;

use AnyEvent;
use LWP::Protocol::AnyEvent::http;
use LWP::UserAgent;

# Check whether we can launch the local webserver
if (! eval {
use lib '../inc', 'inc';
require Test::HTTP::LocalServer;
1;
}) {
plan skip_all => "Couldn't launch test server: $@";
} else {
plan tests => 4;
};

# Launch a timer
my $timer_events = 0;
my $t = AnyEvent->timer(
after => 1, interval => 1, cb => sub { diag "Waiting for reply\n"; $timer_events++ }
);

my $client = LWP::UserAgent->new();

my $server = Test::HTTP::LocalServer->spawn(
#debug => 1,
);
my $url = $server->url;
diag "Retrieving URL: " . $url;

$client->proxy(http => $url);

my $fetch_url = "http://no.such.domain";
my $res = $client->get($fetch_url);
is $res->code, 200, "Got response";

is $fetch_url, $res->content, "Sent proxy requet";


undef $t; # stop the timer

diag "Shutting down server";
$server->stop;
undef $server;
diag "Done";

0 comments on commit 8c5e75b

Please sign in to comment.