]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
www_coderepo: serve CSS files per-repo
authorEric Wong <e@80x24.org>
Tue, 1 Oct 2024 07:40:09 +0000 (07:40 +0000)
committerEric Wong <e@80x24.org>
Wed, 2 Oct 2024 17:40:47 +0000 (17:40 +0000)
For WWW admins configuring CSS via <link> or @import directives,
we need to ensure users can download per-repo CSS files via
$CODEREPO_NICK/$KEY.css

lib/PublicInbox/WWW.pm
lib/PublicInbox/WwwCoderepo.pm

index 4dfe83c135a95e3b241f7ae098a44e6a8882d0a0..420132d9e8818ecc709ce2901d7a9f226fd6b3c2 100644 (file)
@@ -747,7 +747,11 @@ sub get_css ($$$) {
        if (!defined($rec) && defined($inbox) && $key eq 'userContent') {
                $rec = [ PublicInbox::UserContent::sample($ctx) ];
        }
-       $rec // return r404();
+       srv_css_rec($rec);
+}
+
+sub srv_css_rec { # $_[0] may be ctx->{www}
+       my $rec = $_[-1] // return r404();
        my ($css, undef) = @$rec; # TODO: Last-Modified + If-Modified-Since
        my $h = [ 'Content-Length', length($css), 'Content-Type', 'text/css' ];
        PublicInbox::GitHTTPBackend::cache_one_year($h);
index 5e086feec29db19d1e1024ec5b0b9d5bc680afa8..413dfee53548d41173938eeccafc0845e2d776e6 100644 (file)
@@ -322,6 +322,12 @@ sub refs_foo { # /$REPO/refs/{heads,tags} endpoints
        $qsp->psgi_yield($ctx->{env}, undef, \&_refs_parse_hdr, $ctx);
 }
 
+sub _get_css ($$) {
+       my ($ctx, $key) = @_;
+       $ctx->{www}->{-css_map} // $ctx->{www}->stylesheets_prepare('');
+       PublicInbox::WWW::srv_css_rec $ctx->{www}->{-css_map}->{$key};
+}
+
 sub srv { # endpoint called by PublicInbox::WWW
        my ($self, $ctx) = @_;
        my $path_info = $ctx->{env}->{PATH_INFO};
@@ -357,6 +363,9 @@ sub srv { # endpoint called by PublicInbox::WWW
        } elsif ($path_info =~ m!\A/(.+?)/(refs/(?:heads|tags))/\z! and
                        ($ctx->{git} = $pi_cfg->get_coderepo($1))) {
                refs_foo($self, $ctx, $2);
+       } elsif ($path_info =~ m!\A/(.+?)/([a-zA-Z0-9_\-\.]+)\.css\z! &&
+                       $pi_cfg->get_coderepo($1)) {
+               _get_css $ctx, $2;
        } elsif ($path_info =~ m!\A/(.*?\*.*?)/*\z!) {
                my $re = PublicInbox::Config::glob2re($1);
                PublicInbox::RepoList::html($self, $ctx, qr!$re\z!) // r(404);