Apache-FakeCookie
view release on metacpan or search on metacpan
FakeCookie.pm view on Meta::CPAN
my($time) = @_;
my(%mult) = ('s'=>1,
'm'=>60,
'h'=>60*60,
'd'=>60*60*24,
'M'=>60*60*24*30,
'y'=>60*60*24*365);
# format for time can be in any of the forms...
# "now" -- expire immediately
# "+180s" -- in 180 seconds
# "+2m" -- in 2 minutes
# "+12h" -- in 12 hours
# "+1d" -- in 1 day
# "+3M" -- in 3 months
# "+2y" -- in 2 years
# "-3m" -- 3 minutes ago(!)
# If you don't supply one of these forms, we assume you are
# specifying the date yourself
my($offset);
if (!$time || (lc($time) eq 'now')) {
$offset = 0;
} elsif ($time=~/^\d+/) {
return $time;
} elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/) {
$offset = ($mult{$2} || 1)*$1;
} else {
return $time;
}
return (time+$offset);
}
sub remove {
my ($self,$name) = @_;
if ($name) {
delete $Cookies->{$name} if exists $Cookies->{$name};
} else {
delete $Cookies->{$self->{-name}}
if exists $Cookies->{$self->{-name}};
}
}
sub as_string {
my $self = shift;
return '' unless $self->name;
my %cook = %$self;
my $cook = ($cook{-name}) ? escape($cook{-name}) . '=' : '';
if ($cook{-value}) {
my $i = '';
foreach(@{$cook{-value}}) {
$cook .= $i . escape($_);
$i = '&';
}
}
foreach(qw(domain path)) {
$cook .= "; $_=" . $cook{"-$_"} if $cook{"-$_"};
}
$cook .= "; expires=$_" if ($_ = expires(\%cook));
$cook .= ($cook{-secure}) ? '; secure' : '';
}
### helpers
sub do_this {
(caller(1))[3] =~ /[^:]+$/;
splice(@_,1,0,'-'.$&);
goto &cookie_item;
}
# get or set a named item in cookie hash
sub cookie_item {
my($self,$item,$val) = @_;
if ( defined $val ) {
#
# Darn! this modifies a cookie item if user is generating
# a replacement cookie and has not yet "baked" it...
# Don't see how this can hurt in the real world... MAR 9-2-02
if ( $item eq '-name' &&
exists $Cookies->{$self->{-name}} ) {
$Cookies->{$val} = $Cookies->{$self->{-name}};
delete $Cookies->{$self->{-name}};
}
$self->{$item} = $val;
}
return (exists $self->{$item}) ? $self->{$item} : '';
}
sub escape {
my ($x) = @_;
return undef unless defined($x);
$x =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
return $x;
}
# unescape URL-data, but leave +'s alone
sub unescape {
my ($x) = @_;
return undef unless defined($x);
$x =~ tr/+/ /; # pluses become spaces
$x =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
return $x;
}
1
__END__
=head1 NAME
Apache::FakeCookie - fake request object for debugging
=head1 SYNOPSIS
use Apache::FakeCookie;
loads into Apache::Cookie namespace
=head1 DESCRIPTION
This module assists authors of Apache::* modules write test suites that
would use B<Apache::Cookie> without actually having to run and query
a server to test the cookie methods. Loaded in the test script after the
author's target module is loaded, B<Apache::FakeCookie>
Usage is the same as B<Apache::Cookie>
=head1 METHODS
Implements all methods of Apache::Cookie
( run in 1.828 second using v1.01-cache-2.11-cpan-7fcb06a456a )