POE::Declare::HTTP::Server - A simple HTTP server based on POE::Declare
# Create the web server my $http = POE::Declare::HTTP::Server->new( Hostname => '127.0.0.1', Port => '8010', Handler => sub { my $server = shift; my $response = shift; # The request is not passed to you but is available if needed my $request = $response->request; # Webby content generation stuff here $response->code( 200 ); $response->header( 'Content-Type' => 'text/plain' ); $response->content( "Hello World!" ); return; }, ); # Control with methods $http->start; $http->stop;
This module provides a simple HTTP server based on POE::Declare.
The implemenetation is intentionally minimalist, making this module an ideal choice for creating specialised web servers embedded in larger applications.
my $server = POE::Declare::HTTP::Server->new( Hostname => '127.0.0.1', Port => '8010', Handler => \&content, StartupEvent => \&startup_done, StartupError => \&startup_failed, ShutdownEvent => \&shutdown_done, );
The new constructor sets up a reusable HTTP server that can be enabled and disabled repeatedly as needed.
new
It takes three required parameters parameters. Hostname, Port and Handler.
Hostname
Port
Handler
The Handler parameter should be a CODE reference that will be passed the server object and a HTTP::Response object. Your code should fill the provided response object, which will be sent to the client when the function ends. If your content will change based on the request, you can obtain the request from the "request" in HTTP::Response method.
CODE
The server supports three messages you can register callbacks for.
The StartupEvent message fires after the server socket has been bound and is available for clients to make requests, and before any connections have been made from clients.
StartupEvent
The StartupError message fires if the server fails to bind to the port, or has some other error during the socket setup process.
StartupError
The ShutdownEvent message fires on the completion of a controlled shutdown.
ShutdownEvent
There is currently no ShutdownError event for unexpected server termination, as this should not occur. An error of this type may, however, be added later.
ShutdownError
The Hostname accessor returns the server to bind to, as originally provided to the constructor.
The Port accessor returns the port number to bind to, as originally provided to the constructor.
The Handler accessor returns the CODE reference that requests will be passed to, as provided to the constructor.
The start method enables the web server. If the server is already running, this method will shortcut and do nothing.
start
If called before POE has been started, the web server will start immediately once POE is running.
The stop method disables the web server. If the server is not running, this method will shortcut and do nothing.
stop
Bugs should be always be reported via the CPAN bug tracker at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Declare-HTTP-Server
For other issues, or commercial enhancement or support, contact the author.
Adam Kennedy <adamk@cpan.org>
POE, http://ali.as/
Copyright 2006 - 2011 Adam Kennedy.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
To install POE::Declare::HTTP::Server, copy and paste the appropriate command in to your terminal.
cpanm
cpanm POE::Declare::HTTP::Server
CPAN shell
perl -MCPAN -e shell install POE::Declare::HTTP::Server
For more information on module installation, please visit the detailed CPAN module installation guide.