Blame lib/HTTP/Config.pm

Packit a09cf7
package HTTP::Config;
Packit a09cf7
Packit a09cf7
use strict;
Packit a09cf7
use warnings;
Packit a09cf7
Packit a09cf7
our $VERSION = '6.18';
Packit a09cf7
Packit a09cf7
use URI;
Packit a09cf7
Packit a09cf7
sub new {
Packit a09cf7
    my $class = shift;
Packit a09cf7
    return bless [], $class;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub entries {
Packit a09cf7
    my $self = shift;
Packit a09cf7
    @$self;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub empty {
Packit a09cf7
    my $self = shift;
Packit a09cf7
    not @$self;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub add {
Packit a09cf7
    if (@_ == 2) {
Packit a09cf7
        my $self = shift;
Packit a09cf7
        push(@$self, shift);
Packit a09cf7
        return;
Packit a09cf7
    }
Packit a09cf7
    my($self, %spec) = @_;
Packit a09cf7
    push(@$self, \%spec);
Packit a09cf7
    return;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub find2 {
Packit a09cf7
    my($self, %spec) = @_;
Packit a09cf7
    my @found;
Packit a09cf7
    my @rest;
Packit a09cf7
 ITEM:
Packit a09cf7
    for my $item (@$self) {
Packit a09cf7
        for my $k (keys %spec) {
Packit a09cf7
            no warnings 'uninitialized';
Packit a09cf7
            if (!exists $item->{$k} || $spec{$k} ne $item->{$k}) {
Packit a09cf7
                push(@rest, $item);
Packit a09cf7
                next ITEM;
Packit a09cf7
            }
Packit a09cf7
        }
Packit a09cf7
        push(@found, $item);
Packit a09cf7
    }
Packit a09cf7
    return \@found unless wantarray;
Packit a09cf7
    return \@found, \@rest;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub find {
Packit a09cf7
    my $self = shift;
Packit a09cf7
    my $f = $self->find2(@_);
Packit a09cf7
    return @$f if wantarray;
Packit a09cf7
    return $f->[0];
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub remove {
Packit a09cf7
    my($self, %spec) = @_;
Packit a09cf7
    my($removed, $rest) = $self->find2(%spec);
Packit a09cf7
    @$self = @$rest if @$removed;
Packit a09cf7
    return @$removed;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
my %MATCH = (
Packit a09cf7
    m_scheme => sub {
Packit a09cf7
        my($v, $uri) = @_;
Packit a09cf7
        return $uri->_scheme eq $v;  # URI known to be canonical
Packit a09cf7
    },
Packit a09cf7
    m_secure => sub {
Packit a09cf7
        my($v, $uri) = @_;
Packit a09cf7
        my $secure = $uri->can("secure") ? $uri->secure : $uri->_scheme eq "https";
Packit a09cf7
        return $secure == !!$v;
Packit a09cf7
    },
Packit a09cf7
    m_host_port => sub {
Packit a09cf7
        my($v, $uri) = @_;
Packit a09cf7
        return unless $uri->can("host_port");
Packit a09cf7
        return $uri->host_port eq $v, 7;
Packit a09cf7
    },
Packit a09cf7
    m_host => sub {
Packit a09cf7
        my($v, $uri) = @_;
Packit a09cf7
        return unless $uri->can("host");
Packit a09cf7
        return $uri->host eq $v, 6;
Packit a09cf7
    },
Packit a09cf7
    m_port => sub {
Packit a09cf7
        my($v, $uri) = @_;
Packit a09cf7
        return unless $uri->can("port");
Packit a09cf7
        return $uri->port eq $v;
Packit a09cf7
    },
Packit a09cf7
    m_domain => sub {
Packit a09cf7
        my($v, $uri) = @_;
Packit a09cf7
        return unless $uri->can("host");
Packit a09cf7
        my $h = $uri->host;
Packit a09cf7
        $h = "$h.local" unless $h =~ /\./;
Packit a09cf7
        $v = ".$v" unless $v =~ /^\./;
Packit a09cf7
        return length($v), 5 if substr($h, -length($v)) eq $v;
Packit a09cf7
        return 0;
Packit a09cf7
    },
Packit a09cf7
    m_path => sub {
Packit a09cf7
        my($v, $uri) = @_;
Packit a09cf7
        return unless $uri->can("path");
Packit a09cf7
        return $uri->path eq $v, 4;
Packit a09cf7
    },
Packit a09cf7
    m_path_prefix => sub {
Packit a09cf7
        my($v, $uri) = @_;
Packit a09cf7
        return unless $uri->can("path");
Packit a09cf7
        my $path = $uri->path;
Packit a09cf7
        my $len = length($v);
Packit a09cf7
        return $len, 3 if $path eq $v;
Packit a09cf7
        return 0 if length($path) <= $len;
Packit a09cf7
        $v .= "/" unless $v =~ m,/\z,,;
Packit a09cf7
        return $len, 3 if substr($path, 0, length($v)) eq $v;
Packit a09cf7
        return 0;
Packit a09cf7
    },
Packit a09cf7
    m_path_match => sub {
Packit a09cf7
        my($v, $uri) = @_;
Packit a09cf7
        return unless $uri->can("path");
Packit a09cf7
        return $uri->path =~ $v;
Packit a09cf7
    },
Packit a09cf7
    m_uri__ => sub {
Packit a09cf7
        my($v, $k, $uri) = @_;
Packit a09cf7
        return unless $uri->can($k);
Packit a09cf7
        return 1 unless defined $v;
Packit a09cf7
        return $uri->$k eq $v;
Packit a09cf7
    },
Packit a09cf7
    m_method => sub {
Packit a09cf7
        my($v, $uri, $request) = @_;
Packit a09cf7
        return $request && $request->method eq $v;
Packit a09cf7
    },
Packit a09cf7
    m_proxy => sub {
Packit a09cf7
        my($v, $uri, $request) = @_;
Packit a09cf7
        return $request && ($request->{proxy} || "") eq $v;
Packit a09cf7
    },
Packit a09cf7
    m_code => sub {
Packit a09cf7
        my($v, $uri, $request, $response) = @_;
Packit a09cf7
        $v =~ s/xx\z//;
Packit a09cf7
        return unless $response;
Packit a09cf7
        return length($v), 2 if substr($response->code, 0, length($v)) eq $v;
Packit a09cf7
    },
Packit a09cf7
    m_media_type => sub {  # for request too??
Packit a09cf7
        my($v, $uri, $request, $response) = @_;
Packit a09cf7
        return unless $response;
Packit a09cf7
        return 1, 1 if $v eq "*/*";
Packit a09cf7
        my $ct = $response->content_type;
Packit a09cf7
        return 2, 1 if $v =~ s,/\*\z,, && $ct =~ m,^\Q$v\E/,;
Packit a09cf7
        return 3, 1 if $v eq "html" && $response->content_is_html;
Packit a09cf7
        return 4, 1 if $v eq "xhtml" && $response->content_is_xhtml;
Packit a09cf7
        return 10, 1 if $v eq $ct;
Packit a09cf7
        return 0;
Packit a09cf7
    },
Packit a09cf7
    m_header__ => sub {
Packit a09cf7
        my($v, $k, $uri, $request, $response) = @_;
Packit a09cf7
        return unless $request;
Packit a09cf7
        return 1 if $request->header($k) eq $v;
Packit a09cf7
        return 1 if $response && $response->header($k) eq $v;
Packit a09cf7
        return 0;
Packit a09cf7
    },
Packit a09cf7
    m_response_attr__ => sub {
Packit a09cf7
        my($v, $k, $uri, $request, $response) = @_;
Packit a09cf7
        return unless $response;
Packit a09cf7
        return 1 if !defined($v) && exists $response->{$k};
Packit a09cf7
        return 0 unless exists $response->{$k};
Packit a09cf7
        return 1 if $response->{$k} eq $v;
Packit a09cf7
        return 0;
Packit a09cf7
    },
Packit a09cf7
);
Packit a09cf7
Packit a09cf7
sub matching {
Packit a09cf7
    my $self = shift;
Packit a09cf7
    if (@_ == 1) {
Packit a09cf7
        if ($_[0]->can("request")) {
Packit a09cf7
            unshift(@_, $_[0]->request);
Packit a09cf7
            unshift(@_, undef) unless defined $_[0];
Packit a09cf7
        }
Packit a09cf7
        unshift(@_, $_[0]->uri_canonical) if $_[0] && $_[0]->can("uri_canonical");
Packit a09cf7
    }
Packit a09cf7
    my($uri, $request, $response) = @_;
Packit a09cf7
    $uri = URI->new($uri) unless ref($uri);
Packit a09cf7
Packit a09cf7
    my @m;
Packit a09cf7
 ITEM:
Packit a09cf7
    for my $item (@$self) {
Packit a09cf7
        my $order;
Packit a09cf7
        for my $ikey (keys %$item) {
Packit a09cf7
            my $mkey = $ikey;
Packit a09cf7
            my $k;
Packit a09cf7
            $k = $1 if $mkey =~ s/__(.*)/__/;
Packit a09cf7
            if (my $m = $MATCH{$mkey}) {
Packit a09cf7
                #print "$ikey $mkey\n";
Packit a09cf7
                my($c, $o);
Packit a09cf7
                my @arg = (
Packit a09cf7
                    defined($k) ? $k : (),
Packit a09cf7
                    $uri, $request, $response
Packit a09cf7
                );
Packit a09cf7
                my $v = $item->{$ikey};
Packit a09cf7
                $v = [$v] unless ref($v) eq "ARRAY";
Packit a09cf7
                for (@$v) {
Packit a09cf7
                    ($c, $o) = $m->($_, @arg);
Packit a09cf7
                    #print "  - $_ ==> $c $o\n";
Packit a09cf7
                    last if $c;
Packit a09cf7
                }
Packit a09cf7
                next ITEM unless $c;
Packit a09cf7
                $order->[$o || 0] += $c;
Packit a09cf7
            }
Packit a09cf7
        }
Packit a09cf7
        $order->[7] ||= 0;
Packit a09cf7
        $item->{_order} = join(".", reverse map sprintf("%03d", $_ || 0), @$order);
Packit a09cf7
        push(@m, $item);
Packit a09cf7
    }
Packit a09cf7
    @m = sort { $b->{_order} cmp $a->{_order} } @m;
Packit a09cf7
    delete $_->{_order} for @m;
Packit a09cf7
    return @m if wantarray;
Packit a09cf7
    return $m[0];
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub add_item {
Packit a09cf7
    my $self = shift;
Packit a09cf7
    my $item = shift;
Packit a09cf7
    return $self->add(item => $item, @_);
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub remove_items {
Packit a09cf7
    my $self = shift;
Packit a09cf7
    return map $_->{item}, $self->remove(@_);
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub matching_items {
Packit a09cf7
    my $self = shift;
Packit a09cf7
    return map $_->{item}, $self->matching(@_);
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
1;
Packit a09cf7
Packit a09cf7
=pod
Packit a09cf7
Packit a09cf7
=encoding UTF-8
Packit a09cf7
Packit a09cf7
=head1 NAME
Packit a09cf7
Packit a09cf7
HTTP::Config - Configuration for request and response objects
Packit a09cf7
Packit a09cf7
=head1 VERSION
Packit a09cf7
Packit a09cf7
version 6.18
Packit a09cf7
Packit a09cf7
=head1 SYNOPSIS
Packit a09cf7
Packit a09cf7
 use HTTP::Config;
Packit a09cf7
 my $c = HTTP::Config->new;
Packit a09cf7
 $c->add(m_domain => ".example.com", m_scheme => "http", verbose => 1);
Packit a09cf7
 
Packit a09cf7
 use HTTP::Request;
Packit a09cf7
 my $request = HTTP::Request->new(GET => "http://www.example.com");
Packit a09cf7
 
Packit a09cf7
 if (my @m = $c->matching($request)) {
Packit a09cf7
    print "Yadayada\n" if $m[0]->{verbose};
Packit a09cf7
 }
Packit a09cf7
Packit a09cf7
=head1 DESCRIPTION
Packit a09cf7
Packit a09cf7
An C<HTTP::Config> object is a list of entries that
Packit a09cf7
can be matched against request or request/response pairs.  Its
Packit a09cf7
purpose is to hold configuration data that can be looked up given a
Packit a09cf7
request or response object.
Packit a09cf7
Packit a09cf7
Each configuration entry is a hash.  Some keys specify matching to
Packit a09cf7
occur against attributes of request/response objects.  Other keys can
Packit a09cf7
be used to hold user data.
Packit a09cf7
Packit a09cf7
The following methods are provided:
Packit a09cf7
Packit a09cf7
=over 4
Packit a09cf7
Packit a09cf7
=item $conf = HTTP::Config->new
Packit a09cf7
Packit a09cf7
Constructs a new empty C<HTTP::Config> object and returns it.
Packit a09cf7
Packit a09cf7
=item $conf->entries
Packit a09cf7
Packit a09cf7
Returns the list of entries in the configuration object.
Packit a09cf7
In scalar context returns the number of entries.
Packit a09cf7
Packit a09cf7
=item $conf->empty
Packit a09cf7
Packit a09cf7
Return true if there are no entries in the configuration object.
Packit a09cf7
This is just a shorthand for C<< not $conf->entries >>.
Packit a09cf7
Packit a09cf7
=item $conf->add( %matchspec, %other )
Packit a09cf7
Packit a09cf7
=item $conf->add( \%entry )
Packit a09cf7
Packit a09cf7
Adds a new entry to the configuration.
Packit a09cf7
You can either pass separate key/value pairs or a hash reference.
Packit a09cf7
Packit a09cf7
=item $conf->remove( %spec )
Packit a09cf7
Packit a09cf7
Removes (and returns) the entries that have matches for all the key/value pairs in %spec.
Packit a09cf7
If %spec is empty this will match all entries; so it will empty the configuration object.
Packit a09cf7
Packit a09cf7
=item $conf->matching( $uri, $request, $response )
Packit a09cf7
Packit a09cf7
=item $conf->matching( $uri )
Packit a09cf7
Packit a09cf7
=item $conf->matching( $request )
Packit a09cf7
Packit a09cf7
=item $conf->matching( $response )
Packit a09cf7
Packit a09cf7
Returns the entries that match the given $uri, $request and $response triplet.
Packit a09cf7
Packit a09cf7
If called with a single $request object then the $uri is obtained by calling its 'uri_canonical' method.
Packit a09cf7
If called with a single $response object, then the request object is obtained by calling its 'request' method;
Packit a09cf7
and then the $uri is obtained as if a single $request was provided.
Packit a09cf7
Packit a09cf7
The entries are returned with the most specific matches first.
Packit a09cf7
In scalar context returns the most specific match or C<undef> in none match.
Packit a09cf7
Packit a09cf7
=item $conf->add_item( $item, %matchspec )
Packit a09cf7
Packit a09cf7
=item $conf->remove_items( %spec )
Packit a09cf7
Packit a09cf7
=item $conf->matching_items( $uri, $request, $response )
Packit a09cf7
Packit a09cf7
Wrappers that hides the entries themselves.
Packit a09cf7
Packit a09cf7
=back
Packit a09cf7
Packit a09cf7
=head2 Matching
Packit a09cf7
Packit a09cf7
The following keys on a configuration entry specify matching.  For all
Packit a09cf7
of these you can provide an array of values instead of a single value.
Packit a09cf7
The entry matches if at least one of the values in the array matches.
Packit a09cf7
Packit a09cf7
Entries that require match against a response object attribute will never match
Packit a09cf7
unless a response object was provided.
Packit a09cf7
Packit a09cf7
=over
Packit a09cf7
Packit a09cf7
=item m_scheme => $scheme
Packit a09cf7
Packit a09cf7
Matches if the URI uses the specified scheme; e.g. "http".
Packit a09cf7
Packit a09cf7
=item m_secure => $bool
Packit a09cf7
Packit a09cf7
If $bool is TRUE; matches if the URI uses a secure scheme.  If $bool
Packit a09cf7
is FALSE; matches if the URI does not use a secure scheme.  An example
Packit a09cf7
of a secure scheme is "https".
Packit a09cf7
Packit a09cf7
=item m_host_port => "$hostname:$port"
Packit a09cf7
Packit a09cf7
Matches if the URI's host_port method return the specified value.
Packit a09cf7
Packit a09cf7
=item m_host => $hostname
Packit a09cf7
Packit a09cf7
Matches if the URI's host method returns the specified value.
Packit a09cf7
Packit a09cf7
=item m_port => $port
Packit a09cf7
Packit a09cf7
Matches if the URI's port method returns the specified value.
Packit a09cf7
Packit a09cf7
=item m_domain => ".$domain"
Packit a09cf7
Packit a09cf7
Matches if the URI's host method return a value that within the given
Packit a09cf7
domain.  The hostname "www.example.com" will for instance match the
Packit a09cf7
domain ".com".
Packit a09cf7
Packit a09cf7
=item m_path => $path
Packit a09cf7
Packit a09cf7
Matches if the URI's path method returns the specified value.
Packit a09cf7
Packit a09cf7
=item m_path_prefix => $path
Packit a09cf7
Packit a09cf7
Matches if the URI's path is the specified path or has the specified
Packit a09cf7
path as prefix.
Packit a09cf7
Packit a09cf7
=item m_path_match => $Regexp
Packit a09cf7
Packit a09cf7
Matches if the regular expression matches the URI's path.  Eg. qr/\.html$/.
Packit a09cf7
Packit a09cf7
=item m_method => $method
Packit a09cf7
Packit a09cf7
Matches if the request method matches the specified value. Eg. "GET" or "POST".
Packit a09cf7
Packit a09cf7
=item m_code => $digit
Packit a09cf7
Packit a09cf7
=item m_code => $status_code
Packit a09cf7
Packit a09cf7
Matches if the response status code matches.  If a single digit is
Packit a09cf7
specified; matches for all response status codes beginning with that digit.
Packit a09cf7
Packit a09cf7
=item m_proxy => $url
Packit a09cf7
Packit a09cf7
Matches if the request is to be sent to the given Proxy server.
Packit a09cf7
Packit a09cf7
=item m_media_type => "*/*"
Packit a09cf7
Packit a09cf7
=item m_media_type => "text/*"
Packit a09cf7
Packit a09cf7
=item m_media_type => "html"
Packit a09cf7
Packit a09cf7
=item m_media_type => "xhtml"
Packit a09cf7
Packit a09cf7
=item m_media_type => "text/html"
Packit a09cf7
Packit a09cf7
Matches if the response media type matches.
Packit a09cf7
Packit a09cf7
With a value of "html" matches if $response->content_is_html returns TRUE.
Packit a09cf7
With a value of "xhtml" matches if $response->content_is_xhtml returns TRUE.
Packit a09cf7
Packit a09cf7
=item m_uri__I<$method> => undef
Packit a09cf7
Packit a09cf7
Matches if the URI object provides the method.
Packit a09cf7
Packit a09cf7
=item m_uri__I<$method> => $string
Packit a09cf7
Packit a09cf7
Matches if the URI's $method method returns the given value.
Packit a09cf7
Packit a09cf7
=item m_header__I<$field> => $string
Packit a09cf7
Packit a09cf7
Matches if either the request or the response have a header $field with the given value.
Packit a09cf7
Packit a09cf7
=item m_response_attr__I<$key> => undef
Packit a09cf7
Packit a09cf7
=item m_response_attr__I<$key> => $string
Packit a09cf7
Packit a09cf7
Matches if the response object has that key, or the entry has the given value.
Packit a09cf7
Packit a09cf7
=back
Packit a09cf7
Packit a09cf7
=head1 SEE ALSO
Packit a09cf7
Packit a09cf7
L<URI>, L<HTTP::Request>, L<HTTP::Response>
Packit a09cf7
Packit a09cf7
=head1 AUTHOR
Packit a09cf7
Packit a09cf7
Gisle Aas <gisle@activestate.com>
Packit a09cf7
Packit a09cf7
=head1 COPYRIGHT AND LICENSE
Packit a09cf7
Packit a09cf7
This software is copyright (c) 1994-2017 by Gisle Aas.
Packit a09cf7
Packit a09cf7
This is free software; you can redistribute it and/or modify it under
Packit a09cf7
the same terms as the Perl 5 programming language system itself.
Packit a09cf7
Packit a09cf7
=cut
Packit a09cf7
Packit a09cf7
__END__
Packit a09cf7
Packit a09cf7
Packit a09cf7
#ABSTRACT: Configuration for request and response objects
Packit a09cf7