]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoIndex.pm
Packages::I18N::*: Add some missing definitions for Khmer
[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 POSIX qw( strftime );
8 use Exporter;
9
10 use Deb::Versions;
11 use Packages::Config qw( $TOPDIR );
12 use Packages::CGI;
13
14 our @ISA = qw( Exporter );
15 our @EXPORT = qw( do_index do_allpackages );
16
17 sub do_index {
18     return send_file( 'index', @_ );
19 }
20 sub do_allpackages {
21     return send_file( 'allpackages', @_ );
22 }
23
24 my %encoding = (
25                 'txt.gz' => 'x-gzip',
26                 );
27 sub send_file {
28     my ($file, $params, $opts) = @_;
29     my $cat = $opts->{cat};
30
31     if ($params->{errors}{suite}) {
32         fatal_error( $cat->g( "suite not valid or not specified" ) );
33     }
34     if (@{$opts->{suite}} > 1) {
35         fatal_error( $cat->g( "more than one suite specified for show_static (%s)",
36                               "@{$opts->{suite}}" ) );
37     }
38     if (@{$opts->{subsection}} > 1) {
39         fatal_error( $cat->g( "more than one subsection specified for show_static (%s)",
40                               "@{$opts->{suite}}" ) );
41     }
42
43     if ($opts->{format} eq 'txt.gz') {
44         $opts->{po_lang} = 'en';
45     }
46     my $wwwdir = "$TOPDIR/www";
47     my $path = "";
48     $path .= "source/" if $opts->{source};
49     $path .= "$opts->{suite}[0]/";
50     $path .= "$opts->{archive}[0]/" if @{$opts->{archive}} == 1;
51     $path .= "$opts->{subsection}[0]/" if @{$opts->{subsection}};
52     $path .= "$opts->{priority}[0]/" if @{$opts->{priority}};
53     $path .= "$file.$opts->{po_lang}.$opts->{format}";
54
55     unless (@Packages::CGI::fatal_errors) {
56         my $buffer;
57         if (open( INDEX, '<', "$wwwdir/$path" )) {
58             my %headers;
59             $headers{'-charset'} = 'UTF-8';
60             $headers{'-type'} = get_mime( $opts->{format}, 'text/plain' );
61             $headers{'-content-encoding'} = $encoding{$opts->{format}} if exists $encoding{$opts->{format}};
62             my ($size,$mtime) = (stat("$wwwdir/$path"))[7,9];
63             $headers{'-content-length'} = $size;
64             $headers{'-vary'} = 'negotiate,accept-language';
65             $headers{'-last-modified'} = strftime("%a, %d %b %Y %T %z", localtime($mtime));
66             $headers{'-expires'} = strftime("%a, %d %b %Y %T %z", localtime($mtime+(12*3600)));
67             print header( %headers );
68
69             binmode INDEX;
70             while (read INDEX, $buffer, 4096) {
71                 print $buffer;
72             }
73             close INDEX;
74             exit;
75         } else {
76             fatal_error( $cat->g( "couldn't read index file %s: %s",
77                                   $path, $! ) );
78         }
79     }
80 }
81
82 1;
83