Skip to content

Instantly share code, notes, and snippets.

@jettero
Created May 2, 2010 11:48
Show Gist options
  • Save jettero/387079 to your computer and use it in GitHub Desktop.
Save jettero/387079 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use common::sense;
SimpleEchoServer->run(port => 80);
package SimpleEchoServer;
use common::sense;
use POSIX;
use Privileges::Drop;
use base 'Net::Server';
my $EOL = "\r\n";
sub post_bind { drop_privileges('nobody'); $0 = "echo_server.pl" }
sub process_request {
my $this = shift;
my $addr = $this->{server}{peeraddr};
my $line = <STDIN>; chomp $line;
$this->log(1, "connection from $addr");
print "$line\n";
}
sub log {
my $this = shift;
my ($l, $m) = @_;
$m =~ s/^\s+//; $m =~ s/\s+$//; $m =~ s/[\r\n]//g;
if( $l > 3 ) {
$m = "[DEBUG] $m";
}
$m = strftime('%Y-%m-%d %H:%M:%S ', localtime) . sprintf('%7s: %s', "[$$]", $m);
$this->SUPER::log($l, "$m\n");
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment