summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Jackson <ijackson@chiark.greenend.org.uk>2019-07-25 02:54:59 +0100
committerIan Jackson <ijackson@chiark.greenend.org.uk>2019-08-11 00:09:27 +0100
commit8ab2d39e45255429ddef54407c7b74a0b1eda65b (patch)
tree7672ab2086e428060d2a3b3053463e17a0873ec1
parent083b1fd54624a7b3fc580fc595b46a958dd726b0 (diff)
http[s] tests: More-or-less functional ftpmaster http server
Not used anywhere yet. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
-rwxr-xr-xtests/ftpmasterapi-static-server85
1 files changed, 49 insertions, 36 deletions
diff --git a/tests/ftpmasterapi-static-server b/tests/ftpmasterapi-static-server
index a58a8e7..cedc6f7 100755
--- a/tests/ftpmasterapi-static-server
+++ b/tests/ftpmasterapi-static-server
@@ -16,61 +16,74 @@
# 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();