22/38

Live example: proxy authentication

    use MIME::Base64 qw( encode_base64 );
    
    # the encoded user:password pair
    my $token = "Basic " . encode_base64( "login:password" );
    chomp $token;    # grr
    
    # the authentication filter
    $proxy->push_filter(
        request => HTTP::Proxy::HeaderFilter::simple->new(
            sub {
                my ( $self, $headers, $request ) = @_;
                my $auth = $self->proxy->hop_headers->header('Proxy-Authorization')
                  || "";
    
                # check the hard-coded credentials
                if ( $auth ne $token ) {
                    my $response = HTTP::Response->new(407);
                    $response->header(
                        Proxy_Authenticate => 'Basic realm="HTTP::Proxy"' );
                    $self->proxy->response($response);
                }
            }
        )
    );