chiark / gitweb /
http[s] tests: More-or-less functional ftpmaster http server
[dgit.git] / tests / ftpmasterapi-static-server
index a58a8e73f0c65971324a1659d705f4d6aed6192f..cedc6f7cc580f93ba8197a2530bc691551f1a3de 100755 (executable)
 # GNU General Public License for more details.
 #
 #
-# invocation:
+# invocation protocol:
 #
-#   < tests/tmp/$test/some-file \
-#   ftpmasterapi-static-server \
-#    tests/tmp/
+#   ftpmasterapi-static-server >port-file tests/tmp/$thing/aq
+#
+# Will write the allocated port number to port-file.
+# Then we fork and the parent exits 0.
+# If port-file is unlinked, we exit.
 
 use strict;
 use IO::Handle;
 
-$SIG{ALRM} = sub { print STDERR "y\n"; };
+our ($webroot) = @ARGV;
+our $port = '';
+
+sub stat_type_check () {
+    die "[$port, $webroot] stdout not ta plain file"
+       unless -f _;
+}
 
-alarm(5);
+stat STDOUT or die $!;
+stat_type_check();
+
+sub start_polling_fstat () {
+    $SIG{ALRM} = sub {
+       stat STDOUT or die $!;
+       my $nlink = (stat _)[3];
+       exit 0 unless $nlink;
+       stat_type_check(); # doesn't seem possible to fail but check anyway
+       alarm(1);
+    };
+    alarm(1);
+}
 
-           package MyServer;
+package ServerClass;
 
 use strict;
 use Socket qw(AF_INET SOCK_STREAM);
 use Socket qw(AF_INET SOCK_STREAM unpack_sockaddr_in);
 use IO::Handle;
-use Data::Dumper;
 
-           use base qw(HTTP::Server::Simple::CGI);
-           use HTTP::Server::Simple::Static;
+use base qw(HTTP::Server::Simple::CGI);
+use HTTP::Server::Simple::Static;
 
-           my $webroot = '/var/www';
+sub handle_request {
+    my ($self, $cgi) = @_;
 
-           sub handle_request {
-               my ( $self, $cgi ) = @_;
-
-               if ( !$self->serve_static( $cgi, $webroot ) ) {
-                   print "HTTP/1.0 404 Not found\r\n";
-                   print $cgi->header,
-                         $cgi->start_html('Not found'),
-                         $cgi->h1('Not found'),
-                         $cgi->end_html;
-               }
-           }
+    if (!$self->serve_static($cgi, $::webroot)) {
+       print "HTTP/1.0 404 Not found\r\n";
+       print $cgi->header,
+           $cgi->start_html('Not found'),
+           $cgi->h1('Not found'),
+           $cgi->end_html;
+    }
+}
 
 sub port () { return 0; }
 
-sub xsetup_listener () 
-{
-my $self=shift;
-print STDERR "foo!", $self->stdio_handle(), "\n";
-my $sock = new IO::Handle;
-socket $sock, AF_INET, SOCK_STREAM, 0 or die $!;
-#$self->stdio_handle($sock);
-}
-
 sub after_setup_listener () {
-my $x = getsockname HTTP::Server::Simple::HTTPDaemon or die $!;
-print STDERR Dumper(unpack_sockaddr_in $x);
+    my $sn = getsockname HTTP::Server::Simple::HTTPDaemon or die $!;
+    ($main::port,) = unpack_sockaddr_in $sn;
+    print main::STDOUT $port, "\n" or die $!;
+    flush main::STDOUT or die $!;
+    my $c = fork // die $!;
+    exit 0 if $c;
+    ::main::start_polling_fstat();
 }
 
-           package main;
+package main;
 
-           my $server = MyServer->new();
-           $server->run();
+our $server = ServerClass->new();
+$server->run();