Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  POE::COMPONENT::SERVER::HTTPSERVER (3)

.ds Aq ’


POE::Component::Server::HTTPServer - serve HTTP requests



    use POE;
    use POE::Component::Server::HTTPServer;

    my $server = POE::Component::Server::HTTPServer->new();
    $server->port( 8080 );
    $server->handlers( [
                        / => new_handler(StaticHandler, ./htdocs),
                        /foo => \&foo_handler,
                        /bar => MyBarHandler->new(),
                       ] );
    my $svc = $server->create_server();
    exit 0;


POE::Component::Server::HTTPServer is a POE-based HTTP server. Requests are dispatched based on an ordered list of prefix => handler pairs.

For each pair in the handlers list in sequence, the given handler is invoked if the prefix matches the request path. Each handler can return either a value indicating to continue processing the request, or one telling HTTPServer that request processing is done. Handlers may also modify the request and tell the HTTPServer to restart the dispatch process.

HTTPServer creates a context object for each request (just a hash reference) which it passes to each handler when invoked. Among other standard attributes, this context object contains the HTTP::Request and HTTP::Response objects for the request being processed, as well as the requested path with and without the matched prefix. Handlers may retrieve and set attributes in this context in order to get information and to modify the state of other handlers.

    Creational Methods

<B>new( B>%args<B> )B> Creates a new HTTPServer object. The following arguments provide shortcuts for the setter methods of the same name:

  port (default: 8080)
  handlers (default: none)
  log_file (default: httpserver.log)
  backstop_handler (default: the NotFoundHandler instance)

For example:

    $s = POE::Component::Server::HTTPServer->new(
            port => 8088,
            handlers => [ / => new_handler("StaticHandler", "./html") ],
            log_file => "/var/log/myhttp.log",
            backstop_handler => \&my_backstop,

new() does not install a POE component in the kernel. Use create_server() to do this, once the server is appropriately configured.

<B>log_file( B>$filename<B> )B>, <B>log_file( )B> Returns and (optionally) sets the filename for the request log file. This log lists all requests handled by the server in a format similar to common httpd log format. By default, this file name will be httpserver.log.
<B>port( B>$port<B> )B>, <B>port( )B> Returns and (optionally) sets the port for the server to listen on. If not explicitly set, the server will listen on port 8080.
<B>handlers( B>$handlers_listref<B> )B>, <B>handlers( )B> Returns and (optionally) sets the list of request handlers. This accepts and returns an array reference. The list referenced by the return value may be modifed, should you prefer to manipulate the handler list that way. By default, this list is empty.

See Request Handlers.

<B>backstop_handler( B>$handler<B> )B>, <B>backstop_handler( )B> Returns and (optionally) sets the backstop handler, the handler invoked if none of the configured handlers finalize the request. If not specified, the server will use the instance of POE::Component::Server::HTTPServer::NotFoundHandler.

    Other Methods

<B>create_server( )B> Sets up and installs the POE server component in the POE kernel. The newly created component object is returned.
<B>dispatch( B>$context<B>, B>$full_request_path<B> )B> Dispatch the current request, as set up in the $context. This is intended for redispatching requests to specific server-relative locations. If $full_request_path is not provided, the context attribute fullpath will be used, as set (originally) by the HTTPServer on recieving a new request.

HTTPServer saves a reference to itself in the context under the key dispatcher, allowing you to call this method inside handlers like this:

    $context->{dispatcher}->dispatch( $context, $newpath );

    Internal Methods

The following methods should be considered private, but may be of interest when subclassing HTTPServer.
<B>_init( B>@args<B> )B> Called by new() to initialize newly created objects.
<B>_get_dispatcher( )B> Badly named: returns the coderef for POE::Component::Server::TCP’s ClientInput property. This is the coderef that does the request dispatching work.
<B>_request_log( )B> Logs a request.

    Exported Functions

The following subroutines are exported by default:
<B>new_handler( B>$short_handler_name<B>, B>@args<B> )B> This is a shortcut for

    "POE::Component::Server::HTTPServer::$short_handler_name"->new( @args );

which is significantly less typing for handlers in the default package. This is intended for use when setting the list of handlers.

    Request Handlers

Request handlers are used to service incoming requests. Each handler in turn is associated with a relative request URI prefix, and may choose to either finalize the request processing or let it continue.

The prefixes are regular expressions, to be matched against the beginning of the request URI (eg, assume a prepended ^). Each handler in sequence is invoked if the request matches this prefix.

The handlers themselves may be either an object implementing the interface in POE::Component::Server::HTTPServer, or a subroutine reference. In the first case, HTTPServer will can the object’s handle() method, and in the second, HTTPServer will execute the subroutine reference. In both cases, HTTPServer will pass the context object to the method or sub as an argument.

HTTPServer always sets certain attributes in the context before invoking the request:
<B>B>$context<B>->{request}B> The HTTP::Request object holding the request message data.
<B>B>$context<B>->{response}B> The HTTP::Response object to use to build the response message.
<B>B>$context<B>->{fullpath}B> The full relative path of the request URI. This is initially equal to $context->{request}->uri()->path(), but may be modified by request handlers.
<B>B>$context<B>->{contextpath}B> The part of the request path after the prefix which matched for the request handler being invoked.
<B>B>$context<B>->{dispatcher}B> The dispatcher (HTTPServer) processing this request. Request handlers may use this object’s dispatch() method to redispatch the request.
Each request handler is passed the context as an argument. Handlers should return either H_CONT, indicating that request processing should continue, or H_FINAL, indicating that the response has been finalized and HTTPServer should stop and return the response message.

There are four standard basic request handlers. The package names for each begin with POE::Component::Server::HTTPServer, but you can use HTTPServer::new_handler() to avoid typing all that. See the documentation for each handler for more detailed information.
<B>NotFoundHandlerB> Creates and finalizes a 404 Not Found response. If the context attribute error_message is set, it will be included in the response body.

An instance of NotFoundHandler is used by HTTPServer as the backstop handler, so that requests not finalized by any other handler result in a usable response.

<B>StaticHandlerB> Serves filesystem resources. May also be subclassed to server interpreted resources based on the underlying filesystem.
<B>ParameterParseHandlerB> Extracts CGI parameters from GET and POST requests, and adds them to the context’s param attribute.
<B>BasicAuthenHandlerB> Performs HTTP basic authentication: interprets request headers and sets the context’s basic_username and basic_password attributes. Issues a basic authen challenge response if the request has no auth headers.


This module was inspired by POE::Component::Server::HTTP, which deals with request processing in a slightly different manner.


POE::Component::Server::HTTPServer::Handler, POE, POE::Component::Server::HTTPServer::Examples


Greg Fast <>


Copyright 2003 Greg Fast.

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

Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 POE::COMPONENT::SERVER::HTTPSERVER (3) 2005-05-20

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.