File: cookies-hash.cgi

package info (click to toggle)
libcgi-tiny-perl 1.003-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 516 kB
  • sloc: perl: 1,307; makefile: 2
file content (29 lines) | stat: -rwxr-xr-x 793 bytes parent folder | download
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
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use CGI::Tiny;
use Cpanel::JSON::XS qw(decode_json encode_json);
use MIME::Base64 qw(decode_base64 encode_base64);

cgi {
  my $cgi = $_;

  my $key = $cgi->param('cookie_key');
  my $hashref;
  if (defined $key) {
    $hashref->{$key} = $cgi->param('cookie_value');
  } else {
    my $cookie = $cgi->cookie('hash');
    $hashref = decode_json decode_base64 $cookie if defined $cookie;
    $key = (keys %$hashref)[0] if defined $hashref;
  }

  if (defined $hashref) {
    my $encoded_value = encode_base64 encode_json($hashref), '';
    $cgi->add_response_cookie(hash => $encoded_value, Path => '/');
    $cgi->render(text => "Set cookie hash key $key: $hashref->{$key}");
  } else {
    $cgi->render(text => "No cookie value set");
  }
};