]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwHighlight.pm
config: support "inboxdir" in addition to "mainrepo"
[public-inbox.git] / lib / PublicInbox / WwwHighlight.pm
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Standalone PSGI app to provide syntax highlighting as-a-service
5 # via "highlight" Perl module ("libhighlight-perl" in Debian).
6 #
7 # This allows exposing highlight as a persistent HTTP service for
8 # other scripts via HTTP PUT requests.  PATH_INFO will be used
9 # as a hint for detecting the language for highlight.
10 #
11 # The following example using curl(1) will do the right thing
12 # regarding the file extension:
13 #
14 #   curl -HExpect: -T /path/to/file http://example.com/
15 #
16 # You can also force a file extension by giving a path
17 # (in this case, "c") via:
18 #
19 #   curl -HExpect: -T /path/to/file http://example.com/x.c
20
21 package PublicInbox::WwwHighlight;
22 use strict;
23 use warnings;
24 use bytes (); # only for bytes::length
25 use HTTP::Status qw(status_message);
26 use parent qw(PublicInbox::HlMod);
27 use PublicInbox::Linkify qw();
28 use PublicInbox::Hval qw(ascii_html);
29
30 # TODO: support highlight(1) for distros which don't package the
31 # SWIG extension.  Also, there may be admins who don't want to
32 # have ugly SWIG-generated code in a long-lived Perl process.
33
34 sub r ($) {
35         my ($code) = @_;
36         my $msg = status_message($code);
37         my $len = length($msg);
38         [ $code, [qw(Content-Type text/plain Content-Length), $len], [$msg] ]
39 }
40
41 # another slurp API hogging up all my memory :<
42 # This is capped by whatever the PSGI server allows,
43 # $ENV{GIT_HTTP_MAX_REQUEST_BUFFER} for PublicInbox::HTTP (10 MB)
44 sub read_in_full ($) {
45         my ($env) = @_;
46
47         my $in = $env->{'psgi.input'};
48         my $off = 0;
49         my $buf = '';
50         my $len = $env->{CONTENT_LENGTH} || 8192;
51         while (1) {
52                 my $r = $in->read($buf, $len, $off);
53                 last unless defined $r;
54                 return \$buf if $r == 0;
55                 $off += $r;
56         }
57         $env->{'psgi.errors'}->print("input read error: $!\n");
58         undef;
59 }
60
61 # entry point for PSGI
62 sub call {
63         my ($self, $env) = @_;
64         my $req_method = $env->{REQUEST_METHOD};
65
66         return r(405) if $req_method ne 'PUT';
67
68         my $bref = read_in_full($env) or return r(500);
69         my $l = PublicInbox::Linkify->new;
70         $l->linkify_1($$bref);
71         if (my $res = $self->do_hl($bref, $env->{PATH_INFO})) {
72                 $bref = $res;
73         } else {
74                 $$bref = ascii_html($$bref);
75         }
76         $l->linkify_2($$bref);
77
78         my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
79         push @$h, 'Content-Length', bytes::length($$bref);
80
81         [ 200, $h, [ $$bref ] ]
82 }
83
84 1;