in reply to Re^2: Perl web server
in thread Perl web server
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.
This style of code can get out of hand quickly, because it tends to turn into a large case statement.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); }
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Perl web server
by BrowserUk (Patriarch) on Jul 11, 2004 at 02:22 UTC |