From b8e00fed0c0cfebafa47ea0c379c27546ba1dd8b Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 20 Jul 2007 16:27:07 +0200 Subject: [PATCH] CGI: Added functions for cookies. --- lib/perl/CGI.pm | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/perl/CGI.pm b/lib/perl/CGI.pm index abff1d8a..14a39e24 100644 --- a/lib/perl/CGI.pm +++ b/lib/perl/CGI.pm @@ -1,6 +1,6 @@ # Poor Man's CGI Module for Perl # -# (c) 2002 Martin Mares +# (c) 2002--2007 Martin Mares # Slightly modified by Tomas Valla # # This software may be freely distributed and used according to the terms @@ -126,4 +126,42 @@ sub self_form(@) { return join('', map { "\n" } sort keys %$out); } +### Cookies + +sub cookie_esc($) { + my $x = shift @_; + if ($x !~ /^[a-zA-Z0-9%]+$/) { + $x =~ s/([\\\"])/\\$1/g; + $x = "\"$x\""; + } + return $x; +} + +sub set_cookie($$@) { + my $key = shift @_; + my $value = shift @_; + my %other = @_; + $other{'version'} = 1 unless defined $other{'version'}; + print "Set-Cookie: $key=", cookie_esc($value); + foreach my $k (keys %other) { + print ";$k=", cookie_esc($other{$k}); + } + print "\n"; +} + +sub parse_cookies() { + my $h = http_get("Cookie") or return (); + my @cook = (); + while (my ($padding,$name,$val,$xx,$rest) = ($h =~ /\s*([,;]\s*)*([^ =]+)=([^ =,;\"]*|\"([^\"\\]|\\.)*\")(\s.*|;.*|$)/)) { + if ($val =~ /^\"/) { + $val =~ s/^\"//; + $val =~ s/\"$//; + $val =~ s/\\(.)/$1/g; + } + push @cook, $name, $val; + $h = $rest; + } + return @cook; +} + 1; # OK -- 2.39.2