package RDA::Object::Cookie; # $Id: Cookie.pm,v 1.4 2008/02/25 14:55:26 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Object/Cookie.pm,v 1.4 2008/02/25 14:55:26 mschenke Exp $ # # Change History # 20080225 MSC Change the copyright notice. =head1 NAME RDA::Object::Cookie - Class Used for Objects to Manage HTTP Cookies. =head1 SYNOPSIS require RDA::Object::Cookie; =head1 DESCRIPTION The objects of the C class are used to manage HTTP cookies. The following methods are available: =cut use strict; BEGIN { use Exporter; use vars qw($SEPARATOR $VERSION @EXPORT @ISA); @ISA = qw(Exporter); @EXPORT = qw($SEPARATOR); } # Define the global public variables $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/); $SEPARATOR = "; "; # Define the global private constants my $DOMAIN = "domain"; my $EXPIRES = "expires"; my $MAX_AGE = "max-age"; my $NS_COOKIE = "NS-cookie"; my $PATH = "path"; my $PORT = "port"; my $SECURE = "secure"; my $VERSION = "version"; # Define the global private variables my %tb_key = ( $DOMAIN => '-dom', $EXPIRES => '-exp', $MAX_AGE => '-exp', $NS_COOKIE => '-nsc', $PATH => '-pth', $PORT => '-prt', $SECURE => '-sec', $VERSION => '-ver', ); # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Object::Cookie-Enew([name =E $value,...])> The object constructor. It enables you to specify initial attributes at object creation time. You can specify attribute names in upper case to indicate attributes that are generated by default. C is represented by a blessed hash reference. The following special keys are used: =over 16 =item B< '-dom' > Cookie domain =item B< '-exp' > Expire date =item B< '-nam' > Cookie name =item B< '-nsc' > Is Netscape cookie? =item B< '-obs' > Obsolete cookie indicator =item B< '-prt' > Cookie port list reference =item B< '-pth' > Cookie path =item B< '-sec' > Secure cookie indicator =item B< '-ver' > Cookie version =item B< '_dom' > Is domain specified? =item B< '_prt' > Is port specified? =item B< '_pth' > Is path specified? =back =cut sub new { my $cls = shift; my ($key, $slf, $val); # Create the object $slf = bless { -dom => 'localhost.local', -nsc => 1, -obs => 0, -prt => undef, -pth => '/', -ver => 0, _dom => 0, _prt => 0, _pth => 0, }, ref($cls) || $cls; # Add the optional attributes while (($key, $val) = splice(@_, 0, 2)) { next unless $key && defined($val); $slf->{lc($key)} = $val; $slf->{lc("_$key")} = 1 if $key =~ m/^\-?([A-Z]+)$/; } # Return the object reference $slf; } =head2 S<$h-Eas_cookie> This method converts the cookie in its external form. =cut sub as_cookie { my ($slf) = @_; my ($buf); $buf = $slf->{'-nam'}.'='.$slf->{'-val'}; if ($slf->{'-ver'} > 0) { $buf .= $SEPARATOR.'$Version='.$slf->{'-ver'}; $buf .= $SEPARATOR.'$Path='.$slf->{'-pth'} if $slf->{'_pth'}; $buf .= $SEPARATOR.'$Domain='.$slf->{'-dom'} if $slf->{'_dom'}; $buf .= $SEPARATOR.'$Port="'.join(',', @{$slf->{'-prt'}}).'"' if $slf->{'_prt'} && ref($slf->{'-prt'}); } $buf; } =head2 S<$h-Eas_string> This method returns the object as a string. =cut sub as_string { my ($slf) = @_; my ($buf); $buf = "Set-Cookie3: ".$slf->{'-nam'}.'='.$slf->{'-val'} .$SEPARATOR.$PATH.'='.$slf->{'-pth'} .$SEPARATOR.$DOMAIN.'='.$slf->{'-dom'}; $buf .= $SEPARATOR.$PORT.'="'.join(',', @{$slf->{'-prt'}}).'"' if ref($slf->{'-prt'}); $buf .= $SEPARATOR.$SECURE if $slf->{'-sec'}; $buf .= $SEPARATOR.'discard' if $slf->{'-obs'}; $buf.$SEPARATOR.$VERSION.'='.$slf->{'-ver'}; } =head2 S<$h-Edecode_cookie($str)> This method decodes a C specification. =cut sub decode_cookie { my ($str) = @_; my ($key, $off, $tbl, $val, @tok); # Abort if the header value cannot be retrieved return undef unless $str; # Decode the cookie $tbl = {}; ($val, @tok) = split(/\s*;\s*/, $str); # Extract the cookie name and value if (($off = index($val, '=')) > 0) { $tbl->{'-nam'} = trim(substr($val, 0, $off)); $tbl->{'-val'} = trim(substr($val, $off + 1)); } # Extract the other information foreach $val (@tok) { if (($off = index($val, '=')) < 0) { $key = $val; $val = ''; } else { $key = substr($val, 0, $off); $val = trim(substr($val, $off + 1)); } next unless exists($tb_key{$key = lc(trim($key))}); $tbl->{$key} = $val unless exists($tbl->{$key = $tb_key{$key}}); } # Align on cookie2 format $tbl->{'-nsc'} = 0; $tbl->{'-ver'} = 0; # Return the cookie definition exists($tbl->{'-nam'}) ? $tbl : undef; } =head2 S<$h-Edecode_cookie2($str)> This method decodes a C specification. =cut sub decode_cookie2 { my ($str) = @_; my ($key, $off, $tbl, $val, @tok); # Abort if the header value cannot be retrieved return undef unless $str; # Decode the cookie $tbl = {}; ($val, @tok) = split(/\s*;\s*/, $str); # Extract the cookie name and value if (($off = index($val, '=')) > 0) { $tbl->{'-nam'} = trim(substr($val, 0, $off)); $tbl->{'-val'} = trim(substr($val, $off + 1)); } # Extract the other information foreach $val (@tok) { if (($off = index($val, '=')) < 0) { $key = $val; $val = ''; } else { $key = substr($val, 0, $off); $val = trim(substr($val, $off + 1)); } next unless exists($tb_key{$key = lc(trim($key))}); $tbl->{$key} = $val unless exists($tbl->{$key = $tb_key{$key}}); } # Assume a default version $tbl->{'-ver'} = 0 unless exists($tbl->{'-ver'}); # Return the cookie definition exists($tbl->{'-nam'}) ? $tbl : undef; } =head2 S<$h-Eequals($cookie)> This method indicates whether the specified cookie is identical. =cut sub equals { my ($slf, $cok) = @_; $slf->{'-nam'} eq $cok->{'-nam'} && $slf->{'-dom'} eq $cok->{'-dom'} && $slf->{'-pth'} eq $cok->{'-pth'}; } =head2 S<$h-Eget_info($key[,$dft])> This method gets a request attribute value. When the attribute is not defined, it returns the default value. =cut sub get_info { my ($slf, $key, $val) = @_; $val = $slf->{$key} if exists($slf->{$key}); $val; } =head2 S<$h-Eget_length> This method returns the length of the cookie path. =cut sub get_length { length(shift->{'-pth'}); } =head2 S<$h-Eis_expired> This method indicates whether the cookie is expired. =cut sub is_expired { 0; } =head2 S<$h-Eis_netscape> This method indicates whether it is a Netscape cookie. =cut sub is_netscape { shift->{'-nsc'}; } =head2 S<$h-Eis_secure> This method indicates whether the cookie can be used with secure connections only. =cut sub is_secure { shift->{'-sec'}; } =head2 S<$h-Eis_valid_port($prt)> This method indicates whether the port is in the port list. =cut sub is_valid_port { my ($slf, $prt) = @_; # Accept it if there is no list return 1 unless ref($slf->{'-prt'}); # Check if the port is in the list foreach my $itm (@{$slf->{'-prt'}}) { return 1 if $prt == $itm; } 0; } # --- Internal routines ------------------------------------------------------- sub trim { my ($str) = @_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str; } 1; __END__ =head1 SEE ALSO See also L, L, L, L, L. =head1 COPYRIGHT NOTICE Copyright (c) 2002-2008, Oracle. All rights reserved. =head1 TRADEMARK NOTICE Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. =cut