]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwHighlight.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / WwwHighlight.pm
1 # Copyright (C) 2019-2021 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 v5.10.1;
24 use parent qw(PublicInbox::HlMod);
25 use PublicInbox::Linkify qw();
26 use PublicInbox::Hval qw(ascii_html);
27 use PublicInbox::WwwStatic qw(r);
28
29 # TODO: support highlight(1) for distros which don't package the
30 # SWIG extension.  Also, there may be admins who don't want to
31 # have ugly SWIG-generated code in a long-lived Perl process.
32
33 # another slurp API hogging up all my memory :<
34 # This is capped by whatever the PSGI server allows,
35 # $ENV{GIT_HTTP_MAX_REQUEST_BUFFER} for PublicInbox::HTTP (10 MB)
36 sub read_in_full ($) {
37         my ($env) = @_;
38
39         my $in = $env->{'psgi.input'};
40         my $off = 0;
41         my $buf = '';
42         my $len = $env->{CONTENT_LENGTH} || 8192;
43         while (1) {
44                 my $r = $in->read($buf, $len, $off);
45                 last unless defined $r;
46                 return \$buf if $r == 0;
47                 $off += $r;
48         }
49         warn "input read error: $!";
50         undef;
51 }
52
53 # entry point for PSGI
54 sub call {
55         my ($self, $env) = @_;
56         my $req_method = $env->{REQUEST_METHOD};
57
58         return r(405) if $req_method ne 'PUT';
59
60         my $bref = read_in_full($env) or return r(500);
61         my $l = PublicInbox::Linkify->new;
62         $l->linkify_1($$bref);
63         if (my $res = $self->do_hl($bref, $env->{PATH_INFO})) {
64                 $bref = $res;
65         } else {
66                 $$bref = ascii_html($$bref);
67         }
68         $l->linkify_2($$bref);
69
70         my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
71         push @$h, 'Content-Length', length($$bref);
72
73         [ 200, $h, [ $$bref ] ]
74 }
75
76 1;