#!/usr/bin/perl

use strict;
use Socket;
use Carp;
my $EOL = "\015\012";

sub spawn;  # forward declaration

my $port = shift || 2345;
# mode to test [directclose|connectandclose|connect40311|connect40310|connectsuccess|getbadlength]
my $mode = shift || 'directclose';

my $proto = getprotobyname('tcp');
$port = $1 if $port =~ /(\d+)/; # untaint port number

socket(Server, PF_INET, SOCK_STREAM, $proto)                || die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,pack("l", 1))   || die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY))                || die "bind: $!";
listen(Server,SOMAXCONN)                                    || die "listen: $!";

my $paddr = accept(Client,Server);

if($mode ne "directclose") {
  # get the HTTP request
  my $cmd = "";
  my $line;
  while ((defined($line = <Client>)) && (($cmd .= $line) !~ /\n[\t\s]*\n/s)) {}

  # send the answer
  if($mode eq "get403") {
    print Client <<CONTENT;
HTTP/1.1 403 Forbidden
Date: Wed, 01 Jul 2009 18:04:31 GMT
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head><title>403 Forbidden</title></head><body>
Forbidden
</body></html>
CONTENT
  }
  if($mode eq "get403badlength") {
    print Client <<CONTENT;
HTTP/1.1 403 Forbidden
Date: Wed, 01 Jul 2009 18:04:31 GMT
Content-Length: 1976
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head><title>403 Forbidden</title></head><body>
Forbidden
</body></html>

CONTENT
  }
  if($mode eq "getbadlength") {
    print Client <<CONTENT;
HTTP/1.1 200 OK
Date: Thu, 02 Jul 2009 09:41:48 GMT
Server: Apache
Content-Length: 1976
Connection: close
Content-Type: text/html; charset=utf-8

<html><head><title>test</title></head></html>
CONTENT
  }
  if($mode eq "connect40311") {
    print Client <<CONTENT;
HTTP/1.1 403 Forbidden
Date: Wed, 01 Jul 2009 18:04:31 GMT
Content-Length: 216
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access 127.0.0.1:47525
on this server.</p>
</body></html>
CONTENT
  }
  if($mode eq "connect40311badlength") {
    print Client <<CONTENT;
HTTP/1.1 403 Forbidden
Date: Wed, 01 Jul 2009 18:04:31 GMT
Content-Length: 300
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access 127.0.0.1:47525
on this server.</p>
</body></html>
CONTENT
  }
  if($mode eq "connect40311nolength") {
    print Client <<CONTENT;
HTTP/1.1 403 Forbidden
Date: Wed, 01 Jul 2009 18:04:31 GMT
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access 127.0.0.1:47525
on this server.</p>
</body></html>
CONTENT
  }
  if($mode eq "connect40310") {
    print Client <<CONTENT;
HTTP/1.0 403 Forbidden
Date: Wed, 01 Jul 2009 18:04:31 GMT
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head><title>403 Forbidden</title></head><body>
Forbidden
</body></html>
CONTENT
  }
  if($mode eq "connectsuccessclose") {
    print Client <<CONTENT;
HTTP/1.0 200 Connection established
CONTENT
    sleep(1);
  }


}

close(Client);
close(Server);


