#!/usr/bin/perl
############################################################
# &outputDebug(); # uncomment this to enable debug mode output
# On the web, see http://www.bath.ac.uk/%7Epy8ieh/cgi/development/tests/linemodel1
############################################################
# name: Line Model (1)
# author: Ian Hickson
# license: GPL
# description: serious stress tests for edge cases of line model
# See also:
# http://www.bath.ac.uk/%7Epy8ieh/cgi/development/tests/linemodel1
# http://www.bath.ac.uk/%7Epy8ieh/internet/projects/mozilla/compatimg-std.html
# http://www.bath.ac.uk/%7Epy8ieh/internet/projects/mozilla/compatimg.html
# http://bugzilla.mozilla.org/show_bug.cgi?id=13097
############################################################

# Initialise System
use lib '../utils';
use strict;
use MIME::Base64;
$output::mediaPath = '../../../media/';
$output::appName = 'HTTP Basic Authentication';
$output::authorContact = 'http://www.bath.ac.uk/%7Epy8ieh/';
$output::authorName = 'Ian Hickson';
$output::authorEmail = 'py8ieh=website=cgi=development=tests=auth_basic@bath.ac.uk';
require 'inputRoutines.pl'; # get cookies, parameters
require 'outputRoutines.pl'; # so that we can output stuff
$output::level = $output::levelFOOT; # we don't use outputRoutines
############################################################

my($user, $pass);

if ($ENV{'HTTP_AUTHORIZATION'} =~ /^Basic (.*)$/os) {
   ($user, $pass) = split(/:/, decode_base64($1), 2);
}

if (($user) and ($user ne $ENV{QUERY_STRING})) {
    print "HTTP/1.1 200 OK\nContent-Type: text/html\n\n";
} else {
    print "HTTP/1.1 401 Unauthorized\nWWW-Authenticate: Basic realm=\"first test\"\nContent-Type: text/html\n\n";
}

print(<<end);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html lang="en">
<head>
  <title>Authentication Test -- HTTP Basic Authentication</title>
  <style type="text/css">
  </style>
</head>

<body>

<h1>HTTP Basic Authentication</h1>

<p>user: <strong>$user</strong>; pass: <strong>$pass</strong> (<a href="nph-auth_basic?$user">try again</a>)</p>

<p>Here is the environment of this CGI script:</p>
<table>
<thead>
<tr>
   <th>Variable</th><th>Value</th>
</tr>
<tbody>
end

foreach my $key (sort(keys %ENV)) {
    print "<tr><td>" . $key . '</td><td>' . $ENV{$key} . "</td></tr>\n";
}

print(<<end);
</table>
</body>
</html>
end

############################################################
