| 1 | package LWP::Protocol::connect::Socket::Base; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '6.09'; # VERSION | ||
| 7 | |||
| 8 | use LWP::UserAgent; | ||
| 9 | use HTTP::Request; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $class = shift; | ||
| 13 | my %args = @_; | ||
| 14 | my $conn = $class->_proxy_connect( \%args ); | ||
| 15 | bless $conn, $class; | ||
| 16 | |||
| 17 | $conn->http_configure( \%args ); | ||
| 18 | return $conn; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub _proxy_connect { | ||
| 22 | my ( $class, $args ) = @_; | ||
| 23 | my $agent = $args->{Agent}; | ||
| 24 | my ($user, $pass); | ||
| 25 | if( defined $args->{ProxyUserinfo} ) { | ||
| 26 | ($user, $pass) = split(':', URI::Escape::uri_unescape( $args->{ProxyUserinfo} ), 2); | ||
| 27 | } | ||
| 28 | my $proxy_host_port = $args->{'ProxyAddr'}.':'.$args->{'ProxyPort'}; | ||
| 29 | my $cur_http_proxy = $agent->proxy('http'); | ||
| 30 | $agent->proxy( http => 'http://'. | ||
| 31 | ( defined $user ? $user.':'.$pass.'@' : '' ). | ||
| 32 | $proxy_host_port.'/' ); | ||
| 33 | |||
| 34 | my $host_port = $args->{PeerAddr}.":".$args->{PeerPort}; | ||
| 35 | my $host = 'http://'.$host_port; | ||
| 36 | my $request = HTTP::Request->new( CONNECT => $host ); | ||
| 37 | my $response = $agent->request( $request ); | ||
| 38 | $agent->proxy( http => $cur_http_proxy ); | ||
| 39 | |||
| 40 | if( $response->is_error ) { | ||
| 41 | die('error while CONNECT thru proxy: '.$response->status_line ); | ||
| 42 | } | ||
| 43 | my $conn = $response->{client_socket}; | ||
| 44 | |||
| 45 | delete $args->{ProxyAddr}; | ||
| 46 | delete $args->{ProxyPort}; | ||
| 47 | delete $args->{ProxyUserinfo}; | ||
| 48 | delete $args->{Agent}; | ||
| 49 | |||
| 50 | return( $conn ); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub http_connect { | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; |