repository

BlowfishCMS

A small content management system for OpenBSD

version 0.45.6 · ISC · 2026-12-03 · releases

106 lines · 5.0 KB · download this file

#!/usr/bin/perl
# Test helper: behaves like cgi-bin/site.pl, but sends each request to a
# long-running bin/blowfish-fcgi process over FastCGI, the way httpd does.
# The test suites accept a script path, so they run unchanged through FastCGI:
#
#   python3 tests/integration.py tests/fcgi-cgi.pl
#
# One service is started per BLOWFISH_ROOT (socket in that folder) and keeps
# running between requests, so state leaking from one visitor to the next shows
# up as test failures. BLOWFISH_FCGI_WORKERS (default 1) sets its worker count;
# with one worker every request goes through the same process.
# Maintenance options (--migrate etc.) run cgi-bin/site.pl directly.
use v5.36;
use FindBin;
use IO::Socket::UNIX;
use POSIX qw(setsid);
use Time::HiRes qw(sleep);

my $app = "$FindBin::Bin/..";
exec $^X, "$app/cgi-bin/site.pl", @ARGV if @ARGV;
my $root = $ENV{BLOWFISH_ROOT} // die "BLOWFISH_ROOT not set\n";
my $sock_path = "$root/fcgi-test.sock";

sub connect_daemon { return IO::Socket::UNIX->new(Type=>SOCK_STREAM(), Peer=>$sock_path) }
# Connect to the test service, or start it the first time: a child process
# detaches (setsid) and runs bin/blowfish-fcgi in the foreground on a socket
# in the test site's folder; then wait up to 5 seconds for the socket.
my $sock = connect_daemon();
unless ($sock) {
    my $pid = fork // die "fork: $!";
    unless ($pid) {
        setsid();
        open STDIN, '<', '/dev/null'; open STDOUT, '>>', "$root/fcgi-test.log"; open STDERR, '>&', \*STDOUT;
        exec $^X, "$app/bin/blowfish-fcgi", '-d', '-s', $sock_path, '-w', ($ENV{BLOWFISH_FCGI_WORKERS} // 1), '-t', 60 or die "exec: $!";
    }
    for (1 .. 100) { last if $sock = connect_daemon(); sleep 0.05 }
    $sock or die "FastCGI test service did not start; see $root/fcgi-test.log\n";
    open my $pf, '>', "$root/fcgi-test.pid"; print {$pf} $pid; close $pf;
}

# Only what httpd would pass as FastCGI parameters, so code relying on the
# worker's own environment (PATH and so on) fails here as it would in production.
my %params = map { ($_ => $ENV{$_}) } grep {
    /\A(?:HTTP_\w+|CONTENT_(?:TYPE|LENGTH)|REQUEST_\w+|SCRIPT_\w+|QUERY_STRING|REMOTE_\w+|SERVER_\w+|GATEWAY_INTERFACE|DOCUMENT_\w+|PATH_INFO|HTTPS|TLS_\w+)\z/
} keys %ENV;
# The request body, exactly CONTENT_LENGTH bytes from standard input.
binmode STDIN; my $body = '';
my $want = $ENV{CONTENT_LENGTH} // 0;
while ($want > length $body) { sysread(STDIN, $body, $want - length $body, length $body) or last }

# One FastCGI record: version 1, the record type, request id 1, the content
# length and padding (records are padded to 8 bytes), then the content.
# Content longer than 65535 bytes is split into several records.
sub record ($type, $content) {
    my $out = '';
    do {
        my $chunk = substr($content, 0, 65535, '');
        my $pad = (8 - length($chunk) % 8) % 8;
        $out .= pack('CCnnCC', 1, $type, 1, length $chunk, $pad, 0).$chunk.("\0" x $pad);
    } while (length $content);
    return $out;
}
# A name or value length in FastCGI's form: one byte below 128, otherwise four
# bytes with the top bit set.
sub nv_len ($n) { return $n < 128 ? pack('C', $n) : pack('N', $n | 0x80000000) }
# The request's parameters as FastCGI name-value pairs.
my $pairs = join '', map { nv_len(length $_).nv_len(length $params{$_}).$_.$params{$_} } sort keys %params;
# The service may answer without reading all of a large body (a refused
# upload) and close the connection, as it may with httpd: then the rest of the
# body isn't sent, and its answer is still read.
local $SIG{PIPE} = 'IGNORE';
# The whole request: BEGIN_REQUEST (role 1 = responder), the parameters, an
# empty PARAMS record (end of parameters), the body as STDIN records, and an
# empty STDIN record (end of body).
my $request = record(1, pack('nCx5', 1, 0))          # BEGIN_REQUEST, responder, close after
    .record(4, $pairs).record(4, '')                   # PARAMS
    .(length $body ? record(5, $body) : '').record(5, ''); # STDIN
while (length $request) {
    my $n = syswrite($sock, $request, 1048576) // last;
    substr($request, 0, $n, '');
}

binmode STDOUT;
# Read the answer: STDOUT records (type 6) are the response, printed as the
# CGI script would; STDERR records (7) go to standard error; END_REQUEST (3)
# means it's complete.
my $buffer = '';
while (1) {
    while (length $buffer >= 8) {
        my ($v, $type, $id, $len, $pad) = unpack('CCnnC', $buffer);
        last if length $buffer < 8 + $len + $pad;
        my $content = substr($buffer, 8, $len);
        substr($buffer, 0, 8 + $len + $pad, '');
        if ($type == 6) { print $content }
        elsif ($type == 7) { print STDERR $content }
        elsif ($type == 3) { exit 0 }
    }
    my $n = sysread($sock, my $more, 65536);
    unless ($n) {
        # The test's folder is usually gone by the time anyone looks, so show the log's end.
        my $log = '';
        if (open my $lf, '<', "$root/fcgi-test.log") { local $/; $log = <$lf> // ''; $log = substr($log, -2000) if length $log > 2000 }
        die "FastCGI connection closed before the service answered. The service's log ends:\n$log\n";
    }
    $buffer .= $more;
}