Skip to content

Commit 1ec2fb5

Browse files
Oblomovgitster
authored andcommitted
gitweb: retrieve snapshot format from PATH_INFO
We parse requests for $project/snapshot/$head.$sfx as equivalent to $project/snapshot/$head?sf=$sfx, where $sfx is any of the known (although not necessarily supported) snapshot formats (or its default suffix). The filename for the resulting package preserves the requested extensions (so asking for a .tgz gives a .tgz, and asking for a .tar.gz gives a .tar.gz), although for obvious reasons it doesn't preserve the basename (git/snapshot/next.tgz returns a file names git-next.tgz). This introduces a potential case for ambiguity if a project has a head that ends with a snapshot-like suffix (.zip, .tgz, .tar.gz, etc) and the sf CGI parameter is not present; however, gitweb only produces URLs with the sf parameter currently, so this is only a potential issue for hand-coded URLs for extremely unusual project. Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5e16684 commit 1ec2fb5

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

gitweb/gitweb.perl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,45 @@ sub evaluate_path_info {
616616
$input_params{'hash_parent'} ||= $parentrefname;
617617
}
618618
}
619+
620+
# for the snapshot action, we allow URLs in the form
621+
# $project/snapshot/$hash.ext
622+
# where .ext determines the snapshot and gets removed from the
623+
# passed $refname to provide the $hash.
624+
#
625+
# To be able to tell that $refname includes the format extension, we
626+
# require the following two conditions to be satisfied:
627+
# - the hash input parameter MUST have been set from the $refname part
628+
# of the URL (i.e. they must be equal)
629+
# - the snapshot format MUST NOT have been defined already (e.g. from
630+
# CGI parameter sf)
631+
# It's also useless to try any matching unless $refname has a dot,
632+
# so we check for that too
633+
if (defined $input_params{'action'} &&
634+
$input_params{'action'} eq 'snapshot' &&
635+
defined $refname && index($refname, '.') != -1 &&
636+
$refname eq $input_params{'hash'} &&
637+
!defined $input_params{'snapshot_format'}) {
638+
# We loop over the known snapshot formats, checking for
639+
# extensions. Allowed extensions are both the defined suffix
640+
# (which includes the initial dot already) and the snapshot
641+
# format key itself, with a prepended dot
642+
while (my ($fmt, %opt) = each %known_snapshot_formats) {
643+
my $hash = $refname;
644+
my $sfx;
645+
$hash =~ s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//;
646+
next unless $sfx = $1;
647+
# a valid suffix was found, so set the snapshot format
648+
# and reset the hash parameter
649+
$input_params{'snapshot_format'} = $fmt;
650+
$input_params{'hash'} = $hash;
651+
# we also set the format suffix to the one requested
652+
# in the URL: this way a request for e.g. .tgz returns
653+
# a .tgz instead of a .tar.gz
654+
$known_snapshot_formats{$fmt}{'suffix'} = $sfx;
655+
last;
656+
}
657+
}
619658
}
620659
evaluate_path_info();
621660

0 commit comments

Comments
 (0)