cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Good day bros. I am trying to make a local HTTP Daemon with module HTTP::Daemin that will use mod CGI to process GET requests. For initial testing I have been trying this
use HTTP::Daemon; use HTTP::Status; use CGI; my $d = HTTP::Daemon->new(LocalPort => 8080) || die; while (my $c = $d->accept) { while (my $r = $c->get_request) { my $query = new CGI($r->content); my $junk = 1; $c->send_error(RC_FORBIDDEN) } $c->close; undef($c); }
But when I call it with something like http://localhost:8080/test.cgi?foo=bar I get nothing in the $query object. Same if I initialize CGI with no parameter. If I look at, for example, $r->as_string the GET string is there, so I'm not sure what I'm doing wrong.

TIA...

Steve

Replies are listed 'Best First'.
Re: Using HTTP::Daemon with CGI
by Errto (Vicar) on Apr 13, 2007 at 20:47 UTC
    I had a similar problem and found assistance here. Basically CGI.pm won't work properly unless you set certain environment variables that mod_cgi or the equivalent would normally take care of for you. Here is a snippet from my application that does the trick:
    my $req = $c->get_request; my $uri = $req->uri; $ENV{REQUEST_METHOD} = $req->method; $ENV{CONTENT_TYPE} = join('; ', $req->content_type); $ENV{CONTENT_LENGTH} = $req->content_length || ''; $ENV{SCRIPT_NAME} = $uri->path || 1; $ENV{QUERY_STRING} = $uri->query || '';
      That did it. Here's the fixed code.

      use HTTP::Daemon; use HTTP::Status; use CGI qw/:standard/; my $d = HTTP::Daemon->new(LocalPort => 8080) || die; while (my $c = $d->accept) { while (my $r = $c->get_request) { my $uri = $r->uri; $ENV{REQUEST_METHOD} = $r->method; $ENV{CONTENT_TYPE} = join('; ', $r->content_type); $ENV{CONTENT_LENGTH} = $r->content_length || ''; $ENV{SCRIPT_NAME} = $uri->path || 1; $ENV{QUERY_STRING} = $uri->query || ''; my $q = new CGI; my $p = $q->param('foo'); my $reply = $q->header.$q->start_html."the foo parameter is $p +<p>".$q->end_html; $c->send_response($reply) } $c->close; undef($c); }