]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoIndex.pm
a0cae2baf2f435cff7fa59f1f698d1d2ee19894c
[deb/packages.git] / lib / Packages / DoIndex.pm
1 package Packages::DoIndex;
2
3 use strict;
4 use warnings;
5
6 use CGI qw( :cgi );
7 use Exporter;
8
9 use Deb::Versions;
10 use Packages::Config qw( $TOPDIR );
11 use Packages::CGI;
12
13 our @ISA = qw( Exporter );
14 our @EXPORT = qw( do_index do_allpackages );
15
16 sub do_index {
17     return send_file( 'index', @_ );
18 }
19 sub do_allpackages {
20     return send_file( 'allpackages', @_ );
21 }
22
23 my %encoding = (
24                 'txt.gz' => 'x-gzip',
25                 );
26 sub send_file {
27     my ($file, $params, $opts) = @_;
28     my $cat = $opts->{cat};
29
30     if ($params->{errors}{suite}) {
31         fatal_error( $cat->g( "suite not valid or not specified" ) );
32     }
33     if (@{$opts->{suite}} > 1) {
34         fatal_error( $cat->g( "more than one suite specified for show_static (%s)",
35                               "@{$opts->{suite}}" ) );
36     }
37     if (@{$opts->{subsection}} > 1) {
38         fatal_error( $cat->g( "more than one subsection specified for show_static (%s)",
39                               "@{$opts->{suite}}" ) );
40     }
41
42     my $wwwdir = "$TOPDIR/www";
43     my $path = "";
44     $path .= "source/" if $opts->{source};
45     $path .= "$opts->{suite}[0]/";
46     $path .= "$opts->{archive}[0]/" if @{$opts->{archive}} == 1;
47     $path .= "$opts->{subsection}[0]/" if @{$opts->{subsection}};
48     $path .= "$opts->{priority}[0]/" if @{$opts->{priority}};
49
50     #FIXME: ugly hack
51     if ($opts->{lang} ne 'en' and !-f "$wwwdir/$path$file.$opts->{lang}.$opts->{format}") {
52         $opts->{lang} = 'en';
53     }
54     $path .= "$file.$opts->{lang}.$opts->{format}";
55
56     unless (@Packages::CGI::fatal_errors) {
57         my $buffer;
58         if (open( INDEX, '<', "$wwwdir/$path" )) {
59             my %headers;
60             $headers{'-charset'} = 'UTF-8';
61             $headers{'-type'} = get_mime( $opts->{format}, 'text/plain' );
62             $headers{'-content-encoding'} = $encoding{$opts->{format}} if exists $encoding{$opts->{format}};
63             my ($size,$mtime) = (stat("$wwwdir/$path"))[7,9];
64             $headers{'-content-length'} = $size;
65             $headers{'-last-modified'} = gmtime($mtime);
66             print header( %headers );
67
68             binmode INDEX;
69             while (read INDEX, $buffer, 4096) {
70                 print $buffer;
71             }
72             close INDEX;
73             exit;
74         } else {
75             fatal_error( $cat->g( "couldn't read index file %s: %s",
76                                   $path, $! ) );
77         }
78     }
79 }
80
81 1;
82