| 1 | ##################################################################### | ||||||||||
| 2 | # | ||||||||||
| 3 | # Copyright (c) 2001, Raphael Manfredi | ||||||||||
| 4 | # | ||||||||||
| 5 | # You may redistribute only under the terms of the Artistic License, | ||||||||||
| 6 | # as specified in the README file that comes with the distribution. | ||||||||||
| 7 | # | ||||||||||
| 8 | |||||||||||
| 9 | # | ||||||||||
| 10 | # Abstract representation of the POST input data, which is a list of incoming | ||||||||||
| 11 | # parameters that can be encoded differently. | ||||||||||
| 12 | # | ||||||||||
| 13 | |||||||||||
| 14 | package CGI::Test::Input; | ||||||||||
| 15 | |||||||||||
| 16 | 13 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 13 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 13 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 13 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | ############################################################ | ||||||||||
| 23 | # | ||||||||||
| 24 | # ->new | ||||||||||
| 25 | # | ||||||||||
| 26 | # Creation routine | ||||||||||
| 27 | # | ||||||||||
| 28 | ############################################################ | ||||||||||
| 29 | sub new | ||||||||||
| 30 | 0 | ▶ | { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 31 | 0 | confess "deferred"; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | ############################################################ | ||||||||||
| 35 | # | ||||||||||
| 36 | # ->_init | ||||||||||
| 37 | # | ||||||||||
| 38 | # Initialization of common attributes | ||||||||||
| 39 | # | ||||||||||
| 40 | ############################################################ | ||||||||||
| 41 | sub _init | ||||||||||
| 42 | 19 | ▶ | { | ||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 19 | my $this = shift; | |||||||||
| 44 | 19 | $this->{stale} = 0; | |||||||||
| 45 | 19 | $this->{fields} = []; # list of [name, value] | |||||||||
| 46 | 19 | $this->{files} = []; # list of [name, value, content or undef] | |||||||||
| 47 | 19 | $this->{length} = 0; | |||||||||
| 48 | 19 | $this->{data} = ''; | |||||||||
| 49 | 19 | return; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | # | ||||||||||
| 53 | # Attribute access | ||||||||||
| 54 | # | ||||||||||
| 55 | |||||||||||
| 56 | ############################################################ | ||||||||||
| 57 | sub _stale | ||||||||||
| 58 | 20 | ▶ | { | ||||||||
|
Subroutine
_stale: called CC=1 SCAR=0.0
| |||||||||||
| 59 | 20 | my $this = shift; | |||||||||
| 60 | 20 | $this->{stale}; | |||||||||
| 61 | } | ||||||||||
| 62 | ############################################################ | ||||||||||
| 63 | sub _fields | ||||||||||
| 64 | 281 | ▶ | { | ||||||||
|
Subroutine
_fields: called CC=1 SCAR=0.0
| |||||||||||
| 65 | 281 | my $this = shift; | |||||||||
| 66 | 281 | $this->{fields}; | |||||||||
| 67 | } | ||||||||||
| 68 | ############################################################ | ||||||||||
| 69 | sub _files | ||||||||||
| 70 | 38 | ▶ | { | ||||||||
|
Subroutine
_files: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 38 | my $this = shift; | |||||||||
| 72 | 38 | $this->{files}; | |||||||||
| 73 | } | ||||||||||
| 74 | ############################################################ | ||||||||||
| 75 | sub length | ||||||||||
| 76 | 2 | ▶ | { | ||||||||
|
Subroutine
length: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 77 | 2 | my $this = shift; | |||||||||
| 78 | 2 | ▶ | $this->_refresh() if $this->_stale(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 2 | $this->{length}; | |||||||||
| 80 | } | ||||||||||
| 81 | ############################################################ | ||||||||||
| 82 | sub data | ||||||||||
| 83 | 18 | ▶ | { | ||||||||
|
Subroutine
data: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 84 | 18 | my $this = shift; | |||||||||
| 85 | 18 | ▶ | $this->_refresh() if $this->_stale(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 18 | $this->{data}; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | ############################################################ | ||||||||||
| 90 | # | ||||||||||
| 91 | # ->set_raw_data | ||||||||||
| 92 | # | ||||||||||
| 93 | # Set raw POST data for this input object | ||||||||||
| 94 | # | ||||||||||
| 95 | ############################################################ | ||||||||||
| 96 | 0 | ▶ | sub set_raw_data { | ||||||||
|
Subroutine
set_raw_data: not called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||
| 97 | 0 | my ($this, $data) = @_; | |||||||||
| 98 | |||||||||||
| 99 | 0 | $this->{data} = $data; | |||||||||
| 100 | 0 | ▶ | $this->{length} = do { use bytes; CORE::length $data }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||||||||||
| 101 | 0 | $this->{stale} = 0; | |||||||||
| 102 | |||||||||||
| 103 | 0 | return $this; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | ############################################################ | ||||||||||
| 107 | # | ||||||||||
| 108 | # ->add_widget | ||||||||||
| 109 | # | ||||||||||
| 110 | # Add new input widget. | ||||||||||
| 111 | # | ||||||||||
| 112 | # This routine is called to build input data for POST requests issued in | ||||||||||
| 113 | # response to a submit button being pressed. | ||||||||||
| 114 | # | ||||||||||
| 115 | ############################################################ | ||||||||||
| 116 | sub add_widget | ||||||||||
| 117 | 281 | ▶ | { | ||||||||
|
Subroutine
add_widget: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 118 | 281 | my $this = shift; | |||||||||
| 119 | 281 | my ($w) = @_; | |||||||||
| 120 | |||||||||||
| 121 | # | ||||||||||
| 122 | # Appart from the fact that file widgets get inserted in a dedicated list, | ||||||||||
| 123 | # the processing here is the same. The 3rd value of the entry for files | ||||||||||
| 124 | # will be undefined, meaning the file will be read at a later time, when | ||||||||||
| 125 | # the input data is built. | ||||||||||
| 126 | # | ||||||||||
| 127 | |||||||||||
| 128 | 281 | my @tuples = $w->submit_tuples; | |||||||||
| 129 | 281 | ▶ | my $array = $w->is_file ? $this->_files : $this->_fields; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | |||||||||||
| 131 | 281 | ▶ | while (my ($name, $value) = splice @tuples, 0, 2) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | { | ||||||||||
| 133 | 291 | ▶ | $value = '' unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 291 | push @$array, [ $name, $value ]; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 281 | $this->{stale} = 1; | |||||||||
| 138 | |||||||||||
| 139 | 281 | return; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | ############################################################ | ||||||||||
| 143 | # | ||||||||||
| 144 | # ->add_field | ||||||||||
| 145 | # | ||||||||||
| 146 | # Add a new name/value pair to the input data. | ||||||||||
| 147 | # | ||||||||||
| 148 | # This routine is meant for manual input data building. | ||||||||||
| 149 | # | ||||||||||
| 150 | ############################################################ | ||||||||||
| 151 | sub add_field | ||||||||||
| 152 | 0 | ▶ | { | ||||||||
|
Subroutine
add_field: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 153 | 0 | my $this = shift; | |||||||||
| 154 | 0 | my ($name, $value) = @_; | |||||||||
| 155 | |||||||||||
| 156 | 0 | ▶ | $value = '' unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | push @{$this->_fields}, [ $name, $value ]; | |||||||||
| 158 | 0 | $this->{stale} = 1; | |||||||||
| 159 | |||||||||||
| 160 | 0 | return; | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | ############################################################ | ||||||||||
| 164 | # | ||||||||||
| 165 | # ->add_file | ||||||||||
| 166 | # | ||||||||||
| 167 | # Add a new upload-file information to the input data. | ||||||||||
| 168 | # The actual reading of the file is deferred up to the moment where we | ||||||||||
| 169 | # need to build the input data. | ||||||||||
| 170 | # | ||||||||||
| 171 | # This routine is meant for manual input data building. | ||||||||||
| 172 | # | ||||||||||
| 173 | ############################################################ | ||||||||||
| 174 | sub add_file | ||||||||||
| 175 | 0 | ▶ | { | ||||||||
|
Subroutine
add_file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 176 | 0 | my $this = shift; | |||||||||
| 177 | 0 | my ($name, $value) = @_; | |||||||||
| 178 | |||||||||||
| 179 | 0 | ▶ | $value = '' unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | 0 | push @{$this->_files}, [ $name, $value ]; | |||||||||
| 181 | 0 | $this->{stale} = 1; | |||||||||
| 182 | |||||||||||
| 183 | 0 | return; | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | ############################################################ | ||||||||||
| 187 | # | ||||||||||
| 188 | # ->add_file_now | ||||||||||
| 189 | # | ||||||||||
| 190 | # Add a new upload-file information to the input data. | ||||||||||
| 191 | # The file is read immediately, and can be disposed of once we return. | ||||||||||
| 192 | # | ||||||||||
| 193 | # This routine is meant for manual input data building. | ||||||||||
| 194 | # | ||||||||||
| 195 | ############################################################ | ||||||||||
| 196 | sub add_file_now | ||||||||||
| 197 | 0 | ▶ | { | ||||||||
|
Subroutine
add_file_now: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 198 | 0 | my $this = shift; | |||||||||
| 199 | 0 | my ($name, $value) = @_; | |||||||||
| 200 | |||||||||||
| 201 | 0 | ▶ | croak "unreadable file '$value'" unless -r $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | |||||||||||
| 203 | 0 | local *FILE; | |||||||||
| 204 | 0 | open(FILE, $value); | |||||||||
| 205 | 0 | binmode FILE; | |||||||||
| 206 | |||||||||||
| 207 | 0 | local $_; | |||||||||
| 208 | 0 | my $content = ''; | |||||||||
| 209 | |||||||||||
| 210 | 0 | ▶ | while (<FILE>) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 211 | { | ||||||||||
| 212 | 0 | $content .= $_; | |||||||||
| 213 | } | ||||||||||
| 214 | 0 | close FILE; | |||||||||
| 215 | |||||||||||
| 216 | 0 | push @{$this->_files}, [ $name, $value, $content ]; | |||||||||
| 217 | 0 | $this->{stale} = 1; | |||||||||
| 218 | |||||||||||
| 219 | 0 | return; | |||||||||
| 220 | } | ||||||||||
| 221 | |||||||||||
| 222 | 0 | ▶ | sub set_mime_type { | ||||||||
|
Subroutine
set_mime_type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 223 | 0 | my ($this, $type) = @_; | |||||||||
| 224 | |||||||||||
| 225 | 0 | $this->{mime_type} = $type; | |||||||||
| 226 | |||||||||||
| 227 | 0 | return $this; | |||||||||
| 228 | } | ||||||||||
| 229 | |||||||||||
| 230 | # | ||||||||||
| 231 | # Interface to be implemented by heirs | ||||||||||
| 232 | # | ||||||||||
| 233 | |||||||||||
| 234 | ############################################################ | ||||||||||
| 235 | sub mime_type | ||||||||||
| 236 | 17 | ▶ | { | ||||||||
|
Subroutine
mime_type: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 237 | 17 | my ($this) = @_; | |||||||||
| 238 | |||||||||||
| 239 | 17 | my $type = $this->{mime_type}; | |||||||||
| 240 | |||||||||||
| 241 | 17 | ▶ | confess "deferred" unless $type; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 242 | |||||||||||
| 243 | 17 | return $type; | |||||||||
| 244 | } | ||||||||||
| 245 | |||||||||||
| 246 | ############################################################ | ||||||||||
| 247 | sub _build_data | ||||||||||
| 248 | 0 | ▶ | { | ||||||||
|
Subroutine
_build_data: not called CC=1 SCAR=6.9
| |||||||||||
| 249 | 0 | confess "deferred"; | |||||||||
| 250 | } | ||||||||||
| 251 | |||||||||||
| 252 | # | ||||||||||
| 253 | # Internal routines | ||||||||||
| 254 | # | ||||||||||
| 255 | |||||||||||
| 256 | ############################################################ | ||||||||||
| 257 | # | ||||||||||
| 258 | # ->_refresh | ||||||||||
| 259 | # | ||||||||||
| 260 | # Recomputes `data' and `length' attributes when stale | ||||||||||
| 261 | # | ||||||||||
| 262 | ############################################################ | ||||||||||
| 263 | sub _refresh | ||||||||||
| 264 | 19 | ▶ | { | ||||||||
|
Subroutine
_refresh: called CC=1 SCAR=0.0
| |||||||||||
| 265 | 19 | my $this = shift; | |||||||||
| 266 | |||||||||||
| 267 | # internal pre-condition | ||||||||||
| 268 | |||||||||||
| 269 | 19 | my $data = $this->_build_data; # deferred | |||||||||
| 270 | |||||||||||
| 271 | 19 | $this->{data} = $data; | |||||||||
| 272 | 19 | $this->{length} = CORE::length $data; | |||||||||
| 273 | 19 | $this->{stale} = 0; | |||||||||
| 274 | |||||||||||
| 275 | 19 | return; | |||||||||
| 276 | } | ||||||||||
| 277 | |||||||||||
| 278 | 13 | 1; | |||||||||
| 279 | |||||||||||
| 280 | =head1 NAME | ||||||||||
| 281 | |||||||||||
| 282 | CGI::Test::Input - Abstract representation of POST input | ||||||||||
| 283 | |||||||||||
| 284 | =head1 SYNOPSIS | ||||||||||
| 285 | |||||||||||
| 286 | # Deferred class, only heirs can be created | ||||||||||
| 287 | # $input holds a CGI::Test::Input object | ||||||||||
| 288 | |||||||||||
| 289 | $input->add_widget($w); # done internally for you | ||||||||||
| 290 | |||||||||||
| 291 | $input->add_field("name", "value"); # manual input construction | ||||||||||
| 292 | $input->add_file("name", "path"); # deferred reading | ||||||||||
| 293 | $input->add_file_now("name", "/tmp/path"); # read file immediately | ||||||||||
| 294 | |||||||||||
| 295 | syswrite INPUT, $input->data, $input->length; # if you really have to | ||||||||||
| 296 | |||||||||||
| 297 | # $test is a CGI::Test object | ||||||||||
| 298 | $test->POST("http://server:70/cgi-bin/script", $input); | ||||||||||
| 299 | |||||||||||
| 300 | =head1 DESCRIPTION | ||||||||||
| 301 | |||||||||||
| 302 | The C<CGI::Test::Input> class is deferred. It is an abstract representation | ||||||||||
| 303 | of HTTP POST request input, as expected by the C<POST> routine of C<CGI::Test>. | ||||||||||
| 304 | |||||||||||
| 305 | Unless you wish to issue a C<POST> request manually to provide carefully | ||||||||||
| 306 | crafted input, you do not need to learn the interface of this hierarchy, | ||||||||||
| 307 | nor even bother knowing about it. | ||||||||||
| 308 | |||||||||||
| 309 | Otherwise, you need to decide which MIME encoding you want, and create an | ||||||||||
| 310 | object of the appropriate type. Note that file uploading requires the use | ||||||||||
| 311 | of the C<multipart/form-data> encoding: | ||||||||||
| 312 | |||||||||||
| 313 | MIME Encoding Type to Create | ||||||||||
| 314 | --------------------------------- --------------------------- | ||||||||||
| 315 | application/x-www-form-urlencoded CGI::Test::Input::URL | ||||||||||
| 316 | multipart/form-data CGI::Test::Input::Multipart | ||||||||||
| 317 | |||||||||||
| 318 | Once the object is created, you will be able to add name/value tuples | ||||||||||
| 319 | corresponding to the CGI parameters to submit. | ||||||||||
| 320 | |||||||||||
| 321 | For instance: | ||||||||||
| 322 | |||||||||||
| 323 | my $input = CGI::Test::Input::Multipart->new(); | ||||||||||
| 324 | $input->add_field("login", "ram"); | ||||||||||
| 325 | $input->add_field("password", "foobar"); | ||||||||||
| 326 | $input->add_file("organization", "/etc/news/organization"); | ||||||||||
| 327 | |||||||||||
| 328 | Then, to inspect what is normally sent to the HTTP server: | ||||||||||
| 329 | |||||||||||
| 330 | print "Content-Type: ", $input->mime_type, "\015\012"; | ||||||||||
| 331 | print "Content-Length: ", $input->length, "\015\012"; | ||||||||||
| 332 | print "\015\012"; | ||||||||||
| 333 | print $input->data; | ||||||||||
| 334 | |||||||||||
| 335 | But usually you'll hand out the $input object to the C<POST> routine | ||||||||||
| 336 | of C<CGI::Test>. | ||||||||||
| 337 | |||||||||||
| 338 | =head1 INTERFACE | ||||||||||
| 339 | |||||||||||
| 340 | =head2 Creation Routine | ||||||||||
| 341 | |||||||||||
| 342 | It is called C<new> as usual. All subclasses have | ||||||||||
| 343 | the same creation routine signature, which takes no parameter. | ||||||||||
| 344 | |||||||||||
| 345 | =head2 Adding Parameters | ||||||||||
| 346 | |||||||||||
| 347 | CGI parameter are name/value tuples. In case of file uploads, they can have | ||||||||||
| 348 | a content as well, the value being the file path on the client machine. | ||||||||||
| 349 | |||||||||||
| 350 | =over 4 | ||||||||||
| 351 | |||||||||||
| 352 | =item C<add_field> I<name>, I<value> | ||||||||||
| 353 | |||||||||||
| 354 | Adds the CGI parameter I<name>, whose value is I<value>. | ||||||||||
| 355 | |||||||||||
| 356 | =item add_file I<name>, I<path> | ||||||||||
| 357 | |||||||||||
| 358 | Adds the file upload parameter I<name>, located at I<path>. | ||||||||||
| 359 | |||||||||||
| 360 | The file is not read immediately, so it must remain available until | ||||||||||
| 361 | the I<data> routine is called, at least. It is not an error if the file | ||||||||||
| 362 | cannot be read at that time. | ||||||||||
| 363 | |||||||||||
| 364 | When not using the C<multipart/form-data> encoding, only the name/path | ||||||||||
| 365 | tuple will be transmitted to the script. | ||||||||||
| 366 | |||||||||||
| 367 | =item add_file_now I<name>, I<path> | ||||||||||
| 368 | |||||||||||
| 369 | Same as C<add_file>, but the file is immediately read and can therefore | ||||||||||
| 370 | be disposed of afterwards. However, the file B<must> exist. | ||||||||||
| 371 | |||||||||||
| 372 | =item add_widget I<widget> | ||||||||||
| 373 | |||||||||||
| 374 | Add any widget, i.e. a C<CGI::Test::Form::Widget> object. This routine | ||||||||||
| 375 | is called internally by C<CGI::Test> to construct the input data when | ||||||||||
| 376 | submiting a form via POST. | ||||||||||
| 377 | |||||||||||
| 378 | =back | ||||||||||
| 379 | |||||||||||
| 380 | =head2 Generation | ||||||||||
| 381 | |||||||||||
| 382 | =over 4 | ||||||||||
| 383 | |||||||||||
| 384 | =item C<data> | ||||||||||
| 385 | |||||||||||
| 386 | Returns the data, under the proper encoding. | ||||||||||
| 387 | |||||||||||
| 388 | =item C<mime_type> | ||||||||||
| 389 | |||||||||||
| 390 | Returns the proper MIME encoding type, suitable for inclusion within | ||||||||||
| 391 | a Content-Type header. | ||||||||||
| 392 | |||||||||||
| 393 | =item C<length> | ||||||||||
| 394 | |||||||||||
| 395 | Returns the data length. | ||||||||||
| 396 | |||||||||||
| 397 | =back | ||||||||||
| 398 | |||||||||||
| 399 | =head1 AUTHORS | ||||||||||
| 400 | |||||||||||
| 401 | The original author is Raphael Manfredi. | ||||||||||
| 402 | |||||||||||
| 403 | Steven Hilton was long time maintainer of this module. | ||||||||||
| 404 | |||||||||||
| 405 | Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>. | ||||||||||
| 406 | |||||||||||
| 407 | =head1 SEE ALSO | ||||||||||
| 408 | |||||||||||
| 409 | CGI::Test(3), CGI::Test::Input::URL(3), CGI::Test::Input::Multipart(3). | ||||||||||
| 410 | |||||||||||
| 411 | =cut | ||||||||||
| 412 | |||||||||||