]> git.deb.at Git - deb/packages.git/blob - lib/Packages/Dispatcher.pm
some more suite aliases
[deb/packages.git] / lib / Packages / Dispatcher.pm
1 #!/usr/bin/perl -T
2 # Packages::Dispatcher -- CGI interface for packages.debian.org
3 #
4 # Copyright (C) 2004-2008 Frank Lichtenheld
5 #
6 #    This program is free software; you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation; either version 1 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program; if not, write to the Free Software
18 #    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 package Packages::Dispatcher;
21
22 use strict;
23 use warnings;
24
25 use CGI;
26 use POSIX;
27 use File::Basename;
28 use Template;
29 use DB_File;
30 use URI::Escape;
31 use Benchmark ':hireswallclock';
32 use I18N::AcceptLanguage;
33 use Date::Parse;
34
35 use Deb::Versions;
36 use Packages::Config qw( $DBDIR $ROOT $TEMPLATEDIR $CACHEDIR
37                          @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES @PRIORITIES
38                          @LANGUAGES @DDTP_LANGUAGES );
39 use Packages::CGI qw( :DEFAULT error get_all_messages );
40 use Packages::DB;
41 use Packages::Search qw( :all );
42 use Packages::Template ();
43 use Packages::Sections;
44 use Packages::I18N::Locale;
45
46 use Packages::DoSearch;
47 use Packages::DoSearchContents;
48 use Packages::DoShow;
49 use Packages::DoIndex;
50 use Packages::DoNewPkg;
51 use Packages::DoDownload;
52 use Packages::DoFilelist;
53
54
55 sub do_dispatch {
56
57     &Packages::CGI::reset;
58     $Packages::Search::too_many_hits = 0;
59
60     # clean up env
61     $ENV{PATH} = "/bin:/usr/bin";
62     delete $ENV{'LANGUAGE'};
63     delete $ENV{'LANG'};
64     delete $ENV{'LC_ALL'};
65     delete $ENV{'LC_MESSAGES'};
66
67     my %SUITES_ALIAS = ( oldstable => 'lenny',
68                          stable => 'squeeze',
69                          testing => 'wheezy',
70                          unstable => 'sid',
71                          'rc-buggy' => 'experimental',
72                          '5.0' => 'lenny',
73                          '6.0' => 'squeeze',
74                          'oldstable-backports' => 'lenny-backports',
75                          'stable-backports' => 'squeeze-backports' );
76
77     # Read in all the variables set by the form
78     my $input;
79     if ($ARGV[0] && ($ARGV[0] eq 'php')) {
80         $input = new CGI(\*STDIN);
81     } else {
82         $input = new CGI;
83     }
84     my $cgi_error = $input->cgi_error;
85     if ($cgi_error) {
86         fatal_error( "Error parsing the request", $cgi_error );
87     }
88
89     my $pet0 = new Benchmark;
90     my $tet0 = new Benchmark;
91     my $debug = DEBUG && $input->param("debug");
92     $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
93     $Packages::CGI::debug = $debug;
94
95     my $homedir = dirname($ENV{SCRIPT_FILENAME}).'/../';
96     &Packages::Config::init( $homedir );
97     &Packages::DB::init();
98     my $last_modified = $Packages::DB::db_read_time;
99     my $now = time;
100     my $expires = $last_modified + (12*3600);
101     $expires = $now + 3600 if $expires < $now;
102     # allow some fudge, since the db mod time is not the end of
103     # the cron job
104     $last_modified = $now if $now - $last_modified < 3600;
105
106     if ($input->http('If-Modified-Since') and
107         (my $modtime = str2time($input->http('If-Modified-Since'), 'UTC'))) {
108         if ($modtime <= $last_modified) {
109             print $input->header(-status => 304);
110             exit;
111         }
112     }
113
114     my %PO_LANGUAGES = map { $_ => 1 } @LANGUAGES;
115     my %DDTP_LANGUAGES = map { $_ => 1 } @DDTP_LANGUAGES;
116     my $acc = I18N::AcceptLanguage->new(defaultLanguage => 'en');
117     my $ddtp_lang = $acc->accepts( $input->http("Accept-Language"),
118                                    \@DDTP_LANGUAGES );
119     my $po_lang = $acc->accepts( $input->http("Accept-Language"),
120                                  \@LANGUAGES );
121     debug( "LANGS=@LANGUAGES DDTP_LANGS=@DDTP_LANGUAGES header=".
122            ($input->http("Accept-Language")||'').
123            " po_lang=$po_lang ddtp_lang=$ddtp_lang", 1 ) if DEBUG;
124
125     # backwards compatibility stuff
126     debug( "SCRIPT_URL=$ENV{SCRIPT_URL} SCRIPT_URI=$ENV{SCRIPT_URI}" ) if DEBUG;
127
128     if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_|) {
129         error( "You reached this site over an old URL. ".
130                "Depending on the exact parameters your search might work or not." );
131         # contents search changed a lot
132         if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_contents|) {
133             $input->param('keywords',$input->param('word')) if $input->param('word');
134             $input->param('searchon','contents');
135             for ($input->param('searchmode')) {
136                 /^searchfiles/ && do {
137                     $input->param('mode','filename');
138                     last;
139                 };
140                 /^filelist/ && do {
141                     $ENV{PATH_INFO} = '/'.join('/',($input->param('version')||'stable',
142                                                     $input->param('keywords'),
143                                                     $input->param('arch')||'i386',
144                                                     'filelist' ));
145                     $input->delete('searchon','version','keywords','arch');
146                     last;
147                 };
148             }
149         }
150     }
151     if ($ENV{is_reportbug}) {
152         $input->param('exact', 1);
153         debug( "reportbug detected, set paramater exact to '1'" ) if DEBUG;
154     }
155
156     my $what_to_do = 'show';
157     my $source = 0;
158     if (my $path = $ENV{'PATH_INFO'} || $input->param('PATH_INFO')) {
159         my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
160
161         debug( "PATH_INFO=$path components=@components", 3) if DEBUG;
162
163         push @components, 'index' if @components && $path =~ m,/$,;
164
165         my %LANGUAGES = map { $_ => 1 } (@LANGUAGES, @DDTP_LANGUAGES);
166         if (@components > 1 and $LANGUAGES{$components[0]}
167             and !$input->param('lang')) {
168             $input->param( 'lang', shift(@components) );
169         }
170         if (@components > 1 and $components[0] eq 'source') {
171             shift @components;
172             $input->param( 'source', 1 );
173         }
174         if (@components > 0 and $components[0] eq 'search') {
175             shift @components;
176             $what_to_do = 'search';
177             # Done
178             fatal_error( "search doesn't take any more path elements" )
179                 if @components;
180         } elsif (@components == 0) {
181             fatal_error( "We're supposed to display the homepage here, instead of getting dispatch.pl" );
182         } elsif (@components == 1) {
183             if ($components[0] eq 'index') {
184                 $what_to_do = 'homepage';
185             } else {
186                 $what_to_do = 'search';
187             }
188         } else {
189
190             for ($components[-1]) {
191                 /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
192                     pop @components;
193                     $what_to_do = $1;
194                     last;
195                 };
196             }
197
198             my %SUITES = map { $_ => 1 } @SUITES;
199             my %SECTIONS = map { $_ => 1 } @SECTIONS;
200             my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
201             my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all', 'any');
202             my %PRIORITIES = map { $_ => 1 } @PRIORITIES;
203             my %params_set;
204             sub set_param_once {
205                 my ($cgi, $params_set, $key, $val) = @_;
206                 debug("set_param_once key=$key val=$val",4) if DEBUG;
207                 if ($params_set->{$key}++) {
208                     fatal_error( "$key set more than once in path" );
209                 } else {
210                     $cgi->param( $key, $val );
211                 }
212             }
213
214             my (@pkg, $need_pkg);
215             foreach (reverse @components) {
216                 $need_pkg = !@pkg
217                     && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
218                 debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
219                 if (!$need_pkg && $SUITES{$_}) {
220                     set_param_once( $input, \%params_set, 'suite', $_);
221                 } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
222                     set_param_once( $input, \%params_set, 'suite', $s);
223                 } elsif (!$need_pkg && $SECTIONS{$_}) {
224                     set_param_once( $input, \%params_set, 'section', $_);
225                 } elsif (!$need_pkg && $sections_descs{$_}) {
226                     set_param_once( $input, \%params_set, 'subsection', $_);
227                 } elsif (!$need_pkg && ($_ eq 'source')) {
228                     set_param_once( $input, \%params_set, 'source', 1);
229                 } elsif ($ARCHITECTURES{$_}) {
230                     set_param_once( $input, \%params_set, 'arch', $_)
231                         unless $_ eq 'any';
232                 } elsif (!$need_pkg && $ARCHIVES{$_}) {
233                     set_param_once( $input, \%params_set, 'archive', $_);
234                 } elsif ($PRIORITIES{$_}) {
235                     set_param_once( $input, \%params_set, 'priority', $_);
236                 } else {
237                     push @pkg, $_;
238                 }
239             }
240             @components = @pkg;
241
242             if (@components > 1) {
243                 fatal_error( "two or more packages specified (@components)" );
244             }
245         } # else if (@components == 1)
246
247         if (@components) {
248             my $c = uri_unescape($components[0]);
249             $input->param( 'keywords', $c );
250             $input->param( 'package', $c );
251         }
252     }
253
254     my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
255
256     my %params_def = ( keywords => { default => undef,
257                                      array => '\s+',
258                                      match => '^([-+\@\w\/.:]+)$',
259                                  },
260                        'package' => { default => undef,
261                                       match => '^([\w.+-]+)$',
262                                       var => \$pkg },
263                        suite => { default => 'default', match => '^([\w-]+)$',
264                                   array => ',', var => \@suites,
265                                   replace => { all => \@SUITES,
266                                                default => \@SUITES,
267                                                %SUITES_ALIAS } },
268                        archive => { default => ($what_to_do eq 'search') ?
269                                         'all' : 'default',
270                                         match => '^([\w-]+)$',
271                                         array => ',', var => \@archives,
272                                         replace => { all => \@ARCHIVES,
273                                                      default => \@ARCHIVES} },
274                        exact => { default => 0, match => '^(\w+)$',  },
275                        lang => { default => undef, match => '^([\w-]+)$',  },
276                        source => { default => 0, match => '^(\d+)$',  },
277                        debug => { default => 0, match => '^(\d+)$',  },
278                        searchon => { default => 'names', match => '^(\w+)$', },
279                        section => { default => 'all', match => '^([\w-]+)$',
280                                     alias => 'release', array => ',',
281                                     var => \@sections,
282                                     replace => { all => \@SECTIONS } },
283                        subsection => { default => 'default', match => '^([\w-]+)$',
284                                        array => ',', var => \@subsections,
285                                        replace => { default => [] } },
286                        priority => { default => 'default', match => '^([\w-]+)$',
287                                      array => ',',
288                                      replace => { default => [] } },
289                        arch => { default => 'any', match => '^([\w-]+)$',
290                                  array => ',', var => \@archs, replace =>
291                                  { any => \@ARCHITECTURES } },
292                        'format' => { default => 'html', match => '^([\w.]+)$',  },
293                        mode => { default => '', match => '^(\w+)$',  },
294                        sort_by => { default => 'file', match => '^(\w+)$', },
295                        );
296     my %opts;
297     my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
298     Packages::CGI::init_url( $input, \%params, \%opts );
299
300     if (defined($opts{lang})) {
301         $opts{po_lang} = $PO_LANGUAGES{$opts{lang}} ? $opts{lang} : 'en';
302         $opts{ddtp_lang} = $DDTP_LANGUAGES{$opts{lang}} ? $opts{lang} : 'en';
303     } else {
304         $opts{po_lang} = $po_lang;
305         $opts{ddtp_lang} = $ddtp_lang;
306     }
307     my $charset = "UTF-8";
308     my $cat = Packages::I18N::Locale->get_handle( $opts{po_lang}, 'en' )
309         or die "get_handle failed for $opts{po_lang}";
310     $opts{cat} = $cat;
311
312     $opts{h_suites} = { map { $_ => 1 } @suites };
313     $opts{h_sections} = { map { $_ => 1 } @sections };
314     $opts{h_archives} = { map { $_ => 1 } @archives };
315     $opts{h_archs} = { map { $_ => 1 } @archs };
316
317     if ((($opts{searchon} eq 'names') && $opts{source}) ||
318         ($opts{searchon} eq 'sourcenames')) {
319         $opts{source} = 1;
320         $opts{searchon} = 'names',
321         $opts{searchon_form} = 'sourcenames';
322     } else {
323         $opts{searchon_form} = $opts{searchon};
324     }
325     if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
326         $what_to_do = 'search_contents';
327     }
328
329     my $pet1 = new Benchmark;
330     my $petd = timediff($pet1, $pet0);
331     debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
332
333     my $template = new Packages::Template( $TEMPLATEDIR, $opts{format},
334                                            { po_lang => $opts{po_lang}, ddtp_lang => $opts{ddtp_lang},
335                                              charset => $charset, cat => $cat,
336                                              debug => ( DEBUG ? $opts{debug} : 0 ) },
337                                            ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
338
339     #FIXME: ugly hack
340     unless (($what_to_do eq 'allpackages' and $opts{format} =~ /^(html|txt\.gz)/)
341             || ($what_to_do eq 'index' and $opts{format} eq 'html')
342             || -e "$TEMPLATEDIR/$opts{format}/${what_to_do}.tmpl") {
343         fatal_error( $cat->g("requested format not available for this document"),
344                      "406 requested format not available");
345     }
346
347     my (%page_content);
348     unless (@Packages::CGI::fatal_errors) {
349         no strict 'refs';
350         &{"do_$what_to_do"}( \%params, \%opts, \%page_content );
351     }
352
353     $page_content{opts} = \%opts;
354     $page_content{params} = \%params;
355
356     unless (@Packages::CGI::fatal_errors) {
357         print $input->header(-charset => $charset,
358                              -type => get_mime($opts{format}),
359                              -vary => 'negotiate,accept-language',
360                              -last_modified => strftime("%a, %d %b %Y %T %z",
361                                                         localtime($last_modified)),
362                              -expires => strftime("%a, %d %b %Y %T %z",
363                                                   localtime($expires)),
364                              );
365         #use Data::Dumper;
366         #print '<pre>'.Dumper(\%ENV, \%page_content, get_all_messages()).'</pre>';
367         print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
368     } elsif ($Packages::CGI::http_code && $Packages::CGI::http_code !~ /^2\d\d/) {
369         print $input->header( -charset => $charset, -status => $Packages::CGI::http_code );
370     } else {
371         # We currently have only an error page in html
372         # so no format support here
373         print $input->header( -charset => $charset );
374         print $template->error_page( get_all_messages() );
375     }
376 }
377
378 1;
379 # vim: ts=8 sw=4