| 1 | # HTTP URI; implements RFC 2396 (primary), 2616 (primary), 2732 (IPv6 | ||
| 2 | # updates) standards | ||
| 3 | # no 2732 yet | ||
| 4 | # | ||
| 5 | # subclass of HTTP:Part; see that for interface requirements | ||
| 6 | # | ||
| 7 | |||
| 8 | package IDS::DataSource::HTTP::URI; | ||
| 9 | use base qw(IDS::DataSource::HTTP::Part); | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | use Carp qw(carp confess); | ||
| 14 | use IDS::DataSource::HTTP::URIAuthority; | ||
| 15 | use IDS::DataSource::HTTP::URIPath; | ||
| 16 | |||
| 17 | $IDS::DataSource::HTTP::URI::VERSION = "1.0"; | ||
| 18 | |||
| 19 | # pull apart a URI from left to right in order to identify errors better | ||
| 20 | # than if we simply used a big regular expression. | ||
| 21 | sub parse { | ||
| 22 | my $self = shift; | ||
| 23 | my $url = $self->{"data"}; # convenience | ||
| 24 | my ($scheme, $rest); | ||
| 25 | my @tokens = (); | ||
| 26 | my $OK = 1; # optimism | ||
| 27 | |||
| 28 | $self->mesg(1, *parse{PACKAGE} . "::parse: URI '$url'"); | ||
| 29 | |||
| 30 | # scheme (RFC 2396 section 3.1) | ||
| 31 | if ($url =~ m!^([a-zA-Z][-+.a-zA-Z0-9]*):(.*)$!) { | ||
| 32 | $scheme = $1; | ||
| 33 | $rest = $2; | ||
| 34 | $self->mesg(2, *parse{PACKAGE} . "::parse: scheme '$scheme'"); | ||
| 35 | } elsif ($url =~ m!^/.*$!) { | ||
| 36 | undef $scheme; | ||
| 37 | $rest = $url; | ||
| 38 | } else { | ||
| 39 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 40 | ${$self->{"params"}}{"source"} . | ||
| 41 | " '$url' is invalid\n"; | ||
| 42 | $self->warn($pmsg, \@tokens, "!Invalid URI"); | ||
| 43 | $OK = 0; | ||
| 44 | |||
| 45 | if (exists(${$self->{"params"}}{"Syntax only"}{"URI"}) && | ||
| 46 | ${$self->{"params"}}{"Syntax only"}{"URI"}) { | ||
| 47 | $self->{"tokens"} = [ "URI syntax NOT OK" ]; | ||
| 48 | } else { | ||
| 49 | $self->{"tokens"} = \@tokens; | ||
| 50 | } | ||
| 51 | |||
| 52 | return; # give up. | ||
| 53 | } | ||
| 54 | $self->mesg(2, *parse{PACKAGE} . "::parse: rest '$rest'"); | ||
| 55 | |||
| 56 | if (defined($scheme) ) { | ||
| 57 | #$scheme = lc($scheme); # per section 3.1? | ||
| 58 | $self->{"scheme"} = $scheme; | ||
| 59 | push @tokens, "Scheme: $scheme"; | ||
| 60 | } else { | ||
| 61 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 62 | ${$self->{"params"}}{"source"} . | ||
| 63 | " '$url' is missing the scheme (protocol)\n"; | ||
| 64 | $self->warn($pmsg, \@tokens, "!Missing scheme"); | ||
| 65 | $rest = $url; # to try to continue | ||
| 66 | $OK = 0; | ||
| 67 | } | ||
| 68 | |||
| 69 | # type of path (RFC 2396 section 3) | ||
| 70 | |||
| 71 | # No path | ||
| 72 | if (! $rest) { | ||
| 73 | # URIs are not required to have paths | ||
| 74 | push @tokens, "No URI path"; | ||
| 75 | $OK = 0; | ||
| 76 | |||
| 77 | if (exists(${$self->{"params"}}{"Syntax only"}{"URI"}) && | ||
| 78 | ${$self->{"params"}}{"Syntax only"}{"URI"}) { | ||
| 79 | $self->{"tokens"} = [ "URI syntax NOT OK" ]; | ||
| 80 | } else { | ||
| 81 | $self->{"tokens"} = \@tokens; | ||
| 82 | } | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | # Unknown path | ||
| 87 | unless ($rest =~ m!^/(.)!) { | ||
| 88 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 89 | ${$self->{"params"}}{"source"} . | ||
| 90 | " cannot determine the path type in '$url'\n"; | ||
| 91 | $self->warn($pmsg, \@tokens, "!Unknown path type"); | ||
| 92 | $OK = 0; | ||
| 93 | |||
| 94 | if (exists(${$self->{"params"}}{"Syntax only"}{"URI"}) && | ||
| 95 | ${$self->{"params"}}{"Syntax only"}{"URI"}) { | ||
| 96 | $self->{"tokens"} = [ "URI syntax NOT OK" ]; | ||
| 97 | } else { | ||
| 98 | $self->{"tokens"} = \@tokens; | ||
| 99 | } | ||
| 100 | |||
| 101 | return; # out of options for parsing | ||
| 102 | } | ||
| 103 | |||
| 104 | # Expected path | ||
| 105 | if ($1 eq '/') { # net_path | ||
| 106 | if ($rest =~ m!^//([^/]+)[/?]?(.*)!) { | ||
| 107 | my $authority = $1; | ||
| 108 | $rest = $2; # might be empty; | ||
| 109 | $self->{"authority"} = new IDS::DataSource::HTTP::URIAuthority($self->{"params"}, $authority); | ||
| 110 | push @tokens, $self->{"authority"}->tokens(); | ||
| 111 | } else { | ||
| 112 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 113 | ${$self->{"params"}}{"source"} . | ||
| 114 | " cannot extract the authority in '$rest'\n"; | ||
| 115 | $self->warn($pmsg, \@tokens, "!Unknown authority type"); | ||
| 116 | $OK = 0; | ||
| 117 | # no return; we will try to get a path | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->{"path"} = new IDS::DataSource::HTTP::URIPath($self->{"params"}, $rest); | ||
| 122 | push @tokens, $self->{"path"}->tokens(); | ||
| 123 | |||
| 124 | $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ", | ||
| 125 | "\n ", \@tokens); | ||
| 126 | if (exists(${$self->{"params"}}{"Syntax only"}{"URI"}) && | ||
| 127 | ${$self->{"params"}}{"Syntax only"}{"URI"}) { | ||
| 128 | $self->{"tokens"} = [ $OK ? "URI syntax OK" | ||
| 129 | : "URI syntax NOT OK" ]; | ||
| 130 | } else { | ||
| 131 | $self->{"tokens"} = \@tokens; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | sub empty { | ||
| 136 | my $self = shift; | ||
| 137 | undef $self->{"data"}, $self->{"tokens"}, $self->{"authority"}, | ||
| 138 | $self->{"path"}, $self->{"scheme"}; | ||
| 139 | } | ||
| 140 | |||
| 141 | # accessor functions not provided by the superclass | ||
| 142 | sub authority { | ||
| 143 | my $self = shift; | ||
| 144 | return $self->{"authority"}; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub path { | ||
| 148 | my $self = shift; | ||
| 149 | return $self->{"path"}; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub scheme { | ||
| 153 | my $self = shift; | ||
| 154 | return $self->{"scheme"}; | ||
| 155 | } | ||
| 156 | |||
| 157 | =head1 AUTHOR INFORMATION | ||
| 158 | |||
| 159 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 160 | |||
| 161 | This library is free software; you can redistribute it and/or modify | ||
| 162 | it under the same terms as Perl itself. | ||
| 163 | |||
| 164 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 165 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 166 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 167 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 168 | speed of the reponse depends on how the research is proceeding. | ||
| 169 | |||
| 170 | =head1 BUGS | ||
| 171 | |||
| 172 | Please report them. | ||
| 173 | |||
| 174 | =head1 SEE ALSO | ||
| 175 | |||
| 176 | L<IDS::Algorithm>, L<IDS::DataSource> | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | 1; |