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