<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package HTTP::Config;

use strict;
use warnings;

our $VERSION = '6.22';

use URI;

sub new {
    my $class = shift;
    return bless [], $class;
}

sub entries {
    my $self = shift;
    @$self;
}

sub empty {
    my $self = shift;
    not @$self;
}

sub add {
    if (@_ == 2) {
        my $self = shift;
        push(@$self, shift);
        return;
    }
    my($self, %spec) = @_;
    push(@$self, \%spec);
    return;
}

sub find2 {
    my($self, %spec) = @_;
    my @found;
    my @rest;
 ITEM:
    for my $item (@$self) {
        for my $k (keys %spec) {
            no warnings 'uninitialized';
            if (!exists $item-&gt;{$k} || $spec{$k} ne $item-&gt;{$k}) {
                push(@rest, $item);
                next ITEM;
            }
        }
        push(@found, $item);
    }
    return \@found unless wantarray;
    return \@found, \@rest;
}

sub find {
    my $self = shift;
    my $f = $self-&gt;find2(@_);
    return @$f if wantarray;
    return $f-&gt;[0];
}

sub remove {
    my($self, %spec) = @_;
    my($removed, $rest) = $self-&gt;find2(%spec);
    @$self = @$rest if @$removed;
    return @$removed;
}

my %MATCH = (
    m_scheme =&gt; sub {
        my($v, $uri) = @_;
        return $uri-&gt;_scheme eq $v;  # URI known to be canonical
    },
    m_secure =&gt; sub {
        my($v, $uri) = @_;
        my $secure = $uri-&gt;can("secure") ? $uri-&gt;secure : $uri-&gt;_scheme eq "https";
        return $secure == !!$v;
    },
    m_host_port =&gt; sub {
        my($v, $uri) = @_;
        return unless $uri-&gt;can("host_port");
        return $uri-&gt;host_port eq $v, 7;
    },
    m_host =&gt; sub {
        my($v, $uri) = @_;
        return unless $uri-&gt;can("host");
        return $uri-&gt;host eq $v, 6;
    },
    m_port =&gt; sub {
        my($v, $uri) = @_;
        return unless $uri-&gt;can("port");
        return $uri-&gt;port eq $v;
    },
    m_domain =&gt; sub {
        my($v, $uri) = @_;
        return unless $uri-&gt;can("host");
        my $h = $uri-&gt;host;
        $h = "$h.local" unless $h =~ /\./;
        $v = ".$v" unless $v =~ /^\./;
        return length($v), 5 if substr($h, -length($v)) eq $v;
        return 0;
    },
    m_path =&gt; sub {
        my($v, $uri) = @_;
        return unless $uri-&gt;can("path");
        return $uri-&gt;path eq $v, 4;
    },
    m_path_prefix =&gt; sub {
        my($v, $uri) = @_;
        return unless $uri-&gt;can("path");
        my $path = $uri-&gt;path;
        my $len = length($v);
        return $len, 3 if $path eq $v;
        return 0 if length($path) &lt;= $len;
        $v .= "/" unless $v =~ m,/\z,,;
        return $len, 3 if substr($path, 0, length($v)) eq $v;
        return 0;
    },
    m_path_match =&gt; sub {
        my($v, $uri) = @_;
        return unless $uri-&gt;can("path");
        return $uri-&gt;path =~ $v;
    },
    m_uri__ =&gt; sub {
        my($v, $k, $uri) = @_;
        return unless $uri-&gt;can($k);
        return 1 unless defined $v;
        return $uri-&gt;$k eq $v;
    },
    m_method =&gt; sub {
        my($v, $uri, $request) = @_;
        return $request &amp;&amp; $request-&gt;method eq $v;
    },
    m_proxy =&gt; sub {
        my($v, $uri, $request) = @_;
        return $request &amp;&amp; ($request-&gt;{proxy} || "") eq $v;
    },
    m_code =&gt; sub {
        my($v, $uri, $request, $response) = @_;
        $v =~ s/xx\z//;
        return unless $response;
        return length($v), 2 if substr($response-&gt;code, 0, length($v)) eq $v;
    },
    m_media_type =&gt; sub {  # for request too??
        my($v, $uri, $request, $response) = @_;
        return unless $response;
        return 1, 1 if $v eq "*/*";
        my $ct = $response-&gt;content_type;
        return 2, 1 if $v =~ s,/\*\z,, &amp;&amp; $ct =~ m,^\Q$v\E/,;
        return 3, 1 if $v eq "html" &amp;&amp; $response-&gt;content_is_html;
        return 4, 1 if $v eq "xhtml" &amp;&amp; $response-&gt;content_is_xhtml;
        return 10, 1 if $v eq $ct;
        return 0;
    },
    m_header__ =&gt; sub {
        my($v, $k, $uri, $request, $response) = @_;
        return unless $request;
        return 1 if $request-&gt;header($k) eq $v;
        return 1 if $response &amp;&amp; $response-&gt;header($k) eq $v;
        return 0;
    },
    m_response_attr__ =&gt; sub {
        my($v, $k, $uri, $request, $response) = @_;
        return unless $response;
        return 1 if !defined($v) &amp;&amp; exists $response-&gt;{$k};
        return 0 unless exists $response-&gt;{$k};
        return 1 if $response-&gt;{$k} eq $v;
        return 0;
    },
);

sub matching {
    my $self = shift;
    if (@_ == 1) {
        if ($_[0]-&gt;can("request")) {
            unshift(@_, $_[0]-&gt;request);
            unshift(@_, undef) unless defined $_[0];
        }
        unshift(@_, $_[0]-&gt;uri_canonical) if $_[0] &amp;&amp; $_[0]-&gt;can("uri_canonical");
    }
    my($uri, $request, $response) = @_;
    $uri = URI-&gt;new($uri) unless ref($uri);

    my @m;
 ITEM:
    for my $item (@$self) {
        my $order;
        for my $ikey (keys %$item) {
            my $mkey = $ikey;
            my $k;
            $k = $1 if $mkey =~ s/__(.*)/__/;
            if (my $m = $MATCH{$mkey}) {
                #print "$ikey $mkey\n";
                my($c, $o);
                my @arg = (
                    defined($k) ? $k : (),
                    $uri, $request, $response
                );
                my $v = $item-&gt;{$ikey};
                $v = [$v] unless ref($v) eq "ARRAY";
                for (@$v) {
                    ($c, $o) = $m-&gt;($_, @arg);
                    #print "  - $_ ==&gt; $c $o\n";
                    last if $c;
                }
                next ITEM unless $c;
                $order-&gt;[$o || 0] += $c;
            }
        }
        $order-&gt;[7] ||= 0;
        $item-&gt;{_order} = join(".", reverse map sprintf("%03d", $_ || 0), @$order);
        push(@m, $item);
    }
    @m = sort { $b-&gt;{_order} cmp $a-&gt;{_order} } @m;
    delete $_-&gt;{_order} for @m;
    return @m if wantarray;
    return $m[0];
}

sub add_item {
    my $self = shift;
    my $item = shift;
    return $self-&gt;add(item =&gt; $item, @_);
}

sub remove_items {
    my $self = shift;
    return map $_-&gt;{item}, $self-&gt;remove(@_);
}

sub matching_items {
    my $self = shift;
    return map $_-&gt;{item}, $self-&gt;matching(@_);
}

1;

=pod

=encoding UTF-8

=head1 NAME

HTTP::Config - Configuration for request and response objects

=head1 VERSION

version 6.22

=head1 SYNOPSIS

 use HTTP::Config;
 my $c = HTTP::Config-&gt;new;
 $c-&gt;add(m_domain =&gt; ".example.com", m_scheme =&gt; "http", verbose =&gt; 1);
 
 use HTTP::Request;
 my $request = HTTP::Request-&gt;new(GET =&gt; "http://www.example.com");
 
 if (my @m = $c-&gt;matching($request)) {
    print "Yadayada\n" if $m[0]-&gt;{verbose};
 }

=head1 DESCRIPTION

An C&lt;HTTP::Config&gt; object is a list of entries that
can be matched against request or request/response pairs.  Its
purpose is to hold configuration data that can be looked up given a
request or response object.

Each configuration entry is a hash.  Some keys specify matching to
occur against attributes of request/response objects.  Other keys can
be used to hold user data.

The following methods are provided:

=over 4

=item $conf = HTTP::Config-&gt;new

Constructs a new empty C&lt;HTTP::Config&gt; object and returns it.

=item $conf-&gt;entries

Returns the list of entries in the configuration object.
In scalar context returns the number of entries.

=item $conf-&gt;empty

Return true if there are no entries in the configuration object.
This is just a shorthand for C&lt;&lt; not $conf-&gt;entries &gt;&gt;.

=item $conf-&gt;add( %matchspec, %other )

=item $conf-&gt;add( \%entry )

Adds a new entry to the configuration.
You can either pass separate key/value pairs or a hash reference.

=item $conf-&gt;remove( %spec )

Removes (and returns) the entries that have matches for all the key/value pairs in %spec.
If %spec is empty this will match all entries; so it will empty the configuration object.

=item $conf-&gt;matching( $uri, $request, $response )

=item $conf-&gt;matching( $uri )

=item $conf-&gt;matching( $request )

=item $conf-&gt;matching( $response )

Returns the entries that match the given $uri, $request and $response triplet.

If called with a single $request object then the $uri is obtained by calling its 'uri_canonical' method.
If called with a single $response object, then the request object is obtained by calling its 'request' method;
and then the $uri is obtained as if a single $request was provided.

The entries are returned with the most specific matches first.
In scalar context returns the most specific match or C&lt;undef&gt; in none match.

=item $conf-&gt;add_item( $item, %matchspec )

=item $conf-&gt;remove_items( %spec )

=item $conf-&gt;matching_items( $uri, $request, $response )

Wrappers that hides the entries themselves.

=back

=head2 Matching

The following keys on a configuration entry specify matching.  For all
of these you can provide an array of values instead of a single value.
The entry matches if at least one of the values in the array matches.

Entries that require match against a response object attribute will never match
unless a response object was provided.

=over

=item m_scheme =&gt; $scheme

Matches if the URI uses the specified scheme; e.g. "http".

=item m_secure =&gt; $bool

If $bool is TRUE; matches if the URI uses a secure scheme.  If $bool
is FALSE; matches if the URI does not use a secure scheme.  An example
of a secure scheme is "https".

=item m_host_port =&gt; "$hostname:$port"

Matches if the URI's host_port method return the specified value.

=item m_host =&gt; $hostname

Matches if the URI's host method returns the specified value.

=item m_port =&gt; $port

Matches if the URI's port method returns the specified value.

=item m_domain =&gt; ".$domain"

Matches if the URI's host method return a value that within the given
domain.  The hostname "www.example.com" will for instance match the
domain ".com".

=item m_path =&gt; $path

Matches if the URI's path method returns the specified value.

=item m_path_prefix =&gt; $path

Matches if the URI's path is the specified path or has the specified
path as prefix.

=item m_path_match =&gt; $Regexp

Matches if the regular expression matches the URI's path.  Eg. qr/\.html$/.

=item m_method =&gt; $method

Matches if the request method matches the specified value. Eg. "GET" or "POST".

=item m_code =&gt; $digit

=item m_code =&gt; $status_code

Matches if the response status code matches.  If a single digit is
specified; matches for all response status codes beginning with that digit.

=item m_proxy =&gt; $url

Matches if the request is to be sent to the given Proxy server.

=item m_media_type =&gt; "*/*"

=item m_media_type =&gt; "text/*"

=item m_media_type =&gt; "html"

=item m_media_type =&gt; "xhtml"

=item m_media_type =&gt; "text/html"

Matches if the response media type matches.

With a value of "html" matches if $response-&gt;content_is_html returns TRUE.
With a value of "xhtml" matches if $response-&gt;content_is_xhtml returns TRUE.

=item m_uri__I&lt;$method&gt; =&gt; undef

Matches if the URI object provides the method.

=item m_uri__I&lt;$method&gt; =&gt; $string

Matches if the URI's $method method returns the given value.

=item m_header__I&lt;$field&gt; =&gt; $string

Matches if either the request or the response have a header $field with the given value.

=item m_response_attr__I&lt;$key&gt; =&gt; undef

=item m_response_attr__I&lt;$key&gt; =&gt; $string

Matches if the response object has that key, or the entry has the given value.

=back

=head1 SEE ALSO

L&lt;URI&gt;, L&lt;HTTP::Request&gt;, L&lt;HTTP::Response&gt;

=head1 AUTHOR

Gisle Aas &lt;gisle@activestate.com&gt;

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 1994-2017 by Gisle Aas.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut

__END__


#ABSTRACT: Configuration for request and response objects

</pre></body></html>