aboutsummaryrefslogtreecommitdiffstats
path: root/acme-webserver
blob: 57ea7897adf3ac4c47acb37ea590120a45a43e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/perl -T

use strict;
use warnings;

my $ROOT = '/.well-known/acme-challenge';

$_ = <STDIN> // exit;
my $proto  = s/ HTTP\/(1\.[01])\r\n\z// ? $1 : die "Error: Bad request\n";
my $method = s/\A(GET|HEAD) //          ? $1 : die "Error: Bad request\n";

# Consume the headers (and ignore them)
while (defined (my $h = <STDIN>)) { last if $h eq "\r\n" };

my ($status_line, $content_type, $content);
if (/\A\Q$ROOT\E\/([A-Za-z0-9_-]+)\z/ and -f $1) {
    if (open my $fh, '<', $1) {
        ($status_line, $content_type) = ('200 OK', 'application/jose+json');
        $content = do { local $/ = undef; <$fh> };
        close $fh;
    }
    else {
        $status_line = '403 Forbidden';
    }
}

print "HTTP/$proto ", ($status_line // '404 Not Found'), "\r\n";
print "Content-Type: $content_type\r\n" if defined $content_type;
print "Content-Length: ".length($content)."\r\n" if defined $content;
print "Connection: close\r\n";
print "\r\n";
print $content if defined $content and $method eq 'GET';