}
$self->{conn_cache} = $cache;
}
$old;
}
sub add_handler {
my($self, $phase, $cb, %spec) = @_;
$spec{line} ||= join(":", (caller)[1,2]);
my $conf = $self->{handlers}{$phase} ||= do {
require HTTP::Config;
HTTP::Config->new;
};
$conf->add(%spec, callback => $cb);
}
sub set_my_handler {
my($self, $phase, $cb, %spec) = @_;
$spec{owner} = (caller(1))[3] unless exists $spec{owner};
$self->remove_handler($phase, %spec);
$spec{line} ||= join(":", (caller)[1,2]);
$self->add_handler($phase, $cb, %spec) if $cb;
}
sub get_my_handler {
my $self = shift;
my $phase = shift;
my $init = pop if @_ % 2;
my %spec = @_;
my $conf = $self->{handlers}{$phase};
unless ($conf) {
return unless $init;
require HTTP::Config;
$conf = $self->{handlers}{$phase} = HTTP::Config->new;
}
$spec{owner} = (caller(1))[3] unless exists $spec{owner};
my @h = $conf->find(%spec);
if (!@h && $init) {
if (ref($init) eq "CODE") {
$init->(\%spec);
}
elsif (ref($init) eq "HASH") {
while (my($k, $v) = each %$init) {
$spec{$k} = $v;
}
}
$spec{callback} ||= sub {};
$spec{line} ||= join(":", (caller)[1,2]);
$conf->add(\%spec);
return \%spec;
}
return wantarray ? @h : $h[0];
}
sub remove_handler {
my($self, $phase, %spec) = @_;
if ($phase) {
my $conf = $self->{handlers}{$phase} || return;
my @h = $conf->remove(%spec);
delete $self->{handlers}{$phase} if $conf->empty;
return @h;
}
return unless $self->{handlers};
return map $self->remove_handler($_), sort keys %{$self->{handlers}};
}
sub handlers {
my($self, $phase, $o) = @_;
my @h;
if ($o->{handlers} && $o->{handlers}{$phase}) {
push(@h, map +{ callback => $_ }, @{$o->{handlers}{$phase}});
}
if (my $conf = $self->{handlers}{$phase}) {
push(@h, $conf->matching($o));
}
return @h;
}
sub run_handlers {
my($self, $phase, $o) = @_;
if (defined(wantarray)) {
for my $h ($self->handlers($phase, $o)) {
my $ret = $h->{callback}->($o, $self, $h);
return $ret if $ret;
}
return undef;
}
for my $h ($self->handlers($phase, $o)) {
$h->{callback}->($o, $self, $h);
}
}
# depreciated
sub use_eval { shift->_elem('use_eval', @_); }
sub use_alarm
{
=8= |