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