3 use File
::MimeInfo
; # CPAN
6 # EVIL HAS NEVER BEEN SO VILE BEFORE:
7 $share_dir = './public_gopher/';
9 # Assume TLS is handled 'upstream';
10 # stdin is request in plain-text new gopher
11 # and stdout is response in plain-text new gopher
14 $request_str = <stdin
>;
18 if ($request_str =~ /\t/) {
20 chomp($size_str); $size_str =~ s/\r$//;
21 $post_size = sprintf('%d', $size_str);
22 # Hack -- making socket non-blocking
23 use Fcntl
qw(F_GETFL F_SETFL O_NONBLOCK);
24 $flags = fcntl(STDIN
, F_GETFL
, 0);
25 $flags = fcntl(STDIN
, F_SETFL
, $flags | O_NONBLOCK
);
26 $data_read = read(STDIN
, $data, $post_size);
30 chomp($request_str); $request_str =~ s/\r//;
31 ($selector, $search) = split(/\t/, $request_str);
33 # Make selector 'safe' :(
34 $selector = '/'.$selector;
35 $selector =~ s/\/\.+//;
36 $selector =~ s/\.\///;
37 $selector =~ s/^\/+//;
40 $path = $share_dir.$selector;
41 # Fetch file by selector
42 if (-e
$path && !-r
$path) {
43 die ng_error_access_denied
();
49 $ENV{"SELECTOR"} = $selector;
50 $ENV{"SEARCH"} = $search;
51 $ENV{'DATA_SIZE'} = $post_size;
52 $tmp_file = "/tmp/post.txt";
53 open(FILE
, '>'.$tmp_file);
55 #while (<STDIN>) { print FILE $_; }
58 exec $path . ' <'.$tmp_file;
62 $mimetype = mimetype
$path;
63 print $size."\t".$mimetype."\r\n";
64 # "cat" file.. (is there a better way?)
67 while (<FILE
>) { print; }
73 print ng_response
( ng_menu_from_dir
($path) );
77 die ng_error_not_found
();
88 my $mimetype = $_[1] || 'text/x-menu';
89 my $header = length ($data) . "\t" . $mimetype. "\r\n";
90 return $header . $data;
94 return ng_response
("e\t". $_[0] . "\terror\t\t\r\n", "text/x-menu");
97 sub ng_error_access_denied
{
98 return ng_error
"Access denied";
100 sub ng_error_not_found
{
101 return ng_error
("File not found [".$path."]");
104 sub ng_menu_from_dir
($path) {
107 @files = glob $path."/*";
108 $buf .= "i\tListing [".$path."]\r\n";
109 $buf .= "m\t..\t..\r\n";
111 $sl = length $share_dir;
112 foreach $file (@files) {
113 $name = substr($file, $l + 1);
114 $selector = substr($file, $sl);
116 if (-x
$file) { $type = 's'; }
117 if (-d
$file) { $type = 'm'; }
118 $buf .= $type."\t".$name."\t".$selector."\r\n";