Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created December 16, 2009 22:56
Show Gist options
  • Save kamipo/258296 to your computer and use it in GitHub Desktop.
Save kamipo/258296 to your computer and use it in GitHub Desktop.
package LWP::UserAgent::ProxyConnect;
use strict;
use warnings;
our $VERSION = '0.01';
use LWP::UserAgent ();
{
use LWP::Protocol::http ();
my $orig = \&LWP::Protocol::http::request;
my $method = sub {
my ($self, $request, $proxy, $arg, $size, $timeout) = @_;
# $request->url->schemeが'https'で$proxyがあるとき
if ($request->url->scheme eq 'https' and $proxy) {
# Crypt::SSLeayのために環境変数をセットして
local $ENV{HTTPS_PROXY} = $proxy->host_port;
no warnings 'uninitialized';
my ($username, $password) = split /:/, $proxy->userinfo;
local $ENV{HTTPS_PROXY_USERNAME} = $username;
local $ENV{HTTPS_PROXY_PASSWORD} = $password;
use warnings 'uninitialized';
# $selfをLWP::Protocol::httpsのインスタンスにして
bless $self, LWP::Protocol::implementor('https');
# $proxyをなしにして元の処理に戻る
$orig->($self, $request, undef, $arg, $size, $timeout);
}
else {
goto $orig;
}
};
no strict 'refs';
no warnings 'redefine';
*{'LWP::Protocol::http::request'} = $method;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment