From: Eric Wong Date: Tue, 1 Oct 2024 07:40:09 +0000 (+0000) Subject: www_coderepo: serve CSS files per-repo X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c91d3916b5776920af7694323cbfd7f7485cce16;p=thirdparty%2Fpublic-inbox.git www_coderepo: serve CSS files per-repo For WWW admins configuring CSS via or @import directives, we need to ensure users can download per-repo CSS files via $CODEREPO_NICK/$KEY.css --- diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm index 4dfe83c13..420132d9e 100644 --- a/lib/PublicInbox/WWW.pm +++ b/lib/PublicInbox/WWW.pm @@ -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); diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm index 5e086feec..413dfee53 100644 --- a/lib/PublicInbox/WwwCoderepo.pm +++ b/lib/PublicInbox/WwwCoderepo.pm @@ -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);