| 1 | # HTTP Cache-Control; Section 14.9 of RFC 2616 | ||
| 2 | # | ||
| 3 | # subclass of HTTP:Part; see that for interface requirements | ||
| 4 | # | ||
| 5 | |||
| 6 | package IDS::DataSource::HTTP::CacheControl; | ||
| 7 | use base qw(IDS::DataSource::HTTP::Part); | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Carp qw(carp confess); | ||
| 12 | |||
| 13 | $IDS::DataSource::HTTP::CacheControl::VERSION = "1.0"; | ||
| 14 | |||
| 15 | sub empty { | ||
| 16 | my $self = shift; | ||
| 17 | undef $self->{"data"}, $self->{"tokens"}; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub parse { | ||
| 21 | my $self = shift; | ||
| 22 | my $data = $self->{"data"}; # convenience | ||
| 23 | my @tokens = (); | ||
| 24 | my $OK = 1; # optimism | ||
| 25 | |||
| 26 | # Simple directives that are a single token. Hash for speed | ||
| 27 | my %directives = ("no-cache" => 1, | ||
| 28 | "no-store" => 1, | ||
| 29 | "no-transform" => 1, | ||
| 30 | "only-if-cached" => 1, | ||
| 31 | "public" => 1, | ||
| 32 | "no-store" => 1, | ||
| 33 | "no-transform" => 1, | ||
| 34 | "must-revalidate" => 1, | ||
| 35 | "proxy-revalidate" => 1, | ||
| 36 | ); | ||
| 37 | # The pattern for a token | ||
| 38 | my $tokenpat = qr/[-a-zA-Z0-9!#\$%^&*_+|.~]+/; # missing '"` (3 chars) | ||
| 39 | |||
| 40 | $self->mesg(1, *parse{PACKAGE} . "::parse: data '$data'"); | ||
| 41 | |||
| 42 | for my $directive (split /,?\s+/, $data) { ### non-standard: accept , as part of sep | ||
| 43 | my $lcdirective = lc($directive); | ||
| 44 | $self->mesg(3, *parse{PACKAGE} . "::parse: lcdirective '$lcdirective'"); | ||
| 45 | |||
| 46 | if (exists($directives{$lcdirective})) { | ||
| 47 | push @tokens, "Cache Control directive: $directive"; | ||
| 48 | } elsif ($lcdirective =~ /^(max-age|min-fresh|s-maxage)=(\d+)$/) { | ||
| 49 | push @tokens, "Cache Control directive: $1"; | ||
| 50 | my $i = new IDS::DataSource::HTTP::Int($self->{"params"}, $2); | ||
| 51 | push @tokens, $i->tokens; | ||
| 52 | } elsif ($lcdirective =~ /^max-stale(=(\d+))?$/) { | ||
| 53 | push @tokens, "Cache Control directive: max-stale"; | ||
| 54 | push @tokens, IDS::DataSource::HTTP::Int->new($self->{"params"}, $2)->tokens | ||
| 55 | if defined($2); | ||
| 56 | } elsif ($lcdirective =~ /^(private|no-cache)(="($tokenpat)")?/) { | ||
| 57 | push @tokens, "Cache Control directive: $1"; | ||
| 58 | push @tokens, "Cache Control $1 field: $3" | ||
| 59 | if defined($3); | ||
| 60 | } elsif ($lcdirective =~ /^($tokenpat)=(("$tokenpat")|$tokenpat)?$/) { | ||
| 61 | push @tokens, "Cache Control extension directive: $1"; | ||
| 62 | push @tokens, "Cache Control extension value: $2" | ||
| 63 | if defined($2); | ||
| 64 | } else { | ||
| 65 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 66 | ${$self->{"params"}}{"source"} . | ||
| 67 | " invalid cache response directive in '$directive'\n"; | ||
| 68 | $self->warn($pmsg, \@tokens, "!Invalid cache response directive"); | ||
| 69 | $OK = 0; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ", | ||
| 74 | "\n ", \@tokens); | ||
| 75 | if (exists(${$self->{"params"}}{"Syntax only"}{"Cache-Control"}) && | ||
| 76 | ${$self->{"params"}}{"Syntax only"}{"Cache-Control"}) { | ||
| 77 | $self->{"tokens"} = [ $OK ? "Cache-Control syntax OK" | ||
| 78 | : "Cache-Control syntax NOT OK" ]; | ||
| 79 | } else { | ||
| 80 | $self->{"tokens"} = \@tokens; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | # accessor functions not provided by the superclass | ||
| 86 | |||
| 87 | =head1 AUTHOR INFORMATION | ||
| 88 | |||
| 89 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 90 | |||
| 91 | This library is free software; you can redistribute it and/or modify | ||
| 92 | it under the same terms as Perl itself. | ||
| 93 | |||
| 94 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 95 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 96 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 97 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 98 | speed of the reponse depends on how the research is proceeding. | ||
| 99 | |||
| 100 | =head1 BUGS | ||
| 101 | |||
| 102 | Please report them. | ||
| 103 | |||
| 104 | =head1 SEE ALSO | ||
| 105 | |||
| 106 | L<IDS::Algorithm>, L<IDS::DataSource> | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | 1; |