|
NAMEPOE::Component::Server::SimpleHTTP - Perl extension to serve HTTP requests in POE. VERSIONversion 2.30 SYNOPSIS use POE;
use POE::Component::Server::SimpleHTTP;
# Start the server!
POE::Component::Server::SimpleHTTP->new(
'ALIAS' => 'HTTPD',
'PORT' => 11111,
'HOSTNAME' => 'MySite.com',
'HANDLERS' => [
{
'DIR' => '^/bar/.*',
'SESSION' => 'HTTP_GET',
'EVENT' => 'GOT_BAR',
},
{
'DIR' => '^/$',
'SESSION' => 'HTTP_GET',
'EVENT' => 'GOT_MAIN',
},
{
'DIR' => '^/foo/.*',
'SESSION' => 'HTTP_GET',
'EVENT' => 'GOT_NULL',
},
{
'DIR' => '.*',
'SESSION' => 'HTTP_GET',
'EVENT' => 'GOT_ERROR',
},
],
'LOGHANDLER' => { 'SESSION' => 'HTTP_GET',
'EVENT' => 'GOT_LOG',
},
'LOG2HANDLER' => { 'SESSION' => 'HTTP_GET',
'EVENT' => 'POSTLOG',
},
# In the testing phase...
'SSLKEYCERT' => [ 'private-key.pem', 'public-cert.pem' ],
'SSLINTERMEDIATECACERT' => 'intermediate-ca-cert.pem',
) or die 'Unable to create the HTTP Server';
# Create our own session to receive events from SimpleHTTP
POE::Session->create(
inline_states => {
'_start' => sub { $_[KERNEL]->alias_set( 'HTTP_GET' );
$_[KERNEL]->post( 'HTTPD', 'GETHANDLERS', $_[SESSION], 'GOT_HANDLERS' );
},
'GOT_BAR' => \&GOT_REQ,
'GOT_MAIN' => \&GOT_REQ,
'GOT_ERROR' => \&GOT_ERR,
'GOT_NULL' => \&GOT_NULL,
'GOT_HANDLERS' => \&GOT_HANDLERS,
'GOT_LOG' => \&GOT_LOG,
},
);
# Start POE!
POE::Kernel->run();
sub GOT_HANDLERS {
# ARG0 = HANDLERS array
my $handlers = $_[ ARG0 ];
# Move the first handler to the last one
push( @$handlers, shift( @$handlers ) );
# Send it off!
$_[KERNEL]->post( 'HTTPD', 'SETHANDLERS', $handlers );
}
sub GOT_NULL {
# ARG0 = HTTP::Request object, ARG1 = HTTP::Response object, ARG2 = the DIR that matched
my( $request, $response, $dirmatch ) = @_[ ARG0 .. ARG2 ];
# Kill this!
$_[KERNEL]->post( 'HTTPD', 'CLOSE', $response );
}
sub GOT_REQ {
# ARG0 = HTTP::Request object, ARG1 = HTTP::Response object, ARG2 = the DIR that matched
my( $request, $response, $dirmatch ) = @_[ ARG0 .. ARG2 ];
# Do our stuff to HTTP::Response
$response->code( 200 );
$response->content( 'Some funky HTML here' );
# We are done!
# For speed, you could use $_[KERNEL]->call( ... )
$_[KERNEL]->post( 'HTTPD', 'DONE', $response );
}
sub GOT_ERR {
# ARG0 = HTTP::Request object, ARG1 = HTTP::Response object, ARG2 = the DIR that matched
my( $request, $response, $dirmatch ) = @_[ ARG0 .. ARG2 ];
# Check for errors
if ( ! defined $request ) {
$_[KERNEL]->post( 'HTTPD', 'DONE', $response );
return;
}
# Do our stuff to HTTP::Response
$response->code( 404 );
$response->content( "Hi visitor from " . $response->connection->remote_ip . ", Page not found -> '" . $request->uri->path . "'" );
# We are done!
# For speed, you could use $_[KERNEL]->call( ... )
$_[KERNEL]->post( 'HTTPD', 'DONE', $response );
}
sub GOT_LOG {
# ARG0 = HTTP::Request object, ARG1 = remote IP
my ($request, $remote_ip) = @_[ARG0,ARG1];
# Do some sort of logging activity.
# If the request was malformed, $request = undef
# CHECK FOR A REQUEST OBJECT BEFORE USING IT.
if( $request ) {
{
warn join(' ', time(), $remote_ip, $request->uri ), "\n";
} else {
warn join(' ', time(), $remote_ip, 'Bad request' ), "\n";
}
return;
}
DESCRIPTIONThis module makes serving up HTTP requests a breeze in POE. The hardest thing to understand in this module is the HANDLERS. That's it! The standard way to use this module is to do this: use POE;
use POE::Component::Server::SimpleHTTP;
POE::Component::Server::SimpleHTTP->new( ... );
POE::Session->create( ... );
POE::Kernel->run();
Starting SimpleHTTPTo start SimpleHTTP, just call it's new method: POE::Component::Server::SimpleHTTP->new(
'ALIAS' => 'HTTPD',
'ADDRESS' => '192.168.1.1',
'PORT' => 11111,
'HOSTNAME' => 'MySite.com',
'HEADERS' => {},
'HANDLERS' => [ ],
);
This method will die on error or return success. This constructor accepts only 7 options.
EventsSimpleHTTP is so simple, there are only 8 events available.
Streaming with SimpleHTTPIt's possible to send data as a stream to clients (unbuffered and integrated in the POE loop). Just create your session to receive events from SimpleHTTP as usually and add a streaming event, this event will be triggered over and over each time you set the $response to a streaming state and once you trigger it: # sets the response as streamed within our session which alias is HTTP_GET
# with the event GOT_STREAM
$response->stream(
session => 'HTTP_GET',
event => 'GOT_STREAM',
dont_flush => 1
);
# then you can simply yield your streaming event, once the GOT_STREAM event
# has reached its end it will be triggered again and again, until you
# send a CLOSE event to the kernel with the appropriate response as parameter
$kernel->yield('GOT_STREAM', $response);
The optional dont_flush option gives the user the ability to control the callback to the streaming event, which means once your stream event has reached its end it won't be called, you have to call it back. You can now send data by chunks and either call yourself back (via POE) or shutdown when your streaming is done (EOF for example). sub GOT_STREAM {
my ( $kernel, $heap, $response ) = @_[KERNEL, HEAP, ARG0];
# sets the content of the response
$response->content("Hello World\n");
# send it to the client
POE::Kernel->post('HTTPD', 'STREAM', $response);
# if we have previously set the dont_flush option
# we have to trigger our event back until the end of
# the stream like this (that can be a yield, of course):
#
# $kernel->delay('GOT_STREAM', 1, $stream );
# otherwise the GOT_STREAM event is triggered continuously until
# we call the CLOSE event on the response like that :
#
if ($heap{'streaming_is_done'}) {
# close the socket and end the stream
POE::Kernel->post('HTTPD', 'CLOSE', $response );
}
}
The dont_flush option is there to be able to control the frequency of flushes to the client. SimpleHTTP NotesYou can enable debugging mode by doing this: sub POE::Component::Server::SimpleHTTP::DEBUG () { 1 }
use POE::Component::Server::SimpleHTTP;
Also, this module will try to keep the Listening socket alive. if it dies, it will open it again for a max of 5 retries. You can override this behavior by doing this: sub POE::Component::Server::SimpleHTTP::MAX_RETRIES () { 10 }
use POE::Component::Server::SimpleHTTP;
For those who are pondering about basic-authentication, here's a tiny snippet to put in the Event handler # Contributed by Rocco Caputo
sub Got_Request {
# ARG0 = HTTP::Request, ARG1 = HTTP::Response
my( $request, $response ) = @_[ ARG0, ARG1 ];
# Get the login
my ( $login, $password ) = $request->authorization_basic();
# Decide what to do
if ( ! defined $login or ! defined $password ) {
# Set the authorization
$response->header( 'WWW-Authenticate' => 'Basic realm="MyRealm"' );
$response->code( 401 );
$response->content( 'FORBIDDEN.' );
# Send it off!
$_[KERNEL]->post( 'SimpleHTTP', 'DONE', $response );
} else {
# Authenticate the user and move on
}
}
EXPORTNothing. ABSTRACTAn easy to use HTTP daemon for POE-enabled programs SEE ALSO L<POE>
L<POE::Filter::HTTPD>
L<HTTP::Request>
L<HTTP::Response>
L<POE::Component::Server::SimpleHTTP::Connection>
L<POE::Component::Server::SimpleHTTP::Response>
L<POE::Component::Server::SimpleHTTP::PreFork>
L<POE::Component::SSLify>
AUTHORApocalypse <APOCAL@cpan.org> COPYRIGHT AND LICENSEThis software is copyright (c) 2023 by Apocalypse, Chris Williams, Eriam Schaffter, Marlon Bailey and Philip Gwyn. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
|