Handle hostnames with upper-case letters
[webmin.git] / software / view.cgi
1 #!/usr/local/bin/perl
2 # view.cgi
3 # Output the contents of a file
4
5 require './software-lib.pl';
6 $p = $ENV{'PATH_INFO'};
7
8 # Try to guess type from filename
9 if ($p =~ /\.([^\.\/]+)$/) {
10         $ext = lc($1);
11         &get_miniserv_config(\%miniserv);
12         open(MIME, $miniserv{'mimetypes'});
13         while(<MIME>) {
14                 s/#.*//g;
15                 if (/(\S+)\s+(.*)/) {
16                         foreach $e (split(/\s+/, $2)) {
17                                 if ($ext eq $e) {
18                                         $type = $1;
19                                         last;
20                                         }
21                                 }
22                         }
23                 }
24         close(MIME);
25         }
26 if (!$type) {
27         # No idea .. use the 'file' command
28         if (`file "$p"` =~ /text|script/) {
29                 $type = "text/plain";
30                 }
31         else {
32                 $type = "application/octet-stream";
33                 }
34         }
35
36 # Dump the file
37 if (!open(FILE, "<$p")) {
38         print "Content-type: text/plain\n\n";
39         print &text('list_eview', $p, $!),"\n";
40         }
41 else {
42         @st = stat($p);
43         print "Content-length: $st[7]\n";
44         print "Content-type: $type\n\n";
45         while(read(FILE, $buf, 1024)) {
46                 print $buf;
47                 }
48         close(FILE);
49         }
50