| 1 | # HTTP URI path; implements RFC 2396, 2616 (primary), standards | ||
| 2 | # | ||
| 3 | # subclass of HTTP:Part; see that for interface requirements | ||
| 4 | # | ||
| 5 | |||
| 6 | package IDS::DataSource::HTTP::URIPath; | ||
| 7 | use base qw(IDS::DataSource::HTTP::Part); | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Carp qw(carp confess); | ||
| 12 | use IDS::Utils qw(split_value); | ||
| 13 | |||
| 14 | $IDS::DataSource::HTTP::URIPath::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 $path = $self->{"data"}; # convenience | ||
| 24 | my (@tokens, @pathtokens, $qpath, $query); | ||
| 25 | |||
| 26 | $self->mesg(1, *parse{PACKAGE} . "::parse: path '$path'"); | ||
| 27 | |||
| 28 | if ($path =~ /^([^?]+)\?(.+)$/) { # query | ||
| 29 | $qpath = $1; | ||
| 30 | $query = $2; | ||
| 31 | $self->mesg(3, *parse{PACKAGE} . "::parse: query path '$path' query '$query'"); | ||
| 32 | |||
| 33 | @pathtokens = split_value("URL Query path part", | ||
| 34 | '([\/])', $qpath); | ||
| 35 | push @tokens, @pathtokens; | ||
| 36 | $self->mesg(3, *parse{PACKAGE} . "::parse: path tokens:\n ", | ||
| 37 | "\n ", \@pathtokens); | ||
| 38 | push @tokens, "URL Query separator: ?"; | ||
| 39 | for my $qpart (split /(\&)/, $query) { | ||
| 40 | $self->mesg(3, *parse{PACKAGE} . "::parse: query part '$qpart'"); | ||
| 41 | if (${$self->{"params"}}{"handle_PHPSESSID"} && $qpart =~ /^PHPSESSID=/) { | ||
| 42 | push @tokens, handle_PHPSESSID($qpart); | ||
| 43 | } elsif ($qpart eq '&') { | ||
| 44 | push @tokens, 'URL Query separator: &' | ||
| 45 | } else { | ||
| 46 | $qpart =~ /^([^=]+)=(.*)/; | ||
| 47 | if (defined($1)) { | ||
| 48 | push @tokens, "URL Query question: $1"; | ||
| 49 | $qpart = $2; | ||
| 50 | } | ||
| 51 | my @subparts = split /\+/, $qpart; | ||
| 52 | #map { $_ = $self->expand_pct($_) } @subparts; | ||
| 53 | push @tokens, ($#subparts >= 1 | ||
| 54 | ? split_value("URL Query value subpart", '\+', $qpart) | ||
| 55 | : "URL Query value part: $qpart"); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } else { # no query | ||
| 59 | $self->mesg(3, *parse{PACKAGE} . "::parse: not a query"); | ||
| 60 | if ($path =~ m!^(.*/)?([^/]*)?$!) { | ||
| 61 | if (${$self->{"params"}}{"file_types_only"}) { | ||
| 62 | # pull off last element and handle it specially if we can | ||
| 63 | $path = defined($1) ? "$1" : "/"; | ||
| 64 | my $file = $2; | ||
| 65 | |||
| 66 | # deal with a the leading / which causes the split_value | ||
| 67 | # to return an empty first element. | ||
| 68 | $path =~ s!^/!! and push @tokens, "URL directory part: /"; | ||
| 69 | my @parts = split_value("URL directory part", '([\/])', $path); | ||
| 70 | #map { $_ = $self->expand_pct($_) } @parts; | ||
| 71 | push @tokens, @parts; | ||
| 72 | defined($file) and push @tokens, $self->id_file($file); | ||
| 73 | } else { | ||
| 74 | # deal with a the leading / which causes the split_value | ||
| 75 | # to return an empty first element. | ||
| 76 | $path =~ s!^/!! and push @tokens, "URL path part: /"; | ||
| 77 | push @tokens, split_value("URL path part", '([\/])', $path); | ||
| 78 | } | ||
| 79 | } else { | ||
| 80 | my $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 81 | ${$self->{"params"}}{"source"} . | ||
| 82 | " invalid path '$path'\n"; | ||
| 83 | $self->warn($pmsg, \@tokens, "!Invalid path '$path'"); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ", | ||
| 88 | "\n ", \@tokens); | ||
| 89 | $self->{"tokens"} = \@tokens; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub id_file { | ||
| 93 | my $self = shift; | ||
| 94 | my $fname = shift; | ||
| 95 | defined($fname) or | ||
| 96 | confess *id_file{PACKAGE} . "::id_file missing fname"; | ||
| 97 | my $type = ''; | ||
| 98 | my $len = ${$self->{"params"}}{"file_name_lengths"} ? | ||
| 99 | length($fname) . " " : ""; | ||
| 100 | |||
| 101 | # common file types | ||
| 102 | $fname =~ /^$/ and $type = "directory"; | ||
| 103 | $fname =~ /\.(gif|tif{1,2}|jpe?g)$/i and $type = "image"; | ||
| 104 | $fname =~ /\.(s?html?)$/i and $type = "html"; | ||
| 105 | $fname =~ /\.(pl)$/i and $type = "Perl"; | ||
| 106 | $fname =~ /\.(php)$/i and $type = "PHP"; | ||
| 107 | $fname =~ /\.(pdf)$/i and $type = "PDF"; | ||
| 108 | $fname =~ /\.(txt)$/i and $type = "text"; | ||
| 109 | $fname =~ /\.(css)$/i and $type = "html style sheet"; | ||
| 110 | $fname =~ /\.(vcs)$/i and $type = "vCalendar"; | ||
| 111 | return ($type ? "$type " : "unknown$len" ) . "file"; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub handle_PHPSESSID { | ||
| 115 | my $str = shift or confess "handle_PHPSESSID called without an argument\n"; | ||
| 116 | |||
| 117 | return $str =~ /PHPSESSID=[a-f0-9]{32}/ | ||
| 118 | ? "PHP Session ID" | ||
| 119 | : "Invalid PHP Session ID"; | ||
| 120 | } | ||
| 121 | |||
| 122 | # accessor functions | ||
| 123 | sub path { | ||
| 124 | my $self = shift; | ||
| 125 | return $self->{"data"}; | ||
| 126 | } | ||
| 127 | |||
| 128 | =head1 AUTHOR INFORMATION | ||
| 129 | |||
| 130 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 131 | |||
| 132 | This library is free software; you can redistribute it and/or modify | ||
| 133 | it under the same terms as Perl itself. | ||
| 134 | |||
| 135 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 136 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 137 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 138 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 139 | speed of the reponse depends on how the research is proceeding. | ||
| 140 | |||
| 141 | =head1 BUGS | ||
| 142 | |||
| 143 | Please report them. | ||
| 144 | |||
| 145 | =head1 SEE ALSO | ||
| 146 | |||
| 147 | L<IDS::Algorithm>, L<IDS::DataSource> | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | 1; |