#!/usr/bin/perl -w
use strict;
use POE;
use lib 'testlib';
use POE::Component::Server::HTTP 0.09;

my $http_port = 8765;

POE::Session->create (
	inline_states => {
		_start => sub {
			my ($kernel,$session,$heap) = @_[KERNEL,SESSION,HEAP];
  	 		print STDERR "_start: \n";

			my $tmp = POE::Component::Server::HTTP->new( 
				Port => $http_port, 
				ContentHandler => { 
					'/' => sub {
						my ($req,$res) = @_;
						POE::Kernel->post($session,"HTTP_request", $req, $res);
						return RC_WAIT;
					},
				},
				# its soooooo b0000rken
				PostHandler => {
					'/' => sub {
						my ($req,$res) = @_;
						$req->header('Connection' => 'Close');
					}
				},
				Headers => { Server => 'My Server' },
			);

			my $http_ses = $kernel->alias_resolve( $tmp->{httpd});
			my $tcp_ses = $kernel->alias_resolve($tmp->{tcp});
			$http_ses->option('trace' => 1);
			$tcp_ses->option('trace' => 1);
		},
		_stop => sub {
			print STDERR "_stop: client session ended.\n"; 
		},
		HTTP_request => sub {
			my ($req,$res,$heap) = @_[ARG0..ARG1,HEAP];
			my $uri = $req->uri();

			$res->header('Content-Type', 'text/plain');
			
			$res->content("-- ".$uri->as_string.":".$uri->path()."\n"); 

			$res->code(RC_OK);
			$res->header('Connection' => 'close');
			$res->continue();

		},
		_default => sub {
			my ($state) = $_[STATE];
			print STDERR "Unhandled $state\n";
		},
	}
);

POE::Kernel->run();
