# Thanks to merlyn for nudging me and giving me this snippet!
use strict;
use HTTP::Daemon;

$|++;

my $d = HTTP::Daemon->new or die;
print $d->url, "\n";

my $counter = 4;
while ($counter-- and my $c = $d->accept) {
  while (my $req = $c->get_request) {
    if ($ENV{TEST_HTTP_VERBOSE}) {
      my @lines = split "\n",$req->as_string;
      warn "# $_\n" for @lines;
    };
    my $res;
    if (my ($user, $pass) = $req->authorization_basic) {
      $res = HTTP::Response->new(200, "OK", undef,
					    "user = '$user' pass = '$pass'");
    } else {
      $res = HTTP::Response->new(401, "Auth Required", undef,
				    "auth required");
      $res->www_authenticate("Basic realm=\"testing realm\"");
    }
    if ($ENV{TEST_HTTP_VERBOSE}) {
      warn "---\n";
      my @lines = split "\n",$res->as_string;
      warn "# $_\n" for @lines;
    };
    $c->send_response($res);
  }
  $c->close;
  undef($c);
}