From 0457ce3d5601de62f695d8a0759a7c0b6923701c Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Thu, 26 Nov 2020 14:11:40 -0500 Subject: mkslackinfo: add github URL rewriting, check the result against Content-Disposition --- mkslackinfo | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/mkslackinfo b/mkslackinfo index 48ef089..40198dd 100755 --- a/mkslackinfo +++ b/mkslackinfo @@ -92,12 +92,59 @@ EOF return $cmd; } +sub check_github_disposition { + my $url = shift; + my $filename = shift; + my $disposition; + + warn "Checking github Content-Disposition header\n"; + open my $fh, "wget --quiet --save-headers -O- $url|"; + while(<$fh>) { + s/\r//; + if(/^Content-Disposition:\s*attachment;\s*filename=(.*)$/i) { + $disposition = $1; + last; + } + last if /^$/; + } + close $fh; + + if(not defined $disposition) { + die "!!! Got no Content-Disposition header from github!\n"; + } elsif($filename eq $disposition) { + warn "=== URL filename matches Content-Disposition filename, good.\n"; + } else { + die "!!! Content-Disposition filename mismatch:\n!!! local: $filename\n!!! remote: $disposition\n"; + } +} + +sub fix_github_url { + my $u = shift; + return $u unless ($u =~ m,https?://github.com/([^/]+)/([^/]+)/archive/([^/]+)(\.(?:tar.gz|zip))$,); + my $user = $1; + my $proj = $2; + my $aversion = $3; + my $pversion = $3; + my $ext = $4; + + warn "Converting short-form github URL to long form\n"; + $pversion =~ s/^v//; # maybe other letters? + my $filename = "$proj-$pversion$ext"; + $u = "https://github.com/$user/$proj/archive/$aversion/$proj-$pversion$ext"; + check_github_disposition($u, $filename); + warn "=== Rewrote github URL as: $url\n"; + return $u; +} + +### main() $url = shift || &usage; # Take package name from the current dir chomp ($package = `pwd`); $package =~ s,.*/,,; +$url = fix_github_url($url); + # Take filename from last part of URL ($file = $url) =~ s,.*/,,; -- cgit v1.2.3