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