The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

POE::Component::Server::SimpleHTTP - Perl extension to serve HTTP requests in POE.

SYNOPSIS

        use POE;
        use POE::Component::Server::SimpleHTTP;

        # Start the server!
        POE::Component::Server::SimpleHTTP->new(
                'ALIAS'         =>      'HTTPD',
                'ADDRESS'       =>      '192.168.1.1',
                'PORT'          =>      11111,
                'HOSTNAME'      =>      'MySite.com',
                'HANDLERS'      =>      [
                        {
                                'DIR'           =>      '^/bar/.*',
                                'SESSION'       =>      'HTTP_GET',
                                'EVENT'         =>      'GOT_BAR',
                        },
                        {
                                'DIR'           =>      '^/$',
                                'SESSION'       =>      'HTTP_GET',
                                'EVENT'         =>      'GOT_MAIN',
                        },
                        {
                                'DIR'           =>      '.*',
                                'SESSION'       =>      'HTTP_GET',
                                'EVENT'         =>      'GOT_ERROR',
                        },
                ],
        ) 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' ) },

                        'GOT_BAR'       =>      \&GOT_REQ,
                        'GOT_MAIN'      =>      \&GOT_REQ,
                        'GOT_ERROR'     =>      \&GOT_ERR,
                },
        );

        # Start POE!
        POE::Kernel->run();

        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!
                $_[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 ];

                # Do our stuff to HTTP::Response
                $response->code( 404 );
                $response->content( "Hi visitor from " . $request->connection->Remote_IP . ", Page not found -> '" . $request->uri->path . "'" );

                # We are done!
                $_[KERNEL]->post( 'HTTPD', 'DONE', $response );
        }

ABSTRACT

        An easy to use HTTP daemon for POE-enabled programs

CHANGES

1.01

        Initial Revision

DESCRIPTION

This 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 SimpleHTTP

To 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 6 options.

ALIAS

This will set the alias SimpleHTTP uses in the POE Kernel. This will default to "SimpleHTTP"

ADDRESS

This value will be passed to POE::Component::Server::TCP to bind to.

PORT

This value will be passed to POE::Component::Server::TCP to bind to.

HOSTNAME

This value is for the HTTP::Request's URI to point to. If this is not supplied, SimpleHTTP will use Sys::Hostname to find it.

HEADERS

This should be a hashref, that will become the default headers on all HTTP::Response objects. You can override this in individual requests by setting it via $request->header( ... )

For more information, consult the HTTP::Headers module.

HANDLERS

This is the hardest part of SimpleHTTP :)

You supply an array, with each element being a hash. All the hashes should contain those 3 keys:

DIR -> The regexp that will be used, more later. SESSION -> The session to send the input EVENT -> The event to trigger

The DIR key should be a valid regexp. This will be matched against the current request path. Pseudocode is: if ( $path =~ /$DIR/ )

NOTE: The path is UNIX style, not MSWIN style ( /blah/foo not \blah\foo )

Now, if you supply 100 handlers, how will SimpleHTTP know what to do? Simple! By passing in an array in the first place, you have already told SimpleHTTP the order of your handlers. They will be tried in order, and if one is not found, SimpleHTTP will DIE!

This allows some cool things like specifying 3 handlers with DIR of: '^/foo/.*', '^/$', '.*'

Now, if the request is not in /foo or not root, your 3rd handler will catch it, becoming the "error" handler!

NOTE: You might get weird Session/Events, make sure your handlers are in order, for example: '^/', '^/foo/.*' The 2nd handler will NEVER get any requests, as the first one will match ( no $ in the regex )

Now, here's what a handler receives: ARG0 -> HTTP::Request object ARG1 -> HTTP::Response object ARG2 -> The exact DIR that matched, so you can see what triggered what

Note: Technically, the HTTP objects are POE::Component::Server::SimpleHTTP::* objects... There's one added feature in the HTTP::Request object, the connection object. See the POD for POE::Component::Server::SimpleHTTP::Request and POE::Component::Server::SimpleHTTP::Connection modules.

Events

SimpleHTTP is so simple, there are only 2 events available.

DONE
        This event accepts only one argument: the HTTP::Response object we sent off.

        Calling this event implies that this particular request is done, and will proceed to close the socket.

        NOTE: This method automatically sets those 3 headers if they are not already set:
                Date            ->      Current date stringified via HTTP::Date
                Content-Type    ->      text/html
                Content-Length  ->      length( $response->content )
SHUTDOWN
        Calling this event makes SimpleHTTP shut down by closing it's TCP socket.

SimpleHTTP Notes

This module is very picky about capitalization!

All of the options are uppercase, to avoid confusion.

You can enable debugging mode by doing this:

        sub POE::Component::SimpleHTTP::DEBUG () { 1 }
        use POE::Component::SimpleHTTP;

For those who are pondering about basic-authentication, here's a tiny snippet to put in the Event handler

        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
                }
        }

EXPORT

Nothing.

SEE ALSO

        L<POE>
        L<POE::Component::Server::HTTP>
        L<POE::Filter::HTTPD>
        L<HTTP::Request>
        L<HTTP::Response>

        L<POE::Component::Server::SimpleHTTP::Connection>
        L<POE::Component::Server::SimpleHTTP::Request>
        L<POE::Component::Server::SimpleHTTP::Response>

AUTHOR

Apocalypse <apocal@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2003 by Apocalypse

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.