From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 1F1041F51B for ; Tue, 28 Nov 2023 14:56:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1701183389; bh=Lb+o2CjRYITHaHOrvSj5l+tezZ9las4cDM97GF/UiHM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=D+pnZKBhkR3TfbwsEv7T2L81td6Xr4STI5w2QEvozs+V/B4+ZrFSKNZWP/IFRt8a8 FYlxoereosYvXbdZT6UF3/r3qEnU7QKE6XbTn+Bo6dnf3bKbXxu8MRuaq8AVZ5FCQk 8udJyngm3S/pNh8fL1HNOhFPDetyY2/Y9eYU8/jE= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 07/14] hval: use File::Spec to make relative paths for href Date: Tue, 28 Nov 2023 14:56:20 +0000 Message-ID: <20231128145628.1455176-8-e@80x24.org> In-Reply-To: <20231128145628.1455176-1-e@80x24.org> References: <20231128145628.1455176-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: File::Spec->abs2rel doesn't touch the filesystem at all when given an absolute base arg ($env->{PATH_INFO}), so we can rely on it to generate relative links that work with the `mount' from Plack::Builder and also people running `wget -r' mirrors. --- lib/PublicInbox/Hval.pm | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/PublicInbox/Hval.pm b/lib/PublicInbox/Hval.pm index e9b9ae64..b804254a 100644 --- a/lib/PublicInbox/Hval.pm +++ b/lib/PublicInbox/Hval.pm @@ -13,6 +13,7 @@ our @EXPORT_OK = qw/ascii_html obfuscate_addrs to_filename src_escape to_attr prurl mid_href fmt_ts ts2str utf8_maybe/; use POSIX qw(strftime); my $enc_ascii = find_encoding('us-ascii'); +use File::Spec; # safe-ish acceptable filename pattern for portability our $FN = '[a-zA-Z0-9][a-zA-Z0-9_\-\.]+[a-zA-Z0-9]'; # needs \z anchor @@ -69,7 +70,16 @@ sub prurl ($$) { $u = $host_match[0] // $u->[0]; # fall through to below: } - index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u; + my $dslash = index($u, '//'); + if ($dslash == 0) { + "$env->{'psgi.url_scheme'}:$u" + } elsif ($dslash < 0 && substr($u, 0, 1) ne '/' && + substr(my $path = $env->{PATH_INFO}, 0, 1) eq '/') { + # this won't touch the FS at all: + File::Spec->abs2rel("/$u", $path); + } else { + $u; + } } # for misguided people who believe in this stuff, give them a