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 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 213D91F5D3; Thu, 26 Sep 2024 00:55:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1727312107; bh=HLZgeJlPctZ536+Z4m2at1REK7Hjwp2Avc4+mjNCo9w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1W/1SZzUtpbvaSMx7pGSOYYTi3S0iZs2FN2GOV11IsGhvNbU9UHFM6ZdmjVeVNxb1 CT1bSWzHr1mN1pShsKv1ltJuI4gTNiooczOARehJs9BveUr0eSX2YYnoHsHFY38+Xk 42jt/JxseDa4SoyNH1zMwW02e3sRuCt2ZfdRJMdU= From: Eric Wong To: meta@public-inbox.org Cc: "Robin H. Johnson" Subject: [PATCH 3/5] www: load CSS files in same dir if @import is in use Date: Thu, 26 Sep 2024 00:55:03 +0000 Message-ID: <20240926005506.3703216-4-e@80x24.org> In-Reply-To: <20240926005506.3703216-1-e@80x24.org> References: <20240926005506.3703216-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: In case user-supplied CSS uses @import statements, load any other unspecified CSS files in that directory to ensure they can be accessed by clients. --- lib/PublicInbox/WWW.pm | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm index 6f7c30b9..65f95eb8 100644 --- a/lib/PublicInbox/WWW.pm +++ b/lib/PublicInbox/WWW.pm @@ -13,6 +13,7 @@ package PublicInbox::WWW; use strict; use v5.10.1; +use autodie qw(chdir opendir); use PublicInbox::Config; use PublicInbox::Git; use PublicInbox::Hval; @@ -593,6 +594,7 @@ sub stylesheets_prepare ($$) { my $stylesheets = $self->{pi_cfg}->{css} || []; my $links = []; my $inline_ok = 1; + my (%css_dir, @css_dir); foreach my $s (@$stylesheets) { my $attr = {}; @@ -606,8 +608,9 @@ sub stylesheets_prepare ($$) { if (defined $attr->{href}) { $inline_ok = 0; } else { - my $fn = $_; - my ($key) = (m!([^/]+?)(?:\.css)?\z!i); + my ($fn, $dir, $key); + $fn = $_; + ($dir, $key) = (m!\A(?:(.+?)/)?([^/]+?)(?:\.css)?\z!i); if ($key !~ /\A[a-zA-Z0-9_\-\.]+\z/) { warn "ignoring $fn, non-ASCII word character\n"; next; @@ -617,6 +620,8 @@ sub stylesheets_prepare ($$) { ($local, $ctime) = @$rec; } elsif (open(my $fh, '<', $fn)) { ($local, $ctime) = _read_css $fh, $mini, $fn; + $local =~ /\@import\b/ && !$css_dir{$dir}++ and + push @css_dir, $dir; $css_map->{$key} = [ $local, $ctime ]; } else { warn "failed to open $fn: $!\n"; @@ -661,6 +666,31 @@ sub stylesheets_prepare ($$) { } else { $self->{-style_inline} = $buf; } + # load potentially imported CSS files in known CSS directories + if (@css_dir && !$self->{-css_dir}) { + opendir my $cwddh, '.'; + for my $d (@css_dir) { + CORE::opendir my $dh, $d or do { + warn "W: opendir($d): $!"; + next; + }; + chdir $dh; + my @css = grep /\.css\z/i, readdir $dh; + for my $fn (@css) { + my ($key) = ($fn =~ m!([^/]+?)(?:\.css)?\z!i); + next if $css_map->{$key}; + -f $fn or next; + # no warning for autoloaded CSS + open my $fh, '<', $fn or next; + -T $fh or next; + my ($local, $ctime) = + _read_css $fh, $mini, "$d/$fn"; + $css_map->{$key} = [ $local, $ctime ]; + } + chdir $cwddh; + } + $self->{-css_dir} = \@css_dir; + } $css_map; }