| 1 | # HTTP Cookies (RFC 2965) | ||
| 2 | # | ||
| 3 | # subclass of HTTP:Part; see that for interface requirements | ||
| 4 | # | ||
| 5 | |||
| 6 | package IDS::DataSource::HTTP::Cookie; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Carp qw(carp confess); | ||
| 11 | use base qw(IDS::DataSource::HTTP::Part); | ||
| 12 | |||
| 13 | $IDS::DataSource::HTTP::Cookie::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 ($name, $value, $avpair); | ||
| 25 | my $OK = 1; # optimism | ||
| 26 | |||
| 27 | $self->mesg(1, *parse{PACKAGE} . "::parse: data '$data'"); | ||
| 28 | |||
| 29 | # split cookie into name and value | ||
| 30 | while (defined($data) && length($data) > 0) { | ||
| 31 | # Using split should get the first ;, and quoted ; are OK in the | ||
| 32 | # value (? are they?). Note that $data gets shorter each | ||
| 33 | # interation through the loop. | ||
| 34 | ($avpair, $data) = split(/;\s*/, $data, 2); | ||
| 35 | if (! defined($avpair) || $avpair eq "") { | ||
| 36 | # Only one cookie, not a collection | ||
| 37 | $avpair = $data; | ||
| 38 | $data = ""; | ||
| 39 | last unless $avpair; | ||
| 40 | } | ||
| 41 | defined($data) or $data = ""; # to avoid warnings from line below | ||
| 42 | $self->mesg(2, *parse{PACKAGE} . "::parse: avpair '$avpair' data '$data'"); | ||
| 43 | |||
| 44 | # Using split should get the first =, and quoted = are OK in the | ||
| 45 | # value | ||
| 46 | ($name, $value) = split(/\s*=\s*/, $avpair, 2); | ||
| 47 | unless (defined($name)) { | ||
| 48 | carp "Undefined cookie name! data '$data' avpair '$avpair'"; | ||
| 49 | $OK = 0; | ||
| 50 | } | ||
| 51 | $self->mesg(2, *parse{PACKAGE} . "::parse: name '$name' value '$value'"); | ||
| 52 | |||
| 53 | if (${$self->{"params"}}{"handle_PHPSESSID"} && $name =~ /^PHPSESSID$/) { | ||
| 54 | my $phpsess = handle_PHPSESSID($value); | ||
| 55 | $OK = 0 unless $phpsess eq "PHP Session ID"; | ||
| 56 | push @tokens, $phpsess; | ||
| 57 | } else { | ||
| 58 | push @tokens, "Cookie: $name"; | ||
| 59 | push @tokens, "Cookie value: $value" # values are optional | ||
| 60 | if defined($value) && ${$self->{"params"}}{"cookie_values"}; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ", | ||
| 65 | "\n ", \@tokens); | ||
| 66 | if (exists(${$self->{"params"}}{"Syntax only"}{"Cookie"}) && | ||
| 67 | ${$self->{"params"}}{"Syntax only"}{"Cookie"}) { | ||
| 68 | $self->{"tokens"} = [ $OK ? "Cookie syntax OK" | ||
| 69 | : "Cookie syntax NOT OK" ]; | ||
| 70 | } else { | ||
| 71 | $self->{"tokens"} = \@tokens; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub handle_PHPSESSID { | ||
| 76 | my $str = shift; | ||
| 77 | defined($str) or confess "handle_PHPSESSID called without an argument\n"; | ||
| 78 | |||
| 79 | return $str =~ /^[a-f0-9]{32}$/ | ||
| 80 | ? "PHP Session ID" | ||
| 81 | : "Invalid PHP Session ID"; | ||
| 82 | } | ||
| 83 | |||
| 84 | # accessor functions not provided by the superclass | ||
| 85 | |||
| 86 | =head1 AUTHOR INFORMATION | ||
| 87 | |||
| 88 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 89 | |||
| 90 | This library is free software; you can redistribute it and/or modify | ||
| 91 | it under the same terms as Perl itself. | ||
| 92 | |||
| 93 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 94 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 95 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 96 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 97 | speed of the reponse depends on how the research is proceeding. | ||
| 98 | |||
| 99 | =head1 BUGS | ||
| 100 | |||
| 101 | Please report them. | ||
| 102 | |||
| 103 | =head1 SEE ALSO | ||
| 104 | |||
| 105 | L<IDS::Algorithm>, L<IDS::DataSource> | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | 1; |