0

I'm trying to get a pre-existing PERL web application running on my local MAMP apache2 server and am having problems getting post data. The app uses cgi.pm to try to grab data from $cgi->param, but param is always empty even when properly formatted post data is sent in x-www-form-urlencoded format.

Additionally, STDIN is always empty and throws an error if not prefaced with \*.

hello-world-form.cgi:

#!/[localpath]/perl5/perlbrew/perls/perl-5.24.0/bin/perl -d

use strict;
use warnings;

print "Content-type: text/html\n\n";
print qq(
  <!doctype html>
  <html>
  <body>
    <form action=\"hello-world.cgi\" method=\"POST\">
      <input type=\"text\" name=\"myparam\" />
      <input type=\"submit\" value=\"submit post var\" />
    </form>
  </body>
  </html>
);

hello-world.cgi:

#!/[localpath]/perlbrew/perls/perl-5.24.0/bin/perl -d

use strict;
use warnings;
use CGI;

my $q = CGI->new();

print "Content-type: text/html\n\n";
print "<p>key = " . $q->param('myparam') . "</p>\n"; #this is always empty
my @names = $q->param;
foreach my $name (@names) {
  print "<p>$name = " . $q->param($name) . "</p>"; #this never runs
}

my $postdata = '';
my $in = \*STDIN;
my $bytes = read($in, $postdata, $ENV{'CONTENT_LENGTH'});
print "<p>postdata = $postdata</p>\n"; # postdata is empty
print "<p>content length = " . $ENV{'CONTENT_LENGTH'} . "</p>\n"; # this number seems correct

foreach my $key (keys %ENV) {
  print "<p>$key --> $ENV{$key}</p>\n"; # nothing special here
}

httpd.conf:

...
LoadModule cgi_module modules/mod_cgi.so
LoadModule perl_module modules/mod_perl.so
...
ScriptAlias /cgi-bin/ "/[siteroot]/cgi-bin/"
Alias /perl/ "/Applications/MAMP/cgi-bin/"
<IfModule perl_module>
    PerlModule ModPerl::Registry
    <Location /perl>
        SetHandler perl-script
        PerlResponseHandler ModPerl::Registry
        PerlOptions +ParseHeaders
        Options +ExecCGI
    </Location>
</IfModule>

I'm running PERL v5.24.0 via perlbrew with these modules:

> perlbrew list-modules

CGI
Date::Parse
Encode::Locale
HTML::Parser
HTML::Tagset
HTTP::Date
HTTP::Message
IO::HTML
LWP::MediaTypes
Perl
Sub::Uplevel
Test::Deep
Test::Needs
Test::NoWarnings
Test::Warn
Try::Tiny
URI

Any ideas?

  • 1
    Please show us also how you send the POST request to your Perl program. – Corion Jan 18 '19 at 08:40
  • 4
    Also, why do you have `-d` on the hashbang line of the script? – Corion Jan 18 '19 at 08:55
  • 3
    (also, there is no data available on `*STDIN` because `CGI` will already have read all available data there. So, please post a [mcve] so we can replicate this. This includes the HTML you're using to submit the POST request. – Corion Jan 18 '19 at 08:59
  • If you already have access to the POSTed request, I suggest adding it as well, including headers and body etc. I doesn't hurt if people can check actually sent values on their own. – Thorsten Schöning Jan 18 '19 at 18:09
  • Brilliant! The `-d` was the issue here and I'll mark that as the answer. I added the form submit code for posterity (I was using postman to test so I didn't have a sample form file before). And thanks for the explanation on the STDIN. Unfortunately, the actual Perl app now breaks without the `-d`, but at least I have a new problem to solve. :) – curiousgage Jan 18 '19 at 18:18

1 Answers1

0

Removing the -d from the shebang solves this issue. Kudos to Corion for the answer.