]> sjero.net Git - wget/blob - tests/FTPServer.pm
"LIST" or "LIST -a" ftp command according to the remote system
[wget] / tests / FTPServer.pm
1 # Part of this code was borrowed from Richard Jones's Net::FTPServer
2 # http://www.annexia.org/freeware/netftpserver
3
4 package FTPServer;
5
6 use strict;
7 use warnings;
8
9 use Cwd;
10 use Socket;
11 use IO::Socket::INET;
12 use IO::Seekable;
13 use POSIX qw(strftime);
14
15 my $log        = undef;
16 my $GOT_SIGURG = 0;
17
18 # CONSTANTS
19
20 # connection states
21 my %_connection_states = (
22     'NEWCONN'  => 0x01,
23     'WAIT4PWD' => 0x02,
24     'LOGGEDIN' => 0x04,
25     'TWOSOCKS' => 0x08,
26 );
27
28 # subset of FTP commands supported by these server and the respective
29 # connection states in which they are allowed
30 my %_commands = (
31     # Standard commands from RFC 959.
32     'CWD'  => $_connection_states{LOGGEDIN} |
33               $_connection_states{TWOSOCKS},
34 #   'EPRT' => $_connection_states{LOGGEDIN},
35 #   'EPSV' => $_connection_states{LOGGEDIN},
36     'LIST' => $_connection_states{TWOSOCKS},
37 #   'LPRT' => $_connection_states{LOGGEDIN},
38 #   'LPSV' => $_connection_states{LOGGEDIN},
39     'PASS' => $_connection_states{WAIT4PWD},
40     'PASV' => $_connection_states{LOGGEDIN},
41     'PORT' => $_connection_states{LOGGEDIN},
42     'PWD'  => $_connection_states{LOGGEDIN} |
43               $_connection_states{TWOSOCKS},
44     'QUIT' => $_connection_states{LOGGEDIN} |
45               $_connection_states{TWOSOCKS},
46     'REST' => $_connection_states{TWOSOCKS},
47     'RETR' => $_connection_states{TWOSOCKS},
48     'SYST' => $_connection_states{LOGGEDIN},
49     'TYPE' => $_connection_states{LOGGEDIN} |
50               $_connection_states{TWOSOCKS},
51     'USER' => $_connection_states{NEWCONN},
52     # From ftpexts Internet Draft.
53     'SIZE' => $_connection_states{LOGGEDIN} |
54               $_connection_states{TWOSOCKS},
55 );
56
57
58
59 # COMMAND-HANDLING ROUTINES
60
61 sub _CWD_command
62 {
63     my ($conn, $cmd, $path) = @_;
64     my $paths = $conn->{'paths'};
65
66     local $_;
67     my $new_path = FTPPaths::path_merge($conn->{'dir'}, $path);
68
69     # Split the path into its component parts and process each separately.
70     if (! $paths->dir_exists($new_path)) {
71         print {$conn->{socket}} "550 Directory not found.\r\n";
72         return;
73     }
74
75     $conn->{'dir'} = $new_path;
76     print {$conn->{socket}} "200 directory changed to $new_path.\r\n";
77 }
78
79 sub _LIST_command
80 {
81     my ($conn, $cmd, $path) = @_;
82     my $paths = $conn->{'paths'};
83
84     my $ReturnEmptyList = ( $paths->GetBehavior('list_empty_if_list_a') &&
85                             $path eq '-a');
86     my $SkipHiddenFiles = ( $paths->GetBehavior('list_no_hidden_if_list') &&
87                             ( ! $path ) );
88
89     if ($paths->GetBehavior('list_fails_if_list_a') && $path eq '-a')
90       {
91             print {$conn->{socket}} "500 Unknown command\r\n";
92             return;
93       }
94
95
96     if (!$paths->GetBehavior('list_dont_clean_path'))
97       {
98         # This is something of a hack. Some clients expect a Unix server
99         # to respond to flags on the 'ls command line'. Remove these flags
100         # and ignore them. This is particularly an issue with ncftp 2.4.3.
101         $path =~ s/^-[a-zA-Z0-9]+\s?//;
102       }
103
104     my $dir = $conn->{'dir'};
105
106     print STDERR "_LIST_command - dir is: $dir\n";
107
108     # Parse the first elements of the path until we find the appropriate
109     # working directory.
110     local $_;
111
112     my $listing;
113     if (!$ReturnEmptyList)
114       {
115         $dir = FTPPaths::path_merge($dir, $path);
116         $listing = $paths->get_list($dir,$SkipHiddenFiles);
117         unless ($listing) {
118             print {$conn->{socket}} "550 File or directory not found.\r\n";
119             return;
120         }
121       }
122
123     print STDERR "_LIST_command - dir is: $dir\n" if $log;
124
125     print {$conn->{socket}} "150 Opening data connection for file listing.\r\n";
126
127     # Open a path back to the client.
128     my $sock = __open_data_connection ($conn);
129     unless ($sock) {
130         print {$conn->{socket}} "425 Can't open data connection.\r\n";
131         return;
132     }
133
134     if (!$ReturnEmptyList)
135       {
136         for my $item (@$listing) {
137             print $sock "$item\r\n";
138         }
139       }
140
141     unless ($sock->close) {
142         print {$conn->{socket}} "550 Error closing data connection: $!\r\n";
143         return;
144     }
145
146     print {$conn->{socket}} "226 Listing complete. Data connection has been closed.\r\n";
147 }
148
149 sub _PASS_command
150 {
151     my ($conn, $cmd, $pass) = @_;
152
153     # TODO: implement authentication?
154
155     print STDERR "switching to LOGGEDIN state\n" if $log;
156     $conn->{state} = $_connection_states{LOGGEDIN};
157
158     if ($conn->{username} eq "anonymous") {
159         print {$conn->{socket}} "202 Anonymous user access is always granted.\r\n";
160     } else {
161         print {$conn->{socket}} "230 Authentication not implemented yet, access is always granted.\r\n";
162     }
163 }
164
165 sub _PASV_command
166 {
167     my ($conn, $cmd, $rest) = @_;
168
169     # Open a listening socket - but don't actually accept on it yet.
170     "0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
171     my $sock = IO::Socket::INET->new (LocalHost => '127.0.0.1',
172                                       LocalPort => '0',
173                                       Listen => 1,
174                                       Reuse => 1,
175                                       Proto => 'tcp',
176                                       Type => SOCK_STREAM);
177
178     unless ($sock) {
179         # Return a code 550 here, even though this is not in the RFC. XXX
180         print {$conn->{socket}} "550 Can't open a listening socket.\r\n";
181         return;
182     }
183
184     $conn->{passive} = 1;
185     $conn->{passive_socket} = $sock;
186
187     # Get our port number.
188     my $sockport = $sock->sockport;
189
190     # Split the port number into high and low components.
191     my $p1 = int ($sockport / 256);
192     my $p2 = $sockport % 256;
193
194     $conn->{state} = $_connection_states{TWOSOCKS};
195
196     # We only accept connections from localhost.
197     print {$conn->{socket}} "227 Entering Passive Mode (127,0,0,1,$p1,$p2)\r\n";
198 }
199
200 sub _PORT_command
201 {
202     my ($conn, $cmd, $rest) = @_;
203
204     # The arguments to PORT are a1,a2,a3,a4,p1,p2 where a1 is the
205     # most significant part of the address (eg. 127,0,0,1) and
206     # p1 is the most significant part of the port.
207     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})/) {
208         print {$conn->{socket}} "501 Syntax error in PORT command.\r\n";
209         return;
210     }
211
212     # Check host address.
213     unless ($1  > 0 && $1 < 224 &&
214             $2 >= 0 && $2 < 256 &&
215             $3 >= 0 && $3 < 256 &&
216             $4 >= 0 && $4 < 256) {
217         print {$conn->{socket}} "501 Invalid host address.\r\n";
218         return;
219     }
220
221     # Construct host address and port number.
222     my $peeraddrstring = "$1.$2.$3.$4";
223     my $peerport = $5 * 256 + $6;
224
225     # Check port number.
226     unless ($peerport > 0 && $peerport < 65536) {
227         print {$conn->{socket}} "501 Invalid port number.\r\n";
228     }
229
230     $conn->{peeraddrstring} = $peeraddrstring;
231     $conn->{peeraddr} = inet_aton ($peeraddrstring);
232     $conn->{peerport} = $peerport;
233     $conn->{passive} = 0;
234
235     $conn->{state} = $_connection_states{TWOSOCKS};
236
237     print {$conn->{socket}} "200 PORT command OK.\r\n";
238 }
239
240 sub _PWD_command
241 {
242     my ($conn, $cmd, $rest) = @_;
243
244     # See RFC 959 Appendix II and draft-ietf-ftpext-mlst-11.txt section 6.2.1.
245     my $pathname = $conn->{dir};
246     $pathname =~ s,/+$,, unless $pathname eq "/";
247     $pathname =~ tr,/,/,s;
248
249     print {$conn->{socket}} "257 \"$pathname\"\r\n";
250 }
251
252 sub _REST_command
253 {
254     my ($conn, $cmd, $restart_from) = @_;
255
256     unless ($restart_from =~ /^([1-9][0-9]*|0)$/) {
257         print {$conn->{socket}} "501 REST command needs a numeric argument.\r\n";
258         return;
259     }
260
261     $conn->{restart} = $1;
262
263     print {$conn->{socket}} "350 Restarting next transfer at $1.\r\n";
264 }
265
266 sub _RETR_command
267 {
268     my ($conn, $cmd, $path) = @_;
269
270     $path = FTPPaths::path_merge($conn->{dir}, $path);
271     my $info = $conn->{'paths'}->get_info($path);
272
273     unless ($info->{'_type'} eq 'f') {
274         print {$conn->{socket}} "550 File not found.\r\n";
275         return;
276     }
277
278     print {$conn->{socket}} "150 Opening " .
279         ($conn->{type} eq 'A' ? "ASCII mode" : "BINARY mode") .
280         " data connection.\r\n";
281
282     # Open a path back to the client.
283     my $sock = __open_data_connection ($conn);
284
285     unless ($sock) {
286         print {$conn->{socket}} "425 Can't open data connection.\r\n";
287         return;
288     }
289
290     my $content = $info->{'content'};
291
292     # Restart the connection from previous point?
293     if ($conn->{restart}) {
294         $content = substr($content, $conn->{restart});
295         $conn->{restart} = 0;
296     }
297
298     # What mode are we sending this file in?
299     unless ($conn->{type} eq 'A') # Binary type.
300     {
301         my ($r, $buffer, $n, $w);
302
303
304         # Copy data.
305         while ($buffer = substr($content, 0, 65536))
306         {
307             $r = length $buffer;
308
309             # Restart alarm clock timer.
310             alarm $conn->{idle_timeout};
311
312             for ($n = 0; $n < $r; )
313             {
314                 $w = syswrite ($sock, $buffer, $r - $n, $n);
315
316                 # Cleanup and exit if there was an error.
317                 unless (defined $w) {
318                     close $sock;
319                     print {$conn->{socket}} "426 File retrieval error: $!. Data connection has been closed.\r\n";
320                     return;
321                 }
322
323                 $n += $w;
324             }
325
326             # Transfer aborted by client?
327             if ($GOT_SIGURG) {
328                 $GOT_SIGURG = 0;
329                 close $sock;
330                 print {$conn->{socket}} "426 Transfer aborted. Data connection closed.\r\n";
331                 return;
332             }
333         }
334
335         # Cleanup and exit if there was an error.
336         unless (defined $r) {
337             close $sock;
338             print {$conn->{socket}} "426 File retrieval error: $!. Data connection has been closed.\r\n";
339             return;
340         }
341     } else { # ASCII type.
342         # Copy data.
343         my @lines = split /\r\n?|\n/, $content;
344         for (@lines) {
345             # Remove any native line endings.
346             s/[\n\r]+$//;
347
348             # Restart alarm clock timer.
349             alarm $conn->{idle_timeout};
350
351             # Write the line with telnet-format line endings.
352             print $sock "$_\r\n";
353
354             # Transfer aborted by client?
355             if ($GOT_SIGURG) {
356                 $GOT_SIGURG = 0;
357                 close $sock;
358                 print {$conn->{socket}} "426 Transfer aborted. Data connection closed.\r\n";
359                 return;
360             }
361         }
362     }
363
364     unless (close ($sock)) {
365         print {$conn->{socket}} "550 File retrieval error: $!.\r\n";
366         return;
367     }
368
369     print {$conn->{socket}} "226 File retrieval complete. Data connection has been closed.\r\n";
370 }
371
372 sub _SIZE_command
373 {
374     my ($conn, $cmd, $path) = @_;
375
376     $path = FTPPaths::path_merge($conn->{dir}, $path);
377     my $info = $conn->{'paths'}->get_info($path);
378     unless ($info) {
379         print {$conn->{socket}} "550 File or directory not found.\r\n";
380         return;
381     }
382
383     if ($info->{'_type'} eq 'd') {
384         print {$conn->{socket}} "550 SIZE command is not supported on directories.\r\n";
385         return;
386     }
387
388     my $size = length $info->{'content'};
389
390     print {$conn->{socket}} "213 $size\r\n";
391 }
392
393 sub _SYST_command
394 {
395     my ($conn, $cmd, $dummy) = @_;
396
397     if ($conn->{'paths'}->GetBehavior('syst_response'))
398       {
399         print {$conn->{socket}} $conn->{'paths'}->GetBehavior('syst_response') . "\r\n";
400       }
401     else
402       {
403         print {$conn->{socket}} "215 UNIX Type: L8\r\n";
404       }
405 }
406
407 sub _TYPE_command
408 {
409     my ($conn, $cmd, $type) = @_;
410
411     # See RFC 959 section 5.3.2.
412     if ($type =~ /^([AI])$/i) {
413         $conn->{type} = 'A';
414     } elsif ($type =~ /^([AI])\sN$/i) {
415         $conn->{type} = 'A';
416     } elsif ($type =~ /^L\s8$/i) {
417         $conn->{type} = 'L8';
418     } else {
419         print {$conn->{socket}} "504 This server does not support TYPE $type.\r\n";
420         return;
421     }
422
423     print {$conn->{socket}} "200 TYPE changed to $type.\r\n";
424 }
425
426 sub _USER_command
427 {
428     my ($conn, $cmd, $username) = @_;
429
430     print STDERR "username: $username\n" if $log;
431     $conn->{username} = $username;
432
433     print STDERR "switching to WAIT4PWD state\n" if $log;
434     $conn->{state} = $_connection_states{WAIT4PWD};
435
436     if ($conn->{username} eq "anonymous") {
437         print {$conn->{socket}} "230 Anonymous user access granted.\r\n";
438     } else {
439         print {$conn->{socket}} "331 Password required.\r\n";
440     }
441 }
442
443
444 # HELPER ROUTINES
445
446 sub __open_data_connection
447 {
448     my $conn = shift;
449
450     my $sock;
451
452     if ($conn->{passive}) {
453         # Passive mode - wait for a connection from the client.
454         accept ($sock, $conn->{passive_socket}) or return undef;
455     } else {
456         # Active mode - connect back to the client.
457         "0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
458         $sock = IO::Socket::INET->new (LocalAddr => '127.0.0.1',
459                                        PeerAddr => $conn->{peeraddrstring},
460                                        PeerPort => $conn->{peerport},
461                                        Proto => 'tcp',
462                                        Type => SOCK_STREAM) or return undef;
463     }
464
465     return $sock;
466 }
467
468
469 ###########################################################################
470 # FTPSERVER CLASS
471 ###########################################################################
472
473 {
474     my %_attr_data = ( # DEFAULT
475         _input           => undef,
476         _localAddr       => 'localhost',
477         _localPort       => undef,
478         _reuseAddr       => 1,
479         _rootDir         => Cwd::getcwd(),
480         _server_behavior => {},
481     );
482
483     sub _default_for
484     {
485         my ($self, $attr) = @_;
486         $_attr_data{$attr};
487     }
488
489     sub _standard_keys
490     {
491         keys %_attr_data;
492     }
493 }
494
495
496 sub new {
497     my ($caller, %args) = @_;
498     my $caller_is_obj = ref($caller);
499     my $class = $caller_is_obj || $caller;
500     my $self = bless {}, $class;
501     foreach my $attrname ($self->_standard_keys()) {
502         my ($argname) = ($attrname =~ /^_(.*)/);
503         if (exists $args{$argname}) {
504             $self->{$attrname} = $args{$argname};
505         } elsif ($caller_is_obj) {
506             $self->{$attrname} = $caller->{$attrname};
507         } else {
508             $self->{$attrname} = $self->_default_for($attrname);
509         }
510     }
511     # create server socket
512     "0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
513     $self->{_server_sock}
514                     = IO::Socket::INET->new (LocalHost => $self->{_localAddr},
515                                              LocalPort => $self->{_localPort},
516                                              Listen => 1,
517                                              Reuse => $self->{_reuseAddr},
518                                              Proto => 'tcp',
519                                              Type => SOCK_STREAM)
520                                         or die "bind: $!";
521
522     foreach my $file (keys %{$self->{_input}}) {
523         my $ref = \$self->{_input}{$file}{content};
524         $$ref =~ s/{{port}}/$self->sockport/eg;
525     }
526
527     return $self;
528 }
529
530
531 sub run
532 {
533     my ($self, $synch_callback) = @_;
534     my $initialized = 0;
535
536     # turn buffering off on STDERR
537     select((select(STDERR), $|=1)[0]);
538
539     # initialize command table
540     my $command_table = {};
541     foreach (keys %_commands) {
542         my $subname = "_${_}_command";
543         $command_table->{$_} = \&$subname;
544     }
545
546     my $old_ils = $/;
547     $/ = "\r\n";
548
549     if (!$initialized) {
550         $synch_callback->();
551         $initialized = 1;
552     }
553
554     $SIG{CHLD} = sub { wait };
555     my $server_sock = $self->{_server_sock};
556
557     # the accept loop
558     while (my $client_addr = accept (my $socket, $server_sock))
559     {
560         # turn buffering off on $socket
561         select((select($socket), $|=1)[0]);
562
563         # find out who connected
564         my ($client_port, $client_ip) = sockaddr_in ($client_addr);
565         my $client_ipnum = inet_ntoa ($client_ip);
566
567         # print who connected
568         print STDERR "got a connection from: $client_ipnum\n" if $log;
569
570         # fork off a process to handle this connection.
571         # my $pid = fork();
572         # unless (defined $pid) {
573         #     warn "fork: $!";
574         #     sleep 5; # Back off in case system is overloaded.
575         #     next;
576         # }
577
578         if (1) { # Child process.
579
580             # install signals
581             $SIG{URG}  = sub {
582                 $GOT_SIGURG  = 1;
583             };
584
585             $SIG{PIPE} = sub {
586                 print STDERR "Client closed connection abruptly.\n";
587                 exit;
588             };
589
590             $SIG{ALRM} = sub {
591                 print STDERR "Connection idle timeout expired. Closing server.\n";
592                 exit;
593             };
594
595             #$SIG{CHLD} = 'IGNORE';
596
597
598             print STDERR "in child\n" if $log;
599
600             my $conn = {
601                 'paths'           => FTPPaths->new($self->{'_input'},
602                                         $self->{'_server_behavior'}),
603                 'socket'          => $socket,
604                 'state'           => $_connection_states{NEWCONN},
605                 'dir'             => '/',
606                 'restart'         => 0,
607                 'idle_timeout'    => 60, # 1 minute timeout
608                 'rootdir'         => $self->{_rootDir},
609             };
610
611             print {$conn->{socket}} "220 GNU Wget Testing FTP Server ready.\r\n";
612
613             # command handling loop
614             for (;;) {
615                 print STDERR "waiting for request\n" if $log;
616
617                 last unless defined (my $req = <$socket>);
618
619                 # Remove trailing CRLF.
620                 $req =~ s/[\n\r]+$//;
621
622                 print STDERR "received request $req\n" if $log;
623
624                 # Get the command.
625                 # See also RFC 2640 section 3.1.
626                 unless ($req =~ m/^([A-Z]{3,4})\s?(.*)/i) {
627                     # badly formed command
628                     exit 0;
629                 }
630
631                 # The following strange 'eval' is necessary to work around a
632                 # very odd bug in Perl 5.6.0. The following assignment to
633                 # $cmd will fail in some cases unless you use $1 in some sort
634                 # of an expression beforehand.
635                 # - RWMJ 2002-07-05.
636                 eval '$1 eq $1';
637
638                 my ($cmd, $rest) = (uc $1, $2);
639
640                 # Got a command which matches in the table?
641                 unless (exists $command_table->{$cmd}) {
642                     print {$conn->{socket}} "500 Unrecognized command.\r\n";
643                     next;
644                 }
645
646                 # Command requires user to be authenticated?
647                 unless ($_commands{$cmd} | $conn->{state}) {
648                     print {$conn->{socket}} "530 Not logged in.\r\n";
649                     next;
650                 }
651
652                 # Handle the QUIT command specially.
653                 if ($cmd eq "QUIT") {
654                     print {$conn->{socket}} "221 Goodbye. Service closing connection.\r\n";
655                     last;
656                 }
657
658                 if (defined ($self->{_server_behavior}{fail_on_pasv})
659                         && $cmd eq 'PASV') {
660                     undef $self->{_server_behavior}{fail_on_pasv};
661                     close $socket;
662                     last;
663                 }
664
665                 # Run the command.
666                 &{$command_table->{$cmd}} ($conn, $cmd, $rest);
667             }
668         } else { # Father
669             close $socket;
670         }
671     }
672
673     $/ = $old_ils;
674 }
675
676 sub sockport {
677     my $self = shift;
678     return $self->{_server_sock}->sockport;
679 }
680
681
682 package FTPPaths;
683
684 use POSIX qw(strftime);
685
686 # not a method
687 sub final_component {
688     my $path = shift;
689
690     $path =~ s|.*/||;
691     return $path;
692 }
693
694 # not a method
695 sub path_merge {
696     my ($a, $b) = @_;
697
698     return $a unless $b;
699
700     if ($b =~ m.^/.) {
701         $a = '';
702         $b =~ s.^/..;
703     }
704     $a =~ s./$..;
705
706     my @components = split('/', $b);
707
708     foreach my $c (@components) {
709         if ($c =~ /^\.?$/) {
710             next;
711         } elsif ($c eq '..') {
712             next if $a eq '';
713             $a =~ s|/[^/]*$||;
714         } else {
715             $a .= "/$c";
716         }
717     }
718
719     return $a;
720 }
721
722 sub new {
723     my ($this, @args) = @_;
724     my $class = ref($this) || $this;
725     my $self = {};
726     bless $self, $class;
727     $self->initialize(@args);
728     return $self;
729 }
730
731 sub initialize {
732     my ($self, $urls, $behavior) = @_;
733     my $paths = {_type => 'd'};
734
735     # From a path like '/foo/bar/baz.txt', construct $paths such that
736     # $paths->{'foo'}->{'bar'}->{'baz.txt'} is
737     # $urls->{'/foo/bar/baz.txt'}.
738     for my $path (keys %$urls) {
739         my @components = split('/', $path);
740         shift @components;
741         my $x = $paths;
742         for my $c (@components) {
743             unless (exists $x->{$c}) {
744                 $x->{$c} = {_type => 'd'};
745             }
746             $x = $x->{$c};
747         }
748         %$x = %{$urls->{$path}};
749         $x->{_type} = 'f';
750     }
751
752     $self->{'_paths'} = $paths;
753     $self->{'_behavior'} = $behavior;
754 }
755
756 sub get_info {
757     my ($self, $path, $node) = @_;
758     $node = $self->{'_paths'} unless $node;
759     my @components = split('/', $path);
760     shift @components if @components && $components[0] eq '';
761
762     for my $c (@components) {
763         if ($node->{'_type'} eq 'd') {
764             $node = $node->{$c};
765         } else {
766             return undef;
767         }
768     }
769     return $node;
770 }
771
772 sub dir_exists {
773     my ($self, $path) = @_;
774     return $self->exists($path, 'd');
775 }
776
777 sub exists {
778     # type is optional, in which case we don't check it.
779     my ($self, $path, $type) = @_;
780     my $paths = $self->{'_paths'};
781
782     die "Invalid path $path (not absolute).\n" unless $path =~ m.^/.;
783     my $info = $self->get_info($path);
784     return 0 unless defined($info);
785     return $info->{'_type'} eq $type if defined($type);
786     return 1;
787 }
788
789 sub _format_for_list {
790     my ($self, $name, $info) = @_;
791
792     # XXX: mode should be specifyable as part of the node info.
793     my $mode_str;
794     if ($info->{'_type'} eq 'd') {
795         $mode_str = 'dr-xr-xr-x';
796     } else {
797         $mode_str = '-r--r--r--';
798     }
799
800     my $size = 0;
801     if ($info->{'_type'} eq 'f') {
802         $size = length  $info->{'content'};
803         if ($self->{'_behavior'}{'bad_list'}) {
804             $size = 0;
805         }
806     }
807     my $date = strftime ("%b %e %H:%M", localtime);
808     return "$mode_str 1  0  0  $size $date $name";
809 }
810
811 sub get_list {
812     my ($self, $path, $no_hidden) = @_;
813     my $info = $self->get_info($path);
814     return undef unless defined $info;
815     my $list = [];
816
817     if ($info->{'_type'} eq 'd') {
818         for my $item (keys %$info) {
819             next if $item =~ /^_/;
820             # 2013-10-17 Andrea Urbani (matfanjol)
821             #            I skip the hidden files if requested
822             if (($no_hidden) &&
823                 (defined($info->{$item}->{'attr'})) &&
824                 (index($info->{$item}->{'attr'}, "H")>=0))
825               {
826                 # This is an hidden file and I don't want to see it!
827                 print STDERR "get_list: Skipped hidden file [$item]\n";
828               }
829             else
830               {
831                 push @$list, $self->_format_for_list($item, $info->{$item});
832               }
833         }
834     } else {
835         push @$list, $self->_format_for_list(final_component($path), $info);
836     }
837
838     return $list;
839 }
840
841 # 2013-10-17 Andrea Urbani (matfanjol)
842 # It returns the behavior of the given name.
843 # In this file I handle also the following behaviors:
844 #  list_dont_clean_path  : if defined, the command
845 #                           $path =~ s/^-[a-zA-Z0-9]+\s?//;
846 #                          is not runt and the given path
847 #                          remains the original one
848 #  list_empty_if_list_a  : if defined, "LIST -a" returns an
849 #                          empty content
850 #  list_fails_if_list_a  : if defined, "LIST -a" returns an
851 #                          error
852 #  list_no_hidden_if_list: if defined, "LIST" doesn't return
853 #                          hidden files.
854 #                          To define an hidden file add
855 #                            attr => "H"
856 #                          to the url files
857 #  syst_response         : if defined, its content is printed
858 #                          out as SYST response
859 sub GetBehavior {
860   my ($self, $name) = @_;
861   return $self->{'_behavior'}{$name};
862 }
863
864 1;
865
866 # vim: et ts=4 sw=4