]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-httpd
httpd: disable Deflater middleware by default on Perl <5.18
[public-inbox.git] / script / public-inbox-httpd
1 #!/usr/bin/perl -w
2 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Standalone HTTP server for public-inbox.
6 use strict;
7 use warnings;
8 use Plack::Util;
9 use PublicInbox::Daemon;
10 use PublicInbox::HTTP;
11 use PublicInbox::HTTPD;
12 use Plack::Builder;
13 my %httpds;
14 my $app;
15 my $refresh = sub {
16         if (@ARGV) {
17                 eval { $app = Plack::Util::load_psgi(@ARGV) };
18                 if ($@) {
19                         die $@,
20 "$0 runs in /, command-line paths must be absolute\n";
21                 }
22         } else {
23                 require PublicInbox::WWW;
24                 my $www = PublicInbox::WWW->new;
25                 $www->preload;
26                 $app = builder {
27                         # Perl 5.16.3 leaks in our "push" response code path
28                         # (e.g. Qspawn) due to something in
29                         # Plack::Util::response_cb, regardless of whether the
30                         # client is sending Accept-Encoding:gzip requests.
31                         # perl5180delta documents many leak fixes, so assume
32                         # 5.18+ is safe for now and bump the check as-need:
33                         $] >= 5.018000 and eval {
34                                 enable 'Deflater',
35                                         content_type => [ qw(
36                                                 text/html
37                                                 text/plain
38                                                 application/atom+xml
39                                                 )]
40                         };
41
42                         eval { enable 'ReverseProxy' };
43                         $@ and warn
44 "Plack::Middleware::ReverseProxy missing,\n",
45 "URL generation for redirects may be wrong if behind a reverse proxy\n";
46
47                         enable 'Head';
48                         sub { $www->call(@_) };
49                 };
50         }
51 };
52
53 PublicInbox::Daemon::run('0.0.0.0:8080', $refresh,
54         sub ($$$) { # post_accept
55                 my ($client, $addr, $srv) = @_;
56                 my $fd = fileno($srv);
57                 my $h = $httpds{$fd} ||= PublicInbox::HTTPD->new($srv, $app);
58                 PublicInbox::HTTP->new($client, $addr, $h),
59         });