]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
ensure bytes::length is available to callers
[public-inbox.git] / lib / PublicInbox / WWW.pm
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Main web interface for mailing list archives
5 #
6 # We focus on the lowest common denominators here:
7 # - targeted at text-only console browsers (w3m, links, etc..)
8 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
9 #   and diff/syntax-highlighting (optional)
10 # - No JavaScript, graphics or icons allowed.
11 # - Must not rely on static content
12 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
13 package PublicInbox::WWW;
14 use 5.008;
15 use strict;
16 use warnings;
17 use bytes (); # only for bytes::length
18 use PublicInbox::Config;
19 use PublicInbox::Hval;
20 use URI::Escape qw(uri_unescape);
21 use PublicInbox::MID qw(mid_escape);
22 require PublicInbox::Git;
23 use PublicInbox::GitHTTPBackend;
24 use PublicInbox::UserContent;
25
26 # TODO: consider a routing tree now that we have more endpoints:
27 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
28 our $MID_RE = qr!([^/]+)!;
29 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
30 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
31 our $OID_RE = qr![a-f0-9]{7,40}!;
32
33 sub new {
34         my ($class, $pi_config) = @_;
35         $pi_config ||= PublicInbox::Config->new;
36         bless { pi_config => $pi_config }, $class;
37 }
38
39 # backwards compatibility, do not use
40 sub run {
41         my ($req, $method) = @_;
42         PublicInbox::WWW->new->call($req->env);
43 }
44
45 my %path_re_cache;
46
47 sub path_re ($) {
48         my $sn = $_[0]->{SCRIPT_NAME};
49         $path_re_cache{$sn} ||= do {
50                 $sn = '/'.$sn unless index($sn, '/') == 0;
51                 $sn =~ s!/\z!!;
52                 qr!\A(?:https?://[^/]+)?\Q$sn\E(/[^\?\#]+)!;
53         };
54 }
55
56 sub call {
57         my ($self, $env) = @_;
58         my $ctx = { env => $env, www => $self };
59
60         # we don't care about multi-value
61         my %qp = map {
62                 utf8::decode($_);
63                 my ($k, $v) = split('=', uri_unescape($_), 2);
64                 $v = '' unless defined $v;
65                 $v =~ tr/+/ /;
66                 ($k, $v)
67         } split(/[&;]+/, $env->{QUERY_STRING});
68         $ctx->{qp} = \%qp;
69
70         # not using $env->{PATH_INFO} here since that's already decoded
71         my ($path_info) = ($env->{REQUEST_URI} =~ path_re($env));
72         my $method = $env->{REQUEST_METHOD};
73
74         if ($method eq 'POST') {
75                 if ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?(git-upload-pack)\z!) {
76                         my ($part, $path) = ($2, $3);
77                         return invalid_inbox($ctx, $1) ||
78                                 serve_git($ctx, $part, $path);
79                 } elsif ($path_info =~ m!$INBOX_RE/!o) {
80                         return invalid_inbox($ctx, $1) || mbox_results($ctx);
81                 }
82         }
83         elsif ($method !~ /\AGET|HEAD\z/) {
84                 return r(405, 'Method Not Allowed');
85         }
86
87         # top-level indices and feeds
88         if ($path_info eq '/') {
89                 r404();
90         } elsif ($path_info =~ m!$INBOX_RE\z!o) {
91                 invalid_inbox($ctx, $1) || r301($ctx, $1);
92         } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
93                 invalid_inbox($ctx, $1) || get_index($ctx);
94         } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
95                 invalid_inbox($ctx, $1) || get_atom($ctx);
96         } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
97                 invalid_inbox($ctx, $1) || get_new($ctx);
98         } elsif ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?
99                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
100                 my ($part, $path) = ($2, $3);
101                 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $path);
102         } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
103                 serve_mbox_range($ctx, $1, $2);
104         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
105                 msg_page($ctx, $1, $2, $3);
106
107         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
108                 my ($idx, $fn) = ($3, $4);
109                 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
110         # in case people leave off the trailing slash:
111         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
112                 my ($inbox, $mid_ue, $suffix) = ($1, $2, $3);
113                 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
114                 r301($ctx, $inbox, $mid_ue, $suffix);
115
116         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
117                 my ($inbox, $mid_ue) = ($1, $2);
118                 r301($ctx, $inbox, $mid_ue, '#R');
119
120         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
121                 r301($ctx, $1, $2);
122         } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
123                 get_text($ctx, $1, $2);
124         } elsif ($path_info =~ m!$INBOX_RE/([\w\-\.]+)\.css\z!o) {
125                 get_css($ctx, $1, $2);
126         } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/\z!o) {
127                 get_vcs_object($ctx, $1, $2);
128         } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/([\w\.\-]+)\z!o) {
129                 get_vcs_object($ctx, $1, $2, $3);
130         } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s\z!o) {
131                 r301($ctx, $1, $2, 's/');
132         # convenience redirects order matters
133         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
134                 r301($ctx, $1, $2);
135
136         } else {
137                 legacy_redirects($ctx, $path_info);
138         }
139 }
140
141 # for CoW-friendliness, MOOOOO!
142 sub preload {
143         my ($self) = @_;
144         require PublicInbox::Feed;
145         require PublicInbox::View;
146         require PublicInbox::SearchThread;
147         require PublicInbox::MIME;
148         require Digest::SHA;
149         require POSIX;
150
151         foreach (qw(PublicInbox::Search PublicInbox::SearchView
152                         PublicInbox::Mbox IO::Compress::Gzip
153                         PublicInbox::NewsWWW)) {
154                 eval "require $_;";
155         }
156         if (ref($self)) {
157                 $self->stylesheets_prepare($_) for ('', '../', '../../');
158         }
159 }
160
161 # private functions below
162
163 sub r404 {
164         my ($ctx) = @_;
165         if ($ctx && $ctx->{mid}) {
166                 require PublicInbox::ExtMsg;
167                 searcher($ctx);
168                 return PublicInbox::ExtMsg::ext_msg($ctx);
169         }
170         r(404, 'Not Found');
171 }
172
173 # simple response for errors
174 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
175
176 # returns undef if valid, array ref response if invalid
177 sub invalid_inbox ($$) {
178         my ($ctx, $inbox) = @_;
179         my $www = $ctx->{www};
180         my $obj = $www->{pi_config}->lookup_name($inbox);
181         if (defined $obj) {
182                 $ctx->{git} = $obj->git;
183                 $ctx->{-inbox} = $obj;
184                 return;
185         }
186
187         # sometimes linkifiers (not ours!) screw up automatic link
188         # generation and link things intended for nntp:// to https?://,
189         # so try to infer links and redirect them to the appropriate
190         # list URL.
191         $www->news_www->call($ctx->{env});
192 }
193
194 # returns undef if valid, array ref response if invalid
195 sub invalid_inbox_mid {
196         my ($ctx, $inbox, $mid_ue) = @_;
197         my $ret = invalid_inbox($ctx, $inbox);
198         return $ret if $ret;
199
200         my $mid = $ctx->{mid} = uri_unescape($mid_ue);
201         my $ibx = $ctx->{-inbox};
202         if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
203                 my ($x2, $x38) = ($1, $2);
204                 # this is horrifically wasteful for legacy URLs:
205                 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
206                 require Email::Simple;
207                 my $s = Email::Simple->new($str);
208                 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
209                 return r301($ctx, $inbox, mid_escape($mid));
210         }
211         undef;
212 }
213
214 # /$INBOX/new.atom                     -> Atom feed, includes replies
215 sub get_atom {
216         my ($ctx) = @_;
217         require PublicInbox::Feed;
218         PublicInbox::Feed::generate($ctx);
219 }
220
221 # /$INBOX/new.html                      -> HTML only
222 sub get_new {
223         my ($ctx) = @_;
224         require PublicInbox::Feed;
225         PublicInbox::Feed::new_html($ctx);
226 }
227
228 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
229 sub get_index {
230         my ($ctx) = @_;
231         require PublicInbox::Feed;
232         searcher($ctx);
233         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
234                 require PublicInbox::SearchView;
235                 PublicInbox::SearchView::sres_top_html($ctx);
236         } else {
237                 PublicInbox::Feed::generate_html_index($ctx);
238         }
239 }
240
241 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
242 sub get_mid_txt {
243         my ($ctx) = @_;
244         require PublicInbox::Mbox;
245         PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
246 }
247
248 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
249 sub get_mid_html {
250         my ($ctx) = @_;
251         require PublicInbox::View;
252         searcher($ctx);
253         PublicInbox::View::msg_page($ctx) || r404($ctx);
254 }
255
256 # /$INBOX/$MESSAGE_ID/t/
257 sub get_thread {
258         my ($ctx, $flat) = @_;
259         searcher($ctx) or return need_search($ctx);
260         $ctx->{flat} = $flat;
261         require PublicInbox::View;
262         PublicInbox::View::thread_html($ctx);
263 }
264
265 # /$INBOX/_/text/$KEY/
266 # /$INBOX/_/text/$KEY/raw
267 # KEY may contain slashes
268 sub get_text {
269         my ($ctx, $inbox, $key) = @_;
270         my $r404 = invalid_inbox($ctx, $inbox);
271         return $r404 if $r404;
272
273         require PublicInbox::WwwText;
274         PublicInbox::WwwText::get_text($ctx, $key);
275 }
276
277 # show git objects (blobs and commits)
278 # /$INBOX/_/$OBJECT_ID/show
279 # /$INBOX/_/${OBJECT_ID}_${FILENAME}
280 # KEY may contain slashes
281 sub get_vcs_object ($$$;$) {
282         my ($ctx, $inbox, $oid, $filename) = @_;
283         my $r404 = invalid_inbox($ctx, $inbox);
284         return $r404 if $r404;
285         require PublicInbox::ViewVCS;
286         PublicInbox::ViewVCS::show($ctx, $oid, $filename);
287 }
288
289 sub ctx_get {
290         my ($ctx, $key) = @_;
291         my $val = $ctx->{$key};
292         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
293         $val;
294 }
295
296 # search support is optional, returns undef if Xapian is not installed
297 # or not configured for the given GIT_DIR
298 sub searcher {
299         my ($ctx) = @_;
300         eval {
301                 require PublicInbox::Search;
302                 $ctx->{srch} = $ctx->{-inbox}->search;
303         };
304 }
305
306 sub need_search {
307         my ($ctx) = @_;
308         my $msg = <<EOF;
309 <html><head><title>Search not available for this
310 public-inbox</title><body><pre>Search is not available for this public-inbox
311 <a href="../">Return to index</a></pre></body></html>
312 EOF
313         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
314 }
315
316 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
317 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
318 # note: I'm not a big fan of other compression formats since they're
319 # significantly more expensive on CPU than gzip and less-widely available,
320 # especially on older systems.  Stick to zlib since that's what git uses.
321 sub get_thread_mbox {
322         my ($ctx, $sfx) = @_;
323         my $srch = searcher($ctx) or return need_search($ctx);
324         require PublicInbox::Mbox;
325         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
326 }
327
328
329 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
330 sub get_thread_atom {
331         my ($ctx) = @_;
332         searcher($ctx) or return need_search($ctx);
333         require PublicInbox::Feed;
334         PublicInbox::Feed::generate_thread_atom($ctx);
335 }
336
337 sub legacy_redirects {
338         my ($ctx, $path_info) = @_;
339
340         # single-message pages
341         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
342                 r301($ctx, $1, $2);
343         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
344                 r301($ctx, $1, $2, 'raw');
345
346         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
347                 r301($ctx, $1, $2);
348
349         # thread display
350         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
351                 r301($ctx, $1, $2, 't/#u');
352
353         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
354                 r301($ctx, $1, $2, "t.mbox$3");
355
356         # even older legacy redirects
357         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
358                 r301($ctx, $1, $2);
359
360         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
361                 r301($ctx, $1, $2, 't/#u');
362
363         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
364                 r301($ctx, $1, $2);
365
366         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
367                 r301($ctx, $1, $2, 'raw');
368
369         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
370                 r301($ctx, $1, $2, "t$3");
371
372         # legacy convenience redirects, order still matters
373         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
374                 r301($ctx, $1, $2);
375         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
376                 r301($ctx, $1, $2, 't/#u');
377         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
378                 r301($ctx, $1, $2);
379
380         # some Message-IDs have slashes in them and the HTTP server
381         # may try to be clever and unescape them :<
382         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
383                 msg_page($ctx, $1, $2, $3);
384
385         # in case people leave off the trailing slash:
386         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
387                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
388         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
389                 r301($ctx, $1, $2);
390         } else {
391                 $ctx->{www}->news_www->call($ctx->{env});
392         }
393 }
394
395 sub r301 {
396         my ($ctx, $inbox, $mid_ue, $suffix) = @_;
397         my $obj = $ctx->{-inbox};
398         unless ($obj) {
399                 my $r404 = invalid_inbox($ctx, $inbox);
400                 return $r404 if $r404;
401                 $obj = $ctx->{-inbox};
402         }
403         my $url = $obj->base_url($ctx->{env});
404         my $qs = $ctx->{env}->{QUERY_STRING};
405         if (defined $mid_ue) {
406                 # common, and much nicer as '@' than '%40':
407                 $mid_ue =~ s/%40/@/g;
408                 $url .= $mid_ue . '/';
409         }
410         $url .= $suffix if (defined $suffix);
411         $url .= "?$qs" if $qs ne '';
412
413         [ 301,
414           [ Location => $url, 'Content-Type' => 'text/plain' ],
415           [ "Redirecting to $url\n" ] ]
416 }
417
418 sub msg_page {
419         my ($ctx, $inbox, $mid_ue, $e) = @_;
420         my $ret;
421         $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
422         '' eq $e and return get_mid_html($ctx);
423         'T/' eq $e and return get_thread($ctx, 1);
424         't/' eq $e and return get_thread($ctx);
425         't.atom' eq $e and return get_thread_atom($ctx);
426         't.mbox' eq $e and return get_thread_mbox($ctx);
427         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
428         'raw' eq $e and return get_mid_txt($ctx);
429
430         # legacy, but no redirect for compatibility:
431         'f/' eq $e and return get_mid_html($ctx);
432         r404($ctx);
433 }
434
435 sub serve_git {
436         my ($ctx, $part, $path) = @_;
437         my $env = $ctx->{env};
438         my $ibx = $ctx->{-inbox};
439         my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
440         $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
441 }
442
443 sub mbox_results {
444         my ($ctx) = @_;
445         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
446                 searcher($ctx) or return need_search($ctx);
447                 require PublicInbox::SearchView;
448                 return PublicInbox::SearchView::mbox_results($ctx);
449         }
450         r404();
451 }
452
453 sub serve_mbox_range {
454         my ($ctx, $inbox, $range) = @_;
455         invalid_inbox($ctx, $inbox) || eval {
456                 require PublicInbox::Mbox;
457                 searcher($ctx);
458                 PublicInbox::Mbox::emit_range($ctx, $range);
459         }
460 }
461
462 sub news_www {
463         my ($self) = @_;
464         $self->{news_www} ||= do {
465                 require PublicInbox::NewsWWW;
466                 PublicInbox::NewsWWW->new($self->{pi_config});
467         }
468 }
469
470 sub get_attach {
471         my ($ctx, $idx, $fn) = @_;
472         require PublicInbox::WwwAttach;
473         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
474 }
475
476 # User-generated content (UGC) may have excessively long lines
477 # and screw up rendering on some browsers, so we use pre-wrap.
478 #
479 # We also force everything to the same scaled font-size because GUI
480 # browsers (tested both Firefox and surf (webkit)) uses a larger font
481 # for the Search <form> element than the rest of the page.  Font size
482 # uniformity is important to people who rely on gigantic fonts.
483 # Finally, we use monospace to ensure the Search field and button
484 # has the same size and spacing as everything else which is
485 # <pre>-formatted anyways.
486 our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
487
488 sub stylesheets_prepare ($$) {
489         my ($self, $upfx) = @_;
490         my $mini = eval {
491                 require CSS::Minifier;
492                 sub { CSS::Minifier::minify(input => $_[0]) };
493         } || eval {
494                 require CSS::Minifier::XS;
495                 sub { CSS::Minifier::XS::minify($_[0]) };
496         } || sub { $_[0] };
497
498         my $css_map = {};
499         my $stylesheets = $self->{pi_config}->{css} || [];
500         my $links = [];
501         my $inline_ok = 1;
502
503         foreach my $s (@$stylesheets) {
504                 my $attr = {};
505                 local $_ = $s;
506                 foreach my $k (qw(media title href)) {
507                         if (s/\s*$k='([^']+)'// || s/\s*$k=(\S+)//) {
508                                 $attr->{$k} = $1;
509                         }
510                 }
511
512                 if (defined $attr->{href}) {
513                         $inline_ok = 0;
514                 } else {
515                         open(my $fh, '<', $_) or do {
516                                 warn "failed to open $_: $!\n";
517                                 next;
518                         };
519                         my ($key) = (m!([^/]+?)(?:\.css)?\z!i);
520                         my $ctime = 0;
521                         my $local = do { local $/; <$fh> };
522                         if ($local =~ /\S/) {
523                                 $ctime = sprintf('%x',(stat($fh))[10]);
524                                 $local = $mini->($local);
525                         }
526                         $css_map->{$key} = $local;
527                         $attr->{href} = "$upfx$key.css?$ctime";
528                         if (defined($attr->{title})) {
529                                 $inline_ok = 0;
530                         } elsif (($attr->{media}||'screen') eq 'screen') {
531                                 $attr->{-inline} = $local;
532                         }
533                 }
534                 push @$links, $attr;
535         }
536
537         my $buf = "<style>$STYLE";
538         if ($inline_ok) {
539                 my @ext; # for media=print and whatnot
540                 foreach my $attr (@$links) {
541                         if (defined(my $str = delete $attr->{-inline})) {
542                                 $buf .= $str;
543                         } else {
544                                 push @ext, $attr;
545                         }
546                 }
547                 $links = \@ext;
548         }
549         $buf .= '</style>';
550
551         if (@$links) {
552                 foreach my $attr (@$links) {
553                         delete $attr->{-inline};
554                         $buf .= "<link\ntype=text/css\nrel=stylesheet";
555                         while (my ($k, $v) = each %$attr) {
556                                 $v = qq{"$v"} if $v =~ /[\s=]/;
557                                 $buf .= qq{\n$k=$v};
558                         }
559                         $buf .= ' />';
560                 }
561                 $self->{"-style-$upfx"} = $buf;
562         } else {
563                 $self->{-style_inline} = $buf;
564         }
565         $self->{-css_map} = $css_map;
566 }
567
568 # returns an HTML fragment with <style> or <link> tags in them
569 # Called by WwwStream by nearly every HTML page
570 sub style {
571         my ($self, $upfx) = @_;
572         $self->{-style_inline} || $self->{"-style-$upfx"} || do {
573                 stylesheets_prepare($self, $upfx);
574                 $self->{-style_inline} || $self->{"-style-$upfx"}
575         };
576 }
577
578 # /$INBOX/$KEY.css endpoint
579 # CSS is configured globally for all inboxes, but we access them on
580 # a per-inbox basis.  This allows administrators to setup per-inbox
581 # static routes to intercept the request before it hits PSGI
582 sub get_css ($$$) {
583         my ($ctx, $inbox, $key) = @_;
584         my $r404 = invalid_inbox($ctx, $inbox);
585         return $r404 if $r404;
586         my $self = $ctx->{www};
587         my $css_map = $self->{-css_map} || stylesheets_prepare($self, '');
588         my $css = $css_map->{$key};
589         if (!defined($css) && $key eq 'userContent') {
590                 my $env = $ctx->{env};
591                 $css = PublicInbox::UserContent::sample($ctx->{-inbox}, $env);
592         }
593         defined $css or return r404();
594         my $h = [ 'Content-Length', bytes::length($css),
595                 'Content-Type', 'text/css' ];
596         PublicInbox::GitHTTPBackend::cache_one_year($h);
597         [ 200, $h, [ $css ] ];
598 }
599
600 1;