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