11 return bless [], $class;
30 my($self, %spec) = @_;
36 my($self, %spec) = @_;
40 for my $item (@$self) {
41 for my $k (keys %spec) {
42 if (!exists $item->{$k} || $spec{$k} ne $item->{$k}) {
49 return \@found unless wantarray;
50 return \@found, \@rest;
55 my $f = $self->find2(@_);
56 return @$f if wantarray;
61 my($self, %spec) = @_;
62 my($removed, $rest) = $self->find2(%spec);
63 @$self = @$rest if @$removed;
70 return $uri->_scheme eq $v; # URI known to be canonical
74 my $secure = $uri->_scheme eq "https";
75 return $secure == !!$v;
79 return unless $uri->can("host_port");
80 return $uri->host_port eq $v, 7;
84 return unless $uri->can("host");
85 return $uri->host eq $v, 6;
89 return unless $uri->can("port");
90 return $uri->port eq $v;
94 return unless $uri->can("host");
96 $h = "$h.local" unless $h =~ /\./;
97 $v = ".$v" unless $v =~ /^\./;
98 return length($v), 5 if substr($h, -length($v)) eq $v;
103 return unless $uri->can("path");
104 return $uri->path eq $v, 4;
106 m_path_prefix => sub {
108 return unless $uri->can("path");
109 my $path = $uri->path;
110 my $len = length($v);
111 return $len, 3 if $path eq $v;
112 return 0 if length($path) <= $len;
113 $v .= "/" unless $v =~ m,/\z,,;
114 return $len, 3 if substr($path, 0, length($v)) eq $v;
117 m_path_match => sub {
119 return unless $uri->can("path");
120 return $uri->path =~ $v;
123 my($v, $k, $uri) = @_;
124 return unless $uri->can($k);
125 return 1 unless defined $v;
126 return $uri->$k eq $v;
129 my($v, $uri, $request) = @_;
130 return $request && $request->method eq $v;
133 my($v, $uri, $request) = @_;
134 return $request && ($request->{proxy} || "") eq $v;
137 my($v, $uri, $request, $response) = @_;
139 return unless $response;
140 return length($v), 2 if substr($response->code, 0, length($v)) eq $v;
142 m_media_type => sub { # for request too??
143 my($v, $uri, $request, $response) = @_;
144 return unless $response;
145 return 1, 1 if $v eq "*/*";
146 my $ct = $response->content_type;
147 return 2, 1 if $v =~ s,/\*\z,, && $ct =~ m,^\Q$v\E/,;
148 return 3, 1 if $v eq "html" && $response->content_is_html;
149 return 4, 1 if $v eq "html" && $response->content_is_xhtml;
150 return 10, 1 if $v eq $ct;
154 my($v, $k, $uri, $request, $response) = @_;
155 return unless $request;
156 return 1 if $request->header($k) eq $v;
157 return 1 if $response && $response->header($k) eq $v;
160 m_response_attr__ => sub {
161 my($v, $k, $uri, $request, $response) = @_;
162 return unless $response;
163 return 1 if !defined($v) && exists $response->{$k};
164 return 0 unless exists $response->{$k};
165 return 1 if $response->{$k} eq $v;
173 if ($_[0]->can("request")) {
174 unshift(@_, $_[0]->request);
175 unshift(@_, undef) unless defined $_[0];
177 unshift(@_, $_[0]->uri_canonical) if $_[0] && $_[0]->can("uri_canonical");
179 my($uri, $request, $response) = @_;
180 $uri = URI->new($uri) unless ref($uri);
184 for my $item (@$self) {
186 for my $ikey (keys %$item) {
189 $k = $1 if $mkey =~ s/__(.*)/__/;
190 if (my $m = $MATCH{$mkey}) {
191 #print "$ikey $mkey\n";
194 defined($k) ? $k : (),
195 $uri, $request, $response
197 my $v = $item->{$ikey};
198 $v = [$v] unless ref($v) eq "ARRAY";
200 ($c, $o) = $m->($_, @arg);
201 #print " - $_ ==> $c $o\n";
205 $order->[$o || 0] += $c;
209 $item->{_order} = join(".", reverse map sprintf("%03d", $_ || 0), @$order);
212 @m = sort { $b->{_order} cmp $a->{_order} } @m;
213 delete $_->{_order} for @m;
214 return @m if wantarray;
221 return $self->add(item => $item, @_);
226 return map $_->{item}, $self->remove(@_);
231 return map $_->{item}, $self->matching(@_);
240 HTTP::Config - Configuration for request and response objects
245 my $c = HTTP::Config->new;
246 $c->add(m_domain => ".example.com", m_scheme => "http", verbose => 1);
249 my $request = HTTP::Request->new(GET => "http://www.example.com");
251 if (my @m = $c->matching($request)) {
252 print "Yadayada\n" if $m[0]->{verbose};
257 An C<HTTP::Config> object is a list of entries that
258 can be matched against request or request/response pairs. Its
259 purpose is to hold configuration data that can be looked up given a
260 request or response object.
262 Each configuration entry is a hash. Some keys specify matching to
263 occur against attributes of request/response objects. Other keys can
264 be used to hold user data.
266 The following methods are provided:
270 =item $conf = HTTP::Config->new
272 Constructs a new empty C<HTTP::Config> object and returns it.
276 Returns the list of entries in the configuration object.
277 In scalar context returns the number of entries.
281 Return true if there are no entries in the configuration object.
282 This is just a shorthand for C<< not $conf->entries >>.
284 =item $conf->add( %matchspec, %other )
286 =item $conf->add( \%entry )
288 Adds a new entry to the configuration.
289 You can either pass separate key/value pairs or a hash reference.
291 =item $conf->remove( %spec )
293 Removes (and returns) the entries that have matches for all the key/value pairs in %spec.
294 If %spec is empty this will match all entries; so it will empty the configuation object.
296 =item $conf->matching( $uri, $request, $response )
298 =item $conf->matching( $uri )
300 =item $conf->matching( $request )
302 =item $conf->matching( $response )
304 Returns the entries that match the given $uri, $request and $response triplet.
306 If called with a single $request object then the $uri is obtained by calling its 'uri_canonical' method.
307 If called with a single $response object, then the request object is obtained by calling its 'request' method;
308 and then the $uri is obtained as if a single $request was provided.
310 The entries are returned with the most specific matches first.
311 In scalar context returns the most specific match or C<undef> in none match.
313 =item $conf->add_item( $item, %matchspec )
315 =item $conf->remove_items( %spec )
317 =item $conf->matching_items( $uri, $request, $response )
319 Wrappers that hides the entries themselves.
325 The following keys on a configuration entry specify matching. For all
326 of these you can provide an array of values instead of a single value.
327 The entry matches if at least one of the values in the array matches.
329 Entries that require match against a response object attribute will never match
330 unless a response object was provided.
334 =item m_scheme => $scheme
336 Matches if the URI uses the specified scheme; e.g. "http".
338 =item m_secure => $bool
340 If $bool is TRUE; matches if the URI uses a secure scheme. If $bool
341 is FALSE; matches if the URI does not use a secure scheme. An example
342 of a secure scheme is "https".
344 =item m_host_port => "$hostname:$port"
346 Matches if the URI's host_port method return the specified value.
348 =item m_host => $hostname
350 Matches if the URI's host method returns the specified value.
352 =item m_port => $port
354 Matches if the URI's port method returns the specified value.
356 =item m_domain => ".$domain"
358 Matches if the URI's host method return a value that within the given
359 domain. The hostname "www.example.com" will for instance match the
362 =item m_path => $path
364 Matches if the URI's path method returns the specified value.
366 =item m_path_prefix => $path
368 Matches if the URI's path is the specified path or has the specified
371 =item m_path_match => $Regexp
373 Matches if the regular expression matches the URI's path. Eg. qr/\.html$/.
375 =item m_method => $method
377 Matches if the request method matches the specified value. Eg. "GET" or "POST".
379 =item m_code => $digit
381 =item m_code => $status_code
383 Matches if the response status code matches. If a single digit is
384 specified; matches for all response status codes beginning with that digit.
386 =item m_proxy => $url
388 Matches if the request is to be sent to the given Proxy server.
390 =item m_media_type => "*/*"
392 =item m_media_type => "text/*"
394 =item m_media_type => "html"
396 =item m_media_type => "xhtml"
398 =item m_media_type => "text/html"
400 Matches if the response media type matches.
402 With a value of "html" matches if $response->content_is_html returns TRUE.
403 With a value of "xhtml" matches if $response->content_is_xhtml returns TRUE.
405 =item m_uri__I<$method> => undef
407 Matches if the URI object provide the method
409 =item m_uri__I<$method> => $string
411 Matches if the URI's $method method returns the given value.
413 =item m_header__I<$field> => $string
415 Matches if either the request or the response have a header $field with the given value.
417 =item m_response_attr__I<$key> => undef
419 =item m_response_attr__I<$key> => $string
421 Matches if the response object has a that key; or the entry has the given value.
427 L<URI>, L<HTTP::Request>, L<HTTP::Response>
431 Copyright 2008, Gisle Aas
433 This library is free software; you can redistribute it and/or
434 modify it under the same terms as Perl itself.