2 # Packages::Dispatcher -- CGI interface for packages.debian.org
4 # Copyright (C) 2004-2008 Frank Lichtenheld
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.
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.
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.
20 package Packages::Dispatcher;
31 use Benchmark ':hireswallclock';
32 use I18N::AcceptLanguage;
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 );
41 use Packages::Search qw( :all );
42 use Packages::Template ();
43 use Packages::Sections;
44 use Packages::I18N::Locale;
46 use Packages::DoSearch;
47 use Packages::DoSearchContents;
49 use Packages::DoIndex;
50 use Packages::DoNewPkg;
51 use Packages::DoDownload;
52 use Packages::DoFilelist;
57 &Packages::CGI::reset;
58 $Packages::Search::too_many_hits = 0;
61 $ENV{PATH} = "/bin:/usr/bin";
62 delete $ENV{'LANGUAGE'};
64 delete $ENV{'LC_ALL'};
65 delete $ENV{'LC_MESSAGES'};
67 my %SUITES_ALIAS = ( oldstable => 'lenny',
71 'rc-buggy' => 'experimental',
74 'oldstable-backports' => 'lenny-backports',
75 'stable-backports' => 'squeeze-backports' );
77 # Read in all the variables set by the form
79 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
80 $input = new CGI(\*STDIN);
84 my $cgi_error = $input->cgi_error;
86 fatal_error( "Error parsing the request", $cgi_error );
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;
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;
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
104 $last_modified = $now if $now - $last_modified < 3600;
106 if ($input->http('If-Modified-Since') and
107 (my $client_timestamp = str2time($input->http('If-Modified-Since'), 'UTC'))) {
108 if ($client_timestamp > $last_modified) {
109 # we are not modified since asked -> return "304 Not Modified"
110 print $input->header(-status => 304);
115 my %PO_LANGUAGES = map { $_ => 1 } @LANGUAGES;
116 my %DDTP_LANGUAGES = map { $_ => 1 } @DDTP_LANGUAGES;
117 my $acc = I18N::AcceptLanguage->new(defaultLanguage => 'en');
118 my $ddtp_lang = $acc->accepts( $input->http("Accept-Language"),
120 my $po_lang = $acc->accepts( $input->http("Accept-Language"),
122 debug( "LANGS=@LANGUAGES DDTP_LANGS=@DDTP_LANGUAGES header=".
123 ($input->http("Accept-Language")||'').
124 " po_lang=$po_lang ddtp_lang=$ddtp_lang", 1 ) if DEBUG;
126 # backwards compatibility stuff
127 debug( "SCRIPT_URL=$ENV{SCRIPT_URL} SCRIPT_URI=$ENV{SCRIPT_URI}" ) if DEBUG;
129 if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_|) {
130 error( "You reached this site over an old URL. ".
131 "Depending on the exact parameters your search might work or not." );
132 # contents search changed a lot
133 if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_contents|) {
134 $input->param('keywords',$input->param('word')) if $input->param('word');
135 $input->param('searchon','contents');
136 for ($input->param('searchmode')) {
137 /^searchfiles/ && do {
138 $input->param('mode','filename');
142 $ENV{PATH_INFO} = '/'.join('/',($input->param('version')||'stable',
143 $input->param('keywords'),
144 $input->param('arch')||'i386',
146 $input->delete('searchon','version','keywords','arch');
152 if ($ENV{is_reportbug}) {
153 $input->param('exact', 1);
154 debug( "reportbug detected, set paramater exact to '1'" ) if DEBUG;
157 my $what_to_do = 'show';
159 if (my $path = $ENV{'PATH_INFO'} || $input->param('PATH_INFO')) {
160 my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
162 debug( "PATH_INFO=$path components=@components", 3) if DEBUG;
164 push @components, 'index' if @components && $path =~ m,/$,;
166 my %LANGUAGES = map { $_ => 1 } (@LANGUAGES, @DDTP_LANGUAGES);
167 if (@components > 1 and $LANGUAGES{$components[0]}
168 and !$input->param('lang')) {
169 $input->param( 'lang', shift(@components) );
171 if (@components > 1 and $components[0] eq 'source') {
173 $input->param( 'source', 1 );
175 if (@components > 0 and $components[0] eq 'search') {
177 $what_to_do = 'search';
179 fatal_error( "search doesn't take any more path elements" )
181 } elsif (@components == 0) {
182 fatal_error( "We're supposed to display the homepage here, instead of getting dispatch.pl" );
183 } elsif (@components == 1) {
184 if ($components[0] eq 'index') {
185 $what_to_do = 'homepage';
187 $what_to_do = 'search';
191 for ($components[-1]) {
192 /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
199 my %SUITES = map { $_ => 1 } @SUITES;
200 my %SECTIONS = map { $_ => 1 } @SECTIONS;
201 my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
202 my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all', 'any');
203 my %PRIORITIES = map { $_ => 1 } @PRIORITIES;
206 my ($cgi, $params_set, $key, $val) = @_;
207 debug("set_param_once key=$key val=$val",4) if DEBUG;
208 if ($params_set->{$key}++) {
209 fatal_error( "$key set more than once in path" );
211 $cgi->param( $key, $val );
215 my (@pkg, $need_pkg);
216 foreach (reverse @components) {
218 && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
219 debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
220 if (!$need_pkg && $SUITES{$_}) {
221 set_param_once( $input, \%params_set, 'suite', $_);
222 } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
223 set_param_once( $input, \%params_set, 'suite', $s);
224 } elsif (!$need_pkg && $SECTIONS{$_}) {
225 set_param_once( $input, \%params_set, 'section', $_);
226 } elsif (!$need_pkg && $sections_descs{$_}) {
227 set_param_once( $input, \%params_set, 'subsection', $_);
228 } elsif (!$need_pkg && ($_ eq 'source')) {
229 set_param_once( $input, \%params_set, 'source', 1);
230 } elsif ($ARCHITECTURES{$_}) {
231 set_param_once( $input, \%params_set, 'arch', $_)
233 } elsif (!$need_pkg && $ARCHIVES{$_}) {
234 set_param_once( $input, \%params_set, 'archive', $_);
235 } elsif ($PRIORITIES{$_}) {
236 set_param_once( $input, \%params_set, 'priority', $_);
243 if (@components > 1) {
244 fatal_error( "two or more packages specified (@components)" );
246 } # else if (@components == 1)
249 my $c = uri_unescape($components[0]);
250 $input->param( 'keywords', $c );
251 $input->param( 'package', $c );
255 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
257 my %params_def = ( keywords => { default => undef,
259 match => '^([-+\@\w\/.:]+)$',
261 'package' => { default => undef,
262 match => '^([\w.+-]+)$',
264 suite => { default => 'default', match => '^([\w-]+)$',
265 array => ',', var => \@suites,
266 replace => { all => \@SUITES,
269 archive => { default => ($what_to_do eq 'search') ?
271 match => '^([\w-]+)$',
272 array => ',', var => \@archives,
273 replace => { all => \@ARCHIVES,
274 default => \@ARCHIVES} },
275 exact => { default => 0, match => '^(\w+)$', },
276 lang => { default => undef, match => '^([\w-]+)$', },
277 source => { default => 0, match => '^(\d+)$', },
278 debug => { default => 0, match => '^(\d+)$', },
279 searchon => { default => 'names', match => '^(\w+)$', },
280 section => { default => 'all', match => '^([\w-]+)$',
281 alias => 'release', array => ',',
283 replace => { all => \@SECTIONS } },
284 subsection => { default => 'default', match => '^([\w-]+)$',
285 array => ',', var => \@subsections,
286 replace => { default => [] } },
287 priority => { default => 'default', match => '^([\w-]+)$',
289 replace => { default => [] } },
290 arch => { default => 'any', match => '^([\w-]+)$',
291 array => ',', var => \@archs, replace =>
292 { any => \@ARCHITECTURES } },
293 'format' => { default => 'html', match => '^([\w.]+)$', },
294 mode => { default => '', match => '^(\w+)$', },
295 sort_by => { default => 'file', match => '^(\w+)$', },
298 my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
299 Packages::CGI::init_url( $input, \%params, \%opts );
301 if (defined($opts{lang})) {
302 $opts{po_lang} = $PO_LANGUAGES{$opts{lang}} ? $opts{lang} : 'en';
303 $opts{ddtp_lang} = $DDTP_LANGUAGES{$opts{lang}} ? $opts{lang} : 'en';
305 $opts{po_lang} = $po_lang;
306 $opts{ddtp_lang} = $ddtp_lang;
308 my $charset = "UTF-8";
309 my $cat = Packages::I18N::Locale->get_handle( $opts{po_lang}, 'en' )
310 or die "get_handle failed for $opts{po_lang}";
313 $opts{h_suites} = { map { $_ => 1 } @suites };
314 $opts{h_sections} = { map { $_ => 1 } @sections };
315 $opts{h_archives} = { map { $_ => 1 } @archives };
316 $opts{h_archs} = { map { $_ => 1 } @archs };
318 if ((($opts{searchon} eq 'names') && $opts{source}) ||
319 ($opts{searchon} eq 'sourcenames')) {
321 $opts{searchon} = 'names',
322 $opts{searchon_form} = 'sourcenames';
324 $opts{searchon_form} = $opts{searchon};
326 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
327 $what_to_do = 'search_contents';
330 my $pet1 = new Benchmark;
331 my $petd = timediff($pet1, $pet0);
332 debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
334 my $template = new Packages::Template( $TEMPLATEDIR, $opts{format},
335 { po_lang => $opts{po_lang}, ddtp_lang => $opts{ddtp_lang},
336 charset => $charset, cat => $cat,
337 debug => ( DEBUG ? $opts{debug} : 0 ) },
338 ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
341 unless (($what_to_do eq 'allpackages' and $opts{format} =~ /^(html|txt\.gz)/)
342 || ($what_to_do eq 'index' and $opts{format} eq 'html')
343 || -e "$TEMPLATEDIR/$opts{format}/${what_to_do}.tmpl") {
344 fatal_error( $cat->g("requested format not available for this document"),
345 "406 requested format not available");
349 unless (@Packages::CGI::fatal_errors) {
351 &{"do_$what_to_do"}( \%params, \%opts, \%page_content );
354 $page_content{opts} = \%opts;
355 $page_content{params} = \%params;
357 unless (@Packages::CGI::fatal_errors) {
358 print $input->header(-charset => $charset,
359 -type => get_mime($opts{format}),
360 -vary => 'negotiate,accept-language',
361 -last_modified => strftime("%a, %d %b %Y %T %z",
362 localtime($last_modified)),
363 -expires => strftime("%a, %d %b %Y %T %z",
364 localtime($expires)),
367 #print '<pre>'.Dumper(\%ENV, \%page_content, get_all_messages()).'</pre>';
368 print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
369 } elsif ($Packages::CGI::http_code && $Packages::CGI::http_code !~ /^2\d\d/) {
370 print $input->header( -charset => $charset, -status => $Packages::CGI::http_code );
372 # We currently have only an error page in html
373 # so no format support here
374 print $input->header( -charset => $charset );
375 print $template->error_page( get_all_messages() );