This example uses features first found in POE 0.1702.

This simple web server listens for requests on a port and responds to them. To keep the example simple, it only responds with the request's headers.

Once it has been run, you can try it out by pointing a web browser at <http://localhost:8088/>;.


use warnings;
use strict;

use POE qw(Component::Server::TCP Filter::HTTPD);
use HTTP::Response;

# Spawn a web server on port 8088 of all interfaces.

  Alias        => "web_server",
  Port         => 8088,
  ClientFilter => 'POE::Filter::HTTPD',

  # The ClientInput function is called to deal with client input.
  # Because this server uses POE::Filter::HTTPD to parse input,
  # ClientInput will receive HTTP requests.

  ClientInput => sub {
    my ($kernel, $heap, $request) = @_[KERNEL, HEAP, ARG0];

    # Filter::HTTPD sometimes generates HTTP::Response objects.
    # They indicate (and contain the response for) errors that occur
    # while parsing the client's HTTP request.  It's easiest to send
    # the responses as they are and finish up.

    if ($request->isa("HTTP::Response")) {

    # The request is real and fully formed.  Build content based on
    # it.  Insert your favorite template module here, or write your
    # own. :)

    my $request_fields = '';
      sub {
        my ($header, $value) = @_;
        $request_fields .= "<tr><td>$header</td><td>$value</td></tr>";

    my $response = HTTP::Response->new(200);
    $response->push_header('Content-type', 'text/html');

      # Break the HTML tag for the wiki.
        . "html><head><title>Your Request</title></head>"
        . "<body>Details about your request:"
        . "<table border='1'>$request_fields</table>"
        . "</body></html>"

    # Once the content has been built, send it back to the client
    # and schedule a shutdown.


# Start POE.  This will run the server until it exits.

exit 0;