3 # This file is part of the dgit test suite.
5 # Copyright (C)2004-2015 Best Practical Solutions, LLC
6 # Copyright (C)2019 Ian Jackson
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
19 # invocation protocol:
21 # ftpmasterapi-static-server >port-file tests/tmp/$thing/aq
23 # Will write the allocated port number to port-file.
24 # Then we fork and the parent exits 0.
25 # If port-file is unlinked, we exit.
30 our ($webroot) = @ARGV;
33 sub stat_type_check () {
34 die "[$port, $webroot] stdout not ta plain file"
38 stat STDOUT or die $!;
41 sub start_polling_fstat () {
43 stat STDOUT or die $!;
44 my $nlink = (stat _)[3];
46 stat_type_check(); # doesn't seem possible to fail but check anyway
55 use Socket qw(AF_INET SOCK_STREAM);
56 use Socket qw(AF_INET SOCK_STREAM unpack_sockaddr_in);
59 use base qw(HTTP::Server::Simple::CGI);
60 use HTTP::Server::Simple::Static;
63 my ($self, $cgi) = @_;
65 if (!$self->serve_static($cgi, $::webroot)) {
66 print "HTTP/1.0 404 Not found\r\n";
68 $cgi->start_html('Not found'),
69 $cgi->h1('Not found'),
74 sub port () { return 0; }
76 sub after_setup_listener () {
77 my $sn = getsockname HTTP::Server::Simple::HTTPDaemon or die $!;
78 ($main::port,) = unpack_sockaddr_in $sn;
79 print main::STDOUT $port, "\n" or die $!;
80 flush main::STDOUT or die $!;
81 my $c = fork // die $!;
83 ::main::start_polling_fstat();
88 our $server = ServerClass->new();