CGI-Capture
view release on metacpan or search on metacpan
lib/CGI/Capture.pm view on Meta::CPAN
package CGI::Capture; # git description: cc2391e
# ABSTRACT: Meticulously thorough capture and replaying of CGI calls
#pod =pod
#pod
#pod =head1 SYNOPSIS
#pod
#pod # Capture the current CGI to a file, and replay it once created
#pod use CGI::Capture 'fileupload.dat';
#pod
#pod # Create an object and capture the state
#pod my $Capture = CGI::Capture->new->capture;
#pod
#pod # Store it in a file and load it back in
#pod $Capture->store('somefile.dat');
#pod my $second = CGI::Capture->apply('somefile.dat');
#pod
#pod # Apply the CGI call to the current environment
#pod $second->apply;
#pod
#pod =head1 DESCRIPTION
#pod
#pod L<CGI> does a terribly bad job of saving CGI calls. C<CGI::Capture> tries
#pod to resolve this and save a CGI call in as much painstaking detail as it
#pod possibly can.
#pod
#pod Because of this, C<CGI::Capture> should work with server logins, cookies,
#pod file uploads, strange execution environments, special environment
#pod variables, the works.
#pod
#pod It does this by capturing a large amount of the perl environment
#pod BEFORE F<CGI.pm> itself gets a chance to look at it, and then restores
#pod it in the same way.
#pod
#pod So in essence, it grabs all of C<STDIN>, C<%ENV>, C<@INC>, and anything
#pod else it can think of. The things it can't replicate, it records anyway
#pod so that later in the debugger it can ensure that the execution
#pod environment is as close as possible to what it captured (and bitch at
#pod you about anything you are doing wrong).
#pod
#pod This is a huge help when resolving problems such as when a bug won't
#pod appear because you aren't debugging the script as the web user and in
#pod the same directory.
#pod
#pod =head2 Using CGI::Capture
#pod
#pod The brain-dead way is to use it as a pragma.
#pod
#pod Add the following to your web application BEFORE you load in CGI itself.
#pod
#pod use CGI::Capture 'cookiebug.dat';
#pod
#pod If the file C<cookiebug.dat> does not exist, CGI::Capture will take a
#pod snapshot of all the bits of the environment that matter to a CGI call, and
#pod freeze it to the file.
#pod
#pod If the file DOES exist however, CGI::Capture will load in the file and
#pod replace the current CGI call with the stored one.
#pod
#pod =head2 Security
#pod
#pod The actual captured CGI files are Storable CGI::Capture objects. If you
#pod want to use CGI::Capture in an environment where you have CODE references
#pod in your @INC path (such as with PAR files), you will need to disable
#pod security for Storable by setting $CGI::Capture::DEPARSE to true, which will
#pod enable B::Deparse and Eval support for stored objects.
#pod
#pod =head2 Hand-Crafting CGI Captures
#pod
#pod In its default usage, B<CGI::Capture> takes an all or nothing approach,
#pod requiring you to capture absolutely every element of a CGI call.
#pod
#pod Sometimes you want to be a little more targeted, and for these situations
#pod an alternative methodology is provided.
#pod
#pod The C<as_yaml> and C<from_yaml> methods allow you to store and retrieve a
#pod CGI capture using L<YAML::Tiny> instead of L<Storable>.
#pod
#pod Once you have stored the CGI capture as a YAML file, you can hand-edit the
#pod capture file, removing any keys you will not want to be restored, keeping
#pod only the useful parts.
#pod
#pod For example, to create a test file upload or CGI request involving
#pod cookies, you could discard everything except for the STDIN section of
#pod the capture file, which will then allow you to reuse the capture on
#pod other hosts, operating systems, and so on.
#pod
#pod =head1 METHODS
#pod
#pod In most cases, the above is all you probably need. However, if you want to
#pod get more fine-grained control, you can create and manipulate CGI::Capture
#pod object directly.
#pod
#pod =cut
use 5.006;
use strict;
use warnings;
use Carp ();
use Config ();
use Storable 2.11 ();
use IO::Scalar 2.110 ();
use YAML::Tiny 1.36 ();
use Params::Util 0.37 qw{ _SCALAR0 _HASH0 _CODE _INSTANCE };
our $VERSION = '1.15';
use CGI::Capture::TieSTDIN ();
our $DEPARSE;
#####################################################################
# Constructor and Accessors
#pod =pod
#pod
#pod =head2 new
#pod
#pod The C<new> only creates a new, empty, capture object.
#pod
#pod Because capturing is destructive to some values (STDIN for example) the
#pod capture method will capture and then immediately reapply the object, so that
#pod the current call can continue.
#pod
#pod Returns a CGI::Capture object. Never dies or returns an error, and so
#pod can be safely method-chained.
#pod
#pod =cut
sub new {
my $class = ref $_[0] ? ref shift : shift;
# Create the empty object
bless {}, $class;
}
# The import expects a file name and does the following.
# 1. If the file does not exist, captures to it and continues.
# 2. If the file exists, restores from it and continues.
# 4. Does nothing if passed nothing.
sub import {
lib/CGI/Capture.pm view on Meta::CPAN
$self->_check( REAL_GROUP_ID => $( );
$self->_check( EFFECTIVE_GROUP_ID => $) );
$self->_check( TAINT => ${^TAINT} );
$self->_check( PERL_VERSION => $] );
$self->_check( CONFIG_PATH => $INC{'Config.pm'} );
$self->_check( PERL_PATH => $Config::Config{perlpath} );
1;
}
# Checks a stored value against its current value
sub _check {
my $self = shift;
my $name = defined $_[0] ? shift : die "Var name not passed to ->_check";
unless ( exists $self->{$name} ) {
# Not defined in the capture, nothing to check
return;
}
my $value = shift;
unless ( defined $self->{$name} or defined $value ) {
return 1;
}
if ( defined $self->{$name} and defined $value ) {
return 1 if $self->{$name} eq $value;
}
# Didn't match
my $current = defined $value ? '"' . quotemeta($value) . '"' : 'undef';
my $cgi = defined $self->{$name} ? '"' . quotemeta($self->{$name}) . '"' : 'undef';
die "Current $name $current does not match the captured CGI call $cgi";
}
# Takes a scalar reference and sets STDIN to read from it
sub _stdin {
my $self = shift;
my $scalar_ref = _SCALAR0($_[0]) ? shift
: die "SCALAR reference not passed to ->_stdin";
tie *MYSTDIN, 'CGI::Capture::TieSTDIN', $scalar_ref;
*STDIN = *MYSTDIN;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
CGI::Capture - Meticulously thorough capture and replaying of CGI calls
=head1 VERSION
version 1.15
=head1 SYNOPSIS
# Capture the current CGI to a file, and replay it once created
use CGI::Capture 'fileupload.dat';
# Create an object and capture the state
my $Capture = CGI::Capture->new->capture;
# Store it in a file and load it back in
$Capture->store('somefile.dat');
my $second = CGI::Capture->apply('somefile.dat');
# Apply the CGI call to the current environment
$second->apply;
=head1 DESCRIPTION
L<CGI> does a terribly bad job of saving CGI calls. C<CGI::Capture> tries
to resolve this and save a CGI call in as much painstaking detail as it
possibly can.
Because of this, C<CGI::Capture> should work with server logins, cookies,
file uploads, strange execution environments, special environment
variables, the works.
It does this by capturing a large amount of the perl environment
BEFORE F<CGI.pm> itself gets a chance to look at it, and then restores
it in the same way.
So in essence, it grabs all of C<STDIN>, C<%ENV>, C<@INC>, and anything
else it can think of. The things it can't replicate, it records anyway
so that later in the debugger it can ensure that the execution
environment is as close as possible to what it captured (and bitch at
you about anything you are doing wrong).
This is a huge help when resolving problems such as when a bug won't
appear because you aren't debugging the script as the web user and in
the same directory.
=head2 Using CGI::Capture
The brain-dead way is to use it as a pragma.
Add the following to your web application BEFORE you load in CGI itself.
use CGI::Capture 'cookiebug.dat';
If the file C<cookiebug.dat> does not exist, CGI::Capture will take a
snapshot of all the bits of the environment that matter to a CGI call, and
freeze it to the file.
If the file DOES exist however, CGI::Capture will load in the file and
replace the current CGI call with the stored one.
=head2 Security
The actual captured CGI files are Storable CGI::Capture objects. If you
want to use CGI::Capture in an environment where you have CODE references
in your @INC path (such as with PAR files), you will need to disable
security for Storable by setting $CGI::Capture::DEPARSE to true, which will
enable B::Deparse and Eval support for stored objects.
=head2 Hand-Crafting CGI Captures
In its default usage, B<CGI::Capture> takes an all or nothing approach,
requiring you to capture absolutely every element of a CGI call.
Sometimes you want to be a little more targeted, and for these situations
an alternative methodology is provided.
The C<as_yaml> and C<from_yaml> methods allow you to store and retrieve a
CGI capture using L<YAML::Tiny> instead of L<Storable>.
Once you have stored the CGI capture as a YAML file, you can hand-edit the
capture file, removing any keys you will not want to be restored, keeping
only the useful parts.
For example, to create a test file upload or CGI request involving
cookies, you could discard everything except for the STDIN section of
the capture file, which will then allow you to reuse the capture on
other hosts, operating systems, and so on.
=head1 METHODS
In most cases, the above is all you probably need. However, if you want to
get more fine-grained control, you can create and manipulate CGI::Capture
object directly.
=head2 new
The C<new> only creates a new, empty, capture object.
Because capturing is destructive to some values (STDIN for example) the
capture method will capture and then immediately reapply the object, so that
the current call can continue.
Returns a CGI::Capture object. Never dies or returns an error, and so
can be safely method-chained.
=head2 store $filename
This method behaves slightly differently in object and static context.
In object context ( $object->store($filename) ) it stores the captured data
to a file via Storable.
In static context ( CGI::Capture->store($filename) ) automatically creates a
new capture object, captures the CGI call, and then stores it, all in one hit.
Returns as for Storable::store or dies if there is a problem storing the file.
Also dies if it finds a CODE reference in @INC and you have not enabled
C<$CGI::Capture::Deparse>.
=head2 retrieve
The C<retrieve> method is used identically to the Storable method of the
same name, and wraps it.
Loads in a stored CGI::Capture object from a file.
If the stored object had a CODE ref in it's @INC, you will also need to
enable $CGI::Capture::DEPARSE when loading the file.
Returns a new CGI::Capture object, or dies on failure.
=head2 as_yaml
To allow for more portable storage and communication of the CGI
environment, the C<as_yaml> method can be used to generate a YAML
document for the request (generated via L<YAML::Tiny>).
Returns a YAML::Tiny object.
=head2 from_yaml
To allow for more portable storage and communication of the CGI
environment, the C<from_yaml> method can be used to restore a
B<CGI::Capture> object from a L<YAML::Tiny> object.
( run in 1.205 second using v1.01-cache-2.11-cpan-b16cb0d3907 )