in reply to Re: Perl web server
in thread Perl web server

Care to suggest how to extend this into a working webserver?

use HTTP::Daemon; use HTTP::Status; my $d = HTTP::Daemon->new || 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' and $r->url->path eq "/xyzzy") { # remember, this is *not* recommended practice :-) $c->send_file_response("/etc/passwd"); } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); }

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^3: Perl web server
by toma (Vicar) on Jul 11, 2004 at 01:58 UTC
    Here's fragment of a little search engine that I wrote. It uses a web interface. It searches an array of data that is retrieved from a file using Storable. The array is searched with regular expressions. In the code posted here, I have left out the routines that generate the HTML and do the searching, leaving just the web server part.

    This is not intended to be a full-featured web server, or even a general-purpose minimal web server. It is a browser UI for a specific application, which is typically only used on a laptop that is disconnected from a network.

    When connected to the network, users access a full-featured database-driven web site instead of this little one. The database behind the full-featured site generates the Storable file used by the little laptop version.

    use Storable qw(retrieve); use HTTP::Daemon; use HTTP::Status; use CGI qw(:standard unescape); $|++; my $d = HTTP::Daemon->new(LocalPort => 8087) || die; print "Point browser to: ", $d->url, "\n"; while (my $c = $d->accept) { my $r = $c->get_request; if ($r->method eq 'GET') { print "Getting content for ",$r->url->path,"\n" if $verbose > 0; if ($r->url->path =~ /\/cdsu/) { my $query = $r->content; my $uri = $r->uri; my $header = $c->send_basic_header; my ($srchpat, $srchres, $srchmsg); if ($uri =~ /srch=([^&]+)/) { $srchpat = $1; $srchpat = unescape($srchpat); $srchmsg = "Search pattern: $srchpat"; $srchpat =~ s/\*/.*/g; $srchpat =~ s/\?/.?/g; $srchres = mpn_search($srchpat); } else { $srchmsg= "Please enter an MPN search value, eg *74*74*"; $srchres= ""; } my $response= gen_content($header, $srchres, $d->url, $srchmsg); print "Query: $query\n", "URI: $uri\n", "Search Pattern: $srchpat\n" if $verbose > 0; $c->send_response($response); } elsif ($r->url->path =~ "/(.*?).gif") { $c->send_file_response("$1.gif"); } else { my $header= $c->send_basic_header; my $response= gen_help($header, $d->url); $c->send_basic_header; $c->send_response($response); } } else { $c->send_error(RC_FORBIDDEN) } $c->close; undef($c); }
    This style of code can get out of hand quickly, because it tends to turn into a large case statement.

    I have a different program that is a little more complex that also uses HTTP::Daemon. In the other program, I used a hash of anonymous subs to generate the content. The key to the hash is a fragment of the URL. It is extracted from the URL with a regular expression. This eliminates the need for a large case statement.

    It should work perfectly the first time! - toma

      Thanks a bunch. I did a web search looking for some sample code but came up empty. With this and corion's code that tachyon linked to elsewhere in this thread I have a starting point.

      I really like the hash of subrefs idea.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon