| 1 | # HTTP AcceptParams; Section 14.1 (etc) of RFC 2616. These parameters are | ||
| 2 | # used for the various accept headers. Hopefully they are close enough | ||
| 3 | # to share one object. | ||
| 4 | # | ||
| 5 | # subclass of HTTP:Part; see that for interface requirements | ||
| 6 | # | ||
| 7 | |||
| 8 | package IDS::DataSource::HTTP::AcceptParams; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | use Carp qw(carp confess); | ||
| 13 | use base qw(IDS::DataSource::HTTP::Part); | ||
| 14 | |||
| 15 | $IDS::DataSource::HTTP::AcceptParams::VERSION = "1.0"; | ||
| 16 | |||
| 17 | sub empty { | ||
| 18 | my $self = shift; | ||
| 19 | undef $self->{"data"}, $self->{"tokens"}; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub parse { | ||
| 23 | my $self = shift; | ||
| 24 | my $data = $self->{"data"}; # convenience | ||
| 25 | my @tokens = (); | ||
| 26 | my $OK = 1; # optimism | ||
| 27 | |||
| 28 | $self->mesg(1, *parse{PACKAGE} . "::parse: data '$data'"); | ||
| 29 | |||
| 30 | # data should start with a ; and contain one or more ;-separated | ||
| 31 | # parameters for the accept. Remove the initial ; here. | ||
| 32 | $data =~ s/^;\s*//; | ||
| 33 | my $ap_pat = qr!^(\w+)(=([-\w.]+))?$!; | ||
| 34 | # my $qvalpat = qr!(0(\.\d{0,3})?)|(1(\.0{0,3})?)!; # not requiring a leading zero for values <1 is | ||
| 35 | # non-standard (but common in web clients), | ||
| 36 | # hence the line below instead. | ||
| 37 | my $qvalpat = qr!(0?(\.\d{0,3})?)|(1(\.0{0,3})?)!; | ||
| 38 | for my $param (split(/;\s*/, $data)) { | ||
| 39 | $self->mesg(2, *parse{PACKAGE} . "::parse: param '$param'"); | ||
| 40 | if ($param =~ m!$ap_pat!g) { | ||
| 41 | my $type = $1; | ||
| 42 | my $value = $3; | ||
| 43 | unless(defined($value)) { | ||
| 44 | $self->warn(*parse{PACKAGE} . "::parse: " . | ||
| 45 | ${$self->{"params"}}{"source"} . ": Undefined value in $param", [], "!undefined value in $param"); | ||
| 46 | next; | ||
| 47 | } | ||
| 48 | if ($type eq "q") { | ||
| 49 | my $end = ${$self->{"params"}}{"recognize_qvalues"} | ||
| 50 | ? "" | ||
| 51 | : ": $value"; | ||
| 52 | if ($value =~ /^$qvalpat$/) { | ||
| 53 | push @tokens, "Accept qvalue$end"; | ||
| 54 | } else { | ||
| 55 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 56 | ${$self->{"params"}}{"source"} . | ||
| 57 | " Invalid accept qvalue '$value'\n"; | ||
| 58 | $self->warn($pmsg, \@tokens, "!invalid accept qvalue"); | ||
| 59 | $OK = 0; | ||
| 60 | } | ||
| 61 | } else { | ||
| 62 | push @tokens, "AcceptParameter: $type"; | ||
| 63 | push @tokens, "AcceptParameter value: $value" | ||
| 64 | if defined($value); | ||
| 65 | } | ||
| 66 | } else { | ||
| 67 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 68 | ${$self->{"params"}}{"source"} . | ||
| 69 | " Bad parameter '$param'\n"; | ||
| 70 | $self->warn($pmsg, \@tokens, "!invalid parameter"); | ||
| 71 | $OK = 0; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ", | ||
| 76 | "\n ", \@tokens); | ||
| 77 | if (exists(${$self->{"params"}}{"Syntax only"}{"Accept"}) && | ||
| 78 | ${$self->{"params"}}{"Syntax only"}{"Accept"}) { | ||
| 79 | $self->{"tokens"} = [ $OK ? "Accept params syntax OK" | ||
| 80 | : "Accept params syntax NOT OK" ]; | ||
| 81 | } else { | ||
| 82 | $self->{"tokens"} = \@tokens; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | # accessor functions not provided by the superclass | ||
| 87 | |||
| 88 | =head1 AUTHOR INFORMATION | ||
| 89 | |||
| 90 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 91 | |||
| 92 | This library is free software; you can redistribute it and/or modify | ||
| 93 | it under the same terms as Perl itself. | ||
| 94 | |||
| 95 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 96 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 97 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 98 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 99 | speed of the reponse depends on how the research is proceeding. | ||
| 100 | |||
| 101 | =head1 BUGS | ||
| 102 | |||
| 103 | Please report them. | ||
| 104 | |||
| 105 | =head1 SEE ALSO | ||
| 106 | |||
| 107 | L<IDS::Algorithm>, L<IDS::DataSource> | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | |||
| 112 | 1; |