X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=lib%2FPackages%2FCGI.pm;h=b15e9edc63162da758865f07f4607bc8a6cf90f7;hb=2c8c7f257bafd443f4e0fe5ec746af4d18b6fb77;hp=94986cba7a3c0860982ec3fce14b3f8a8daa5d26;hpb=ab47ae363dddbc35743572c62fae6350dcb7cf96;p=deb%2Fpackages.git
diff --git a/lib/Packages/CGI.pm b/lib/Packages/CGI.pm
index 94986cb..b15e9ed 100644
--- a/lib/Packages/CGI.pm
+++ b/lib/Packages/CGI.pm
@@ -4,25 +4,39 @@ use strict;
use warnings;
use Exporter;
-use Packages::Config;
our @ISA = qw( Exporter );
-our @EXPORT = qw( fatal_error error hint debug msg note
- print_errors print_hints print_debug print_msgs
- print_notes DEBUG make_url make_search_url );
+our @EXPORT = qw( DEBUG debug fatal_error get_mime );
+our @EXPORT_OK = qw( error hint msg note get_all_messages
+ make_url make_search_url );
# define this to 0 in production mode
use constant DEBUG => 1;
our $debug = 0;
+my %mime_types = (
+ txt => 'text/plain',
+ 'txt.gz' => 'text/plain',
+ html => 'text/html',
+ rss => 'application/rss+xml',
+ rfc822 => 'text/plain',
+ );
+
+sub get_mime {
+ return $mime_types{$_[0]} || $_[1] || 'text/html';
+}
+
our (@fatal_errors, @errors, @debug, @msgs, @hints, @notes);
+our $http_code;
sub reset {
@fatal_errors = @errors = @debug = @msgs = @hints = @notes = ();
+ $http_code = 200;
}
sub fatal_error {
push @fatal_errors, $_[0];
+ $http_code = $_[1] if $_[1];
}
sub error {
push @errors, $_[0];
@@ -40,50 +54,22 @@ sub msg {
sub note {
push @notes, [ @_ ];
}
-sub print_errors {
- return unless @fatal_errors || @errors;
- print '
';
- foreach ((@fatal_errors, @errors)) {
- print "
ERROR: $_
";
- }
- print '
';
-}
-sub print_debug {
+sub get_errors { (@fatal_errors, @errors) }
+sub get_debug {
return unless $debug && @debug;
- print '';
- print '
Debugging:
';
- foreach (@debug) {
- print "$_\n";
- }
- print '
';
+ return @debug;
}
-sub print_hints {
- return unless @hints;
- print '';
- foreach (@hints) {
- print "
$_
";
- }
- print '
';
-}
-sub print_msgs {
- print '';
- foreach (@msgs) {
- print "
$_
";
- }
- print '
';
-}
-sub print_notes {
- foreach (@notes) {
- my ( $title, $note ) = @$_;
-
- print '';
- if ($note) {
- print "
$title
";
- } else {
- $note = $title;
- }
- print "
$note
";
- }
+sub get_msgs { @msgs };
+sub get_hints { @hints };
+sub get_notes { @notes };
+sub get_all_messages {
+ return {
+ errors => [ @fatal_errors, @errors ],
+ debugs => $debug ? \@debug : [],
+ msgs => \@msgs,
+ hints => \@hints,
+ notes => \@notes,
+ };
}
our $USE_PAGED_MODE = 1;
@@ -360,6 +346,13 @@ sub printindexline {
# return ( $start, $end );
#}
+sub string2id {
+ my $string = "@_";
+
+ $string =~ s/[^\w:.-]/_/g;
+ return $string;
+}
+
our ( %url_params, %query_params );
sub init_url {
@@ -400,9 +393,14 @@ sub init_url {
}
sub make_url {
- my ($add_path, $add_query, $override) = @_;
+ my ($add_path, $add_query, @override) = @_;
my (@path, @query_string) = ()x2;
- $override ||= {};
+ my $override = {};
+ if (ref $override[0]) {
+ $override = $override[0];
+ } elsif (@override) {
+ $override = { @override };
+ }
push @path, $Packages::Config::ROOT;
foreach my $p (qw(lang source suite archive arch)) {
@@ -416,24 +414,31 @@ sub make_url {
$val = $override->{$p} if exists $override->{$p};
push @query_string, "$p=$val" if $val;
}
- push @path, $add_path if $add_path;
+ push @path, $add_path if $add_path and $add_path ne '/';
push @query_string, $add_query if $add_query;
my $path = join( '/', @path );
my $query_string = join( '&', @query_string );
+ $path .= '/' if $add_path and $add_path eq '/';
+ $path .= "?$query_string" if $query_string;
- return "$path?$query_string";
+ return $path;
}
sub make_search_url {
- my ($add_path, $add_query, $override) = @_;
+ my ($add_path, $add_query, @override) = @_;
my (@path, @query_string) = ()x2;
- $override ||= {};
+ my $override ||= {};
+ if (ref $override[0]) {
+ $override = $override[0];
+ } elsif (@override) {
+ $override = { @override };
+ }
push @path, $Packages::Config::SEARCH_URL
if $Packages::Config::SEARCH_URL;
foreach my $p (qw(lang source suite archive section subsection
- exact mode searchon format debug)) {
+ arch exact mode searchon format debug)) {
my $val = $url_params{$p};
$val = $query_params{$p} if exists $query_params{$p};
$val = $override->{$p} if exists $override->{$p};
@@ -443,7 +448,7 @@ sub make_search_url {
push @query_string, $add_query if $add_query;
my $path = join( '/', @path );
- my $query_string = join( '&', @query_string );
+ my $query_string = join( '&', @query_string );
return "$path?$query_string";
}