| 1 | package IDS::DataSource::HTTP::Part; | ||
| 2 | use base qw(IDS::DataSource::HTTP); | ||
| 3 | |||
| 4 | =head1 IDS::DataSource::HTTP::Part | ||
| 5 | |||
| 6 | =head2 Introduction | ||
| 7 | |||
| 8 | A common superclass for parts of an HTTP request. All real uses will | ||
| 9 | be through subclasses. | ||
| 10 | |||
| 11 | We expect descendents to: | ||
| 12 | |||
| 13 | =over | ||
| 14 | |||
| 15 | =item Whenever a string is loaded, parse then (no lazy evaluation). | ||
| 16 | This is because *we* have the function to return the tokens. | ||
| 17 | |||
| 18 | =item Parameters relating to parsing are in the "params" entry, which | ||
| 19 | is a hash reference. It is initialized here. | ||
| 20 | |||
| 21 | =item An optional initializer follows the parameters to new. | ||
| 22 | |||
| 23 | =item Elements of self: | ||
| 24 | |||
| 25 | =over | ||
| 26 | |||
| 27 | =item data => the data that caused the parse that we have | ||
| 28 | |||
| 29 | =item tokens => The results of the parse | ||
| 30 | |||
| 31 | =item params => parameters that may affect parsing | ||
| 32 | |||
| 33 | =back | ||
| 34 | |||
| 35 | =item The subclass must implement the following functions: | ||
| 36 | parse, empty | ||
| 37 | |||
| 38 | =back | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | use strict; | ||
| 43 | use warnings; | ||
| 44 | use Carp qw(cluck carp confess); | ||
| 45 | use IO::Handle; | ||
| 46 | |||
| 47 | $IDS::DataSource::HTTP::Part::VERSION = "2.0"; | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my $proto = shift; | ||
| 51 | my $class = ref($proto) || $proto; | ||
| 52 | my $self = { }; | ||
| 53 | (defined($class) && $class) or | ||
| 54 | confess *new{PACKAGE}, "::new is missing the class."; | ||
| 55 | bless $self, $class; | ||
| 56 | |||
| 57 | $self->default_parameters; | ||
| 58 | my $source = $self->handle_parameters(@_); | ||
| 59 | |||
| 60 | $self->init($source) if defined($source); | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | # init from a string | ||
| 66 | sub init { | ||
| 67 | my $self = shift; | ||
| 68 | |||
| 69 | $self->empty(); | ||
| 70 | |||
| 71 | $self->{"data"} = shift; | ||
| 72 | defined($self->{"data"}) or | ||
| 73 | confess *init{PACKAGE} . "::init called without a string"; | ||
| 74 | |||
| 75 | $self->cleanup; | ||
| 76 | $self->parse; | ||
| 77 | } | ||
| 78 | |||
| 79 | =over | ||
| 80 | |||
| 81 | =item parameters() | ||
| 82 | =item parameters(param) | ||
| 83 | =item parameters(param, value, ...) | ||
| 84 | |||
| 85 | =back | ||
| 86 | |||
| 87 | Set or retrieve the current parameters (individual or group). | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub parameters { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | return (wantarray ? %{$self->{"params"}} : $self->{"params"}) | ||
| 95 | if ($#_ == -1); | ||
| 96 | |||
| 97 | return ${$self->{"params"}}{$_[0]} if ($#_ == 0); | ||
| 98 | |||
| 99 | if ($#_ == 1) { | ||
| 100 | my $old = ${$self->{"params"}}{$_[0]}; | ||
| 101 | ${$self->{"params"}}{$_[0]} = $_[1]; | ||
| 102 | return $old; | ||
| 103 | } | ||
| 104 | |||
| 105 | scalar(@_) % 2 != 0 and confess "odd > 1 number of parameters passed to ", | ||
| 106 | *parameters{PACKAGE}, ". See documentation for proper usage.\n"; | ||
| 107 | |||
| 108 | for (my $i = 0; $i < $#_; $i+=2) { | ||
| 109 | ${$self->{"params"}}{$_[$i]} = $_[$i+1]; | ||
| 110 | } | ||
| 111 | |||
| 112 | return 1; | ||
| 113 | } | ||
| 114 | |||
| 115 | =over | ||
| 116 | |||
| 117 | =item default_parameters() | ||
| 118 | |||
| 119 | Sets all of the default values for the parameters. Normally called by | ||
| 120 | new() or one of its descendents. | ||
| 121 | |||
| 122 | =back | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub default_parameters { | ||
| 127 | my $self = shift; | ||
| 128 | |||
| 129 | ${$self->{"params"}}{"verbose"} = 0; | ||
| 130 | } | ||
| 131 | |||
| 132 | =over | ||
| 133 | |||
| 134 | =item handle_parameters($self, @_) | ||
| 135 | |||
| 136 | Handle the parameter string that IDS::Algorithm::new() will accept. | ||
| 137 | Extracted from new() for subclass usage. Returns a filehandle if we | ||
| 138 | were called with a filehandle from which to load. | ||
| 139 | |||
| 140 | =back | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub handle_parameters { | ||
| 145 | my $self = shift; | ||
| 146 | my $loadfh; | ||
| 147 | |||
| 148 | # This first case is for historical compatibility where the | ||
| 149 | # parameters were a hash reference passed as the first argument. | ||
| 150 | # This usage is now deprecated, and will hopefully eventually be | ||
| 151 | # removed. | ||
| 152 | my @params = @_; | ||
| 153 | if ($params[0] =~ /^HASH/ && $#params == 1) { | ||
| 154 | my ($params, $string) = @params; | ||
| 155 | $self->parameters(%{$params}); | ||
| 156 | return $string; | ||
| 157 | } elsif (defined($params[0])) { | ||
| 158 | # If we have a non-zero even number of arguments, we have | ||
| 159 | # nothing but parameters | ||
| 160 | if (scalar(@params) % 2 == 0) { | ||
| 161 | undef $loadfh; | ||
| 162 | } else { | ||
| 163 | $loadfh = shift; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | $self->parameters(@params); | ||
| 167 | return $loadfh; | ||
| 168 | } | ||
| 169 | |||
| 170 | =over | ||
| 171 | |||
| 172 | =item cleanup | ||
| 173 | |||
| 174 | Clean data to prepare for tokenizing. | ||
| 175 | |||
| 176 | =back | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | sub cleanup { | ||
| 181 | my $self = shift; | ||
| 182 | $self->{"data"} =~ s/\r//g; | ||
| 183 | $self->{"data"} =~ s/\s+$//s; # any spaces at the end (is this valid?) | ||
| 184 | $self->{"data"} =~ s/\n\s+/ /g; # fix continuation lines | ||
| 185 | |||
| 186 | # some %-escaped chars back to normal | ||
| 187 | $self->{"data"} =~ s/%7E/~/g; | ||
| 188 | #$self->{"data"} =~ s/%20/ /g; | ||
| 189 | return $self; | ||
| 190 | } | ||
| 191 | |||
| 192 | =over | ||
| 193 | |||
| 194 | =item data() | ||
| 195 | |||
| 196 | Return the data used for the tokens we have. If called in array mode, | ||
| 197 | we return the inidividual lines, otherwise the join of those line. | ||
| 198 | |||
| 199 | =back | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | sub data { | ||
| 204 | my $self = shift; | ||
| 205 | return wantarray ? @{$self->{"lines"}} : $self->{"data"}; | ||
| 206 | } | ||
| 207 | |||
| 208 | =over | ||
| 209 | |||
| 210 | =item postdata() | ||
| 211 | |||
| 212 | Return the data part of a POST request. Only has meaning in a POST | ||
| 213 | request. | ||
| 214 | |||
| 215 | =back | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | sub postdata { | ||
| 220 | my $self = shift; | ||
| 221 | |||
| 222 | return $self->{"postdata"}; | ||
| 223 | } | ||
| 224 | |||
| 225 | =over | ||
| 226 | |||
| 227 | =item source() | ||
| 228 | =item source(value) | ||
| 229 | |||
| 230 | Set and/or get the data source. | ||
| 231 | |||
| 232 | =back | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | sub source { | ||
| 237 | my $self = shift; | ||
| 238 | if (defined($_[0])) { | ||
| 239 | my $old = ${$self->{"params"}}{"source"}; | ||
| 240 | ${$self->{"params"}}{"source"} = $_[0]; | ||
| 241 | return $old; | ||
| 242 | } else { | ||
| 243 | return ${$self->{"params"}}{"source"}; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | =over | ||
| 248 | |||
| 249 | =item tokens() | ||
| 250 | |||
| 251 | Return the tokens that result from parsing the structure. The tokens | ||
| 252 | can be returned as an array or a reference to the internal array holding | ||
| 253 | them (for efficiency). Modify this referenced array at your own risk. | ||
| 254 | |||
| 255 | Note that this function can remove values. | ||
| 256 | Note that this function can convert everything to lower case. | ||
| 257 | Both of these options are controlled by parameters, and neither affects | ||
| 258 | the internal version of the tokens. | ||
| 259 | |||
| 260 | =back | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | sub tokens { | ||
| 265 | my $self = shift; | ||
| 266 | my @result; | ||
| 267 | |||
| 268 | unless (defined($self->{"tokens"})) { | ||
| 269 | cluck *tokens{PACKAGE}, "::tokens: no tokens for myself"; | ||
| 270 | $self->parse; | ||
| 271 | } | ||
| 272 | |||
| 273 | defined($self->{"tokens"}) or | ||
| 274 | return []; | ||
| 275 | # confess *tokens{PACKAGE} . "::tokens parsing produced no tokens!"; | ||
| 276 | |||
| 277 | # Easy cases are handled efficiently | ||
| 278 | return $self->{"tokens"} if | ||
| 279 | ! wantarray && ${$self->{"params"}}{"with_values"} && | ||
| 280 | ! ${$self->{"params"}}{"lc_only"}; | ||
| 281 | |||
| 282 | return @{$self->{"tokens"}} if | ||
| 283 | wantarray && ${$self->{"params"}}{"with_values"} && | ||
| 284 | ! ${$self->{"params"}}{"lc_only"}; | ||
| 285 | |||
| 286 | # we have handled the simple cases. We're committed to making at | ||
| 287 | # least one change to what we have stored. | ||
| 288 | my @tokens = @{$self->{"tokens"}}; | ||
| 289 | |||
| 290 | map { | ||
| 291 | s/:.*$// unless ${$self->{"params"}}{"with_values"}; | ||
| 292 | lc if ${$self->{"params"}}{"lc_only"}; | ||
| 293 | } @tokens; | ||
| 294 | return wantarray ? @tokens : \@tokens; | ||
| 295 | } | ||
| 296 | |||
| 297 | =over | ||
| 298 | |||
| 299 | =item expand_pct(data) | ||
| 300 | |||
| 301 | Expand the %-substitutions. This function currently does not handle | ||
| 302 | unicode and &-expansions. Should it? | ||
| 303 | |||
| 304 | =back | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | sub expand_pct { | ||
| 309 | my $self = shift; | ||
| 310 | my $data = shift; | ||
| 311 | |||
| 312 | while ($data =~ /%([0-9A-Fa-f]{2})/) { | ||
| 313 | $data =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/e; | ||
| 314 | } | ||
| 315 | return $data; | ||
| 316 | } | ||
| 317 | |||
| 318 | =head2 Functions for subclasses | ||
| 319 | |||
| 320 | The followng functions are required in subclasses. | ||
| 321 | |||
| 322 | =over | ||
| 323 | |||
| 324 | =item parse() | ||
| 325 | |||
| 326 | Parse the structure and return the resulting tokens. | ||
| 327 | |||
| 328 | =back | ||
| 329 | |||
| 330 | =cut | ||
| 331 | |||
| 332 | sub parse { | ||
| 333 | my $self = shift; | ||
| 334 | confess *parse{PACKAGE} . "::parse missing in subclass " . ref($self); | ||
| 335 | } | ||
| 336 | |||
| 337 | =over | ||
| 338 | |||
| 339 | =item empty() | ||
| 340 | |||
| 341 | Delete all data in preparation for loading new data. | ||
| 342 | |||
| 343 | =back | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | sub empty { | ||
| 348 | my $self = shift; | ||
| 349 | ref($self) eq *empty{PACKAGE} or | ||
| 350 | confess *empty{PACKAGE} . "::empty missing in subclass " . ref($self); | ||
| 351 | undef $self->{"data"}, $self->{"postdata"}, $self->{"tokens"}; | ||
| 352 | } | ||
| 353 | |||
| 354 | =head1 AUTHOR INFORMATION | ||
| 355 | |||
| 356 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 357 | |||
| 358 | This library is free software; you can redistribute it and/or modify | ||
| 359 | it under the same terms as Perl itself. | ||
| 360 | |||
| 361 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 362 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 363 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 364 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 365 | speed of the reponse depends on how the research is proceeding. | ||
| 366 | |||
| 367 | =head1 BUGS | ||
| 368 | |||
| 369 | Please report them. | ||
| 370 | |||
| 371 | =head1 SEE ALSO | ||
| 372 | |||
| 373 | L<IDS::Algorithm>, L<IDS::DataSource> | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | 1; |