| 1 | package IDS::DataSource::HTTP; | ||
| 2 | use base qw(IDS::DataSource); | ||
| 3 | |||
| 4 | =head1 IDS::DataSource::HTTP | ||
| 5 | |||
| 6 | =head2 Introduction | ||
| 7 | |||
| 8 | This class is a subclass of IDS::DataSource, and meets its interface | ||
| 9 | specification. | ||
| 10 | |||
| 11 | All real uses will be through subclasses. | ||
| 12 | |||
| 13 | We expect descendents to: | ||
| 14 | |||
| 15 | =over | ||
| 16 | |||
| 17 | =item * | ||
| 18 | |||
| 19 | Whenever a string is loaded, parse then (no lazy evaluation). | ||
| 20 | This is because *we* have the function to return the tokens. | ||
| 21 | |||
| 22 | =item * | ||
| 23 | |||
| 24 | Parameters relating to parsing are in the "params" entry, which | ||
| 25 | is a hash reference. It is initialized here. | ||
| 26 | |||
| 27 | =item * | ||
| 28 | |||
| 29 | An optional initializer follows the parameters to new. | ||
| 30 | |||
| 31 | =item * | ||
| 32 | |||
| 33 | Elements of self: | ||
| 34 | |||
| 35 | =over | ||
| 36 | |||
| 37 | =item data | ||
| 38 | |||
| 39 | the data that caused the parse that we have | ||
| 40 | |||
| 41 | =item tokens | ||
| 42 | |||
| 43 | The results of the parse | ||
| 44 | |||
| 45 | =item params | ||
| 46 | |||
| 47 | parameters that may affect parsing | ||
| 48 | |||
| 49 | =back | ||
| 50 | |||
| 51 | =item * | ||
| 52 | |||
| 53 | The subclass must implement the following functions: | ||
| 54 | parse, empty | ||
| 55 | |||
| 56 | =back | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | use strict; | ||
| 61 | use warnings; | ||
| 62 | use Carp qw(cluck carp confess); | ||
| 63 | use IO::Handle; | ||
| 64 | |||
| 65 | $IDS::DataSource::HTTP::VERSION = "2.0"; | ||
| 66 | |||
| 67 | use IDS::DataSource::HTTP::Accept; | ||
| 68 | use IDS::DataSource::HTTP::Authorization; | ||
| 69 | use IDS::DataSource::HTTP::Agent; | ||
| 70 | use IDS::DataSource::HTTP::CacheControl; | ||
| 71 | use IDS::DataSource::HTTP::Charset; | ||
| 72 | use IDS::DataSource::HTTP::Cookie; | ||
| 73 | use IDS::DataSource::HTTP::Date; | ||
| 74 | use IDS::DataSource::HTTP::EmailAddr; | ||
| 75 | use IDS::DataSource::HTTP::Encoding; | ||
| 76 | use IDS::DataSource::HTTP::ETag; | ||
| 77 | use IDS::DataSource::HTTP::Expectation; | ||
| 78 | use IDS::DataSource::HTTP::Host; | ||
| 79 | use IDS::DataSource::HTTP::Int; | ||
| 80 | use IDS::DataSource::HTTP::Language; | ||
| 81 | use IDS::DataSource::HTTP::MethodLine; | ||
| 82 | use IDS::DataSource::HTTP::RetryAfter; | ||
| 83 | use IDS::DataSource::HTTP::Range; | ||
| 84 | use IDS::DataSource::HTTP::IfRange; | ||
| 85 | use IDS::DataSource::HTTP::Referer; | ||
| 86 | use IDS::DataSource::HTTP::URI; | ||
| 87 | use IDS::DataSource::HTTP::URIAuthority; | ||
| 88 | use IDS::DataSource::HTTP::Via; | ||
| 89 | use IDS::DataSource::HTTP::IfModifiedSince; | ||
| 90 | use IDS::DataSource::HTTP::XForwarded; | ||
| 91 | |||
| 92 | =over | ||
| 93 | |||
| 94 | =item load(source) | ||
| 95 | |||
| 96 | load exactly one request from a file, IO::Handle, or supplied string. | ||
| 97 | If a string is used, it is up to the caller to ensure that more than | ||
| 98 | one request is not in the string. | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub load { | ||
| 105 | my $self = shift; | ||
| 106 | my $source = shift; | ||
| 107 | defined($source) or | ||
| 108 | confess *load{PACKAGE} . "::load called without a source\n"; | ||
| 109 | |||
| 110 | $self->empty(); | ||
| 111 | if ($source =~ /\n/) { | ||
| 112 | ${$self->{"params"}}{"source"} = "Supplied string"; | ||
| 113 | $self->{"data"} = $source; | ||
| 114 | $self->cleanup; | ||
| 115 | } elsif (-f $source) { | ||
| 116 | ${$self->{"params"}}{"source"} = $source; | ||
| 117 | my $fh = new IO::File("< $source"); | ||
| 118 | defined($fh) or carp "Unable to open $source: $!"; | ||
| 119 | $self->read_session($fh); | ||
| 120 | } elsif ($source->isa("IO::Handle")) { | ||
| 121 | ${$self->{"params"}}{"source"} = "IO::handle"; | ||
| 122 | $self->read_session($source); | ||
| 123 | } else { | ||
| 124 | confess *load{PACKAGE} . "::load: I do not know what to do with '$source'\n"; | ||
| 125 | } | ||
| 126 | $self->parse; | ||
| 127 | $self->{"todel"} = undef; | ||
| 128 | } | ||
| 129 | |||
| 130 | =over | ||
| 131 | |||
| 132 | =item cleanup | ||
| 133 | |||
| 134 | Clean data to prepare for tokenizing. | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub cleanup { | ||
| 141 | my $self = shift; | ||
| 142 | $self->{"data"} =~ s/\r//g; | ||
| 143 | $self->{"data"} =~ s/\s+$//s; # any spaces at the end (is this valid?) | ||
| 144 | $self->{"data"} =~ s/\n\s+/ /g; # fix continuation lines | ||
| 145 | |||
| 146 | # some %-escaped chars back to normal | ||
| 147 | $self->{"data"} =~ s/%7E/~/g; | ||
| 148 | #$self->{"data"} =~ s/%20/ /g; | ||
| 149 | return $self; | ||
| 150 | } | ||
| 151 | |||
| 152 | =over | ||
| 153 | |||
| 154 | =item read_session(filehandle) | ||
| 155 | |||
| 156 | Read an HTTP session (up to EOF or a blank line) from the filehandle | ||
| 157 | passed as an argument. | ||
| 158 | |||
| 159 | =back | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | sub read_session { | ||
| 164 | my $self = shift; | ||
| 165 | my $fh = shift or | ||
| 166 | cluck *read_session{PACKAGE} . | ||
| 167 | "::read_session called without a filehandle"; | ||
| 168 | |||
| 169 | my $data = ""; | ||
| 170 | my @lines = (); | ||
| 171 | my $first = 1; | ||
| 172 | while (<$fh>) { # note last below as an alternate exit for loop | ||
| 173 | push @lines, $_; | ||
| 174 | |||
| 175 | if ($first) { | ||
| 176 | next if /^[\r\n]+$/; # skip blank lines at the beginning A | ||
| 177 | # kludge, but appears to be necesary | ||
| 178 | # for some POST methods | ||
| 179 | $first = 0; | ||
| 180 | } else { | ||
| 181 | last if /^[\r\n]+$/; | ||
| 182 | } | ||
| 183 | s/\r//g; # delete all CRs | ||
| 184 | |||
| 185 | $data .= $_; | ||
| 186 | } | ||
| 187 | $self->{"lines"} = \@lines; | ||
| 188 | my $m = "read_session: loop end eof? " . $fh->eof; | ||
| 189 | $self->mesg(2, $m); | ||
| 190 | $self->mesg(2, "read_session: end of loop data '$data'"); | ||
| 191 | |||
| 192 | $data =~ s/ +$//; # remove final spaces (if any) | ||
| 193 | $data =~ s/\n\s+/ /mg; # fix continuation lines | ||
| 194 | |||
| 195 | if ($data =~ /^POST|PROPFIND/m) { # try to deal properly with POST/PROPFIND data | ||
| 196 | if ($data =~ /Content-Length: (\d+)/) { | ||
| 197 | my ($postdata, $content_len); | ||
| 198 | $content_len = $1; | ||
| 199 | read $fh, $postdata, $content_len; | ||
| 200 | $self->{"postdata"} = $postdata; | ||
| 201 | my $msg = join('', "read_session: postdata ($content_len bytes) is: '$postdata' ", length($postdata), " chars"); | ||
| 202 | $self->mesg(1, $msg); | ||
| 203 | } else { # Nope, weird request. | ||
| 204 | $self->mesg(1, "read_session: no Content-Length in POST/PROPFIND request"); | ||
| 205 | $self->mesg(1, "read_session: data '$data'"); | ||
| 206 | my $msg = join('', "read_session: remaining data: '", <$fh>, "'"); | ||
| 207 | $self->mesg(1, $msg); | ||
| 208 | #$fh->close; ### can we recover from this? | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | if ($data) { | ||
| 213 | $self->{"data"} = $data; | ||
| 214 | $self->cleanup; | ||
| 215 | return 1; | ||
| 216 | } else { | ||
| 217 | $self->warn(${$self->{"params"}}{"source"} . ": data was empty", [],""); | ||
| 218 | return 0; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | =over | ||
| 223 | |||
| 224 | =item read_next(filehandle) | ||
| 225 | |||
| 226 | Read the next request. We are expecting a list of files on the | ||
| 227 | filehandle passed in. We will either read the next request from the from the | ||
| 228 | currently-open file, or open the next file in the list, as needed. | ||
| 229 | |||
| 230 | =back | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | # This function has to keep track of state, making it more complex than | ||
| 235 | # a loop in a caller. | ||
| 236 | sub read_next { | ||
| 237 | my $self = shift; | ||
| 238 | my $fname_fh = shift; | ||
| 239 | defined($fname_fh) or | ||
| 240 | cluck *read_next{PACKAGE} . "::read_next called without a filehandle"; | ||
| 241 | |||
| 242 | my $fh = $self->{"current_fh"}; | ||
| 243 | |||
| 244 | # reset everything for the new request | ||
| 245 | $self->empty(); | ||
| 246 | |||
| 247 | my $ret; | ||
| 248 | do { | ||
| 249 | # See if we are done. | ||
| 250 | return undef if $fname_fh->eof && (!defined($fh) || $fh->eof); | ||
| 251 | |||
| 252 | # while we need another file and we have files in the list; | ||
| 253 | # will run 0 times if there is more to read from the current | ||
| 254 | # file. | ||
| 255 | while (! (defined($fh) && ! $fh->eof) && ! $fname_fh->eof) { | ||
| 256 | $self->mesg(1, "Processed " . $self->{"fname"} . "\n") | ||
| 257 | if exists($self->{"fname"}) && defined($self->{"fname"}); | ||
| 258 | $fh = $self->open_next($fname_fh); #resets fname and session count | ||
| 259 | } | ||
| 260 | # see if we ran out of filenames | ||
| 261 | return undef unless defined($fh); | ||
| 262 | |||
| 263 | $self->{"session"}++; | ||
| 264 | ${$self->{"params"}}{"source"} = $self->{"fname"} . " request " . | ||
| 265 | $self->{"session"}; | ||
| 266 | $ret = $self->read_session($fh); | ||
| 267 | } until ($ret); | ||
| 268 | $self->{"current_fh"} = $fh; | ||
| 269 | $self->parse; | ||
| 270 | return 1; | ||
| 271 | } | ||
| 272 | |||
| 273 | # open_next is a utility function used by read_files; should not be | ||
| 274 | # called outside of this object | ||
| 275 | sub open_next { | ||
| 276 | my $self = shift; | ||
| 277 | my $fname_fh = shift; | ||
| 278 | my $fname; | ||
| 279 | |||
| 280 | # handle missing files | ||
| 281 | do { | ||
| 282 | # skip blank or commented lines | ||
| 283 | do { | ||
| 284 | $fname = $fname_fh->getline; | ||
| 285 | last unless $fname; | ||
| 286 | chomp $fname; | ||
| 287 | # support comments in input | ||
| 288 | $fname =~ s/\s*#.*$//; | ||
| 289 | } while (! $fname_fh->eof && $fname =~ /^$/); | ||
| 290 | $self->{"fname"} = $fname; | ||
| 291 | $self->{"session"} = 0; | ||
| 292 | $self->{"current_fh"} = new IO::File "$fname" or | ||
| 293 | warn "Unable to open $fname: $!\n"; | ||
| 294 | } while (! $self->{"current_fh"}); | ||
| 295 | $self->mesg(1, "Opened $fname"); | ||
| 296 | return $self->{"current_fh"}; | ||
| 297 | } | ||
| 298 | |||
| 299 | =over | ||
| 300 | |||
| 301 | =item data() | ||
| 302 | |||
| 303 | Return the data used for the tokens we have. If called in array mode, | ||
| 304 | we return the individual lines, otherwise the join of those line. | ||
| 305 | |||
| 306 | =back | ||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | sub data { | ||
| 311 | my $self = shift; | ||
| 312 | return wantarray ? @{$self->{"lines"}} : $self->{"data"}; | ||
| 313 | } | ||
| 314 | |||
| 315 | =over | ||
| 316 | |||
| 317 | =item postdata() | ||
| 318 | |||
| 319 | Return the data part of a POST request. Only has meaning in a POST | ||
| 320 | request. | ||
| 321 | |||
| 322 | =back | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | sub postdata { | ||
| 327 | my $self = shift; | ||
| 328 | |||
| 329 | return $self->{"postdata"}; | ||
| 330 | } | ||
| 331 | |||
| 332 | # other, undocumented accessor functions | ||
| 333 | |||
| 334 | sub path { | ||
| 335 | my $self = shift; | ||
| 336 | return $self->{"methodline"}->path; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub method { | ||
| 340 | my $self = shift; | ||
| 341 | return $self->{"methodline"}->method; | ||
| 342 | } | ||
| 343 | |||
| 344 | sub host { | ||
| 345 | my $self = shift; | ||
| 346 | return $self->{"kv"}{"Host"}; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub kv { | ||
| 350 | my $self = shift; | ||
| 351 | defined($self->{"tokens"}) or tokenize($self, {}); | ||
| 352 | return $self->{"kv"}; | ||
| 353 | } | ||
| 354 | |||
| 355 | =over | ||
| 356 | |||
| 357 | =item source() | ||
| 358 | =item source(value) | ||
| 359 | |||
| 360 | Set and/or get the data source. | ||
| 361 | |||
| 362 | =back | ||
| 363 | |||
| 364 | =cut | ||
| 365 | |||
| 366 | sub source { | ||
| 367 | my $self = shift; | ||
| 368 | if (defined($_[0])) { | ||
| 369 | my $old = ${$self->{"params"}}{"source"}; | ||
| 370 | ${$self->{"params"}}{"source"} = $_[0]; | ||
| 371 | return $old; | ||
| 372 | } else { | ||
| 373 | return ${$self->{"params"}}{"source"}; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | =over | ||
| 378 | |||
| 379 | =item tokens() | ||
| 380 | |||
| 381 | Return the tokens that result from parsing the structure. The tokens | ||
| 382 | can be returned as an array or a reference to the internal array holding | ||
| 383 | them (for efficiency). Modify this referenced array at your own risk. | ||
| 384 | |||
| 385 | Note that this function can remove values. | ||
| 386 | Note that this function can convert everything to lower case. | ||
| 387 | Both of these options are controlled by parameters, and neither affects | ||
| 388 | the internal version of the tokens. | ||
| 389 | |||
| 390 | =back | ||
| 391 | |||
| 392 | =cut | ||
| 393 | |||
| 394 | sub tokens { | ||
| 395 | my $self = shift; | ||
| 396 | my @result; | ||
| 397 | |||
| 398 | defined($self->{"tokens"}) or $self->parse; # just in case | ||
| 399 | defined($self->{"tokens"}) or | ||
| 400 | return []; | ||
| 401 | # confess *tokens{PACKAGE} . "::tokens parsing produced no tokens!"; | ||
| 402 | |||
| 403 | # Easy cases are handled efficiently | ||
| 404 | if (${$self->{"params"}}{"with_values"} && ! ${$self->{"params"}}{"lc_only"}) { | ||
| 405 | return wantarray ? @{$self->{"tokens"}} : $self->{"tokens"}; | ||
| 406 | } | ||
| 407 | |||
| 408 | # we have handled the simple cases. We're committed to making at | ||
| 409 | # least one change to what we have stored. | ||
| 410 | my @tokens = @{$self->{"tokens"}}; | ||
| 411 | |||
| 412 | map { | ||
| 413 | s/:.*$// unless ${$self->{"params"}}{"with_values"}; | ||
| 414 | lc if ${$self->{"params"}}{"lc_only"}; | ||
| 415 | } @tokens; | ||
| 416 | return wantarray ? @tokens : \@tokens; | ||
| 417 | } | ||
| 418 | |||
| 419 | =over | ||
| 420 | |||
| 421 | =item expand_pct(data) | ||
| 422 | |||
| 423 | Expand the %-substitutions. This function currently does not handle | ||
| 424 | unicode and &-expansions. Should it? | ||
| 425 | |||
| 426 | =back | ||
| 427 | |||
| 428 | =cut | ||
| 429 | |||
| 430 | sub expand_pct { | ||
| 431 | my $self = shift; | ||
| 432 | my $data = shift; | ||
| 433 | |||
| 434 | while ($data =~ /%([0-9A-Fa-f]{2})/) { | ||
| 435 | $data =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/e; | ||
| 436 | } | ||
| 437 | return $data; | ||
| 438 | } | ||
| 439 | |||
| 440 | # keys is a hash to speed up the searching (use the hash instead of grep) | ||
| 441 | # note all lc, because the standard says they are case insensitive. | ||
| 442 | ### Note that we assume all IDS::DataSource::HTTP:: subobjects use the | ||
| 443 | ### IDS::DataSource::HTTP::Part constructor. | ||
| 444 | # Comments below refer to section numbers in RFC 2616 | ||
| 445 | # ??? for a section means that it appears not to be a standard, but in | ||
| 446 | # use anyway. | ||
| 447 | %IDS::DataSource::HTTP::complex_keys = ( | ||
| 448 | 'accept' => 'IDS::DataSource::HTTP::Accept', # 14.1 | ||
| 449 | 'accept-charset' => 'IDS::DataSource::HTTP::Charset', # 14.2 | ||
| 450 | 'accept-encoding' => 'IDS::DataSource::HTTP::Encoding', # 14.3 | ||
| 451 | 'accept-language' => 'IDS::DataSource::HTTP::Language', # 14.4 | ||
| 452 | # 14.5 Accept-Ranges is server->client | ||
| 453 | # 14.6 Age is server or proxy->client | ||
| 454 | # 14.7 Allow is server->client | ||
| 455 | 'authorization' => 'IDS::DataSource::HTTP::Authorization', # 14.8 | ||
| 456 | 'cache-control' => 'IDS::DataSource::HTTP::CacheControl', # 14.9 | ||
| 457 | # 14.10 Connection; simple and already handled by key+value | ||
| 458 | # 14.11 Content-Encoding; simple and already handled by key+value | ||
| 459 | # 14.12 Content-Language; simple and already handled by key+value | ||
| 460 | 'content-length' => 'IDS::DataSource::HTTP::Int', # 14.13 | ||
| 461 | # 14.14 Content-Location likely to be server->client | ||
| 462 | 'content-md5' => 'IDS::DataSource::HTTP::MD5', # 14.15 | ||
| 463 | # 14.16 Content-Range is server->client | ||
| 464 | 'content-type' => 'IDS::DataSource::HTTP::Accept', # 14.17 | ||
| 465 | 'date' => 'IDS::DataSource::HTTP::Date', # 14.18 | ||
| 466 | 'etag' => 'IDS::DataSource::HTTP::ETag', # 14.19 | ||
| 467 | 'expect' => 'IDS::DataSource::HTTP::Expectation', # 14.20 | ||
| 468 | 'expires' => 'IDS::DataSource::HTTP::Date', # 14.21 | ||
| 469 | 'from' => 'IDS::DataSource::HTTP::EmailAddr', # 14.22 | ||
| 470 | # for host, we accept more than the standard, since | ||
| 471 | # authority may include user info | ||
| 472 | 'host' => 'IDS::DataSource::HTTP::URIAuthority', # 14.23 | ||
| 473 | 'if-match' => 'IDS::DataSource::HTTP::ETag', # 14.24 | ||
| 474 | 'since' => 'IDS::DataSource::HTTP::Date', # 14.25 | ||
| 475 | 'if-none-match' => 'IDS::DataSource::HTTP::ETag', # 14.26 | ||
| 476 | 'if-range' => 'IDS::DataSource::HTTP::IfRange', # 14.27 | ||
| 477 | 'if-modified-since' => 'IDS::DataSource::HTTP::IfModifiedSince', # 14.28 | ||
| 478 | 'if-unmodified-since' => 'IDS::DataSource::HTTP::Date', # 14.28 | ||
| 479 | 'unless-modified-since' => 'IDS::DataSource::HTTP::Date', # 14.28 | ||
| 480 | 'unless-unmodified-since' => 'IDS::DataSource::HTTP::Date', # 14.28 | ||
| 481 | 'last-modified' => 'IDS::DataSource::HTTP::Date', # 14.29 | ||
| 482 | 'location' => 'IDS::DataSource::HTTP::URI', # 14.30 | ||
| 483 | 'max-forwards' => 'IDS::DataSource::HTTP::Int', # 14.31 | ||
| 484 | # ### Do we need 14.32 Pragma? | ||
| 485 | # 14.33 Proxy-Authenticate is proxy->client | ||
| 486 | # 14.34 Proxy-Authorization is client->proxy | ||
| 487 | 'range' => 'IDS::DataSource::HTTP::Range', # 14.35 | ||
| 488 | 'referer' => 'IDS::DataSource::HTTP::Referer', # 14.36 | ||
| 489 | 'retry-after' => 'IDS::DataSource::HTTP::RetryAfter', # 14.37 | ||
| 490 | # 14.38 Server is server->client | ||
| 491 | 'te' => 'IDS::DataSource::HTTP::Encoding', # 14.39 | ||
| 492 | # ### might need 14.40 Trailer | ||
| 493 | 'transfer-encoding' => 'IDS::DataSource::HTTP::Encoding', # 14.41 | ||
| 494 | # ### need 14.42 Upgrade (although unlikely to see) | ||
| 495 | 'user-agent' => 'IDS::DataSource::HTTP::Agent', # 14.43 | ||
| 496 | # 14.44 Vary is server-> | ||
| 497 | 'via' => 'IDS::DataSource::HTTP::Via', # 14.45 | ||
| 498 | # ### might need 14.46 Warning ? | ||
| 499 | # 14.47 WWW-Authenticate is server->client | ||
| 500 | |||
| 501 | 'cookie' => 'IDS::DataSource::HTTP::Cookie', # RFC 2965 | ||
| 502 | 'charset' => 'IDS::DataSource::HTTP::Charset', # ??? | ||
| 503 | 'encoding' => 'IDS::DataSource::HTTP::Encoding', # ??? | ||
| 504 | 'forwarded' => 'IDS::DataSource::HTTP::Via', # ??? | ||
| 505 | 'x-forwarded-for' => 'IDS::DataSource::HTTP::XForwarded', # ??? | ||
| 506 | # Agent is not in 2616, but it appears to be used in a | ||
| 507 | # similar manner to user-agent | ||
| 508 | 'agent' => 'IDS::DataSource::HTTP::Agent', # ??? | ||
| 509 | ); | ||
| 510 | |||
| 511 | %IDS::DataSource::HTTP::simple_keys = ( | ||
| 512 | 'allow' => 1, | ||
| 513 | 'connection' => 1, | ||
| 514 | 'content-encoding' => 1, | ||
| 515 | 'content-language' => 1, | ||
| 516 | 'content-location' => 1, | ||
| 517 | 'content-range' => 1, | ||
| 518 | 'if-range' => 1, | ||
| 519 | 'proxy-authorization' => 1, | ||
| 520 | 'transfer-encoding' => 1, | ||
| 521 | ); | ||
| 522 | |||
| 523 | # break the request into the component tokens. | ||
| 524 | sub parse { | ||
| 525 | my $self = shift; | ||
| 526 | unless ($self->{"data"}) { | ||
| 527 | cluck "Warning: " . *tokenize{PACKAGE} . "::tokenize called with no data."; | ||
| 528 | $self->{"tokens"} = []; | ||
| 529 | return $self; | ||
| 530 | } | ||
| 531 | if ($self->{"data"} =~ /^$/) { | ||
| 532 | return $self->mesg(0, "Empty data from " . ${$self->{"params"}}{"source"}); | ||
| 533 | } | ||
| 534 | |||
| 535 | my @tokens = (); | ||
| 536 | |||
| 537 | my @lines = split /\n/, $self->{"data"}; | ||
| 538 | chomp(@lines); | ||
| 539 | $self->mesg(1, *parse{PACKAGE} . "::parse: Line is '$lines[0]'"); | ||
| 540 | $self->{"methodline"} = new IDS::DataSource::HTTP::MethodLine($self->{"params"}, $lines[0]); | ||
| 541 | push @tokens, $self->{"methodline"}->tokens(); | ||
| 542 | |||
| 543 | map { $self->mesg(1, *parse{PACKAGE} . "::parse: Line is '$_'"); | ||
| 544 | push @tokens, $self->parse_kv($_); | ||
| 545 | } @lines[1..$#lines]; | ||
| 546 | |||
| 547 | $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ", | ||
| 548 | "\n ", \@tokens); | ||
| 549 | $self->{"tokens"} = \@tokens; | ||
| 550 | } | ||
| 551 | |||
| 552 | sub parse_kv { | ||
| 553 | my $self = shift; | ||
| 554 | my $line = shift; | ||
| 555 | my $pmsg; | ||
| 556 | my @tokens = (); | ||
| 557 | |||
| 558 | my $kvpat = qr/([^:]+):(\s*(.*))?/; | ||
| 559 | if ($line =~ /$kvpat/) { | ||
| 560 | my $key = $1; | ||
| 561 | my $value = $3; | ||
| 562 | $self->mesg(3, *parse{PACKAGE} . "::parse_kv: key '$key' value '$value'"); | ||
| 563 | $self->{"kv"}{$key} = $value; | ||
| 564 | if (exists($IDS::DataSource::HTTP::simple_keys{lc($key)})) { | ||
| 565 | push @tokens, "Key: $key"; | ||
| 566 | if (defined($value) and $value ne "") { | ||
| 567 | push @tokens, "Value: $value"; | ||
| 568 | } else { | ||
| 569 | $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 570 | ${$self->{"params"}}{"source"} . | ||
| 571 | " missing value for '$key'\n"; | ||
| 572 | $self->warn($pmsg, \@tokens, "!missing value"); | ||
| 573 | } | ||
| 574 | } elsif (exists($IDS::DataSource::HTTP::complex_keys{lc($key)})) { | ||
| 575 | $self->mesg(3, *parse{PACKAGE} . "::parse_kv: complex key " . lc($key)); | ||
| 576 | push @tokens, "Key: $key"; | ||
| 577 | if (defined($value) and $value ne "") { | ||
| 578 | $self->{$key} = IDS::DataSource::HTTP::Part::new($IDS::DataSource::HTTP::complex_keys{lc($key)}, | ||
| 579 | $self->{"params"}, $value); | ||
| 580 | push @tokens, $self->{$key}->tokens; | ||
| 581 | } else { | ||
| 582 | $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 583 | ${$self->{"params"}}{"source"} . | ||
| 584 | " missing value for '$key'\n"; | ||
| 585 | $self->warn($pmsg, \@tokens, "!missing value"); | ||
| 586 | } | ||
| 587 | } else { | ||
| 588 | push @tokens, "Unknown Key: $key"; | ||
| 589 | ### discard the value. Is this reasonable? | ||
| 590 | } | ||
| 591 | } elsif ($line =~ /^$/) { | ||
| 592 | push @tokens, "End-of-request"; | ||
| 593 | } else { | ||
| 594 | $pmsg = *parse{PACKAGE} . "::parse: In " . | ||
| 595 | ${$self->{"params"}}{"source"} . | ||
| 596 | " Unknown line '$line'\n"; | ||
| 597 | $self->warn($pmsg, \@tokens, "!unknown line"); | ||
| 598 | } | ||
| 599 | |||
| 600 | return @tokens; | ||
| 601 | } | ||
| 602 | |||
| 603 | sub empty { | ||
| 604 | my $self = shift; | ||
| 605 | undef $self->{"data"}, $self->{"tokens"}, $self->{"methodline"}, | ||
| 606 | $self->{"kv"}, $self->{"Referer"}, | ||
| 607 | $self->{"Accept"}, $self->{"AcceptLanguage"}, $self->{"Date"}, | ||
| 608 | $self->{"User-Agent"}, $self->{"Agent"}; | ||
| 609 | } | ||
| 610 | |||
| 611 | =head2 Functions required by IDS::DataSource | ||
| 612 | |||
| 613 | =over | ||
| 614 | |||
| 615 | =item default_parameters() | ||
| 616 | |||
| 617 | Sets all of the default values for the parameters. Normally called by | ||
| 618 | new() or one of its descendents. | ||
| 619 | |||
| 620 | =back | ||
| 621 | |||
| 622 | =cut | ||
| 623 | |||
| 624 | sub default_parameters { | ||
| 625 | my $self = shift; | ||
| 626 | my %params = ( | ||
| 627 | # general parameters | ||
| 628 | # source # filled in by IDS::DataSource::HTTP::read_session, | ||
| 629 | # it is the source of data, | ||
| 630 | # used when producing error/warn msgs | ||
| 631 | "msg_fh" => $self->fh_or_stdout,# Where warning messages go; nowhere if undef | ||
| 632 | "return_warnings" => 1, # Warnings are tokens | ||
| 633 | "print_warnings" => 0, # Print warning messages (to msg_fh) | ||
| 634 | "verbose" => 0, # Print extra information; larger means more | ||
| 635 | |||
| 636 | # what to return as tokens | ||
| 637 | "with_values" => 1, # whether to put values in the parsed http | ||
| 638 | "lc_only" => 1, # whether to map all of the http to lower case | ||
| 639 | "file_types_only" => 1, # return only file types, not names | ||
| 640 | "file_name_lengths" => 1, # return filename length of unknown file types | ||
| 641 | # various data validation options | ||
| 642 | "recognize_hostnames" => 1, # return simply if a hostname is valid or not | ||
| 643 | "recognize_ipaddr" => 1, # return simply if an addr is valid or not | ||
| 644 | "lookup_hosts" => 0, # lookup hosts and addrs to ensure DNS entries? | ||
| 645 | "recognize_dates" => 1, # return simply if a date is valid or not | ||
| 646 | "handle_PHPSESSID" => 1, # validate PHP Session ID to avoid hash | ||
| 647 | "handle_EntityTag" => 1, # validate Entity Tags to avoid hashes | ||
| 648 | "recognize_qvalues" => 1, # validate qvalue; otherwise, return value | ||
| 649 | "email_user_length_only" => 1, # only return the email address length, | ||
| 650 | # not value | ||
| 651 | "cookie_values" => 0, # whether or not to include cookie values | ||
| 652 | ); | ||
| 653 | $self->{"params"} = \%params; | ||
| 654 | $self->{"try_deleting"} = [ qw(Referer Cookie Accept-Language | ||
| 655 | Accept-Charset Accept) ]; | ||
| 656 | $self->{"todel"} = undef; | ||
| 657 | } | ||
| 658 | |||
| 659 | =over | ||
| 660 | |||
| 661 | =item param_options() | ||
| 662 | |||
| 663 | Command-line option specifiers for our parameters for GetOpt::Long. | ||
| 664 | |||
| 665 | =back | ||
| 666 | |||
| 667 | =cut | ||
| 668 | |||
| 669 | sub param_options { | ||
| 670 | my $self = shift; | ||
| 671 | |||
| 672 | return ( | ||
| 673 | "values!" => \${$self->{"params"}}{"with_values"}, | ||
| 674 | "lc!" => \${$self->{"params"}}{"lc_only"}, | ||
| 675 | "file_types_only!" => \${$self->{"params"}}{"file_types_only"}, | ||
| 676 | "file_name_lengths!" => \${$self->{"params"}}{"file_name_lengths"}, | ||
| 677 | "http_verbose=i" => \${$self->{"params"}}{"verbose"}, | ||
| 678 | "return_warnings!" => \${$self->{"params"}}{"return_warnings"}, | ||
| 679 | "print_warnings!" => \${$self->{"params"}}{"print_warnings"}, | ||
| 680 | "recognize_hostnames!" => \${$self->{"params"}}{"recognize_hostnames"}, | ||
| 681 | "recognize_ipaddr!" => \${$self->{"params"}}{"recognize_ipaddr"}, | ||
| 682 | "lookup_hosts!" => \${$self->{"params"}}{"lookup_hosts"}, | ||
| 683 | "recognize_dates!" => \${$self->{"params"}}{"recognize_dates"}, | ||
| 684 | "handle_PHPSESSID!" => \${$self->{"params"}}{"handle_PHPSESSID"}, | ||
| 685 | "handle_EntityTag!" => \${$self->{"params"}}{"handle_EntityTag"}, | ||
| 686 | "recognize_qvalues!" => \${$self->{"params"}}{"recognize_qvalues"}, | ||
| 687 | "email_user_length_only!" => \${$self->{"params"}}{"email_user_length_only"}, | ||
| 688 | "alternate_delete=s@" => $self->{"try_deleting"}, | ||
| 689 | "cookie_values!" => \${$self->{"params"}}{"cookie_values"}, | ||
| 690 | ); | ||
| 691 | } | ||
| 692 | |||
| 693 | =over | ||
| 694 | |||
| 695 | =item alternate() | ||
| 696 | |||
| 697 | Try deleting lines and re-parsing. We have to keep track of state. | ||
| 698 | Note that the various reading functions will reset the state. | ||
| 699 | |||
| 700 | This function returns either a new IDS::DataSource::HTTP object or | ||
| 701 | undef. If called after it returns undef, it will start the process | ||
| 702 | over. | ||
| 703 | |||
| 704 | =back | ||
| 705 | |||
| 706 | =cut | ||
| 707 | |||
| 708 | sub alternate { | ||
| 709 | my $self = shift; | ||
| 710 | my (@todel, @lines, $hdr, $http); | ||
| 711 | |||
| 712 | # keep track of state with $self->{"todel"}, which is a reference to | ||
| 713 | # a list. If this value is undefined, then this is the first time. | ||
| 714 | if (! defined(${$self->{"todel"}})) { | ||
| 715 | @todel = @{$self->{"try_deleting"}}; | ||
| 716 | } elsif (! @{${$self->{"todel"}}}) { | ||
| 717 | undef ${$self->{"todel"}}; | ||
| 718 | return undef; # nothing more to do | ||
| 719 | } else { | ||
| 720 | @todel = @{${$self->{"todel"}}} | ||
| 721 | } | ||
| 722 | |||
| 723 | do { | ||
| 724 | $hdr = pop @todel; | ||
| 725 | @lines = grep { !/^$hdr: /i } @{$self->{"lines"}}; | ||
| 726 | } while (@todel && $#lines == $#{$self->{"lines"}}); | ||
| 727 | ${$self->{"todel"}} = \@todel; | ||
| 728 | |||
| 729 | # did we achieve anything with the deleting? | ||
| 730 | if ($#lines == $#{$self->{"lines"}}) { | ||
| 731 | undef ${$self->{"todel"}}; | ||
| 732 | return undef; | ||
| 733 | } | ||
| 734 | |||
| 735 | $http = new IDS::DataSource::HTTP; | ||
| 736 | $http->load(join("\r\n", @lines)); | ||
| 737 | return ("Without $hdr", $http); | ||
| 738 | } | ||
| 739 | |||
| 740 | =head1 AUTHOR INFORMATION | ||
| 741 | |||
| 742 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 743 | |||
| 744 | This library is free software; you can redistribute it and/or modify | ||
| 745 | it under the same terms as Perl itself. | ||
| 746 | |||
| 747 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 748 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 749 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 750 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 751 | speed of the reponse depends on how the research is proceeding. | ||
| 752 | |||
| 753 | =head1 BUGS | ||
| 754 | |||
| 755 | Please report them. | ||
| 756 | |||
| 757 | =head1 SEE ALSO | ||
| 758 | |||
| 759 | L<IDS::Test>, L<IDS::DataSource>, L<IDS::Algorithm> | ||
| 760 | |||
| 761 | =cut | ||
| 762 | |||
| 763 | 1; |