HTTP/Date.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 188 CC 15 SCAR 40.1
CC
15
Cov
0%
parse
72.0
validate_date
72.0
empty
2.0
CRAP 55.3
1 # HTTP Date; Section 3.3 of RFC 2616
2 #
3 # subclass of HTTP:Part; see that for interface requirements
4 #
5 # For reference, here are examples of the three accepted date formats:
6 # Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
7 # Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
8 # Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
9 # Example from Section 3.3.1 of RFC 2616
10 #
11 # Unfortunately, we are also admonished in the same section:
12 # Recipients of date values are encouraged to be robust in accepting
13 # date values that may have been sent by non-HTTP applications,
14 # as is sometimes the case when retrieving or posting messages via
15 # proxies/gateways to SMTP or NNTP.
16 # As a result, this section is a bit messy to be able to handle the
17 # various formats I have seen in use.
18 #
19  
20 package IDS::DataSource::HTTP::Date;
21  
22 use strict;
23 use warnings;
24 use Carp qw(carp confess);
25 use base qw(IDS::DataSource::HTTP::Part);
26  
27 $IDS::DataSource::HTTP::Date::VERSION = "1.0";
28  
29 sub empty {
30 my $self = shift;
31 undef $self->{"data"}, $self->{"tokens"};
32 }
33  
34 sub parse {
35 my $self = shift;
36 my $date = $self->{"data"}; # convenience
37 my @tokens;
38 my ($weekday, $day, $month, $year, $hour, $minute, $second);
39 my ($TZ, $x); # x is don't care, a placeholder
40 my $OK = 0;
41 my $type = "None yet";
42  
43 $self->mesg(1, *parse{PACKAGE} . "::parse: data '$date'");
44  
45 # various patterns to make the matches below cleaner
46 my $day1pat = qr'(Mon|Tue|Wed|Thu|Fri|Sat|Sun)';
47 my $day2pat = qr'(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)';
48 my $monpat = qr'(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)';
49 my $monpat2 = qr'(January|February|March|April|May|June|July|August|September|October|November|December)'; # not allowed in legal dates
50 my $timepat = qr'(\d{2}):(\d{2}):(\d{2})';
51 my $TZpat = qr'(\w{3})';
52 my $date822 = qr"(\d{1,2}) $monpat (\d{4})"o; ### non-standard: accept 1 or 2 day digits
53 my $date850 = qr"(\d{2})-$monpat-(\d{2})"o;
54 my $dateansi = qr"$monpat (\d{2}|( \d))"o;
55 my $rfc1123 = qr"^$day1pat, $date822 $timepat $TZpat$"o;
56 my $rfc850 = qr"^$day2pat, $date850 $timepat $TZpat$"o;
57 my $asctime = qr"^$day1pat $dateansi $timepat ($TZpat )?(\d{4})$"o;
58 my $invalid1 = qr"^$day2pat, ( ?\d{1,2})-($monpat2)-(\d{4}) $timepat $TZpat$"o;
59  
60 if (($weekday, $day, $month, $year, $hour, $minute, $second, $TZ) = ($date =~ m/$rfc1123/)) {
61 # RFC 822 updated by RFC 1123
62 $type = "RFC1123";
63 $OK = $self->validate_date($type, $weekday, $day, $month,
64 $year, $hour, $minute, $second, $TZ);
65 } elsif (($weekday, $day, $month, $year, $hour, $minute, $second, $TZ) = ($date =~ m/$rfc850/)) {
66 # RFC 850; obsoleted by RFC 1036
67 $type = "RFC850";
68 $OK = $self->validate_date($type, $weekday, $day, $month,
69 $year, $hour, $minute, $second, $TZ);
70 } elsif (($weekday, $month, $day, $x, $hour, $minute, $second, $x, $TZ, $year) = ($date =~ m/$asctime/)) {
71 ### Note: We accept a non-standard version as well (with a TZ)
72 # ANSI C asctime
73 $day =~ s/^\s*//; # remove any leading spaces; the standard allows them
74 $TZ = defined($TZ) ? $TZ : "No TZ";
75 $type = "ASCTIME";
76 $OK = $self->validate_date($type, $weekday, $day, $month,
77 $year, $hour, $minute, $second, $TZ);
78 } elsif (($weekday, $day, $month, $x, $year, $hour, $minute, $second, $TZ) = ($date =~ m/$invalid1/)) {
79 $type = "Invalid1";
80 # print STDERR "$invalid1\n";
81 # print STDERR "$weekday, $day, $month, $year, $hour, $minute, $second, $TZ\n";
82 $OK = $self->validate_date($type, $weekday, $day, $month,
83 $year, $hour, $minute, $second, $TZ);
84 } else {
85 my $pmsg = *parse{PACKAGE} . "::parse: In " .
86 ${$self->{"params"}}{"source"} .
87 " date '$date' does not match date patterns\n";
88 $self->warn($pmsg, \@tokens, "!Nomatch date");
89 $OK = 0; # not OK
90 }
91  
92 ### need to switch to the same scheme used elsewhere for syntax checks
93 ### only
94 if ($OK) {
95 if (${$self->{"params"}}{"recognize_dates"}) {
96 push @tokens, "Valid $type date";
97 } else {
98 # This always puts things in the same order, even though the
99 # original date format may have a different order. Valid?
100 # commented out to find out which is sometimes undefined
101 # push @tokens, "Weekday: $weekday", "Day: $day",
102 # "Month: $month", "Year: $year",
103 # "Hour: $hour", "Minute: $minute",
104 # "Second: $second", "Date type: $type";
105 push @tokens, "Weekday: $weekday";
106 push @tokens, "Day: $day";
107 push @tokens, "Month: $month";
108 push @tokens, "Year: $year";
109 push @tokens, "Hour: $hour";
110 push @tokens, "Minute: $minute";
111 push @tokens, "Second: $second";
112 push @tokens, "Date type: $type";
113 }
114 } else {
115 my $pmsg = *parse{PACKAGE} . "::parse: In " .
116 ${$self->{"params"}}{"source"} .
117 " invalid value in '$date'\n";
118 $self->warn($pmsg, \@tokens, "!Invalid date value");
119 }
120  
121 $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ",
122 "\n ", \@tokens);
123 $self->{"tokens"} = \@tokens;
124 }
125  
126 # Check basic assumptions about the date. This function could be made
127 # more accurate by checking the days actually in the month as the max
128 sub validate_date {
129 my $self = shift;
130 my ($type, $weekday, $day, $month,
131 $year, $hour, $minute, $second, $TZ) = @_;
132 my $level = 0;
133 my $name = *parse{PACKAGE} . "::validate_date";
134 # Timezones from http://www.timeanddate.com/library/abbreviations/timezones/
135 my @timezones = qw(ACDT ACST ADT AEDT AEST AKDT AKST AST AWST BST
136 CDT CDT CEST CET CST CST CXT EDT EDT EEST EET EST
137 EST GMT HAA HAC HADT HAE HAP HAR HAST HAT HAY HNA
138 HNC HNE HNP HNR HNT HNY IST MDT MESZ MEZ MST NDT
139 NFT NST PDT PST UTC WEST WET WST
140 );
141  
142 my $OK = 1; # assume valid
143  
144 map {$OK &= defined($_)} ($type, $weekday, $day, $month, $year, $hour, $minute, $second);
145 # verify still OK
146 unless ($OK) {
147 $self->mesg($level, "$name: undefined value received");
148 return 0; # nothing further needed
149 }
150  
151 # This check is covered by the pattern match now
152 # if ($type eq "RFC850") { # RFC 850 uses full day names
153 # $OK &= $weekday =~ /^(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)$/;
154 # } else { # other formats use 3-char day names
155 # $OK &= $weekday =~ /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)$/;
156 # }
157 # $OK or $self->mesg($level, "$name: bad weekday '$weekday'");
158  
159 $OK &= $day =~ /^\d+$/;
160 $OK &= $day >= 1 && $day <= 31;
161 $self->mesg($level, "$name: bad day '$day'") unless $OK;
162  
163 # This check is covered by thte pattern match now
164 #$OK &= $month =~ /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$/;
165 #$self->mesg($level, "$name: bad month '$month'") unless $OK;
166  
167 # year sanity checks?
168  
169 $OK &= $hour =~ /^\d+$/ && $hour >= 0 && $hour <= 23;
170 $self->mesg($level, "$name: bad hour '$hour'") unless $OK;
171 $OK &= $minute =~ /^\d+$/ && $minute >= 0 && $minute <= 59;
172 $self->mesg($level, "$name: bad minute '$minute'") unless $OK;
173 $OK &= $second =~ /^\d+$/ && $second >= 0 && $second <= 59;
174 $self->mesg($level, "$name: bad second '$second'") unless $OK;
175  
176 # This check is replaced by the one below
177 # if ($type eq "ASCTIME") { # the only one not to use a TZ
178 # $OK &= !defined($TZ) || $TZ eq "";
179 # } else {
180 # $OK &= $TZ eq "GMT";
181 # }
182 # Be generous in what we accept; if the TZ is defined and non-null
183 # (and not "No TZ"), see if it is any of the known world timezones
184 if (defined($TZ) && $TZ && $TZ ne "No TZ") {
185 my $tzok = 0;
186 map { $TZ eq $_ and $tzok = 1 } @timezones;
187 $OK &= $tzok
188 }
189 $self->mesg($level, "$name: bad timezone '$TZ'") unless $OK;
190  
191 return $OK;
192 }
193  
194 # accessor functions not provided by the superclass
195  
196 =head1 AUTHOR INFORMATION
197  
198 Copyright 2005-2007, Kenneth Ingham. All rights reserved.
199  
200 This library is free software; you can redistribute it and/or modify
201 it under the same terms as Perl itself.
202  
203 Address bug reports and comments to: ids_test at i-pi.com. When sending
204 bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm,
205 IDS::DataSource.pm, the version of Perl, and the name and version of the
206 operating system you are using. Since Kenneth is a PhD student, the
207 speed of the reponse depends on how the research is proceeding.
208  
209 =head1 BUGS
210  
211 Please report them.
212  
213 =head1 SEE ALSO
214  
215 L<IDS::Algorithm>, L<IDS::DataSource>
216  
217 =cut
218  
219 1;