mirror of
https://github.com/moparisthebest/wget
synced 2024-07-03 16:38:41 -04:00
Stylistic and idiomatic cleanups in Perl tests
This commit is contained in:
parent
9f83e0e13c
commit
8078adee7f
@ -1,3 +1,12 @@
|
|||||||
|
2014-10-31 Pär Karlsson <feinorgh@gmail.com>
|
||||||
|
* WgetTests.pm: Proper conditional operators, tidied up code, idiomatic
|
||||||
|
improvements as per modern Perl best practices.
|
||||||
|
* WgetFeature.pm: Tidied up code, idiomatic improvements for readability
|
||||||
|
* FTPServer.pm: Tidied up code (perltidy -gnu)
|
||||||
|
* FTPTest.pm: Likewise
|
||||||
|
* HTTPServer.pm: Likewise
|
||||||
|
* HTTPTest.pm: Likewise
|
||||||
|
|
||||||
2014-10-30 Mike Frysinger <vapier@gentoo.org>
|
2014-10-30 Mike Frysinger <vapier@gentoo.org>
|
||||||
|
|
||||||
* WgetFeature.pm: fix skip exit code to 77
|
* WgetFeature.pm: fix skip exit code to 77
|
||||||
|
@ -28,34 +28,31 @@ my %_connection_states = (
|
|||||||
# subset of FTP commands supported by these server and the respective
|
# subset of FTP commands supported by these server and the respective
|
||||||
# connection states in which they are allowed
|
# connection states in which they are allowed
|
||||||
my %_commands = (
|
my %_commands = (
|
||||||
|
|
||||||
# Standard commands from RFC 959.
|
# Standard commands from RFC 959.
|
||||||
'CWD' => $_connection_states{LOGGEDIN} |
|
'CWD' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS},
|
||||||
$_connection_states{TWOSOCKS},
|
|
||||||
# 'EPRT' => $_connection_states{LOGGEDIN},
|
# 'EPRT' => $_connection_states{LOGGEDIN},
|
||||||
# 'EPSV' => $_connection_states{LOGGEDIN},
|
# 'EPSV' => $_connection_states{LOGGEDIN},
|
||||||
'LIST' => $_connection_states{TWOSOCKS},
|
'LIST' => $_connection_states{TWOSOCKS},
|
||||||
|
|
||||||
# 'LPRT' => $_connection_states{LOGGEDIN},
|
# 'LPRT' => $_connection_states{LOGGEDIN},
|
||||||
# 'LPSV' => $_connection_states{LOGGEDIN},
|
# 'LPSV' => $_connection_states{LOGGEDIN},
|
||||||
'PASS' => $_connection_states{WAIT4PWD},
|
'PASS' => $_connection_states{WAIT4PWD},
|
||||||
'PASV' => $_connection_states{LOGGEDIN},
|
'PASV' => $_connection_states{LOGGEDIN},
|
||||||
'PORT' => $_connection_states{LOGGEDIN},
|
'PORT' => $_connection_states{LOGGEDIN},
|
||||||
'PWD' => $_connection_states{LOGGEDIN} |
|
'PWD' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS},
|
||||||
$_connection_states{TWOSOCKS},
|
'QUIT' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS},
|
||||||
'QUIT' => $_connection_states{LOGGEDIN} |
|
|
||||||
$_connection_states{TWOSOCKS},
|
|
||||||
'REST' => $_connection_states{TWOSOCKS},
|
'REST' => $_connection_states{TWOSOCKS},
|
||||||
'RETR' => $_connection_states{TWOSOCKS},
|
'RETR' => $_connection_states{TWOSOCKS},
|
||||||
'SYST' => $_connection_states{LOGGEDIN},
|
'SYST' => $_connection_states{LOGGEDIN},
|
||||||
'TYPE' => $_connection_states{LOGGEDIN} |
|
'TYPE' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS},
|
||||||
$_connection_states{TWOSOCKS},
|
|
||||||
'USER' => $_connection_states{NEWCONN},
|
'USER' => $_connection_states{NEWCONN},
|
||||||
|
|
||||||
# From ftpexts Internet Draft.
|
# From ftpexts Internet Draft.
|
||||||
'SIZE' => $_connection_states{LOGGEDIN} |
|
'SIZE' => $_connection_states{LOGGEDIN} | $_connection_states{TWOSOCKS},
|
||||||
$_connection_states{TWOSOCKS},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# COMMAND-HANDLING ROUTINES
|
# COMMAND-HANDLING ROUTINES
|
||||||
|
|
||||||
sub _CWD_command
|
sub _CWD_command
|
||||||
@ -67,7 +64,8 @@ sub _CWD_command
|
|||||||
my $new_path = FTPPaths::path_merge($conn->{'dir'}, $path);
|
my $new_path = FTPPaths::path_merge($conn->{'dir'}, $path);
|
||||||
|
|
||||||
# Split the path into its component parts and process each separately.
|
# Split the path into its component parts and process each separately.
|
||||||
if (! $paths->dir_exists($new_path)) {
|
if (!$paths->dir_exists($new_path))
|
||||||
|
{
|
||||||
print {$conn->{socket}} "550 Directory not found.\r\n";
|
print {$conn->{socket}} "550 Directory not found.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -81,10 +79,10 @@ sub _LIST_command
|
|||||||
my ($conn, $cmd, $path) = @_;
|
my ($conn, $cmd, $path) = @_;
|
||||||
my $paths = $conn->{'paths'};
|
my $paths = $conn->{'paths'};
|
||||||
|
|
||||||
my $ReturnEmptyList = ( $paths->GetBehavior('list_empty_if_list_a') &&
|
my $ReturnEmptyList =
|
||||||
$path eq '-a');
|
($paths->GetBehavior('list_empty_if_list_a') && $path eq '-a');
|
||||||
my $SkipHiddenFiles = ( $paths->GetBehavior('list_no_hidden_if_list') &&
|
my $SkipHiddenFiles =
|
||||||
( ! $path ) );
|
($paths->GetBehavior('list_no_hidden_if_list') && (!$path));
|
||||||
|
|
||||||
if ($paths->GetBehavior('list_fails_if_list_a') && $path eq '-a')
|
if ($paths->GetBehavior('list_fails_if_list_a') && $path eq '-a')
|
||||||
{
|
{
|
||||||
@ -92,7 +90,6 @@ sub _LIST_command
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!$paths->GetBehavior('list_dont_clean_path'))
|
if (!$paths->GetBehavior('list_dont_clean_path'))
|
||||||
{
|
{
|
||||||
# This is something of a hack. Some clients expect a Unix server
|
# This is something of a hack. Some clients expect a Unix server
|
||||||
@ -114,7 +111,8 @@ sub _LIST_command
|
|||||||
{
|
{
|
||||||
$dir = FTPPaths::path_merge($dir, $path);
|
$dir = FTPPaths::path_merge($dir, $path);
|
||||||
$listing = $paths->get_list($dir, $SkipHiddenFiles);
|
$listing = $paths->get_list($dir, $SkipHiddenFiles);
|
||||||
unless ($listing) {
|
unless ($listing)
|
||||||
|
{
|
||||||
print {$conn->{socket}} "550 File or directory not found.\r\n";
|
print {$conn->{socket}} "550 File or directory not found.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -126,24 +124,28 @@ sub _LIST_command
|
|||||||
|
|
||||||
# Open a path back to the client.
|
# Open a path back to the client.
|
||||||
my $sock = __open_data_connection($conn);
|
my $sock = __open_data_connection($conn);
|
||||||
unless ($sock) {
|
unless ($sock)
|
||||||
|
{
|
||||||
print {$conn->{socket}} "425 Can't open data connection.\r\n";
|
print {$conn->{socket}} "425 Can't open data connection.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$ReturnEmptyList)
|
if (!$ReturnEmptyList)
|
||||||
{
|
{
|
||||||
for my $item (@$listing) {
|
for my $item (@$listing)
|
||||||
|
{
|
||||||
print $sock "$item\r\n";
|
print $sock "$item\r\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unless ($sock->close) {
|
unless ($sock->close)
|
||||||
|
{
|
||||||
print {$conn->{socket}} "550 Error closing data connection: $!\r\n";
|
print {$conn->{socket}} "550 Error closing data connection: $!\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
print {$conn->{socket}} "226 Listing complete. Data connection has been closed.\r\n";
|
print {$conn->{socket}}
|
||||||
|
"226 Listing complete. Data connection has been closed.\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _PASS_command
|
sub _PASS_command
|
||||||
@ -155,10 +157,15 @@ sub _PASS_command
|
|||||||
print STDERR "switching to LOGGEDIN state\n" if $log;
|
print STDERR "switching to LOGGEDIN state\n" if $log;
|
||||||
$conn->{state} = $_connection_states{LOGGEDIN};
|
$conn->{state} = $_connection_states{LOGGEDIN};
|
||||||
|
|
||||||
if ($conn->{username} eq "anonymous") {
|
if ($conn->{username} eq "anonymous")
|
||||||
print {$conn->{socket}} "202 Anonymous user access is always granted.\r\n";
|
{
|
||||||
} else {
|
print {$conn->{socket}}
|
||||||
print {$conn->{socket}} "230 Authentication not implemented yet, access is always granted.\r\n";
|
"202 Anonymous user access is always granted.\r\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
print {$conn->{socket}}
|
||||||
|
"230 Authentication not implemented yet, access is always granted.\r\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,14 +175,17 @@ sub _PASV_command
|
|||||||
|
|
||||||
# Open a listening socket - but don't actually accept on it yet.
|
# Open a listening socket - but don't actually accept on it yet.
|
||||||
"0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
|
"0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
|
||||||
my $sock = IO::Socket::INET->new (LocalHost => '127.0.0.1',
|
my $sock = IO::Socket::INET->new(
|
||||||
|
LocalHost => '127.0.0.1',
|
||||||
LocalPort => '0',
|
LocalPort => '0',
|
||||||
Listen => 1,
|
Listen => 1,
|
||||||
Reuse => 1,
|
Reuse => 1,
|
||||||
Proto => 'tcp',
|
Proto => 'tcp',
|
||||||
Type => SOCK_STREAM);
|
Type => SOCK_STREAM
|
||||||
|
);
|
||||||
|
|
||||||
unless ($sock) {
|
unless ($sock)
|
||||||
|
{
|
||||||
# Return a code 550 here, even though this is not in the RFC. XXX
|
# Return a code 550 here, even though this is not in the RFC. XXX
|
||||||
print {$conn->{socket}} "550 Can't open a listening socket.\r\n";
|
print {$conn->{socket}} "550 Can't open a listening socket.\r\n";
|
||||||
return;
|
return;
|
||||||
@ -204,16 +214,24 @@ sub _PORT_command
|
|||||||
# The arguments to PORT are a1,a2,a3,a4,p1,p2 where a1 is the
|
# The arguments to PORT are a1,a2,a3,a4,p1,p2 where a1 is the
|
||||||
# most significant part of the address (eg. 127,0,0,1) and
|
# most significant part of the address (eg. 127,0,0,1) and
|
||||||
# p1 is the most significant part of the port.
|
# p1 is the most significant part of the port.
|
||||||
unless ($rest =~ /^\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})/) {
|
unless ($rest =~
|
||||||
|
/^\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})/
|
||||||
|
)
|
||||||
|
{
|
||||||
print {$conn->{socket}} "501 Syntax error in PORT command.\r\n";
|
print {$conn->{socket}} "501 Syntax error in PORT command.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check host address.
|
# Check host address.
|
||||||
unless ($1 > 0 && $1 < 224 &&
|
unless ( $1 > 0
|
||||||
$2 >= 0 && $2 < 256 &&
|
&& $1 < 224
|
||||||
$3 >= 0 && $3 < 256 &&
|
&& $2 >= 0
|
||||||
$4 >= 0 && $4 < 256) {
|
&& $2 < 256
|
||||||
|
&& $3 >= 0
|
||||||
|
&& $3 < 256
|
||||||
|
&& $4 >= 0
|
||||||
|
&& $4 < 256)
|
||||||
|
{
|
||||||
print {$conn->{socket}} "501 Invalid host address.\r\n";
|
print {$conn->{socket}} "501 Invalid host address.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -223,7 +241,8 @@ sub _PORT_command
|
|||||||
my $peerport = $5 * 256 + $6;
|
my $peerport = $5 * 256 + $6;
|
||||||
|
|
||||||
# Check port number.
|
# Check port number.
|
||||||
unless ($peerport > 0 && $peerport < 65536) {
|
unless ($peerport > 0 && $peerport < 65536)
|
||||||
|
{
|
||||||
print {$conn->{socket}} "501 Invalid port number.\r\n";
|
print {$conn->{socket}} "501 Invalid port number.\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,8 +272,10 @@ sub _REST_command
|
|||||||
{
|
{
|
||||||
my ($conn, $cmd, $restart_from) = @_;
|
my ($conn, $cmd, $restart_from) = @_;
|
||||||
|
|
||||||
unless ($restart_from =~ /^([1-9][0-9]*|0)$/) {
|
unless ($restart_from =~ /^([1-9][0-9]*|0)$/)
|
||||||
print {$conn->{socket}} "501 REST command needs a numeric argument.\r\n";
|
{
|
||||||
|
print {$conn->{socket}}
|
||||||
|
"501 REST command needs a numeric argument.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,19 +291,21 @@ sub _RETR_command
|
|||||||
$path = FTPPaths::path_merge($conn->{dir}, $path);
|
$path = FTPPaths::path_merge($conn->{dir}, $path);
|
||||||
my $info = $conn->{'paths'}->get_info($path);
|
my $info = $conn->{'paths'}->get_info($path);
|
||||||
|
|
||||||
unless ($info->{'_type'} eq 'f') {
|
unless ($info->{'_type'} eq 'f')
|
||||||
|
{
|
||||||
print {$conn->{socket}} "550 File not found.\r\n";
|
print {$conn->{socket}} "550 File not found.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
print {$conn->{socket}} "150 Opening " .
|
print {$conn->{socket}} "150 Opening "
|
||||||
($conn->{type} eq 'A' ? "ASCII mode" : "BINARY mode") .
|
. ($conn->{type} eq 'A' ? "ASCII mode" : "BINARY mode")
|
||||||
" data connection.\r\n";
|
. " data connection.\r\n";
|
||||||
|
|
||||||
# Open a path back to the client.
|
# Open a path back to the client.
|
||||||
my $sock = __open_data_connection($conn);
|
my $sock = __open_data_connection($conn);
|
||||||
|
|
||||||
unless ($sock) {
|
unless ($sock)
|
||||||
|
{
|
||||||
print {$conn->{socket}} "425 Can't open data connection.\r\n";
|
print {$conn->{socket}} "425 Can't open data connection.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -290,7 +313,8 @@ sub _RETR_command
|
|||||||
my $content = $info->{'content'};
|
my $content = $info->{'content'};
|
||||||
|
|
||||||
# Restart the connection from previous point?
|
# Restart the connection from previous point?
|
||||||
if ($conn->{restart}) {
|
if ($conn->{restart})
|
||||||
|
{
|
||||||
$content = substr($content, $conn->{restart});
|
$content = substr($content, $conn->{restart});
|
||||||
$conn->{restart} = 0;
|
$conn->{restart} = 0;
|
||||||
}
|
}
|
||||||
@ -315,9 +339,11 @@ sub _RETR_command
|
|||||||
$w = syswrite($sock, $buffer, $r - $n, $n);
|
$w = syswrite($sock, $buffer, $r - $n, $n);
|
||||||
|
|
||||||
# Cleanup and exit if there was an error.
|
# Cleanup and exit if there was an error.
|
||||||
unless (defined $w) {
|
unless (defined $w)
|
||||||
|
{
|
||||||
close $sock;
|
close $sock;
|
||||||
print {$conn->{socket}} "426 File retrieval error: $!. Data connection has been closed.\r\n";
|
print {$conn->{socket}}
|
||||||
|
"426 File retrieval error: $!. Data connection has been closed.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,25 +351,32 @@ sub _RETR_command
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Transfer aborted by client?
|
# Transfer aborted by client?
|
||||||
if ($GOT_SIGURG) {
|
if ($GOT_SIGURG)
|
||||||
|
{
|
||||||
$GOT_SIGURG = 0;
|
$GOT_SIGURG = 0;
|
||||||
close $sock;
|
close $sock;
|
||||||
print {$conn->{socket}} "426 Transfer aborted. Data connection closed.\r\n";
|
print {$conn->{socket}}
|
||||||
|
"426 Transfer aborted. Data connection closed.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$sent += $r;
|
$sent += $r;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Cleanup and exit if there was an error.
|
# Cleanup and exit if there was an error.
|
||||||
unless (defined $r) {
|
unless (defined $r)
|
||||||
|
{
|
||||||
close $sock;
|
close $sock;
|
||||||
print {$conn->{socket}} "426 File retrieval error: $!. Data connection has been closed.\r\n";
|
print {$conn->{socket}}
|
||||||
|
"426 File retrieval error: $!. Data connection has been closed.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else { # ASCII type.
|
}
|
||||||
|
else
|
||||||
|
{ # ASCII type.
|
||||||
# Copy data.
|
# Copy data.
|
||||||
my @lines = split /\r\n?|\n/, $content;
|
my @lines = split /\r\n?|\n/, $content;
|
||||||
for (@lines) {
|
for (@lines)
|
||||||
|
{
|
||||||
# Remove any native line endings.
|
# Remove any native line endings.
|
||||||
s/[\n\r]+$//;
|
s/[\n\r]+$//;
|
||||||
|
|
||||||
@ -354,21 +387,25 @@ sub _RETR_command
|
|||||||
print $sock "$_\r\n";
|
print $sock "$_\r\n";
|
||||||
|
|
||||||
# Transfer aborted by client?
|
# Transfer aborted by client?
|
||||||
if ($GOT_SIGURG) {
|
if ($GOT_SIGURG)
|
||||||
|
{
|
||||||
$GOT_SIGURG = 0;
|
$GOT_SIGURG = 0;
|
||||||
close $sock;
|
close $sock;
|
||||||
print {$conn->{socket}} "426 Transfer aborted. Data connection closed.\r\n";
|
print {$conn->{socket}}
|
||||||
|
"426 Transfer aborted. Data connection closed.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unless (close ($sock)) {
|
unless (close($sock))
|
||||||
|
{
|
||||||
print {$conn->{socket}} "550 File retrieval error: $!.\r\n";
|
print {$conn->{socket}} "550 File retrieval error: $!.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
print {$conn->{socket}} "226 File retrieval complete. Data connection has been closed.\r\n";
|
print {$conn->{socket}}
|
||||||
|
"226 File retrieval complete. Data connection has been closed.\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _SIZE_command
|
sub _SIZE_command
|
||||||
@ -377,13 +414,16 @@ sub _SIZE_command
|
|||||||
|
|
||||||
$path = FTPPaths::path_merge($conn->{dir}, $path);
|
$path = FTPPaths::path_merge($conn->{dir}, $path);
|
||||||
my $info = $conn->{'paths'}->get_info($path);
|
my $info = $conn->{'paths'}->get_info($path);
|
||||||
unless ($info) {
|
unless ($info)
|
||||||
|
{
|
||||||
print {$conn->{socket}} "550 File or directory not found.\r\n";
|
print {$conn->{socket}} "550 File or directory not found.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($info->{'_type'} eq 'd') {
|
if ($info->{'_type'} eq 'd')
|
||||||
print {$conn->{socket}} "550 SIZE command is not supported on directories.\r\n";
|
{
|
||||||
|
print {$conn->{socket}}
|
||||||
|
"550 SIZE command is not supported on directories.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -398,7 +438,8 @@ sub _SYST_command
|
|||||||
|
|
||||||
if ($conn->{'paths'}->GetBehavior('syst_response'))
|
if ($conn->{'paths'}->GetBehavior('syst_response'))
|
||||||
{
|
{
|
||||||
print {$conn->{socket}} $conn->{'paths'}->GetBehavior('syst_response') . "\r\n";
|
print {$conn->{socket}} $conn->{'paths'}->GetBehavior('syst_response')
|
||||||
|
. "\r\n";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -411,14 +452,22 @@ sub _TYPE_command
|
|||||||
my ($conn, $cmd, $type) = @_;
|
my ($conn, $cmd, $type) = @_;
|
||||||
|
|
||||||
# See RFC 959 section 5.3.2.
|
# See RFC 959 section 5.3.2.
|
||||||
if ($type =~ /^([AI])$/i) {
|
if ($type =~ /^([AI])$/i)
|
||||||
|
{
|
||||||
$conn->{type} = $1;
|
$conn->{type} = $1;
|
||||||
} elsif ($type =~ /^([AI])\sN$/i) {
|
}
|
||||||
|
elsif ($type =~ /^([AI])\sN$/i)
|
||||||
|
{
|
||||||
$conn->{type} = $1;
|
$conn->{type} = $1;
|
||||||
} elsif ($type =~ /^L\s8$/i) {
|
}
|
||||||
|
elsif ($type =~ /^L\s8$/i)
|
||||||
|
{
|
||||||
$conn->{type} = 'L8';
|
$conn->{type} = 'L8';
|
||||||
} else {
|
}
|
||||||
print {$conn->{socket}} "504 This server does not support TYPE $type.\r\n";
|
else
|
||||||
|
{
|
||||||
|
print {$conn->{socket}}
|
||||||
|
"504 This server does not support TYPE $type.\r\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -435,14 +484,16 @@ sub _USER_command
|
|||||||
print STDERR "switching to WAIT4PWD state\n" if $log;
|
print STDERR "switching to WAIT4PWD state\n" if $log;
|
||||||
$conn->{state} = $_connection_states{WAIT4PWD};
|
$conn->{state} = $_connection_states{WAIT4PWD};
|
||||||
|
|
||||||
if ($conn->{username} eq "anonymous") {
|
if ($conn->{username} eq "anonymous")
|
||||||
|
{
|
||||||
print {$conn->{socket}} "230 Anonymous user access granted.\r\n";
|
print {$conn->{socket}} "230 Anonymous user access granted.\r\n";
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
print {$conn->{socket}} "331 Password required.\r\n";
|
print {$conn->{socket}} "331 Password required.\r\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# HELPER ROUTINES
|
# HELPER ROUTINES
|
||||||
|
|
||||||
sub __open_data_connection
|
sub __open_data_connection
|
||||||
@ -451,23 +502,28 @@ sub __open_data_connection
|
|||||||
|
|
||||||
my $sock;
|
my $sock;
|
||||||
|
|
||||||
if ($conn->{passive}) {
|
if ($conn->{passive})
|
||||||
|
{
|
||||||
# Passive mode - wait for a connection from the client.
|
# Passive mode - wait for a connection from the client.
|
||||||
accept($sock, $conn->{passive_socket}) or return undef;
|
accept($sock, $conn->{passive_socket}) or return undef;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
# Active mode - connect back to the client.
|
# Active mode - connect back to the client.
|
||||||
"0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
|
"0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
|
||||||
$sock = IO::Socket::INET->new (LocalAddr => '127.0.0.1',
|
$sock = IO::Socket::INET->new(
|
||||||
|
LocalAddr => '127.0.0.1',
|
||||||
PeerAddr => $conn->{peeraddrstring},
|
PeerAddr => $conn->{peeraddrstring},
|
||||||
PeerPort => $conn->{peerport},
|
PeerPort => $conn->{peerport},
|
||||||
Proto => 'tcp',
|
Proto => 'tcp',
|
||||||
Type => SOCK_STREAM) or return undef;
|
Type => SOCK_STREAM
|
||||||
|
)
|
||||||
|
or return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $sock;
|
return $sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# FTPSERVER CLASS
|
# FTPSERVER CLASS
|
||||||
###########################################################################
|
###########################################################################
|
||||||
@ -494,34 +550,44 @@ sub __open_data_connection
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub new
|
||||||
sub new {
|
{
|
||||||
my ($caller, %args) = @_;
|
my ($caller, %args) = @_;
|
||||||
my $caller_is_obj = ref($caller);
|
my $caller_is_obj = ref($caller);
|
||||||
my $class = $caller_is_obj || $caller;
|
my $class = $caller_is_obj || $caller;
|
||||||
my $self = bless {}, $class;
|
my $self = bless {}, $class;
|
||||||
foreach my $attrname ($self->_standard_keys()) {
|
foreach my $attrname ($self->_standard_keys())
|
||||||
|
{
|
||||||
my ($argname) = ($attrname =~ /^_(.*)/);
|
my ($argname) = ($attrname =~ /^_(.*)/);
|
||||||
if (exists $args{$argname}) {
|
if (exists $args{$argname})
|
||||||
|
{
|
||||||
$self->{$attrname} = $args{$argname};
|
$self->{$attrname} = $args{$argname};
|
||||||
} elsif ($caller_is_obj) {
|
}
|
||||||
|
elsif ($caller_is_obj)
|
||||||
|
{
|
||||||
$self->{$attrname} = $caller->{$attrname};
|
$self->{$attrname} = $caller->{$attrname};
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$self->{$attrname} = $self->_default_for($attrname);
|
$self->{$attrname} = $self->_default_for($attrname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# create server socket
|
# create server socket
|
||||||
"0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
|
"0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
|
||||||
$self->{_server_sock}
|
$self->{_server_sock} =
|
||||||
= IO::Socket::INET->new (LocalHost => $self->{_localAddr},
|
IO::Socket::INET->new(
|
||||||
|
LocalHost => $self->{_localAddr},
|
||||||
LocalPort => $self->{_localPort},
|
LocalPort => $self->{_localPort},
|
||||||
Listen => 1,
|
Listen => 1,
|
||||||
Reuse => $self->{_reuseAddr},
|
Reuse => $self->{_reuseAddr},
|
||||||
Proto => 'tcp',
|
Proto => 'tcp',
|
||||||
Type => SOCK_STREAM)
|
Type => SOCK_STREAM
|
||||||
|
)
|
||||||
or die "bind: $!";
|
or die "bind: $!";
|
||||||
|
|
||||||
foreach my $file (keys %{$self->{_input}}) {
|
foreach my $file (keys %{$self->{_input}})
|
||||||
|
{
|
||||||
my $ref = \$self->{_input}{$file}{content};
|
my $ref = \$self->{_input}{$file}{content};
|
||||||
$$ref =~ s/{{port}}/$self->sockport/eg;
|
$$ref =~ s/{{port}}/$self->sockport/eg;
|
||||||
}
|
}
|
||||||
@ -529,7 +595,6 @@ sub new {
|
|||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sub run
|
sub run
|
||||||
{
|
{
|
||||||
my ($self, $synch_callback) = @_;
|
my ($self, $synch_callback) = @_;
|
||||||
@ -540,7 +605,8 @@ sub run
|
|||||||
|
|
||||||
# initialize command table
|
# initialize command table
|
||||||
my $command_table = {};
|
my $command_table = {};
|
||||||
foreach (keys %_commands) {
|
foreach (keys %_commands)
|
||||||
|
{
|
||||||
my $subname = "_${_}_command";
|
my $subname = "_${_}_command";
|
||||||
$command_table->{$_} = \&$subname;
|
$command_table->{$_} = \&$subname;
|
||||||
}
|
}
|
||||||
@ -548,7 +614,8 @@ sub run
|
|||||||
my $old_ils = $/;
|
my $old_ils = $/;
|
||||||
$/ = "\r\n";
|
$/ = "\r\n";
|
||||||
|
|
||||||
if (!$initialized) {
|
if (!$initialized)
|
||||||
|
{
|
||||||
$synch_callback->();
|
$synch_callback->();
|
||||||
$initialized = 1;
|
$initialized = 1;
|
||||||
}
|
}
|
||||||
@ -577,7 +644,8 @@ sub run
|
|||||||
# next;
|
# next;
|
||||||
# }
|
# }
|
||||||
|
|
||||||
if (1) { # Child process.
|
if (1)
|
||||||
|
{ # Child process.
|
||||||
|
|
||||||
# install signals
|
# install signals
|
||||||
$SIG{URG} = sub {
|
$SIG{URG} = sub {
|
||||||
@ -590,18 +658,18 @@ sub run
|
|||||||
};
|
};
|
||||||
|
|
||||||
$SIG{ALRM} = sub {
|
$SIG{ALRM} = sub {
|
||||||
print STDERR "Connection idle timeout expired. Closing server.\n";
|
print STDERR
|
||||||
|
"Connection idle timeout expired. Closing server.\n";
|
||||||
exit;
|
exit;
|
||||||
};
|
};
|
||||||
|
|
||||||
#$SIG{CHLD} = 'IGNORE';
|
#$SIG{CHLD} = 'IGNORE';
|
||||||
|
|
||||||
|
|
||||||
print STDERR "in child\n" if $log;
|
print STDERR "in child\n" if $log;
|
||||||
|
|
||||||
my $conn = {
|
my $conn = {
|
||||||
'paths' => FTPPaths->new($self->{'_input'},
|
'paths' =>
|
||||||
$self->{'_server_behavior'}),
|
FTPPaths->new($self->{'_input'}, $self->{'_server_behavior'}),
|
||||||
'socket' => $socket,
|
'socket' => $socket,
|
||||||
'state' => $_connection_states{NEWCONN},
|
'state' => $_connection_states{NEWCONN},
|
||||||
'dir' => '/',
|
'dir' => '/',
|
||||||
@ -610,10 +678,12 @@ sub run
|
|||||||
'rootdir' => $self->{_rootDir},
|
'rootdir' => $self->{_rootDir},
|
||||||
};
|
};
|
||||||
|
|
||||||
print {$conn->{socket}} "220 GNU Wget Testing FTP Server ready.\r\n";
|
print {$conn->{socket}}
|
||||||
|
"220 GNU Wget Testing FTP Server ready.\r\n";
|
||||||
|
|
||||||
# command handling loop
|
# command handling loop
|
||||||
for (;;) {
|
for (; ;)
|
||||||
|
{
|
||||||
print STDERR "waiting for request\n" if $log;
|
print STDERR "waiting for request\n" if $log;
|
||||||
|
|
||||||
last unless defined(my $req = <$socket>);
|
last unless defined(my $req = <$socket>);
|
||||||
@ -625,7 +695,8 @@ sub run
|
|||||||
|
|
||||||
# Get the command.
|
# Get the command.
|
||||||
# See also RFC 2640 section 3.1.
|
# See also RFC 2640 section 3.1.
|
||||||
unless ($req =~ m/^([A-Z]{3,4})\s?(.*)/i) {
|
unless ($req =~ m/^([A-Z]{3,4})\s?(.*)/i)
|
||||||
|
{
|
||||||
# badly formed command
|
# badly formed command
|
||||||
exit 0;
|
exit 0;
|
||||||
}
|
}
|
||||||
@ -640,25 +711,30 @@ sub run
|
|||||||
my ($cmd, $rest) = (uc $1, $2);
|
my ($cmd, $rest) = (uc $1, $2);
|
||||||
|
|
||||||
# Got a command which matches in the table?
|
# Got a command which matches in the table?
|
||||||
unless (exists $command_table->{$cmd}) {
|
unless (exists $command_table->{$cmd})
|
||||||
|
{
|
||||||
print {$conn->{socket}} "500 Unrecognized command.\r\n";
|
print {$conn->{socket}} "500 Unrecognized command.\r\n";
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Command requires user to be authenticated?
|
# Command requires user to be authenticated?
|
||||||
unless ($_commands{$cmd} | $conn->{state}) {
|
unless ($_commands{$cmd} | $conn->{state})
|
||||||
|
{
|
||||||
print {$conn->{socket}} "530 Not logged in.\r\n";
|
print {$conn->{socket}} "530 Not logged in.\r\n";
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Handle the QUIT command specially.
|
# Handle the QUIT command specially.
|
||||||
if ($cmd eq "QUIT") {
|
if ($cmd eq "QUIT")
|
||||||
print {$conn->{socket}} "221 Goodbye. Service closing connection.\r\n";
|
{
|
||||||
|
print {$conn->{socket}}
|
||||||
|
"221 Goodbye. Service closing connection.\r\n";
|
||||||
last;
|
last;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (defined($self->{_server_behavior}{fail_on_pasv})
|
if (defined($self->{_server_behavior}{fail_on_pasv})
|
||||||
&& $cmd eq 'PASV') {
|
&& $cmd eq 'PASV')
|
||||||
|
{
|
||||||
undef $self->{_server_behavior}{fail_on_pasv};
|
undef $self->{_server_behavior}{fail_on_pasv};
|
||||||
close $socket;
|
close $socket;
|
||||||
last;
|
last;
|
||||||
@ -667,7 +743,9 @@ sub run
|
|||||||
# Run the command.
|
# Run the command.
|
||||||
&{$command_table->{$cmd}}($conn, $cmd, $rest);
|
&{$command_table->{$cmd}}($conn, $cmd, $rest);
|
||||||
}
|
}
|
||||||
} else { # Father
|
}
|
||||||
|
else
|
||||||
|
{ # Father
|
||||||
close $socket;
|
close $socket;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -675,18 +753,19 @@ sub run
|
|||||||
$/ = $old_ils;
|
$/ = $old_ils;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub sockport {
|
sub sockport
|
||||||
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
return $self->{_server_sock}->sockport;
|
return $self->{_server_sock}->sockport;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
package FTPPaths;
|
package FTPPaths;
|
||||||
|
|
||||||
use POSIX qw(strftime);
|
use POSIX qw(strftime);
|
||||||
|
|
||||||
# not a method
|
# not a method
|
||||||
sub final_component {
|
sub final_component
|
||||||
|
{
|
||||||
my $path = shift;
|
my $path = shift;
|
||||||
|
|
||||||
$path =~ s|.*/||;
|
$path =~ s|.*/||;
|
||||||
@ -694,34 +773,49 @@ sub final_component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# not a method
|
# not a method
|
||||||
sub path_merge {
|
sub path_merge
|
||||||
my ($a, $b) = @_;
|
{
|
||||||
|
my ($path_a, $path_b) = @_;
|
||||||
|
|
||||||
return $a unless $b;
|
if (!$path_b)
|
||||||
|
{
|
||||||
if ($b =~ m.^/.) {
|
return $path_a;
|
||||||
$a = '';
|
|
||||||
$b =~ s.^/..;
|
|
||||||
}
|
}
|
||||||
$a =~ s./$..;
|
|
||||||
|
|
||||||
my @components = split('/', $b);
|
if ($path_b =~ m.^/.)
|
||||||
|
{
|
||||||
|
$path_a = '';
|
||||||
|
$path_b =~ s.^/..;
|
||||||
|
}
|
||||||
|
$path_a =~ s./$..;
|
||||||
|
|
||||||
foreach my $c (@components) {
|
my @components = split m{/}msx, $path_b;
|
||||||
if ($c =~ /^\.?$/) {
|
|
||||||
|
foreach my $c (@components)
|
||||||
|
{
|
||||||
|
if ($c =~ /^\.?$/)
|
||||||
|
{
|
||||||
next;
|
next;
|
||||||
} elsif ($c eq '..') {
|
}
|
||||||
next if $a eq '';
|
elsif ($c eq '..')
|
||||||
$a =~ s|/[^/]*$||;
|
{
|
||||||
} else {
|
if (!$path_a)
|
||||||
$a .= "/$c";
|
{
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
$path_a =~ s|/[^/]*$||;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$path_a .= "/$c";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $a;
|
return $path_a;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub new {
|
sub new
|
||||||
|
{
|
||||||
my ($this, @args) = @_;
|
my ($this, @args) = @_;
|
||||||
my $class = ref($this) || $this;
|
my $class = ref($this) || $this;
|
||||||
my $self = {};
|
my $self = {};
|
||||||
@ -730,19 +824,23 @@ sub new {
|
|||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub initialize {
|
sub initialize
|
||||||
|
{
|
||||||
my ($self, $urls, $behavior) = @_;
|
my ($self, $urls, $behavior) = @_;
|
||||||
my $paths = {_type => 'd'};
|
my $paths = {_type => 'd'};
|
||||||
|
|
||||||
# From a path like '/foo/bar/baz.txt', construct $paths such that
|
# From a path like '/foo/bar/baz.txt', construct $paths such that
|
||||||
# $paths->{'foo'}->{'bar'}->{'baz.txt'} is
|
# $paths->{'foo'}->{'bar'}->{'baz.txt'} is
|
||||||
# $urls->{'/foo/bar/baz.txt'}.
|
# $urls->{'/foo/bar/baz.txt'}.
|
||||||
for my $path (keys %$urls) {
|
for my $path (keys %$urls)
|
||||||
my @components = split('/', $path);
|
{
|
||||||
|
my @components = split m{/}msx, $path;
|
||||||
shift @components;
|
shift @components;
|
||||||
my $x = $paths;
|
my $x = $paths;
|
||||||
for my $c (@components) {
|
for my $c (@components)
|
||||||
unless (exists $x->{$c}) {
|
{
|
||||||
|
if (!exists $x->{$c})
|
||||||
|
{
|
||||||
$x->{$c} = {_type => 'd'};
|
$x->{$c} = {_type => 'd'};
|
||||||
}
|
}
|
||||||
$x = $x->{$c};
|
$x = $x->{$c};
|
||||||
@ -753,30 +851,38 @@ sub initialize {
|
|||||||
|
|
||||||
$self->{'_paths'} = $paths;
|
$self->{'_paths'} = $paths;
|
||||||
$self->{'_behavior'} = $behavior;
|
$self->{'_behavior'} = $behavior;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_info {
|
sub get_info
|
||||||
|
{
|
||||||
my ($self, $path, $node) = @_;
|
my ($self, $path, $node) = @_;
|
||||||
$node = $self->{'_paths'} unless $node;
|
$node = $self->{'_paths'} unless $node;
|
||||||
my @components = split('/', $path);
|
my @components = split('/', $path);
|
||||||
shift @components if @components && $components[0] eq '';
|
shift @components if @components && $components[0] eq '';
|
||||||
|
|
||||||
for my $c (@components) {
|
for my $c (@components)
|
||||||
if ($node->{'_type'} eq 'd') {
|
{
|
||||||
|
if ($node->{'_type'} eq 'd')
|
||||||
|
{
|
||||||
$node = $node->{$c};
|
$node = $node->{$c};
|
||||||
} else {
|
}
|
||||||
return undef;
|
else
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $node;
|
return $node;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub dir_exists {
|
sub dir_exists
|
||||||
|
{
|
||||||
my ($self, $path) = @_;
|
my ($self, $path) = @_;
|
||||||
return $self->exists($path, 'd');
|
return $self->path_exists($path, 'd');
|
||||||
}
|
}
|
||||||
|
|
||||||
sub exists {
|
sub path_exists
|
||||||
|
{
|
||||||
# type is optional, in which case we don't check it.
|
# type is optional, in which case we don't check it.
|
||||||
my ($self, $path, $type) = @_;
|
my ($self, $path, $type) = @_;
|
||||||
my $paths = $self->{'_paths'};
|
my $paths = $self->{'_paths'};
|
||||||
@ -788,21 +894,27 @@ sub exists {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _format_for_list {
|
sub _format_for_list
|
||||||
|
{
|
||||||
my ($self, $name, $info) = @_;
|
my ($self, $name, $info) = @_;
|
||||||
|
|
||||||
# XXX: mode should be specifyable as part of the node info.
|
# XXX: mode should be specifyable as part of the node info.
|
||||||
my $mode_str;
|
my $mode_str;
|
||||||
if ($info->{'_type'} eq 'd') {
|
if ($info->{'_type'} eq 'd')
|
||||||
|
{
|
||||||
$mode_str = 'dr-xr-xr-x';
|
$mode_str = 'dr-xr-xr-x';
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$mode_str = '-r--r--r--';
|
$mode_str = '-r--r--r--';
|
||||||
}
|
}
|
||||||
|
|
||||||
my $size = 0;
|
my $size = 0;
|
||||||
if ($info->{'_type'} eq 'f') {
|
if ($info->{'_type'} eq 'f')
|
||||||
|
{
|
||||||
$size = length $info->{'content'};
|
$size = length $info->{'content'};
|
||||||
if ($self->{'_behavior'}{'bad_list'}) {
|
if ($self->{'_behavior'}{'bad_list'})
|
||||||
|
{
|
||||||
$size = 0;
|
$size = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -810,20 +922,27 @@ sub _format_for_list {
|
|||||||
return "$mode_str 1 0 0 $size $date $name";
|
return "$mode_str 1 0 0 $size $date $name";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_list {
|
sub get_list
|
||||||
|
{
|
||||||
my ($self, $path, $no_hidden) = @_;
|
my ($self, $path, $no_hidden) = @_;
|
||||||
my $info = $self->get_info($path);
|
my $info = $self->get_info($path);
|
||||||
return undef unless defined $info;
|
if (!defined $info)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
my $list = [];
|
my $list = [];
|
||||||
|
|
||||||
if ($info->{'_type'} eq 'd') {
|
if ($info->{'_type'} eq 'd')
|
||||||
for my $item (keys %$info) {
|
{
|
||||||
|
for my $item (keys %$info)
|
||||||
|
{
|
||||||
next if $item =~ /^_/;
|
next if $item =~ /^_/;
|
||||||
|
|
||||||
# 2013-10-17 Andrea Urbani (matfanjol)
|
# 2013-10-17 Andrea Urbani (matfanjol)
|
||||||
# I skip the hidden files if requested
|
# I skip the hidden files if requested
|
||||||
if (($no_hidden) &&
|
if ( ($no_hidden)
|
||||||
(defined($info->{$item}->{'attr'})) &&
|
&& (defined($info->{$item}->{'attr'}))
|
||||||
(index($info->{$item}->{'attr'}, "H")>=0))
|
&& (index($info->{$item}->{'attr'}, "H") >= 0))
|
||||||
{
|
{
|
||||||
# This is an hidden file and I don't want to see it!
|
# This is an hidden file and I don't want to see it!
|
||||||
print STDERR "get_list: Skipped hidden file [$item]\n";
|
print STDERR "get_list: Skipped hidden file [$item]\n";
|
||||||
@ -833,7 +952,9 @@ sub get_list {
|
|||||||
push @$list, $self->_format_for_list($item, $info->{$item});
|
push @$list, $self->_format_for_list($item, $info->{$item});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
push @$list, $self->_format_for_list(final_component($path), $info);
|
push @$list, $self->_format_for_list(final_component($path), $info);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -858,7 +979,8 @@ sub get_list {
|
|||||||
# to the url files
|
# to the url files
|
||||||
# syst_response : if defined, its content is printed
|
# syst_response : if defined, its content is printed
|
||||||
# out as SYST response
|
# out as SYST response
|
||||||
sub GetBehavior {
|
sub GetBehavior
|
||||||
|
{
|
||||||
my ($self, $name) = @_;
|
my ($self, $name) = @_;
|
||||||
return $self->{'_behavior'}{$name};
|
return $self->{'_behavior'}{$name};
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ use WgetTests;
|
|||||||
our @ISA = qw(WgetTest);
|
our @ISA = qw(WgetTest);
|
||||||
my $VERSION = 0.01;
|
my $VERSION = 0.01;
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
my %_attr_data = ( # DEFAULT
|
my %_attr_data = ( # DEFAULT
|
||||||
);
|
);
|
||||||
@ -28,27 +27,30 @@ my $VERSION = 0.01;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _setup_server
|
||||||
sub _setup_server {
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
$self->{_server} = FTPServer->new (input => $self->{_input},
|
$self->{_server} = FTPServer->new(
|
||||||
server_behavior =>
|
input => $self->{_input},
|
||||||
$self->{_server_behavior},
|
server_behavior => $self->{_server_behavior},
|
||||||
LocalAddr => 'localhost',
|
LocalAddr => 'localhost',
|
||||||
ReuseAddr => 1,
|
ReuseAddr => 1,
|
||||||
rootDir => "$self->{_workdir}/$self->{_name}/input") or die "Cannot create server!!!";
|
rootDir => "$self->{_workdir}/$self->{_name}/input"
|
||||||
|
)
|
||||||
|
or die "Cannot create server!!!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _launch_server
|
||||||
sub _launch_server {
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $synch_func = shift;
|
my $synch_func = shift;
|
||||||
|
|
||||||
$self->{_server}->run($synch_func);
|
$self->{_server}->run($synch_func);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _substitute_port {
|
sub _substitute_port
|
||||||
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $ret = shift;
|
my $ret = shift;
|
||||||
$ret =~ s/{{port}}/$self->{_server}->sockport/eg;
|
$ret =~ s/{{port}}/$self->{_server}->sockport/eg;
|
||||||
|
@ -14,41 +14,52 @@ my $VERSION = 0.01;
|
|||||||
my $CRLF = "\015\012"; # "\r\n" is not portable
|
my $CRLF = "\015\012"; # "\r\n" is not portable
|
||||||
my $log = undef;
|
my $log = undef;
|
||||||
|
|
||||||
sub run {
|
sub run
|
||||||
|
{
|
||||||
my ($self, $urls, $synch_callback) = @_;
|
my ($self, $urls, $synch_callback) = @_;
|
||||||
my $initialized = 0;
|
my $initialized = 0;
|
||||||
|
|
||||||
while (1) {
|
while (1)
|
||||||
if (!$initialized) {
|
{
|
||||||
|
if (!$initialized)
|
||||||
|
{
|
||||||
$synch_callback->();
|
$synch_callback->();
|
||||||
$initialized = 1;
|
$initialized = 1;
|
||||||
}
|
}
|
||||||
my $con = $self->accept();
|
my $con = $self->accept();
|
||||||
print STDERR "Accepted a new connection\n" if $log;
|
print STDERR "Accepted a new connection\n" if $log;
|
||||||
while (my $req = $con->get_request) {
|
while (my $req = $con->get_request)
|
||||||
|
{
|
||||||
#my $url_path = $req->url->path;
|
#my $url_path = $req->url->path;
|
||||||
my $url_path = $req->url->as_string;
|
my $url_path = $req->url->as_string;
|
||||||
if ($url_path =~ m{/$}) { # append 'index.html'
|
if ($url_path =~ m{/$})
|
||||||
|
{ # append 'index.html'
|
||||||
$url_path .= 'index.html';
|
$url_path .= 'index.html';
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ($url_path =~ m{^/}) { # remove trailing '/'
|
#if ($url_path =~ m{^/}) { # remove trailing '/'
|
||||||
# $url_path = substr ($url_path, 1);
|
# $url_path = substr ($url_path, 1);
|
||||||
#}
|
#}
|
||||||
if ($log) {
|
if ($log)
|
||||||
|
{
|
||||||
print STDERR "Method: ", $req->method, "\n";
|
print STDERR "Method: ", $req->method, "\n";
|
||||||
print STDERR "Path: ", $url_path, "\n";
|
print STDERR "Path: ", $url_path, "\n";
|
||||||
print STDERR "Available URLs: ", "\n";
|
print STDERR "Available URLs: ", "\n";
|
||||||
foreach my $key (keys %$urls) {
|
foreach my $key (keys %$urls)
|
||||||
|
{
|
||||||
print STDERR $key, "\n";
|
print STDERR $key, "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (exists($urls->{$url_path})) {
|
if (exists($urls->{$url_path}))
|
||||||
|
{
|
||||||
print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
|
print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
|
||||||
next unless ($req->method eq "HEAD" || $req->method eq "GET");
|
next unless ($req->method eq "HEAD" || $req->method eq "GET");
|
||||||
|
|
||||||
my $url_rec = $urls->{$url_path};
|
my $url_rec = $urls->{$url_path};
|
||||||
$self->send_response($req, $url_rec, $con);
|
$self->send_response($req, $url_rec, $con);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
|
print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
|
||||||
$con->send_error($HTTP::Status::RC_FORBIDDEN);
|
$con->send_error($HTTP::Status::RC_FORBIDDEN);
|
||||||
last;
|
last;
|
||||||
@ -59,53 +70,65 @@ sub run {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub send_response {
|
sub send_response
|
||||||
|
{
|
||||||
my ($self, $req, $url_rec, $con) = @_;
|
my ($self, $req, $url_rec, $con) = @_;
|
||||||
|
|
||||||
# create response
|
# create response
|
||||||
my ($code, $msg, $headers);
|
my ($code, $msg, $headers);
|
||||||
my $send_content = ($req->method eq "GET");
|
my $send_content = ($req->method eq "GET");
|
||||||
if (exists $url_rec->{'auth_method'}) {
|
if (exists $url_rec->{'auth_method'})
|
||||||
|
{
|
||||||
($send_content, $code, $msg, $headers) =
|
($send_content, $code, $msg, $headers) =
|
||||||
$self->handle_auth($req, $url_rec);
|
$self->handle_auth($req, $url_rec);
|
||||||
} elsif (!$self->verify_request_headers ($req, $url_rec)) {
|
}
|
||||||
|
elsif (!$self->verify_request_headers($req, $url_rec))
|
||||||
|
{
|
||||||
($send_content, $code, $msg, $headers) =
|
($send_content, $code, $msg, $headers) =
|
||||||
('', 400, 'Mismatch on expected headers', {});
|
('', 400, 'Mismatch on expected headers', {});
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
($code, $msg) = @{$url_rec}{'code', 'msg'};
|
($code, $msg) = @{$url_rec}{'code', 'msg'};
|
||||||
$headers = $url_rec->{headers};
|
$headers = $url_rec->{headers};
|
||||||
}
|
}
|
||||||
my $resp = HTTP::Response->new($code, $msg);
|
my $resp = HTTP::Response->new($code, $msg);
|
||||||
print STDERR "HTTP::Response: \n", $resp->as_string if $log;
|
print STDERR "HTTP::Response: \n", $resp->as_string if $log;
|
||||||
|
|
||||||
while (my ($name, $value) = each %{$headers}) {
|
while (my ($name, $value) = each %{$headers})
|
||||||
|
{
|
||||||
# print STDERR "setting header: $name = $value\n";
|
# print STDERR "setting header: $name = $value\n";
|
||||||
$resp->header($name => $value);
|
$resp->header($name => $value);
|
||||||
}
|
}
|
||||||
print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
|
print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
|
||||||
|
|
||||||
if ($send_content) {
|
if ($send_content)
|
||||||
|
{
|
||||||
my $content = $url_rec->{content};
|
my $content = $url_rec->{content};
|
||||||
if (exists($url_rec->{headers}{"Content-Length"})) {
|
if (exists($url_rec->{headers}{"Content-Length"}))
|
||||||
|
{
|
||||||
# Content-Length and length($content) don't match
|
# Content-Length and length($content) don't match
|
||||||
# manually prepare the HTTP response
|
# manually prepare the HTTP response
|
||||||
$con->send_basic_header($url_rec->{code}, $resp->message, $resp->protocol);
|
$con->send_basic_header($url_rec->{code}, $resp->message,
|
||||||
|
$resp->protocol);
|
||||||
print $con $resp->headers_as_string($CRLF);
|
print $con $resp->headers_as_string($CRLF);
|
||||||
print $con $CRLF;
|
print $con $CRLF;
|
||||||
print $con $content;
|
print $con $content;
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
if ($req->header("Range") && !$url_rec->{'force_code'}) {
|
if ($req->header("Range") && !$url_rec->{'force_code'})
|
||||||
|
{
|
||||||
$req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
|
$req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
|
||||||
my $content_len = length($content);
|
my $content_len = length($content);
|
||||||
my $start = $1 ? $1 : 0;
|
my $start = $1 ? $1 : 0;
|
||||||
my $end = $2 ? $2 : ($content_len - 1);
|
my $end = $2 ? $2 : ($content_len - 1);
|
||||||
my $len = $2 ? ($2 - $start) : ($content_len - $start);
|
my $len = $2 ? ($2 - $start) : ($content_len - $start);
|
||||||
if ($len > 0) {
|
if ($len > 0)
|
||||||
|
{
|
||||||
$resp->header("Accept-Ranges" => "bytes");
|
$resp->header("Accept-Ranges" => "bytes");
|
||||||
$resp->header("Content-Length" => $len);
|
$resp->header("Content-Length" => $len);
|
||||||
$resp->header("Content-Range"
|
$resp->header(
|
||||||
=> "bytes $start-$end/$content_len");
|
"Content-Range" => "bytes $start-$end/$content_len");
|
||||||
$resp->header("Keep-Alive" => "timeout=15, max=100");
|
$resp->header("Keep-Alive" => "timeout=15, max=100");
|
||||||
$resp->header("Connection" => "Keep-Alive");
|
$resp->header("Connection" => "Keep-Alive");
|
||||||
$con->send_basic_header(206,
|
$con->send_basic_header(206,
|
||||||
@ -113,7 +136,9 @@ sub send_response {
|
|||||||
print $con $resp->headers_as_string($CRLF);
|
print $con $resp->headers_as_string($CRLF);
|
||||||
print $con $CRLF;
|
print $con $CRLF;
|
||||||
print $con substr($content, $start, $len);
|
print $con substr($content, $start, $len);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$con->send_basic_header(416, "Range Not Satisfiable",
|
$con->send_basic_header(416, "Range Not Satisfiable",
|
||||||
$resp->protocol);
|
$resp->protocol);
|
||||||
$resp->header("Keep-Alive" => "timeout=15, max=100");
|
$resp->header("Keep-Alive" => "timeout=15, max=100");
|
||||||
@ -122,10 +147,12 @@ sub send_response {
|
|||||||
}
|
}
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
# fill in content
|
# fill in content
|
||||||
$content = $self->_substitute_port($content) if defined $content;
|
$content = $self->_substitute_port($content) if defined $content;
|
||||||
$resp->content($content);
|
$resp->content($content);
|
||||||
print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
|
print STDERR "HTTP::Response with content: \n", $resp->as_string
|
||||||
|
if $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
$con->send_response($resp);
|
$con->send_response($resp);
|
||||||
@ -134,60 +161,81 @@ sub send_response {
|
|||||||
|
|
||||||
# Generates appropriate response content based on the authentication
|
# Generates appropriate response content based on the authentication
|
||||||
# status of the URL.
|
# status of the URL.
|
||||||
sub handle_auth {
|
sub handle_auth
|
||||||
|
{
|
||||||
my ($self, $req, $url_rec) = @_;
|
my ($self, $req, $url_rec) = @_;
|
||||||
my ($send_content, $code, $msg, $headers);
|
my ($send_content, $code, $msg, $headers);
|
||||||
|
|
||||||
# Catch failure to set code, msg:
|
# Catch failure to set code, msg:
|
||||||
$code = 500;
|
$code = 500;
|
||||||
$msg = "Didn't set response code in handle_auth";
|
$msg = "Didn't set response code in handle_auth";
|
||||||
|
|
||||||
# Most cases, we don't want to send content.
|
# Most cases, we don't want to send content.
|
||||||
$send_content = 0;
|
$send_content = 0;
|
||||||
|
|
||||||
# Initialize headers
|
# Initialize headers
|
||||||
$headers = {};
|
$headers = {};
|
||||||
my $authhdr = $req->header('Authorization');
|
my $authhdr = $req->header('Authorization');
|
||||||
|
|
||||||
# Have we sent the challenge yet?
|
# Have we sent the challenge yet?
|
||||||
unless ($url_rec->{auth_challenged} || $url_rec->{auth_no_challenge}) {
|
unless ($url_rec->{auth_challenged} || $url_rec->{auth_no_challenge})
|
||||||
|
{
|
||||||
# Since we haven't challenged yet, we'd better not
|
# Since we haven't challenged yet, we'd better not
|
||||||
# have received authentication (for our testing purposes).
|
# have received authentication (for our testing purposes).
|
||||||
if ($authhdr) {
|
if ($authhdr)
|
||||||
|
{
|
||||||
$code = 400;
|
$code = 400;
|
||||||
$msg = "You sent auth before I sent challenge";
|
$msg = "You sent auth before I sent challenge";
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
# Send challenge
|
# Send challenge
|
||||||
$code = 401;
|
$code = 401;
|
||||||
$msg = "Authorization Required";
|
$msg = "Authorization Required";
|
||||||
$headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'}
|
$headers->{'WWW-Authenticate'} =
|
||||||
. " realm=\"wget-test\"";
|
$url_rec->{'auth_method'} . " realm=\"wget-test\"";
|
||||||
$url_rec->{auth_challenged} = 1;
|
$url_rec->{auth_challenged} = 1;
|
||||||
}
|
}
|
||||||
} elsif (!defined($authhdr)) {
|
}
|
||||||
|
elsif (!defined($authhdr))
|
||||||
|
{
|
||||||
# We've sent the challenge; we should have received valid
|
# We've sent the challenge; we should have received valid
|
||||||
# authentication with this one. A normal server would just
|
# authentication with this one. A normal server would just
|
||||||
# resend the challenge; but since this is a test, wget just
|
# resend the challenge; but since this is a test, wget just
|
||||||
# failed it.
|
# failed it.
|
||||||
$code = 400;
|
$code = 400;
|
||||||
$msg = "You didn't send auth after I sent challenge";
|
$msg = "You didn't send auth after I sent challenge";
|
||||||
if ($url_rec->{auth_no_challenge}) {
|
if ($url_rec->{auth_no_challenge})
|
||||||
$msg = "--auth-no-challenge but no auth sent."
|
{
|
||||||
|
$msg = "--auth-no-challenge but no auth sent.";
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
my ($sent_method) = ($authhdr =~ /^(\S+)/g);
|
my ($sent_method) = ($authhdr =~ /^(\S+)/g);
|
||||||
unless ($sent_method eq $url_rec->{'auth_method'}) {
|
unless ($sent_method eq $url_rec->{'auth_method'})
|
||||||
|
{
|
||||||
# Not the authorization type we were expecting.
|
# Not the authorization type we were expecting.
|
||||||
$code = 400;
|
$code = 400;
|
||||||
$msg = "Expected auth type $url_rec->{'auth_method'} but got "
|
$msg = "Expected auth type $url_rec->{'auth_method'} but got "
|
||||||
. "$sent_method";
|
. "$sent_method";
|
||||||
} elsif (($sent_method eq 'Digest'
|
}
|
||||||
&& &verify_auth_digest($authhdr, $url_rec, \$msg))
|
elsif (
|
||||||
||
|
(
|
||||||
($sent_method eq 'Basic'
|
$sent_method eq 'Digest'
|
||||||
&& &verify_auth_basic($authhdr, $url_rec, \$msg))) {
|
&& &verify_auth_digest($authhdr, $url_rec, \$msg)
|
||||||
|
)
|
||||||
|
|| ( $sent_method eq 'Basic'
|
||||||
|
&& &verify_auth_basic($authhdr, $url_rec, \$msg))
|
||||||
|
)
|
||||||
|
{
|
||||||
# SUCCESSFUL AUTH: send expected message, headers, content.
|
# SUCCESSFUL AUTH: send expected message, headers, content.
|
||||||
($code, $msg) = @{$url_rec}{'code', 'msg'};
|
($code, $msg) = @{$url_rec}{'code', 'msg'};
|
||||||
$headers = $url_rec->{headers};
|
$headers = $url_rec->{headers};
|
||||||
$send_content = 1;
|
$send_content = 1;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$code = 400;
|
$code = 400;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -195,43 +243,58 @@ sub handle_auth {
|
|||||||
return ($send_content, $code, $msg, $headers);
|
return ($send_content, $code, $msg, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub verify_auth_digest {
|
sub verify_auth_digest
|
||||||
|
{
|
||||||
return undef; # Not yet implemented.
|
return undef; # Not yet implemented.
|
||||||
}
|
}
|
||||||
|
|
||||||
sub verify_auth_basic {
|
sub verify_auth_basic
|
||||||
|
{
|
||||||
require MIME::Base64;
|
require MIME::Base64;
|
||||||
my ($authhdr, $url_rec, $msgref) = @_;
|
my ($authhdr, $url_rec, $msgref) = @_;
|
||||||
my $expected = MIME::Base64::encode_base64($url_rec->{'user'} . ':'
|
my $expected =
|
||||||
. $url_rec->{'passwd'}, '');
|
MIME::Base64::encode_base64(
|
||||||
|
$url_rec->{'user'} . ':' . $url_rec->{'passwd'},
|
||||||
|
'');
|
||||||
my ($got) = $authhdr =~ /^Basic (.*)$/;
|
my ($got) = $authhdr =~ /^Basic (.*)$/;
|
||||||
if ($got eq $expected) {
|
if ($got eq $expected)
|
||||||
|
{
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$$msgref = "Wanted ${expected} got ${got}";
|
$$msgref = "Wanted ${expected} got ${got}";
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub verify_request_headers {
|
sub verify_request_headers
|
||||||
|
{
|
||||||
my ($self, $req, $url_rec) = @_;
|
my ($self, $req, $url_rec) = @_;
|
||||||
|
|
||||||
return 1 unless exists $url_rec->{'request_headers'};
|
return 1 unless exists $url_rec->{'request_headers'};
|
||||||
for my $hdrname (keys %{$url_rec->{'request_headers'}}) {
|
for my $hdrname (keys %{$url_rec->{'request_headers'}})
|
||||||
|
{
|
||||||
my $must_not_match;
|
my $must_not_match;
|
||||||
my $ehdr = $url_rec->{'request_headers'}{$hdrname};
|
my $ehdr = $url_rec->{'request_headers'}{$hdrname};
|
||||||
if ($must_not_match = ($hdrname =~ /^!(\w+)/)) {
|
if ($must_not_match = ($hdrname =~ /^!(\w+)/))
|
||||||
|
{
|
||||||
$hdrname = $1;
|
$hdrname = $1;
|
||||||
}
|
}
|
||||||
my $rhdr = $req->header($hdrname);
|
my $rhdr = $req->header($hdrname);
|
||||||
if ($must_not_match) {
|
if ($must_not_match)
|
||||||
if (defined $rhdr && $rhdr =~ $ehdr) {
|
{
|
||||||
|
if (defined $rhdr && $rhdr =~ $ehdr)
|
||||||
|
{
|
||||||
$rhdr = '' unless defined $rhdr;
|
$rhdr = '' unless defined $rhdr;
|
||||||
print STDERR "\n*** Match forbidden $hdrname: $rhdr =~ $ehdr\n";
|
print STDERR "\n*** Match forbidden $hdrname: $rhdr =~ $ehdr\n";
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
unless (defined $rhdr && $rhdr =~ $ehdr) {
|
else
|
||||||
|
{
|
||||||
|
unless (defined $rhdr && $rhdr =~ $ehdr)
|
||||||
|
{
|
||||||
$rhdr = '' unless defined $rhdr;
|
$rhdr = '' unless defined $rhdr;
|
||||||
print STDERR "\n*** Mismatch on $hdrname: $rhdr =~ $ehdr\n";
|
print STDERR "\n*** Mismatch on $hdrname: $rhdr =~ $ehdr\n";
|
||||||
return undef;
|
return undef;
|
||||||
@ -242,7 +305,8 @@ sub verify_request_headers {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _substitute_port {
|
sub _substitute_port
|
||||||
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $ret = shift;
|
my $ret = shift;
|
||||||
$ret =~ s/{{port}}/$self->sockport/eg;
|
$ret =~ s/{{port}}/$self->sockport/eg;
|
||||||
|
@ -9,7 +9,6 @@ use WgetTests;
|
|||||||
our @ISA = qw(WgetTest);
|
our @ISA = qw(WgetTest);
|
||||||
my $VERSION = 0.01;
|
my $VERSION = 0.01;
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
my %_attr_data = ( # DEFAULT
|
my %_attr_data = ( # DEFAULT
|
||||||
);
|
);
|
||||||
@ -28,23 +27,24 @@ my $VERSION = 0.01;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _setup_server
|
||||||
sub _setup_server {
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
$self->{_server} = HTTPServer->new(LocalAddr => 'localhost',
|
$self->{_server} = HTTPServer->new(LocalAddr => 'localhost',
|
||||||
ReuseAddr => 1)
|
ReuseAddr => 1)
|
||||||
or die "Cannot create server!!!";
|
or die "Cannot create server!!!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _launch_server
|
||||||
sub _launch_server {
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $synch_func = shift;
|
my $synch_func = shift;
|
||||||
|
|
||||||
$self->{_server}->run($self->{_input}, $synch_func);
|
$self->{_server}->run($self->{_input}, $synch_func);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _substitute_port {
|
sub _substitute_port
|
||||||
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $ret = shift;
|
my $ret = shift;
|
||||||
$ret =~ s/{{port}}/$self->{_server}->sockport/eg;
|
$ret =~ s/{{port}}/$self->{_server}->sockport/eg;
|
||||||
|
@ -3,25 +3,40 @@ package WgetFeature;
|
|||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
|
our $VERSION = 0.01;
|
||||||
|
|
||||||
|
use Carp;
|
||||||
|
use English qw(-no_match_vars);
|
||||||
use WgetTests;
|
use WgetTests;
|
||||||
|
|
||||||
our %skip_messages;
|
our %SKIP_MESSAGES;
|
||||||
require 'WgetFeature.cfg';
|
{
|
||||||
|
open my $fh, '<', 'WgetFeature.cfg'
|
||||||
|
or croak "Cannot open 'WgetFeature.cfg': $ERRNO";
|
||||||
|
my @lines = <$fh>;
|
||||||
|
close $fh or carp "Cannot close 'WgetFeature.cfg': $ERRNO";
|
||||||
|
eval {
|
||||||
|
@lines;
|
||||||
|
1;
|
||||||
|
} or carp "Cannot eval 'WgetFeature.cfg': $ERRNO";
|
||||||
|
}
|
||||||
|
|
||||||
sub import
|
sub import
|
||||||
{
|
{
|
||||||
my ($class, $feature) = @_;
|
my ($class, $feature) = @_;
|
||||||
|
|
||||||
my $output = `$WgetTest::WGETPATH --version`;
|
my $output = `$WgetTest::WGETPATH --version`;
|
||||||
my ($list) = $output =~ /^([\+\-]\S+(?:\s+[\+\-]\S+)+)/m;
|
my ($list) = $output =~ m/^([+-]\S+(?:\s+[+-]\S+)+)/msx;
|
||||||
my %have_features = map {
|
my %have_features;
|
||||||
my $feature = $_;
|
for my $f (split m/\s+/msx, $list)
|
||||||
$feature =~ s/^.//;
|
{
|
||||||
($feature, /^\+/ ? 1 : 0);
|
my $feat = $f;
|
||||||
} split /\s+/, $list;
|
$feat =~ s/^.//msx;
|
||||||
|
$have_features{$feat} = $f =~ m/^[+]/msx ? 1 : 0;
|
||||||
unless ($have_features{$feature}) {
|
}
|
||||||
print $skip_messages{$feature}, "\n";
|
if (!$have_features{$feature})
|
||||||
|
{
|
||||||
|
print "$SKIP_MESSAGES{$feature}\n";
|
||||||
exit 77; # skip
|
exit 77; # skip
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,30 @@
|
|||||||
package WgetTest;
|
package WgetTest;
|
||||||
$VERSION = 0.01;
|
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
|
our $VERSION = 0.01;
|
||||||
|
|
||||||
|
use Carp;
|
||||||
use Cwd;
|
use Cwd;
|
||||||
|
use English qw(-no_match_vars);
|
||||||
use File::Path;
|
use File::Path;
|
||||||
|
use IO::Handle;
|
||||||
use POSIX qw(locale_h);
|
use POSIX qw(locale_h);
|
||||||
use locale;
|
use locale;
|
||||||
|
|
||||||
our $WGETPATH = "../src/wget";
|
our $WGETPATH = '../src/wget';
|
||||||
|
|
||||||
my @unexpected_downloads = ();
|
my @unexpected_downloads = ();
|
||||||
|
|
||||||
{
|
{
|
||||||
my %_attr_data = ( # DEFAULT
|
my %_attr_data = ( # DEFAULT
|
||||||
_cmdline => "",
|
_cmdline => q{},
|
||||||
_workdir => Cwd::getcwd(),
|
_workdir => Cwd::getcwd(),
|
||||||
_errcode => 0,
|
_errcode => 0,
|
||||||
_existing => {},
|
_existing => {},
|
||||||
_input => {},
|
_input => {},
|
||||||
_name => $0,
|
_name => $PROGRAM_NAME,
|
||||||
_output => {},
|
_output => {},
|
||||||
_server_behavior => {},
|
_server_behavior => {},
|
||||||
);
|
);
|
||||||
@ -28,56 +32,70 @@ my @unexpected_downloads = ();
|
|||||||
sub _default_for
|
sub _default_for
|
||||||
{
|
{
|
||||||
my ($self, $attr) = @_;
|
my ($self, $attr) = @_;
|
||||||
$_attr_data{$attr};
|
return $_attr_data{$attr};
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _standard_keys
|
sub _standard_keys
|
||||||
{
|
{
|
||||||
keys %_attr_data;
|
return keys %_attr_data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub new
|
||||||
sub new {
|
{
|
||||||
my ($caller, %args) = @_;
|
my ($caller, %args) = @_;
|
||||||
my $caller_is_obj = ref($caller);
|
my $caller_is_obj = ref $caller;
|
||||||
my $class = $caller_is_obj || $caller;
|
my $class = $caller_is_obj || $caller;
|
||||||
|
|
||||||
#print STDERR "class = ", $class, "\n";
|
#print STDERR "class = ", $class, "\n";
|
||||||
#print STDERR "_attr_data {workdir} = ", $WgetTest::_attr_data{_workdir}, "\n";
|
#print STDERR "_attr_data {workdir} = ", $WgetTest::_attr_data{_workdir}, "\n";
|
||||||
my $self = bless {}, $class;
|
my $self = bless {}, $class;
|
||||||
foreach my $attrname ($self->_standard_keys()) {
|
for my $attrname ($self->_standard_keys())
|
||||||
|
{
|
||||||
|
|
||||||
#print STDERR "attrname = ", $attrname, " value = ";
|
#print STDERR "attrname = ", $attrname, " value = ";
|
||||||
my ($argname) = ($attrname =~ /^_(.*)/);
|
my ($argname) = ($attrname =~ m/^_(.*)/msx);
|
||||||
if (exists $args{$argname}) {
|
if (exists $args{$argname})
|
||||||
|
{
|
||||||
|
|
||||||
#printf STDERR "Setting up $attrname\n";
|
#printf STDERR "Setting up $attrname\n";
|
||||||
$self->{$attrname} = $args{$argname};
|
$self->{$attrname} = $args{$argname};
|
||||||
} elsif ($caller_is_obj) {
|
}
|
||||||
|
elsif ($caller_is_obj)
|
||||||
|
{
|
||||||
|
|
||||||
#printf STDERR "Copying $attrname\n";
|
#printf STDERR "Copying $attrname\n";
|
||||||
$self->{$attrname} = $caller->{$attrname};
|
$self->{$attrname} = $caller->{$attrname};
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
#printf STDERR "Using default for $attrname\n";
|
#printf STDERR "Using default for $attrname\n";
|
||||||
$self->{$attrname} = $self->_default_for($attrname);
|
$self->{$attrname} = $self->_default_for($attrname);
|
||||||
}
|
}
|
||||||
|
|
||||||
#print STDERR $attrname, '=', $self->{$attrname}, "\n";
|
#print STDERR $attrname, '=', $self->{$attrname}, "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
#printf STDERR "_workdir default = ", $self->_default_for("_workdir");
|
#printf STDERR "_workdir default = ", $self->_default_for("_workdir");
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub run
|
||||||
sub run {
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $result_message = "Test successful.\n";
|
my $result_message = "Test successful.\n";
|
||||||
my $errcode;
|
my $errcode;
|
||||||
|
|
||||||
$self->{_name} =~ s{.*/}{}; # remove path
|
$self->{_name} =~ s{.*/}{}msx; # remove path
|
||||||
$self->{_name} =~ s{\.[^.]+$}{}; # remove extension
|
$self->{_name} =~ s{[.][^.]+$}{}msx; # remove extension
|
||||||
printf "Running test $self->{_name}\n";
|
printf "Running test $self->{_name}\n";
|
||||||
|
|
||||||
# Setup
|
# Setup
|
||||||
my $new_result = $self->_setup();
|
my $new_result = $self->_setup();
|
||||||
chdir ("$self->{_workdir}/$self->{_name}/input");
|
chdir "$self->{_workdir}/$self->{_name}/input"
|
||||||
if (defined $new_result) {
|
or carp "Could not chdir to input directory: $ERRNO";
|
||||||
|
if (defined $new_result)
|
||||||
|
{
|
||||||
$result_message = $new_result;
|
$result_message = $new_result;
|
||||||
$errcode = 1;
|
$errcode = 1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -87,115 +105,143 @@ sub run {
|
|||||||
my $pid = $self->_fork_and_launch_server();
|
my $pid = $self->_fork_and_launch_server();
|
||||||
|
|
||||||
# Call wget
|
# Call wget
|
||||||
chdir ("$self->{_workdir}/$self->{_name}/output");
|
chdir "$self->{_workdir}/$self->{_name}/output"
|
||||||
|
or carp "Could not chdir to output directory: $ERRNO";
|
||||||
|
|
||||||
my $cmdline = $self->{_cmdline};
|
my $cmdline = $self->{_cmdline};
|
||||||
$cmdline = $self->_substitute_port($cmdline);
|
$cmdline = $self->_substitute_port($cmdline);
|
||||||
$cmdline = ($cmdline =~ m{^/.*}) ? $cmdline : "$self->{_workdir}/$cmdline";
|
$cmdline =
|
||||||
|
($cmdline =~ m{^/.*}msx) ? $cmdline : "$self->{_workdir}/$cmdline";
|
||||||
|
|
||||||
my $valgrind = $ENV{VALGRIND_TESTS};
|
my $valgrind = $ENV{VALGRIND_TESTS};
|
||||||
if (!defined $valgrind || $valgrind == "" || $valgrind == "0") {
|
if (!defined $valgrind || $valgrind eq q{} || $valgrind == 0)
|
||||||
|
{
|
||||||
|
|
||||||
# Valgrind not requested - leave $cmdline as it is
|
# Valgrind not requested - leave $cmdline as it is
|
||||||
} elsif ($valgrind == "1") {
|
}
|
||||||
$cmdline = "valgrind --error-exitcode=301 --leak-check=yes --track-origins=yes " . $cmdline;
|
elsif ($valgrind == 1)
|
||||||
} else {
|
{
|
||||||
$cmdline = $valgrind . " " . $cmdline;
|
$cmdline =
|
||||||
|
'valgrind --error-exitcode=301 --leak-check=yes --track-origins=yes '
|
||||||
|
. $cmdline;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$cmdline = "$valgrind $cmdline";
|
||||||
}
|
}
|
||||||
|
|
||||||
print "Calling $cmdline\n";
|
print "Calling $cmdline\n";
|
||||||
$errcode = system($cmdline);
|
$errcode = system $cmdline;
|
||||||
$errcode >>= 8; # XXX: should handle abnormal error codes.
|
$errcode >>= 8; # XXX: should handle abnormal error codes.
|
||||||
|
|
||||||
# Shutdown server
|
# Shutdown server
|
||||||
# if we didn't explicitely kill the server, we would have to call
|
# if we didn't explicitely kill the server, we would have to call
|
||||||
# waitpid ($pid, 0) here in order to wait for the child process to
|
# waitpid ($pid, 0) here in order to wait for the child process to
|
||||||
# terminate
|
# terminate
|
||||||
kill ('TERM', $pid);
|
kill 'TERM', $pid;
|
||||||
|
|
||||||
# Verify download
|
# Verify download
|
||||||
unless ($errcode == $self->{_errcode}) {
|
if ($errcode != $self->{_errcode})
|
||||||
$result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
|
{
|
||||||
goto cleanup;
|
$result_message =
|
||||||
|
"Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
|
||||||
|
goto CLEANUP;
|
||||||
}
|
}
|
||||||
my $error_str;
|
my $error_str;
|
||||||
if ($error_str = $self->_verify_download()) {
|
if ($error_str = $self->_verify_download())
|
||||||
|
{
|
||||||
$result_message = $error_str;
|
$result_message = $error_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup:
|
CLEANUP:
|
||||||
$self->_cleanup();
|
$self->_cleanup();
|
||||||
|
|
||||||
print $result_message;
|
print $result_message;
|
||||||
return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
|
return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _setup
|
||||||
sub _setup {
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
#print $self->{_name}, "\n";
|
chdir $self->{_workdir}
|
||||||
chdir ($self->{_workdir});
|
or carp "Could not chdir into $self->{_workdir}: $ERRNO";
|
||||||
|
|
||||||
# Create temporary directory
|
# Create temporary directory
|
||||||
mkdir ($self->{_name});
|
mkdir $self->{_name} or carp "Could not mkdir '$self->{_name}': $ERRNO";
|
||||||
chdir ($self->{_name});
|
chdir $self->{_name}
|
||||||
mkdir ("input");
|
or carp "Could not chdir into '$self->{_name}': $ERRNO";
|
||||||
mkdir ("output");
|
mkdir 'input' or carp "Could not mkdir 'input' $ERRNO";
|
||||||
|
mkdir 'output' or carp "Could not mkdir 'output': $ERRNO";
|
||||||
|
|
||||||
# Setup existing files
|
# Setup existing files
|
||||||
chdir ("output");
|
chdir 'output' or carp "Could not chdir into 'output': $ERRNO";
|
||||||
foreach my $filename (keys %{$self->{_existing}}) {
|
for my $filename (keys %{$self->{_existing}})
|
||||||
open (FILE, ">$filename")
|
{
|
||||||
|
open my $fh, '>', $filename
|
||||||
or return "Test failed: cannot open pre-existing file $filename\n";
|
or return "Test failed: cannot open pre-existing file $filename\n";
|
||||||
|
|
||||||
my $file = $self->{_existing}->{$filename};
|
my $file = $self->{_existing}->{$filename};
|
||||||
print FILE $file->{content}
|
print {$fh} $file->{content}
|
||||||
or return "Test failed: cannot write pre-existing file $filename\n";
|
or return "Test failed: cannot write pre-existing file $filename\n";
|
||||||
|
|
||||||
close (FILE);
|
close $fh or carp $ERRNO;
|
||||||
|
|
||||||
if (exists($file->{timestamp})) {
|
if (exists($file->{timestamp}))
|
||||||
|
{
|
||||||
utime $file->{timestamp}, $file->{timestamp}, $filename
|
utime $file->{timestamp}, $file->{timestamp}, $filename
|
||||||
or return "Test failed: cannot set timestamp on pre-existing file $filename\n";
|
or return
|
||||||
|
"Test failed: cannot set timestamp on pre-existing file $filename\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
chdir ("../input");
|
chdir '../input' or carp "Cannot chdir into '../input': $ERRNO";
|
||||||
$self->_setup_server();
|
$self->_setup_server();
|
||||||
|
|
||||||
chdir ($self->{_workdir});
|
chdir $self->{_workdir}
|
||||||
|
or carp "Cannot chdir into '$self->{_workdir}': $ERRNO";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _cleanup
|
||||||
sub _cleanup {
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
chdir ($self->{_workdir});
|
chdir $self->{_workdir}
|
||||||
File::Path::rmtree ($self->{_name}) unless $ENV{WGET_TEST_NO_CLEANUP};
|
or carp "Could not chdir into '$self->{_workdir}': $ERRNO";
|
||||||
|
if (!$ENV{WGET_TEST_NO_CLEANUP})
|
||||||
|
{
|
||||||
|
File::Path::rmtree($self->{_name});
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
# not a method
|
# not a method
|
||||||
sub quotechar {
|
sub quotechar
|
||||||
my $c = ord( shift );
|
{
|
||||||
if ($c >= 0x7 && $c <= 0xD) {
|
my $c = ord shift;
|
||||||
return '\\' . qw(a b t n v f r)[$c - 0x7];
|
if ($c >= 0x7 && $c <= 0xD)
|
||||||
} else {
|
{
|
||||||
return sprintf('\\x%02x', $c);
|
return q{\\} . qw(a b t n v f r) [$c - 0x7];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return sprintf '\\x%02x', $c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# not a method
|
# not a method
|
||||||
sub _show_diff {
|
sub _show_diff
|
||||||
|
{
|
||||||
|
my ($expected, $actual) = @_;
|
||||||
my $SNIPPET_SIZE = 10;
|
my $SNIPPET_SIZE = 10;
|
||||||
|
|
||||||
my ($expected, $actual) = @_;
|
my $str = q{};
|
||||||
|
|
||||||
my $str = '';
|
|
||||||
my $explen = length $expected;
|
my $explen = length $expected;
|
||||||
my $actlen = length $actual;
|
my $actlen = length $actual;
|
||||||
|
|
||||||
if ($explen != $actlen) {
|
if ($explen != $actlen)
|
||||||
|
{
|
||||||
$str .= "Sizes don't match: expected = $explen, actual = $actlen\n";
|
$str .= "Sizes don't match: expected = $explen, actual = $actlen\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,24 +249,31 @@ sub _show_diff {
|
|||||||
my $line = 1;
|
my $line = 1;
|
||||||
my $col = 1;
|
my $col = 1;
|
||||||
my $i;
|
my $i;
|
||||||
for ($i=0; $i != $min; ++$i) {
|
|
||||||
last if substr($expected, $i, 1) ne substr($actual, $i, 1);
|
# for ($i=0; $i != $min; ++$i) {
|
||||||
if (substr($expected, $i, 1) eq '\n') {
|
for my $i (0 .. $min - 1)
|
||||||
|
{
|
||||||
|
last if substr($expected, $i, 1) ne substr $actual, $i, 1;
|
||||||
|
if (substr($expected, $i, 1) eq q{\n})
|
||||||
|
{
|
||||||
$line++;
|
$line++;
|
||||||
$col = 0;
|
$col = 0;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$col++;
|
$col++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
my $snip_start = $i - ($SNIPPET_SIZE / 2);
|
my $snip_start = $i - ($SNIPPET_SIZE / 2);
|
||||||
if ($snip_start < 0) {
|
if ($snip_start < 0)
|
||||||
|
{
|
||||||
$SNIPPET_SIZE += $snip_start; # Take it from the end.
|
$SNIPPET_SIZE += $snip_start; # Take it from the end.
|
||||||
$snip_start = 0;
|
$snip_start = 0;
|
||||||
}
|
}
|
||||||
my $exp_snip = substr($expected, $snip_start, $SNIPPET_SIZE);
|
my $exp_snip = substr $expected, $snip_start, $SNIPPET_SIZE;
|
||||||
my $act_snip = substr($actual, $snip_start, $SNIPPET_SIZE);
|
my $act_snip = substr $actual, $snip_start, $SNIPPET_SIZE;
|
||||||
$exp_snip =~s/[^[:print:]]/ quotechar($&) /ge;
|
$exp_snip =~ s/[^[:print:]]/ quotechar($&) /gemsx;
|
||||||
$act_snip =~s/[^[:print:]]/ quotechar($&) /ge;
|
$act_snip =~ s/[^[:print:]]/ quotechar($&) /gemsx;
|
||||||
$str .= "Mismatch at line $line, col $col:\n";
|
$str .= "Mismatch at line $line, col $col:\n";
|
||||||
$str .= " $exp_snip\n";
|
$str .= " $exp_snip\n";
|
||||||
$str .= " $act_snip\n";
|
$str .= " $act_snip\n";
|
||||||
@ -228,102 +281,138 @@ sub _show_diff {
|
|||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _verify_download {
|
sub _verify_download
|
||||||
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
chdir ("$self->{_workdir}/$self->{_name}/output");
|
chdir "$self->{_workdir}/$self->{_name}/output"
|
||||||
|
or carp "Could not chdir into output directory: $ERRNO";
|
||||||
|
|
||||||
# use slurp mode to read file content
|
# use slurp mode to read file content
|
||||||
my $old_input_record_separator = $/;
|
my $old_input_record_separator = $INPUT_RECORD_SEPARATOR;
|
||||||
undef $/;
|
local $INPUT_RECORD_SEPARATOR = undef;
|
||||||
|
|
||||||
while (my ($filename, $filedata) = each %{$self->{_output}}) {
|
while (my ($filename, $filedata) = each %{$self->{_output}})
|
||||||
open (FILE, $filename)
|
{
|
||||||
|
open my $fh, '<', $filename
|
||||||
or return "Test failed: file $filename not downloaded\n";
|
or return "Test failed: file $filename not downloaded\n";
|
||||||
|
|
||||||
my $content = <FILE>;
|
my $content = <$fh>;
|
||||||
|
|
||||||
|
close $fh or carp $ERRNO;
|
||||||
|
|
||||||
my $expected_content = $filedata->{'content'};
|
my $expected_content = $filedata->{'content'};
|
||||||
$expected_content = $self->_substitute_port($expected_content);
|
$expected_content = $self->_substitute_port($expected_content);
|
||||||
unless ($content eq $expected_content) {
|
if ($content ne $expected_content)
|
||||||
|
{
|
||||||
return "Test failed: wrong content for file $filename\n"
|
return "Test failed: wrong content for file $filename\n"
|
||||||
. _show_diff($expected_content, $content);
|
. _show_diff($expected_content, $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exists($filedata->{'timestamp'})) {
|
if (exists($filedata->{'timestamp'}))
|
||||||
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
|
{
|
||||||
$atime, $mtime, $ctime, $blksize, $blocks) = stat FILE;
|
my (
|
||||||
|
$dev, $ino, $mode, $nlink, $uid,
|
||||||
|
$gid, $rdev, $size, $atime, $mtime,
|
||||||
|
$ctime, $blksize, $blocks
|
||||||
|
)
|
||||||
|
= stat $filename;
|
||||||
|
|
||||||
$mtime == $filedata->{'timestamp'}
|
$mtime == $filedata->{'timestamp'}
|
||||||
or return "Test failed: wrong timestamp for file $filename\n";
|
or return "Test failed: wrong timestamp for file $filename\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
close (FILE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$/ = $old_input_record_separator;
|
local $INPUT_RECORD_SEPARATOR = $old_input_record_separator;
|
||||||
|
|
||||||
# make sure no unexpected files were downloaded
|
# make sure no unexpected files were downloaded
|
||||||
chdir ("$self->{_workdir}/$self->{_name}/output");
|
chdir "$self->{_workdir}/$self->{_name}/output"
|
||||||
|
or carp "Could not change into output directory: $ERRNO";
|
||||||
|
|
||||||
__dir_walk('.',
|
__dir_walk(
|
||||||
sub { push @unexpected_downloads,
|
q{.},
|
||||||
$_[0] unless (exists $self->{_output}{$_[0]} || $self->{_existing}{$_[0]}) },
|
sub {
|
||||||
sub { shift; return @_ } );
|
if (!(exists $self->{_output}{$_[0]} || $self->{_existing}{$_[0]}))
|
||||||
if (@unexpected_downloads) {
|
{
|
||||||
return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n";
|
push @unexpected_downloads, $_[0];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
sub { shift; return @_ }
|
||||||
|
);
|
||||||
|
if (@unexpected_downloads)
|
||||||
|
{
|
||||||
|
return 'Test failed: unexpected downloaded files [' . join ', ',
|
||||||
|
@unexpected_downloads . "]\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return q{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub __dir_walk
|
||||||
sub __dir_walk {
|
{
|
||||||
my ($top, $filefunc, $dirfunc) = @_;
|
my ($top, $filefunc, $dirfunc) = @_;
|
||||||
|
|
||||||
my $DIR;
|
my $DIR;
|
||||||
|
|
||||||
if (-d $top) {
|
if (-d $top)
|
||||||
|
{
|
||||||
my $file;
|
my $file;
|
||||||
unless (opendir $DIR, $top) {
|
if (!opendir $DIR, $top)
|
||||||
warn "Couldn't open directory $DIR: $!; skipping.\n";
|
{
|
||||||
|
warn "Couldn't open directory $DIR: $ERRNO; skipping.\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
my @results;
|
my @results;
|
||||||
while ($file = readdir $DIR) {
|
while ($file = readdir $DIR)
|
||||||
next if $file eq '.' || $file eq '..';
|
{
|
||||||
my $nextdir = $top eq '.' ? $file : "$top/$file";
|
next if $file eq q{.} || $file eq q{..};
|
||||||
|
my $nextdir = $top eq q{.} ? $file : "$top/$file";
|
||||||
push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
|
push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dirfunc ? $dirfunc->($top, @results) : ();
|
return $dirfunc ? $dirfunc->($top, @results) : ();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
return $filefunc ? $filefunc->($top) : ();
|
return $filefunc ? $filefunc->($top) : ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sub _fork_and_launch_server
|
sub _fork_and_launch_server
|
||||||
{
|
{
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!";
|
pipe FROM_CHILD, TO_PARENT or croak 'Cannot create pipe!';
|
||||||
select((select(TO_PARENT), $| = 1)[0]);
|
TO_PARENT->autoflush();
|
||||||
|
|
||||||
|
my $pid = fork;
|
||||||
|
if ($pid < 0)
|
||||||
|
{
|
||||||
|
carp 'Cannot fork';
|
||||||
|
}
|
||||||
|
elsif ($pid == 0)
|
||||||
|
{
|
||||||
|
|
||||||
my $pid = fork();
|
|
||||||
if ($pid < 0) {
|
|
||||||
die "Cannot fork";
|
|
||||||
} elsif ($pid == 0) {
|
|
||||||
# child
|
# child
|
||||||
close FROM_CHILD;
|
close FROM_CHILD or carp $ERRNO;
|
||||||
|
|
||||||
# FTP Server has to start with english locale due to use of strftime month names in LIST command
|
# FTP Server has to start with english locale due to use of strftime month names in LIST command
|
||||||
setlocale(LC_ALL,"C");
|
setlocale(LC_ALL, 'C');
|
||||||
$self->_launch_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT });
|
$self->_launch_server(
|
||||||
} else {
|
sub {
|
||||||
|
print {*TO_PARENT} "SYNC\n";
|
||||||
|
close TO_PARENT or carp $ERRNO;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
# father
|
# father
|
||||||
close TO_PARENT;
|
close TO_PARENT or carp $ERRNO;
|
||||||
chomp(my $line = <FROM_CHILD>);
|
chomp(my $line = <FROM_CHILD>);
|
||||||
close FROM_CHILD;
|
close FROM_CHILD or carp $ERRNO;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $pid;
|
return $pid;
|
||||||
|
Loading…
Reference in New Issue
Block a user