Created
May 2, 2010 11:48
-
-
Save jettero/387079 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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