+sub decode_basic_auth {
+ my ($q) = @_;
+
+ my ($method, $base64) = split(/\s+/, $q->http('Authorization'));
+ if (!defined $method) {
+ return;
+ } elsif ($method ne 'Basic') {
+ return;
+ }
+
+ my ($username, $password) = split(':', decode_base64($base64), 2);
+ if (!defined $username) {
+ return;
+ }
+
+ return ($username, $password);
+}
+
+sub print_401 {
+ print header(-type => 'text/html',
+ -status => '401 Unauthorized',
+ -WWW_Authenticate => 'Basic realm="blerg"');
+ print <<DOC;
+<!DOCTYPE html>
+<h1>401 Unauthorized</h1>
+Please log in.
+DOC
+}
+