|
POE::Component::Server::SimpleHTTP::PreFork->new(
'ALIAS' => 'HTTPD' ,
'PORT' => 8080,
'HANDLERS' => [
{
'DIR' => '^/$' ,
'SESSION' => 'HTTP_GET' ,
'EVENT' => 'GOT_MAIN' ,
},
{
'DIR' => '.*' ,
'SESSION' => 'HTTP_GET' ,
'EVENT' => 'GOT_ERR' ,
},
],
'HEADERS' => {
'Server' => 'My Own Server' ,
},
'FORKHANDLERS' => { 'HTTP_GET' => 'FORKED' },
'MINSPARESERVERS' => 5,
'MAXSPARESERVERS' => 10,
'MAXCLIENTS' => 256,
'STARTSERVERS' => 10,
) or die 'Unable to create the HTTP Server' ;
POE::Session->create(
inline_states => {
'_start' => sub { $_ [KERNEL]->alias_set( 'HTTP_GET' ) },
'GOT_MAIN' => \ &GOT_REQ ,
'GOT_ERR' => \ &GOT_ERR ,
'FORKED' => \ &GOT_FORKED ,
},
);
POE::Kernel->run();
exit ;
sub GOT_FORKED {
warn "Forked\n" ;
return ;
}
sub GOT_REQ {
my ( $request , $response , $dirmatch ) = @_ [ ARG0 .. ARG2 ];
$response ->code( 200 );
$response ->content( 'Hi, you fetched ' . $request ->uri );
$_ [KERNEL]->post( 'HTTPD' , 'DONE' , $response );
}
sub GOT_ERR {
my ( $request , $response , $dirmatch ) = @_ [ ARG0 .. ARG2 ];
if ( ! defined $request ) {
$_ [KERNEL]->post( 'HTTPD' , 'DONE' , $response );
return ;
}
$response ->code( 404 );
$response ->content( "Hi visitor from " . $response ->connection->remote_ip . ", Page not found -> '" . $request ->uri->path . "'" );
$_ [KERNEL]->post( 'HTTPD' , 'DONE' , $response );
}
|