44 lines
1.2 KiB
Perl
44 lines
1.2 KiB
Perl
# Copyright (C) 2026 Zaher Salman
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
# Minimal application/x-www-form-urlencoded parser used only by the repository
|
|
# tests when the host does not provide CGI.pm. Production continues to use the
|
|
# system CGI module and never deploys this tests/ directory.
|
|
package CGI;
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
sub _decode_component {
|
|
my ($value) = @_;
|
|
$value = "" if !defined($value);
|
|
$value =~ tr/+/ /;
|
|
$value =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
|
|
return $value;
|
|
}
|
|
|
|
sub new {
|
|
my ($class) = @_;
|
|
my $length = $ENV{CONTENT_LENGTH} || 0;
|
|
my $body = "";
|
|
read(STDIN, $body, $length) if $length > 0;
|
|
my %parameters = ();
|
|
foreach my $pair (split(/&/, $body, -1)) {
|
|
next if $pair eq "";
|
|
my ($key, $value) = split(/=/, $pair, 2);
|
|
$key = _decode_component($key);
|
|
$value = _decode_component($value);
|
|
push(@{$parameters{$key}}, $value);
|
|
}
|
|
return bless({ parameters => \%parameters }, $class);
|
|
}
|
|
|
|
sub param {
|
|
my ($self, $name) = @_;
|
|
return keys(%{$self->{parameters}}) if !defined($name);
|
|
my $values = $self->{parameters}{$name} || [];
|
|
return wantarray ? @{$values} : (@{$values} ? $values->[0] : undef);
|
|
}
|
|
|
|
1;
|