1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Standalone PSGI app to provide syntax highlighting as-a-service
5 # via "highlight" Perl module ("libhighlight-perl" in Debian).
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.
11 # The following example using curl(1) will do the right thing
12 # regarding the file extension:
14 # curl -HExpect: -T /path/to/file http://example.com/
16 # You can also force a file extension by giving a path
17 # (in this case, "c") via:
19 # curl -HExpect: -T /path/to/file http://example.com/x.c
21 package PublicInbox::WwwHighlight;
24 use bytes (); # only for bytes::length
25 use parent qw(PublicInbox::HlMod);
26 use PublicInbox::Linkify qw();
27 use PublicInbox::Hval qw(ascii_html);
28 use PublicInbox::WwwStatic qw(r);
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.
34 # another slurp API hogging up all my memory :<
35 # This is capped by whatever the PSGI server allows,
36 # $ENV{GIT_HTTP_MAX_REQUEST_BUFFER} for PublicInbox::HTTP (10 MB)
37 sub read_in_full ($) {
40 my $in = $env->{'psgi.input'};
43 my $len = $env->{CONTENT_LENGTH} || 8192;
45 my $r = $in->read($buf, $len, $off);
46 last unless defined $r;
47 return \$buf if $r == 0;
50 $env->{'psgi.errors'}->print("input read error: $!\n");
54 # entry point for PSGI
56 my ($self, $env) = @_;
57 my $req_method = $env->{REQUEST_METHOD};
59 return r(405) if $req_method ne 'PUT';
61 my $bref = read_in_full($env) or return r(500);
62 my $l = PublicInbox::Linkify->new;
63 $l->linkify_1($$bref);
64 if (my $res = $self->do_hl($bref, $env->{PATH_INFO})) {
67 $$bref = ascii_html($$bref);
69 $l->linkify_2($$bref);
71 my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
72 push @$h, 'Content-Length', bytes::length($$bref);
74 [ 200, $h, [ $$bref ] ]