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

Esteemed monks,

Following your answers to my question of yesterday in Adding HTTP server to Windows App - suggestions please!, I have tried some test code and have an issue which I do not understand. I am using latest ActiveState Perl (5.8.7 I think it is) on Windows XP sp1. Using the example code from the HTTPServer POD...

#!/usr/bin/perl -w use strict; use Net::HTTPServer; my $server = new Net::HTTPServer( port => 6789, log => "STDOUT" ); $server->RegisterURL( "/foo/bar.pl", \&test ); my $port = $server->Start(); $server->Process(); if ($port) { print "Port: $port\n"; } sub test { my $env = shift; my $res; $res = "<html>\n"; $res .= " <head>\n"; $res .= " <title>This is a test</title>\n"; $res .= " </head>\n"; $res .= " <body>\n"; $res .= " <pre>\n"; foreach my $var ( keys ( %{$env})) { $res .= "$var -> ".$env->{$var}."\n"; } $res .= " </pre>\n"; $res .= " </body>\n"; $res .= "</html>\n"; return ["200", (), $res ]; }
I then try to connect to the appropriate URL (according to the doc's it is: http://drkwolfca.dnsalias.org:6789/foo/bar.pl?test=bing ) and I get the following on the console at the server end:
INIT: Starting the server INIT: Attempting to listen on port 6789 2005/06/28 15:32:25 - Server running on port 6789 PROC: Process: type(single) PROC: Wait for 10 seconds PROC: Do we block? 1 PROC: Wait for 10 seconds PROC: Incoming traffic PROC: We have a client, let's treat them well. Your vendor has not defined POSIX macro F_GETFL, used at C:/Perl/site/ +lib/Net/HTTPServer.pm line 1091
and we are back to prompt.

I have looked at the doc's for the POSIX module on my system and it would appear that the constant is defined. What is going on? Here is the code from within HTTPServer:

###################################################################### +######### # # _nonblock - given a socket, make it non-blocking # ###################################################################### +######### sub _nonblock { my $self = shift; my $socket = shift; my $flags = fcntl($socket, F_GETFL, 0) or croak("Can't get flags for socket: $!\n"); fcntl($socket, F_SETFL, $flags | O_NONBLOCK) or croak("Can't make socket nonblocking: $!\n"); }
Many thanks...
jdtoronto

Replies are listed 'Best First'.
Re: Advice on HTTPServer problem please
by fmerges (Chaplain) on Jun 28, 2005 at 20:47 UTC

    Hi,

    From the POD of the module:

    $server->RegisterURL("/foo/bar.pl",\&test); sub test { my $req = shift; # Net::HTTPServer::Request object my $res = $req->Response(); # Net::HTTPServer::Response object $res->Print("<html>\n"); $res->Print(" <head>\n"); $res->Print(" <title>This is a test</title>\n"); $res->Print(" </head>\n"); $res->Print(" <body>\n"); $res->Print(" <pre>\n"); foreach my $var (keys(%{$req->Env()})) { $res->Print("$var -> ".$req->Env($var)."\n"); } $res->Print(" </pre>\n"); $res->Print(" </body>\n"); $res->Print("</html>\n"); return $res; }

    You are mixing things together... for example:

    On sub test you don't get the $ENV var, what you get is the Request object.

    After calling $server->Process() you enter in a loop so the next statement won't be executed

    You're not authenticating, you want to send a HTML page answer to a client, so you only need to print to STDOUT = client the html output

    You can print the port listening on, after calling the Start() method, but not after calling Process() without giving a timeout value...

    Better take a second look at the module doc. ;-)

    Regards,

    :-)

    Update: don't know exactly what you wanna do, but I recommend you, taking a look at POE, at http://poe.perl.org

      Just shows you shouldn't always assume that the example in the POD will run! Thanks for the advice. I will look at POE also.

      jdtoronto

Re: Advice on HTTPServer problem please
by jdtoronto (Prior) on Jun 29, 2005 at 16:01 UTC
    Esteemed monks,

    I got bitten again! ActiveState only has version 1.0.2 of this module packaged. The response from fmerges yesterday refers to the version 1.1.1 doc's. So I was totally confused for a couple of hours here. Once I realised that problem I went looking for the newer module and tried to install it using CPAN, but it wouldn't make.

    So in the best, unapproved, higly risky and ill advised tradition, I copied the .pm files into the appropriate location in the directory tree. With great trepidation I tried the sample code - it worked!

    Now all I have to do is figure out how to install this thing the right way. But at least for now I can progress my application. Thank you for your help.

    jdtoronto