]> git.deb.at Git - deb/packages.git/blob - lib/Packages/Dispatcher.pm
986b12a92212b3acbecb4dc951519c8c15a35482
[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 => 'sarge',
68                          stable => 'etch',
69                          testing => 'lenny',
70                          unstable => 'sid',
71                          '3.1' => 'sarge',
72                          '4.0' => 'etch' );
73
74     # Read in all the variables set by the form
75     my $input;
76     if ($ARGV[0] && ($ARGV[0] eq 'php')) {
77         $input = new CGI(\*STDIN);
78     } else {
79         $input = new CGI;
80     }
81     my $cgi_error = $input->cgi_error;
82     if ($cgi_error) {
83         fatal_error( "Error parsing the request", $cgi_error );
84     }
85
86     my $pet0 = new Benchmark;
87     my $tet0 = new Benchmark;
88     my $debug = DEBUG && $input->param("debug");
89     $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
90     $Packages::CGI::debug = $debug;
91
92     my $homedir = dirname($ENV{SCRIPT_FILENAME}).'/../';
93     &Packages::Config::init( $homedir );
94     &Packages::DB::init();
95
96     if ($input->http('If-Modified-Since') and
97         (my $modtime = str2time($input->http('If-Modified-Since'), 'UTC'))) {
98         if ($modtime < $Packages::DB::db_read_time) {
99             print $input->header(-status => 304);
100             exit;
101         }
102     }
103
104     my %PO_LANGUAGES = map { $_ => 1 } @LANGUAGES;
105     my %DDTP_LANGUAGES = map { $_ => 1 } @DDTP_LANGUAGES;
106     my $acc = I18N::AcceptLanguage->new(defaultLanguage => 'en');
107     my $ddtp_lang = $acc->accepts( $input->http("Accept-Language"),
108                                    \@DDTP_LANGUAGES );
109     my $po_lang = $acc->accepts( $input->http("Accept-Language"),
110                                  \@LANGUAGES );
111     debug( "LANGS=@LANGUAGES DDTP_LANGS=@DDTP_LANGUAGES header=".
112            ($input->http("Accept-Language")||'').
113            " po_lang=$po_lang ddtp_lang=$ddtp_lang", 1 ) if DEBUG;
114
115     # backwards compatibility stuff
116     debug( "SCRIPT_URL=$ENV{SCRIPT_URL} SCRIPT_URI=$ENV{SCRIPT_URI}" ) if DEBUG;
117
118     if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_|) {
119         error( "You reached this site over an old URL. ".
120                "Depending on the exact parameters your search might work or not." );
121         # contents search changed a lot
122         if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_contents|) {
123             $input->param('keywords',$input->param('word')) if $input->param('word');
124             $input->param('searchon','contents');
125             for ($input->param('searchmode')) {
126                 /^searchfiles/ && do {
127                     $input->param('mode','filename');
128                     last;
129                 };
130                 /^filelist/ && do {
131                     $ENV{PATH_INFO} = '/'.join('/',($input->param('version')||'stable',
132                                                     $input->param('keywords'),
133                                                     $input->param('arch')||'i386',
134                                                     'filelist' ));
135                     $input->delete('searchon','version','keywords','arch');
136                     last;
137                 };
138             }
139         }
140     }
141     if ($ENV{is_reportbug}) {
142         $input->param('exact', 1);
143         debug( "reportbug detected, set paramater exact to '1'" ) if DEBUG;
144     }
145
146     my $what_to_do = 'show';
147     my $source = 0;
148     if (my $path = $ENV{'PATH_INFO'} || $input->param('PATH_INFO')) {
149         my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
150
151         debug( "PATH_INFO=$path components=@components", 3) if DEBUG;
152
153         push @components, 'index' if @components && $path =~ m,/$,;
154
155         my %LANGUAGES = map { $_ => 1 } (@LANGUAGES, @DDTP_LANGUAGES);
156         if (@components > 1 and $LANGUAGES{$components[0]}
157             and !$input->param('lang')) {
158             $input->param( 'lang', shift(@components) );
159         }
160         if (@components > 1 and $components[0] eq 'source') {
161             shift @components;
162             $input->param( 'source', 1 );
163         }
164         if (@components > 0 and $components[0] eq 'search') {
165             shift @components;
166             $what_to_do = 'search';
167             # Done
168             fatal_error( "search doesn't take any more path elements" )
169                 if @components;
170         } elsif (@components == 0) {
171             fatal_error( "We're supposed to display the homepage here, instead of getting dispatch.pl" );
172         } elsif (@components == 1) {
173             $what_to_do = 'search';
174         } else {
175
176             for ($components[-1]) {
177                 /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
178                     pop @components;
179                     $what_to_do = $1;
180                     last;
181                 };
182             }
183
184             my %SUITES = map { $_ => 1 } @SUITES;
185             my %SECTIONS = map { $_ => 1 } @SECTIONS;
186             my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
187             my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all', 'any');
188             my %PRIORITIES = map { $_ => 1 } @PRIORITIES;
189             my %params_set;
190             sub set_param_once {
191                 my ($cgi, $params_set, $key, $val) = @_;
192                 debug("set_param_once key=$key val=$val",4) if DEBUG;
193                 if ($params_set->{$key}++) {
194                     fatal_error( "$key set more than once in path" );
195                 } else {
196                     $cgi->param( $key, $val );
197                 }
198             }
199
200             my (@pkg, $need_pkg);
201             foreach (reverse @components) {
202                 $need_pkg = !@pkg
203                     && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
204                 debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
205                 if (!$need_pkg && $SUITES{$_}) {
206                     set_param_once( $input, \%params_set, 'suite', $_);
207                 } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
208                     set_param_once( $input, \%params_set, 'suite', $s);
209                 } elsif (!$need_pkg && $SECTIONS{$_}) {
210                     set_param_once( $input, \%params_set, 'section', $_);
211                 } elsif (!$need_pkg && $sections_descs{$_}) {
212                     set_param_once( $input, \%params_set, 'subsection', $_);
213                 } elsif (!$need_pkg && ($_ eq 'source')) {
214                     set_param_once( $input, \%params_set, 'source', 1);
215                 } elsif ($ARCHITECTURES{$_}) {
216                     set_param_once( $input, \%params_set, 'arch', $_)
217                         unless $_ eq 'any';
218                 } elsif (!$need_pkg && $ARCHIVES{$_}) {
219                     set_param_once( $input, \%params_set, 'archive', $_);
220                 } elsif ($PRIORITIES{$_}) {
221                     set_param_once( $input, \%params_set, 'priority', $_);
222                 } else {
223                     push @pkg, $_;
224                 }
225             }
226             @components = @pkg;
227
228             if (@components > 1) {
229                 fatal_error( "two or more packages specified (@components)" );
230             }
231         } # else if (@components == 1)
232
233         if (@components) {
234             my $c = uri_unescape($components[0]);
235             $input->param( 'keywords', $c );
236             $input->param( 'package', $c );
237         }
238     }
239
240     my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
241
242     my %params_def = ( keywords => { default => undef,
243                                      array => '\s+',
244                                      match => '^([-+\@\w\/.:]+)$',
245                                  },
246                        'package' => { default => undef,
247                                       match => '^([\w.+-]+)$',
248                                       var => \$pkg },
249                        suite => { default => 'default', match => '^([\w-]+)$',
250                                   array => ',', var => \@suites,
251                                   replace => { all => \@SUITES,
252                                                default => \@SUITES,
253                                                %SUITES_ALIAS } },
254                        archive => { default => ($what_to_do eq 'search') ?
255                                         'all' : 'default',
256                                         match => '^([\w-]+)$',
257                                         array => ',', var => \@archives,
258                                         replace => { all => \@ARCHIVES,
259                                                      default => \@ARCHIVES} },
260                        exact => { default => 0, match => '^(\w+)$',  },
261                        lang => { default => undef, match => '^([\w-]+)$',  },
262                        source => { default => 0, match => '^(\d+)$',  },
263                        debug => { default => 0, match => '^(\d+)$',  },
264                        searchon => { default => 'names', match => '^(\w+)$', },
265                        section => { default => 'all', match => '^([\w-]+)$',
266                                     alias => 'release', array => ',',
267                                     var => \@sections,
268                                     replace => { all => \@SECTIONS } },
269                        subsection => { default => 'default', match => '^([\w-]+)$',
270                                        array => ',', var => \@subsections,
271                                        replace => { default => [] } },
272                        priority => { default => 'default', match => '^([\w-]+)$',
273                                      array => ',',
274                                      replace => { default => [] } },
275                        arch => { default => 'any', match => '^([\w-]+)$',
276                                  array => ',', var => \@archs, replace =>
277                                  { any => \@ARCHITECTURES } },
278                        'format' => { default => 'html', match => '^([\w.]+)$',  },
279                        mode => { default => '', match => '^(\w+)$',  },
280                        sort_by => { default => 'file', match => '^(\w+)$', },
281                        );
282     my %opts;
283     my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
284     Packages::CGI::init_url( $input, \%params, \%opts );
285
286     if (defined($opts{lang})) {
287         $opts{po_lang} = $PO_LANGUAGES{$opts{lang}} ? $opts{lang} : 'en';
288         $opts{ddtp_lang} = $DDTP_LANGUAGES{$opts{lang}} ? $opts{lang} : 'en';
289     } else {
290         $opts{po_lang} = $po_lang;
291         $opts{ddtp_lang} = $ddtp_lang;
292     }
293     my $charset = "UTF-8";
294     my $cat = Packages::I18N::Locale->get_handle( $opts{po_lang}, 'en' )
295         or die "get_handle failed for $opts{po_lang}";
296     $opts{cat} = $cat;
297
298     $opts{h_suites} = { map { $_ => 1 } @suites };
299     $opts{h_sections} = { map { $_ => 1 } @sections };
300     $opts{h_archives} = { map { $_ => 1 } @archives };
301     $opts{h_archs} = { map { $_ => 1 } @archs };
302
303     if ((($opts{searchon} eq 'names') && $opts{source}) ||
304         ($opts{searchon} eq 'sourcenames')) {
305         $opts{source} = 1;
306         $opts{searchon} = 'names',
307         $opts{searchon_form} = 'sourcenames';
308     } else {
309         $opts{searchon_form} = $opts{searchon};
310     }
311     if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
312         $what_to_do = 'search_contents';
313     }
314
315     my $pet1 = new Benchmark;
316     my $petd = timediff($pet1, $pet0);
317     debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
318
319     my $template = new Packages::Template( $TEMPLATEDIR, $opts{format},
320                                            { po_lang => $opts{po_lang}, ddtp_lang => $opts{ddtp_lang},
321                                              charset => $charset, cat => $cat,
322                                              debug => ( DEBUG ? $opts{debug} : 0 ) },
323                                            ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
324
325     #FIXME: ugly hack
326     unless (($what_to_do eq 'allpackages' and $opts{format} =~ /^(html|txt\.gz)/)
327             || ($what_to_do eq 'index' and $opts{format} eq 'html')
328             || -e "$TEMPLATEDIR/$opts{format}/${what_to_do}.tmpl") {
329         fatal_error( $cat->g("requested format not available for this document"),
330                      "406 requested format not available");
331     }
332
333     my (%page_content);
334     unless (@Packages::CGI::fatal_errors) {
335         no strict 'refs';
336         &{"do_$what_to_do"}( \%params, \%opts, \%page_content );
337     }
338
339     $page_content{opts} = \%opts;
340     $page_content{params} = \%params;
341
342     unless (@Packages::CGI::fatal_errors) {
343         print $input->header(-charset => $charset,
344                              -type => get_mime($opts{format}),
345                              -vary => 'negotiate,accept-language',
346                              -last_modified => strftime("%a, %d %b %Y %T %z",
347                                                         localtime($Packages::DB::db_read_time)),
348                              );
349         #use Data::Dumper;
350         #print '<pre>'.Dumper(\%ENV, \%page_content, get_all_messages()).'</pre>';
351         print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
352     } elsif ($Packages::CGI::http_code && $Packages::CGI::http_code !~ /^2\d\d/) {
353         print $input->header( -charset => $charset, -status => $Packages::CGI::http_code );
354     } else {
355         # We currently have only an error page in html
356         # so no format support here
357         print $input->header( -charset => $charset );
358         print $template->error_page( get_all_messages() );
359     }
360 }
361
362 1;
363 # vim: ts=8 sw=4