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

Hi all. I'm trying to set up a simple local server to process some html forms. I know I could use HTTP::Server::Simple for this, but I am on a windoze machine and it looks like it has not been tested on that platform for some time (if someone has it working please let me know).

So I am using HTTP::Daemon. I almost have it working, but I can't figure out how to get it to complete the transaction withe the browser. Here is the server code:

use feature ':5.10'; $| = 1; use HTTP::Daemon; use HTTP::Status; my $d = HTTP::Daemon->new(LocalPort => 8080) || die; print "Please contact me at: <URL:", $d->url, ">\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { if ($r->method eq 'GET') { say $r->uri->query; $c->send_status_line; } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); }
And here is the html:
<html> <form action="http://127.0.0.1:8080/process/" method="get"> <input type="checkbox" name="vehicle1" value="Bike"> <label for="vehicle1"> I have a bike</label><br> <input type="checkbox" name="vehicle2" value="Car"> <label for="vehicle2"> I have a car</label><br> <input type="checkbox" name="vehicle3" value="Boat" checked> <label for="vehicle3"> I have a boat</label><br><br> <input type="submit" value="Submit"> </form> </html>
When I click submit, the server gets the request and prints the query string, but the send_status_line does not produce any response by the browser. It continues to sit there waiting for a response. How to fix?

Replies are listed 'Best First'.
Re: How to complete a transaction with HTTP::Daemon?
by Corion (Patriarch) on Jul 08, 2021 at 06:08 UTC

    I think you want to send a complete response and not only a status line?

    In Test::HTTP::LocalServer, I have log-server, which is a small HTTP server using HTTP::Daemon - maybe you can rip out the parts that don't interest you?

    The meat is:

    SERVERLOOP: { my $quitserver; while (my $c = $d->accept) { debug "New connection"; while (my $r = $c->get_request) { debug "Request:\n" . $r->as_string; my $location = ($r->uri->path || "/"); my ($link1,$link2) = ('',''); if ($location =~ m!^/link/([^/]+)/(.*)$!) { ($link1,$link2) = ($1,$2); }; my $res; if ($location eq '/get_server_log') { $res = HTTP::Response->new(200, "OK", undef, $log); $log = ''; debug "Response:\n" . $res->as_string if $res; eval { $c->send_response($res) if $res; }; if (my $err = $@) { debug "Server raised error: $err"; if ($err !~ /^Planned error\b/) { warn $err; }; $c->close; }; if (! $res) { $c->close; }; last if $quitserver; } sleep 1; undef($c); last SERVERLOOP if $quitserver; }; undef $d; };
Re: How to complete a transaction with HTTP::Daemon?
by karlgoethebier (Abbot) on Jul 08, 2021 at 10:42 UTC
    «How to fix?»

    You could use a real webserver

    «The Crux of the Biscuit is the Apostrophe»