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

Hello, I'd really love some working example of a self contained web server + CGI using HTTP::Server::Simple::CGI,
I seem not to understand properly the module perldoc....
How do you impelement a server which does http://localhost:8080/hello.cgi?name=dude

Hello dude!

Thanks G.
  • Comment on need some HTTP::Server::Simple::CGI example

Replies are listed 'Best First'.
Re: need some HTTP::Server::Simple::CGI example
by almut (Canon) on Sep 01, 2007 at 03:28 UTC

    Here's a simple (and hopefully self-explanatory) example:

    #!/usr/bin/perl { package MyWebServer; use HTTP::Server::Simple::CGI; our @ISA = qw(HTTP::Server::Simple::CGI); my %dispatch = ( '/hello.cgi' => \&resp_hello, # ... ); sub handle_request { my $self = shift; my $cgi = shift; my $path = $cgi->path_info(); my $handler = $dispatch{$path}; if (ref($handler) eq "CODE") { print "HTTP/1.0 200 OK\r\n"; $handler->($cgi); } else { print "HTTP/1.0 404 Not found\r\n"; print $cgi->header, $cgi->start_html('Not found'), $cgi->h1('Not found'), $cgi->end_html; } } sub resp_hello { my $cgi = shift; # CGI.pm object return if !ref $cgi; my $who = $cgi->param('name'); print $cgi->header, $cgi->start_html("Hello"), $cgi->h1("Hello $who!"), $cgi->end_html; } } # end of package MyWebServer # start the server on port 8080 my $pid = MyWebServer->new(8080)->background(); print "Use 'kill $pid' to stop server.\n";

    After having started the script, you should be able to access it via the URL you specified. Good luck.

      ++ almut

      If OP is on windows, kill won't kill because it doesn't exist there; perhaps add:

      print "or, on Windows, stop server with Cntrl-C.\n";
      Almut, Your example really helped me. I was having trouble figuring out how to make H:S:S work. I annotated the H:S:S POD with a link to your post. Would you mind if your example was placed in the POD for H:S:S? Thanks, Mark

        I'm glad it helped, and of course I wouldn't mind if it finds its way into the POD.

Re: need some HTTP::Server::Simple::CGI example
by stvn (Monsignor) on Sep 01, 2007 at 14:28 UTC

    When I wrote CGI::Application::Server I used the source code from HTTP::Server::Simple::Kwiki as my guide, it helped quite a lot.

    Of course it all depends on how robust or complex you want your self contained server to be. A simple server with little or no error checking is really easy, a full fledged server which is robust in the face of bad HTTP input is much more difficult.

    For instance, here is a simple (5 line) server "framework" which I have used in the past for quick stuff.

    package Foo::Server; use HTTP::Server::Simple::CGI; our @ISA = qw(HTTP::Server::Simple::CGI); our $CALLBACK; sub handle_request { $CALLBACK->(@_) }
    The usage is as simple as:
    $Foo::Server::CALLBACK = sub { my ($server, $cgi) = @_; print "Hello " . ($cgi->param('name') || 'World') }; Foo::Server->new()->run();

    -stvn