| 1 | # HTTP Range; RFC 2616 sections 14.16, | ||
| 2 | # | ||
| 3 | # subclass of HTTP:Part; see that for interface requirements | ||
| 4 | # | ||
| 5 | |||
| 6 | package IDS::DataSource::HTTP::Range; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Carp qw(carp confess); | ||
| 11 | use base qw(IDS::DataSource::HTTP::Part); | ||
| 12 | use IDS::DataSource::HTTP::Int; | ||
| 13 | |||
| 14 | $IDS::DataSource::HTTP::Range::VERSION = "1.0"; | ||
| 15 | |||
| 16 | sub empty { | ||
| 17 | my $self = shift; | ||
| 18 | undef $self->{"data"}, $self->{"tokens"}; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub parse { | ||
| 22 | my $self = shift; | ||
| 23 | my $data = $self->{"data"}; # convenience | ||
| 24 | my ($units, $range, @tokens); | ||
| 25 | |||
| 26 | $self->mesg(1, *parse{PACKAGE} . "::parse: data '$data'"); | ||
| 27 | |||
| 28 | ### according to Section 3.11, \w is not a big enough class | ||
| 29 | if ($data =~ m!^((bytes)|(\w+))=(.*)$!) { | ||
| 30 | push @tokens, "Range units: $1"; | ||
| 31 | my $ranges = $4; | ||
| 32 | for $range (split /,\s*/, $ranges) { | ||
| 33 | $range =~ /(\d+)-(\d+)?/; | ||
| 34 | if (defined($1)) { # start is optional | ||
| 35 | push @tokens, "Range start"; | ||
| 36 | my $start = new IDS::DataSource::HTTP::Int($self->{"params"}, $1); | ||
| 37 | push @tokens, $start->tokens; | ||
| 38 | } else { | ||
| 39 | push @tokens, "No range start"; | ||
| 40 | } | ||
| 41 | if (defined($2)) { # end is optional | ||
| 42 | push @tokens, "Range end"; | ||
| 43 | my $end = new IDS::DataSource::HTTP::Int($self->{"params"}, $2); | ||
| 44 | push @tokens, $end->tokens; | ||
| 45 | } else { | ||
| 46 | push @tokens, "No range end"; | ||
| 47 | } | ||
| 48 | if (! defined($1) && ! defined($2)) { # error | ||
| 49 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 50 | ${$self->{"params"}}{"source"} . | ||
| 51 | " invalid range (with units) in '$data'\n"; | ||
| 52 | $self->warn($pmsg, \@tokens, "!Invalid range"); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } else { | ||
| 56 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 57 | ${$self->{"params"}}{"source"} . | ||
| 58 | " invalid range in '$data'\n"; | ||
| 59 | $self->warn($pmsg, \@tokens, "!Invalid range"); | ||
| 60 | } | ||
| 61 | |||
| 62 | $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ", | ||
| 63 | "\n ", \@tokens); | ||
| 64 | $self->{"tokens"} = \@tokens; | ||
| 65 | } | ||
| 66 | |||
| 67 | # accessor functions not provided by the superclass | ||
| 68 | |||
| 69 | =head1 AUTHOR INFORMATION | ||
| 70 | |||
| 71 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 72 | |||
| 73 | This library is free software; you can redistribute it and/or modify | ||
| 74 | it under the same terms as Perl itself. | ||
| 75 | |||
| 76 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 77 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 78 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 79 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 80 | speed of the reponse depends on how the research is proceeding. | ||
| 81 | |||
| 82 | =head1 BUGS | ||
| 83 | |||
| 84 | Please report them. | ||
| 85 | |||
| 86 | =head1 SEE ALSO | ||
| 87 | |||
| 88 | L<IDS::Algorithm>, L<IDS::DataSource> | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | 1; |