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;
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 );
40 use Packages::Search qw( :all );
41 use Packages::Template ();
42 use Packages::Sections;
43 use Packages::I18N::Locale;
45 use Packages::DoSearch;
46 use Packages::DoSearchContents;
48 use Packages::DoIndex;
49 use Packages::DoNewPkg;
50 use Packages::DoDownload;
51 use Packages::DoFilelist;
56 &Packages::CGI::reset;
57 $Packages::Search::too_many_hits = 0;
60 $ENV{PATH} = "/bin:/usr/bin";
61 delete $ENV{'LANGUAGE'};
63 delete $ENV{'LC_ALL'};
64 delete $ENV{'LC_MESSAGES'};
66 my %SUITES_ALIAS = ( oldstable => 'sarge',
73 # Read in all the variables set by the form
75 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
76 $input = new CGI(\*STDIN);
80 my $cgi_error = $input->cgi_error;
82 fatal_error( "Error parsing the request", $cgi_error );
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;
91 my $homedir = dirname($ENV{SCRIPT_FILENAME}).'/../';
92 &Packages::Config::init( $homedir );
93 &Packages::DB::init();
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"),
100 my $po_lang = $acc->accepts( $input->http("Accept-Language"),
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;
106 # backwards compatibility stuff
107 debug( "SCRIPT_URL=$ENV{SCRIPT_URL} SCRIPT_URI=$ENV{SCRIPT_URI}" ) if DEBUG;
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');
122 $ENV{PATH_INFO} = '/'.join('/',($input->param('version')||'stable',
123 $input->param('keywords'),
124 $input->param('arch')||'i386',
126 $input->delete('searchon','version','keywords','arch');
132 if ($ENV{is_reportbug}) {
133 $input->param('exact', 1);
134 debug( "reportbug detected, set paramater exact to '1'" ) if DEBUG;
137 my $what_to_do = 'show';
139 if (my $path = $ENV{'PATH_INFO'} || $input->param('PATH_INFO')) {
140 my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
142 debug( "PATH_INFO=$path components=@components", 3) if DEBUG;
144 push @components, 'index' if @components && $path =~ m,/$,;
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) );
151 if (@components > 1 and $components[0] eq 'source') {
153 $input->param( 'source', 1 );
155 if (@components > 0 and $components[0] eq 'search') {
157 $what_to_do = 'search';
159 fatal_error( "search doesn't take any more path elements" )
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';
167 for ($components[-1]) {
168 /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
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;
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" );
187 $cgi->param( $key, $val );
191 my (@pkg, $need_pkg);
192 foreach (reverse @components) {
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', $_)
209 } elsif (!$need_pkg && $ARCHIVES{$_}) {
210 set_param_once( $input, \%params_set, 'archive', $_);
211 } elsif ($PRIORITIES{$_}) {
212 set_param_once( $input, \%params_set, 'priority', $_);
219 if (@components > 1) {
220 fatal_error( "two or more packages specified (@components)" );
222 } # else if (@components == 1)
225 my $c = uri_unescape($components[0]);
226 $input->param( 'keywords', $c );
227 $input->param( 'package', $c );
231 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
233 my %params_def = ( keywords => { default => undef,
235 match => '^([-+\@\w\/.:]+)$',
237 'package' => { default => undef,
238 match => '^([\w.+-]+)$',
240 suite => { default => 'default', match => '^([\w-]+)$',
241 array => ',', var => \@suites,
242 replace => { all => \@SUITES,
245 archive => { default => ($what_to_do eq 'search') ?
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 => ',',
259 replace => { all => \@SECTIONS } },
260 subsection => { default => 'default', match => '^([\w-]+)$',
261 array => ',', var => \@subsections,
262 replace => { default => [] } },
263 priority => { default => 'default', match => '^([\w-]+)$',
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+)$', },
274 my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
275 Packages::CGI::init_url( $input, \%params, \%opts );
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';
281 $opts{po_lang} = $po_lang;
282 $opts{ddtp_lang} = $ddtp_lang;
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}";
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 };
294 if ((($opts{searchon} eq 'names') && $opts{source}) ||
295 ($opts{searchon} eq 'sourcenames')) {
297 $opts{searchon} = 'names',
298 $opts{searchon_form} = 'sourcenames';
300 $opts{searchon_form} = $opts{searchon};
302 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
303 $what_to_do = 'search_contents';
306 my $pet1 = new Benchmark;
307 my $petd = timediff($pet1, $pet0);
308 debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
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 } : {} ) );
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");
325 unless (@Packages::CGI::fatal_errors) {
327 &{"do_$what_to_do"}( \%params, \%opts, \%page_content );
330 $page_content{opts} = \%opts;
331 $page_content{params} = \%params;
333 unless (@Packages::CGI::fatal_errors) {
334 print $input->header(-charset => $charset, -type => get_mime($opts{format}) );
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 );
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() );