dark

Simple webserver in Perl

blank

I had to mimic a certain web application for a customer I’m currently working for, so I’ve created a small standard webserver in Perl using HTTP::Daemon.

The only thing it does, is serving one particular file.

#!/usr/bin/perl
use strict; use warnings;
use HTTP::Daemon;
use HTTP::Status;

my $devices_file      = 'QIP-ip.activeobj.txt';
my $devices_file_path = $devices_file;

my $d = HTTP::Daemon->new( LocalPort => 8080 ) || die "Kak: $!n";
$d->product_tokens('VirtualQIP dev device');

print "Please contact me at: url, ">n";
while (my $c = $d->accept) {
    while (my $r = $c->get_request) {
        if ($r->method eq 'GET' and $r->uri->path =~ /$devices_file/) {
            # remember, this is *not* recommended practice
            print localtime() . " [debug] GET " . $r->uri->path() . "n";
            $c->send_file_response($devices_file_path);
            print localtime() . " [debug] SENT " . $r->uri->path() . "n";
        }
        else {
            print localtime() . " [debug] UNKNOWN URI " . $r->uri->path() . "n";
            $c->send_error(RC_FORBIDDEN)
        }
    }
    $c->close;
    undef($c);
}
Previous Post

Calculate netmask in Perl

Next Post

Updating Darwin ports on Mac OS X

Related Posts