| 1 | # HTTP Request | ||
| 2 | # | ||
| 3 | package IDS::DataSource::HTTP::Request; | ||
| 4 | use base qw(IDS::DataSource::HTTP); | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Carp qw(cluck carp confess); | ||
| 9 | use Data::Dumper; | ||
| 10 | use Socket; | ||
| 11 | use IDS::DataSource::HTTP::Accept; | ||
| 12 | use IDS::DataSource::HTTP::Authorization; | ||
| 13 | use IDS::DataSource::HTTP::Agent; | ||
| 14 | use IDS::DataSource::HTTP::CacheControl; | ||
| 15 | use IDS::DataSource::HTTP::Charset; | ||
| 16 | use IDS::DataSource::HTTP::Cookie; | ||
| 17 | use IDS::DataSource::HTTP::Date; | ||
| 18 | use IDS::DataSource::HTTP::EmailAddr; | ||
| 19 | use IDS::DataSource::HTTP::Encoding; | ||
| 20 | use IDS::DataSource::HTTP::ETag; | ||
| 21 | use IDS::DataSource::HTTP::Expectation; | ||
| 22 | use IDS::DataSource::HTTP::Host; | ||
| 23 | use IDS::DataSource::HTTP::Int; | ||
| 24 | use IDS::DataSource::HTTP::Language; | ||
| 25 | use IDS::DataSource::HTTP::MethodLine; | ||
| 26 | use IDS::DataSource::HTTP::RetryAfter; | ||
| 27 | use IDS::DataSource::HTTP::Range; | ||
| 28 | use IDS::DataSource::HTTP::IfRange; | ||
| 29 | use IDS::DataSource::HTTP::Referer; | ||
| 30 | use IDS::DataSource::HTTP::URI; | ||
| 31 | use IDS::DataSource::HTTP::URIAuthority; | ||
| 32 | use IDS::DataSource::HTTP::Via; | ||
| 33 | use IDS::DataSource::HTTP::IfModifiedSince; | ||
| 34 | use IDS::DataSource::HTTP::XForwarded; | ||
| 35 | |||
| 36 | $IDS::DataSource::HTTP::Request::VERSION = "2.0"; | ||
| 37 | |||
| 38 | # keys is a hash to speed up the searching (use the hash instead of grep) | ||
| 39 | # note all lc, because the standard says they are case insensitive. | ||
| 40 | ### Note that we assume all IDS::DataSource::HTTP:: subobjects use the | ||
| 41 | ### IDS::DataSource::HTTP::Part constructor. | ||
| 42 | # Comments below refer to section numbers in RFC 2616 | ||
| 43 | # ??? for a section means that it appears not to be a standard, but in | ||
| 44 | # use anyway. | ||
| 45 | %IDS::DataSource::HTTP::Request::complex_keys = ( | ||
| 46 | 'accept' => 'IDS::DataSource::HTTP::Accept', # 14.1 | ||
| 47 | 'accept-charset' => 'IDS::DataSource::HTTP::Charset', # 14.2 | ||
| 48 | 'accept-encoding' => 'IDS::DataSource::HTTP::Encoding', # 14.3 | ||
| 49 | 'accept-language' => 'IDS::DataSource::HTTP::Language', # 14.4 | ||
| 50 | # 14.5 Accept-Ranges is server->client | ||
| 51 | # 14.6 Age is server or proxy->client | ||
| 52 | # 14.7 Allow is server->client | ||
| 53 | 'authorization' => 'IDS::DataSource::HTTP::Authorization', # 14.8 | ||
| 54 | 'cache-control' => 'IDS::DataSource::HTTP::CacheControl', # 14.9 | ||
| 55 | # 14.10 Connection; simple and already handled by key+value | ||
| 56 | # 14.11 Content-Encoding; simple and already handled by key+value | ||
| 57 | # 14.12 Content-Language; simple and already handled by key+value | ||
| 58 | 'content-length' => 'IDS::DataSource::HTTP::Int', # 14.13 | ||
| 59 | # 14.14 Content-Location likely to be server->client | ||
| 60 | 'content-md5' => 'IDS::DataSource::HTTP::MD5', # 14.15 | ||
| 61 | # 14.16 Content-Range is server->client | ||
| 62 | 'content-type' => 'IDS::DataSource::HTTP::Accept', # 14.17 | ||
| 63 | 'date' => 'IDS::DataSource::HTTP::Date', # 14.18 | ||
| 64 | 'etag' => 'IDS::DataSource::HTTP::ETag', # 14.19 | ||
| 65 | 'expect' => 'IDS::DataSource::HTTP::Expectation', # 14.20 | ||
| 66 | 'expires' => 'IDS::DataSource::HTTP::Date', # 14.21 | ||
| 67 | 'from' => 'IDS::DataSource::HTTP::EmailAddr', # 14.22 | ||
| 68 | # for host, we accept more than the standard, since | ||
| 69 | # authority may include user info | ||
| 70 | 'host' => 'IDS::DataSource::HTTP::URIAuthority', # 14.23 | ||
| 71 | 'if-match' => 'IDS::DataSource::HTTP::ETag', # 14.24 | ||
| 72 | 'since' => 'IDS::DataSource::HTTP::Date', # 14.25 | ||
| 73 | 'if-none-match' => 'IDS::DataSource::HTTP::ETag', # 14.26 | ||
| 74 | 'if-range' => 'IDS::DataSource::HTTP::IfRange', # 14.27 | ||
| 75 | 'if-modified-since' => 'IDS::DataSource::HTTP::IfModifiedSince', # 14.28 | ||
| 76 | 'if-unmodified-since' => 'IDS::DataSource::HTTP::Date', # 14.28 | ||
| 77 | 'unless-modified-since' => 'IDS::DataSource::HTTP::Date', # 14.28 | ||
| 78 | 'unless-unmodified-since' => 'IDS::DataSource::HTTP::Date', # 14.28 | ||
| 79 | 'last-modified' => 'IDS::DataSource::HTTP::Date', # 14.29 | ||
| 80 | 'location' => 'IDS::DataSource::HTTP::URI', # 14.30 | ||
| 81 | 'max-forwards' => 'IDS::DataSource::HTTP::Int', # 14.31 | ||
| 82 | # ### Do we need 14.32 Pragma? | ||
| 83 | # 14.33 Proxy-Authenticate is proxy->client | ||
| 84 | # 14.34 Proxy-Authorization is client->proxy | ||
| 85 | 'range' => 'IDS::DataSource::HTTP::Range', # 14.35 | ||
| 86 | 'referer' => 'IDS::DataSource::HTTP::Referer', # 14.36 | ||
| 87 | 'retry-after' => 'IDS::DataSource::HTTP::RetryAfter', # 14.37 | ||
| 88 | # 14.38 Server is server->client | ||
| 89 | 'te' => 'IDS::DataSource::HTTP::Encoding', # 14.39 | ||
| 90 | # ### might need 14.40 Trailer | ||
| 91 | 'transfer-encoding' => 'IDS::DataSource::HTTP::Encoding', # 14.41 | ||
| 92 | # ### need 14.42 Upgrade (although unlikely to see) | ||
| 93 | 'user-agent' => 'IDS::DataSource::HTTP::Agent', # 14.43 | ||
| 94 | # 14.44 Vary is server-> | ||
| 95 | 'via' => 'IDS::DataSource::HTTP::Via', # 14.45 | ||
| 96 | # ### might need 14.46 Warning ? | ||
| 97 | # 14.47 WWW-Authenticate is server->client | ||
| 98 | |||
| 99 | 'cookie' => 'IDS::DataSource::HTTP::Cookie', # RFC 2965 | ||
| 100 | 'charset' => 'IDS::DataSource::HTTP::Charset', # ??? | ||
| 101 | 'encoding' => 'IDS::DataSource::HTTP::Encoding', # ??? | ||
| 102 | 'Forwarded' => 'IDS::DataSource::HTTP::Via', # ??? | ||
| 103 | 'x-forwarded-for' => 'IDS::DataSource::HTTP::XForwarded', # ??? | ||
| 104 | # Agent is not in 2616, but it appears to be used in a | ||
| 105 | # similar manner to user-agent | ||
| 106 | 'agent' => 'IDS::DataSource::HTTP::Agent', # ??? | ||
| 107 | ); | ||
| 108 | |||
| 109 | %IDS::DataSource::HTTP::Request::simple_keys = ( | ||
| 110 | 'allow' => 1, | ||
| 111 | 'connection' => 1, | ||
| 112 | 'content-encoding' => 1, | ||
| 113 | 'content-language' => 1, | ||
| 114 | 'content-location' => 1, | ||
| 115 | 'content-range' => 1, | ||
| 116 | 'cookie' => 1, | ||
| 117 | 'if-range' => 1, | ||
| 118 | 'proxy-authorization' => 1, | ||
| 119 | 'transfer-encoding' => 1, | ||
| 120 | ); | ||
| 121 | |||
| 122 | # break the request into the component tokens. | ||
| 123 | sub parse { | ||
| 124 | my $self = shift; | ||
| 125 | unless ($self->{"data"}) { | ||
| 126 | cluck *tokenize{PACKAGE} . "::tokenize called with no data; " . | ||
| 127 | Dumper($self) . "\n"; | ||
| 128 | $self->{"tokens"} = []; | ||
| 129 | return $self; | ||
| 130 | } | ||
| 131 | if ($self->{"data"} =~ /^$/) { | ||
| 132 | return $self->mesg(0, "Empty data from " . ${$self->{"params"}}{"source"}); | ||
| 133 | } | ||
| 134 | |||
| 135 | my @tokens = (); | ||
| 136 | |||
| 137 | my @lines = split /\n/, $self->{"data"}; | ||
| 138 | chomp(@lines); | ||
| 139 | $self->mesg(1, *parse{PACKAGE} . "::parse: Line is '$lines[0]'"); | ||
| 140 | $self->{"methodline"} = new IDS::DataSource::HTTP::MethodLine($self->{"params"}, $lines[0]); | ||
| 141 | push @tokens, $self->{"methodline"}->tokens(); | ||
| 142 | |||
| 143 | map { $self->mesg(1, *parse{PACKAGE} . "::parse: Line is '$_'"); | ||
| 144 | push @tokens, parse_kv($self, $_); | ||
| 145 | } @lines[1..$#lines]; | ||
| 146 | |||
| 147 | $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ", | ||
| 148 | "\n ", \@tokens); | ||
| 149 | $self->{"tokens"} = \@tokens; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub parse_kv { | ||
| 153 | my $self = shift; | ||
| 154 | my $line = shift; | ||
| 155 | my $pmsg; | ||
| 156 | my @tokens = (); | ||
| 157 | |||
| 158 | my $kvpat = qr/([^:]+):(\s*(.*))?/; | ||
| 159 | if ($line =~ /$kvpat/) { | ||
| 160 | my $key = $1; | ||
| 161 | my $value = $3; | ||
| 162 | $self->mesg(3, *parse{PACKAGE} . "::parse_kv: key '$key' value '$value'"); | ||
| 163 | $self->{"kv"}{$key} = $value; | ||
| 164 | if (exists($IDS::DataSource::HTTP::Request::simple_keys{lc($key)}) || | ||
| 165 | exists($IDS::DataSource::HTTP::Request::complex_keys{lc($key)})) { | ||
| 166 | push @tokens, "Key: $key"; | ||
| 167 | if (defined($value) and $value ne "") { | ||
| 168 | if (exists($IDS::DataSource::HTTP::Request::complex_keys{lc($key)})) { | ||
| 169 | $self->{$key} = IDS::DataSource::HTTP::new($IDS::DataSource::HTTP::Request::complex_keys{lc($key)}, | ||
| 170 | $self->{"params"}, $value); | ||
| 171 | push @tokens, $self->{$key}->tokens; | ||
| 172 | } else { # assume simple key | ||
| 173 | push @tokens, "Value: $value"; | ||
| 174 | } | ||
| 175 | } else { | ||
| 176 | $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 177 | ${$self->{"params"}}{"source"} . | ||
| 178 | " missing value for '$key'\n"; | ||
| 179 | $self->warn($pmsg, \@tokens, "!missing value"); | ||
| 180 | } | ||
| 181 | } else { | ||
| 182 | push @tokens, "Unknown Key: $key"; | ||
| 183 | # discard the value. Is this reasonable? | ||
| 184 | } | ||
| 185 | } elsif ($line =~ /^$/) { | ||
| 186 | push @tokens, "End-of-request"; | ||
| 187 | } else { | ||
| 188 | $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 189 | ${$self->{"params"}}{"source"} . | ||
| 190 | " Unknown line '$line'\n"; | ||
| 191 | $self->warn($pmsg, \@tokens, "!unknown line"); | ||
| 192 | } | ||
| 193 | |||
| 194 | return @tokens; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub empty { | ||
| 198 | my $self = shift; | ||
| 199 | undef $self->{"data"}, $self->{"tokens"}, $self->{"methodline"}, | ||
| 200 | $self->{"kv"}, $self->{"Referer"}, | ||
| 201 | $self->{"Accept"}, $self->{"AcceptLanguage"}, $self->{"Date"}, | ||
| 202 | $self->{"User-Agent"}, $self->{"Agent"}; | ||
| 203 | } | ||
| 204 | |||
| 205 | # accessor functions | ||
| 206 | sub data { | ||
| 207 | my $self = shift; | ||
| 208 | return $self->{"data"}; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub path { | ||
| 212 | my $self = shift; | ||
| 213 | return $self->{"methodline"}->path; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub method { | ||
| 217 | my $self = shift; | ||
| 218 | return $self->{"methodline"}->method; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub host { | ||
| 222 | my $self = shift; | ||
| 223 | return $self->{"kv"}{"Host"}; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub tokens { | ||
| 227 | my $self = shift; | ||
| 228 | my @result; | ||
| 229 | |||
| 230 | defined($self->{"tokens"}) or $self->parse(); | ||
| 231 | |||
| 232 | # Easy cases are handled efficiently | ||
| 233 | return $self->{"tokens"} if | ||
| 234 | ! wantarray && ${$self->{"params"}}{"with_values"} && | ||
| 235 | ! ${$self->{"params"}}{"lc_only"}; | ||
| 236 | |||
| 237 | return @{$self->{"tokens"}} if | ||
| 238 | wantarray && ${$self->{"params"}}{"with_values"} && | ||
| 239 | ! ${$self->{"params"}}{"lc_only"}; | ||
| 240 | |||
| 241 | # we have handled the simple cases. We're committed to making at | ||
| 242 | # least one change to what we have stored. | ||
| 243 | my @tokens = @{$self->{"tokens"}}; | ||
| 244 | |||
| 245 | map { | ||
| 246 | s/:.*$// unless ${$self->{"params"}}{"with_values"}; | ||
| 247 | lc if ${$self->{"params"}}{"lc_only"}; | ||
| 248 | } @tokens; | ||
| 249 | return wantarray ? @tokens : \@tokens; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub kv { | ||
| 253 | my $self = shift; | ||
| 254 | defined($self->{"tokens"}) or tokenize($self, {}); | ||
| 255 | return $self->{"kv"}; | ||
| 256 | } | ||
| 257 | |||
| 258 | =head1 AUTHOR INFORMATION | ||
| 259 | |||
| 260 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 261 | |||
| 262 | This library is free software; you can redistribute it and/or modify | ||
| 263 | it under the same terms as Perl itself. | ||
| 264 | |||
| 265 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 266 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 267 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 268 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 269 | speed of the reponse depends on how the research is proceeding. | ||
| 270 | |||
| 271 | =head1 BUGS | ||
| 272 | |||
| 273 | Please report them. | ||
| 274 | |||
| 275 | =head1 SEE ALSO | ||
| 276 | |||
| 277 | L<IDS::Algorithm>, L<IDS::DataSource> | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | 1; |