| 1 | #!/usr/local/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | ##################################### | ||||||||||||||||||||||||||||
| 4 | # An HTTP Socket Server for LaTeXML # | ||||||||||||||||||||||||||||
| 5 | ##################################### | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # Created and provided by Deyan Ginev | ||||||||||||||||||||||||||||
| 8 | # released in the Public Domain | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}' | ||||||||||||||||||||||||||||
| 11 | if 0; # not running under some shell | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # General packages | ||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use FindBin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | our $VERSION = '1.4.2'; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | my $RealBin_safe; | ||||||||||||||||||||||||||||
| 21 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 22 | 1 | $FindBin::RealBin =~ /^([^\0]+)\z/; # Valid Unix path TODO: Windows, revisit regexp | |||||||||||||||||||||||||||
| 23 | 1 | $RealBin_safe = $1; | |||||||||||||||||||||||||||
| 24 | 1 | ▶ | die 'Fatal:IO:tainted RealBin was tainted! Failing...' unless ($RealBin_safe && (-e $RealBin_safe . '/latexmls')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$RealBin_safe and -e $RealBin_safe . '/latexmls'
MC/DC0%0/2
$RealBin_safe and -e $RealBin_safe . '/latexmls'
$RealBin_safe
-e $RealBin_safe . '/latexmls'
Truth table33%1/3
A and B
A= $RealBin_safeB= -e $RealBin_safe . '/latexmls'
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | # TODO: We probably want file cat for things like /../lib instead of spelling out a Unix path | ||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use lib $RealBin_safe. "/../lib"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | # Server-specific | ||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use HTTP::Daemon; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use POSIX qw( setsid ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use URI::Escape qw(uri_unescape); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use JSON::XS qw(encode_json decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | # LaTeXML packages | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | use LaTeXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 36 | use LaTeXML::Common::Config; | ||||||||||||||||||||||||||||
| 37 | use LaTeXML::Util::Pathname; | ||||||||||||||||||||||||||||
| 38 | #********************************************************************** | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Bookkeeping | ||||||||||||||||||||||||||||
| 41 | my $ARGV_SAVED = [@ARGV], #Save, so that we reinvoke when flushed | ||||||||||||||||||||||||||||
| 42 | # Read default options | ||||||||||||||||||||||||||||
| 43 | my $g_opts = LaTeXML::Common::Config->new('input_limit' => 100, expire => 600); | ||||||||||||||||||||||||||||
| 44 | $g_opts->read(\@ARGV); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Set up the server | ||||||||||||||||||||||||||||
| 47 | # Global constants can be read into fixed variables: | ||||||||||||||||||||||||||||
| 48 | my $address = $g_opts->get('address') || 'localhost'; | ||||||||||||||||||||||||||||
| 49 | my $port = $g_opts->get('port') || '3334'; | ||||||||||||||||||||||||||||
| 50 | my $server = setup_server($address, $port); | ||||||||||||||||||||||||||||
| 51 | my $server_port = $server->sockport(); | ||||||||||||||||||||||||||||
| 52 | my $input_limit = $g_opts->get('input_limit'); | ||||||||||||||||||||||||||||
| 53 | my $input_counter = 0; | ||||||||||||||||||||||||||||
| 54 | print STDERR "LaTeXML server listening on $server_port\n"; | ||||||||||||||||||||||||||||
| 55 | #********************************************************************** | ||||||||||||||||||||||||||||
| 56 | # Daemonize, | ||||||||||||||||||||||||||||
| 57 | # if we expect to auto-reboot and want standalone robustness | ||||||||||||||||||||||||||||
| 58 | daemonize($input_limit); | ||||||||||||||||||||||||||||
| 59 | #********************************************************************** | ||||||||||||||||||||||||||||
| 60 | # Set up the processing. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Install signal-handlers | ||||||||||||||||||||||||||||
| 63 | local $SIG{'ALRM'} = 'timeout_handler'; | ||||||||||||||||||||||||||||
| 64 | local $SIG{'TERM'} = 'term_handler'; | ||||||||||||||||||||||||||||
| 65 | local $SIG{'INT'} = 'term_handler'; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | alarm($g_opts->get('expire')); #Timeout if inactive for too long... | ||||||||||||||||||||||||||||
| 68 | #********************************************************************** | ||||||||||||||||||||||||||||
| 69 | # Process files on demand: | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | while (my $client = $server->accept()) { | ||||||||||||||||||||||||||||
| 72 | while (my $request = $client->get_request) { | ||||||||||||||||||||||||||||
| 73 | if (!$request->content) { | ||||||||||||||||||||||||||||
| 74 | $client->send_response(HTTP::Response->new(400, 'Fatal:http:request Empty request?!')); | ||||||||||||||||||||||||||||
| 75 | $client->close; | ||||||||||||||||||||||||||||
| 76 | next; | ||||||||||||||||||||||||||||
| 77 | } elsif ($request->method ne 'POST') { | ||||||||||||||||||||||||||||
| 78 | $client->send_response(HTTP::Response->new(405, 'Fatal:http:request Only POST allowed')); | ||||||||||||||||||||||||||||
| 79 | $client->close; | ||||||||||||||||||||||||||||
| 80 | next; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | # Bookkeeping... | ||||||||||||||||||||||||||||
| 83 | alarm(0); # We are processing, ignore timeout here | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Thaw request into options: | ||||||||||||||||||||||||||||
| 86 | my $opts = LaTeXML::Common::Config->new(); | ||||||||||||||||||||||||||||
| 87 | my $config_build_return = eval { | ||||||||||||||||||||||||||||
| 88 | # Split, and make sure keys with no values get an empty string stub | ||||||||||||||||||||||||||||
| 89 | my $keyvals = [ | ||||||||||||||||||||||||||||
| 90 | map { $$_[0], $$_[1] ? decode('utf-8',uri_unescape($$_[1])) : '' } | ||||||||||||||||||||||||||||
| 91 | map { [split(/=/,$_,2)] } # Split key=values | ||||||||||||||||||||||||||||
| 92 | map { split(/\&/,$_)} # Split URI options | ||||||||||||||||||||||||||||
| 93 | $request->content ]; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | $opts->read_keyvals($keyvals,silent=>1); }; | ||||||||||||||||||||||||||||
| 96 | if (!$config_build_return || $@) { | ||||||||||||||||||||||||||||
| 97 | # Wrong options, report error. | ||||||||||||||||||||||||||||
| 98 | $@ = "See 'latexmlc --help' for the full options specification" unless $@; | ||||||||||||||||||||||||||||
| 99 | $client->send_response(HTTP::Response->new(400, "Fatal:http:request You have used illegal or ill-formed options in your request.\n Details:$@")); | ||||||||||||||||||||||||||||
| 100 | $client->close; | ||||||||||||||||||||||||||||
| 101 | next; | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | # Local if peerhost is localhost: | ||||||||||||||||||||||||||||
| 104 | $opts->set('local', $client->peerhost eq '127.0.0.1'); | ||||||||||||||||||||||||||||
| 105 | $opts->set('source', $opts->get('tex')) if (!($opts->get('source')) && $opts->get('tex')); | ||||||||||||||||||||||||||||
| 106 | $opts->delete('tex'); | ||||||||||||||||||||||||||||
| 107 | # Prepare TeX source | ||||||||||||||||||||||||||||
| 108 | my $source = $opts->get('source'); | ||||||||||||||||||||||||||||
| 109 | $opts->delete('source'); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | my $base = $opts->get('base'); | ||||||||||||||||||||||||||||
| 112 | if ($base && !pathname_is_url($base)) { | ||||||||||||||||||||||||||||
| 113 | my $canonical_base = pathname_canonical($base); | ||||||||||||||||||||||||||||
| 114 | if ($canonical_base ne pathname_cwd()) { | ||||||||||||||||||||||||||||
| 115 | chdir $canonical_base | ||||||||||||||||||||||||||||
| 116 | or croak("Fatal:server:chdir Can't chdir to $canonical_base: $!"); | ||||||||||||||||||||||||||||
| 117 | $LaTeXML::Util::Pathname::cached_cwd = $canonical_base; | ||||||||||||||||||||||||||||
| 118 | # Prevent spurious warning; we may not use the cached_cwd, but we really want to have it around | ||||||||||||||||||||||||||||
| 119 | $LaTeXML::Util::Pathname::cached_cwd if 0; | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # We now have a LaTeXML options object - $opts. | ||||||||||||||||||||||||||||
| 124 | # Make sure the g_opts are included as defaults | ||||||||||||||||||||||||||||
| 125 | foreach ($g_opts->keys) { | ||||||||||||||||||||||||||||
| 126 | $opts->set($_, $g_opts->get($_)) unless defined $$opts{opts}{$_}; | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | # Print to regular STDERR if debugging is requested | ||||||||||||||||||||||||||||
| 129 | *STDERR = *STDERR_SAVED if ($LaTeXML::DEBUG); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Boot/find a daemon: | ||||||||||||||||||||||||||||
| 132 | my $converter = LaTeXML->get_converter($opts); | ||||||||||||||||||||||||||||
| 133 | # Initialize if needed | ||||||||||||||||||||||||||||
| 134 | $converter->initialize_session unless $$converter{ready}; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | #Send a request: | ||||||||||||||||||||||||||||
| 137 | $input_counter++ if $input_limit; | ||||||||||||||||||||||||||||
| 138 | my $response = $converter->convert($source); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Send back the serialized result, log and status: | ||||||||||||||||||||||||||||
| 141 | # ensure these are well-typed for consumers that will be doing strict JSON | ||||||||||||||||||||||||||||
| 142 | $$response{status_code} = int($$response{status_code}); # ensure integer | ||||||||||||||||||||||||||||
| 143 | $$response{result} = "" if ! defined $$response{result}; # avoid null | ||||||||||||||||||||||||||||
| 144 | $client->send_response(HTTP::Response->new(200, 'OK', undef, encode_json($response))); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | #Flush daemon, every $input_limit files: | ||||||||||||||||||||||||||||
| 147 | if ($input_limit && | ||||||||||||||||||||||||||||
| 148 | ($input_counter >= $input_limit)) { | ||||||||||||||||||||||||||||
| 149 | $client->close; | ||||||||||||||||||||||||||||
| 150 | exec("$RealBin_safe/latexmls", @$ARGV_SAVED) | ||||||||||||||||||||||||||||
| 151 | or croak("Fatal:server:restart Daemon autoflush Failed!"); | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # Terminate immediately if requested | ||||||||||||||||||||||||||||
| 155 | timeout_handler() if ($opts->get('expire') == -1); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Continue looking for a new request, timeout if inactive for too long... | ||||||||||||||||||||||||||||
| 158 | alarm($opts->get('expire')); | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | $client->close; | ||||||||||||||||||||||||||||
| 161 | undef($client); | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | sub timeout_handler { | ||||||||||||||||||||||||||||
| 165 | print STDERR "Daemon timed out after inactivity.\n"; | ||||||||||||||||||||||||||||
| 166 | print STDERR "Self destructing...\n"; | ||||||||||||||||||||||||||||
| 167 | term_handler(); | ||||||||||||||||||||||||||||
| 168 | return; | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub term_handler { | ||||||||||||||||||||||||||||
| 172 | print STDERR "Exiting...\n"; | ||||||||||||||||||||||||||||
| 173 | exit; | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # TODO: Think about if the croaks can't be done better, so that the client | ||||||||||||||||||||||||||||
| 177 | # receives the error/log message? or is that overkill? | ||||||||||||||||||||||||||||
| 178 | sub daemonize { | ||||||||||||||||||||||||||||
| 179 | my $restartable = shift; | ||||||||||||||||||||||||||||
| 180 | #This causes more problems than it solves (e.g. sTeX setup) | ||||||||||||||||||||||||||||
| 181 | #chdir '/' or croak "Can't chdir to /: $!"; | ||||||||||||||||||||||||||||
| 182 | open STDIN, '<', '/dev/null' | ||||||||||||||||||||||||||||
| 183 | or croak("Fatal:server:daemonize Can't read /dev/null: $!"); | ||||||||||||||||||||||||||||
| 184 | open STDOUT, '>', '/dev/null' | ||||||||||||||||||||||||||||
| 185 | or croak("Fatal:server:daemonize Can't write to /dev/null: $!"); | ||||||||||||||||||||||||||||
| 186 | if ($restartable && $restartable > 0) { | ||||||||||||||||||||||||||||
| 187 | defined(my $pid = fork) | ||||||||||||||||||||||||||||
| 188 | or croak("Fatal:server:daemonize Can't fork: $!"); | ||||||||||||||||||||||||||||
| 189 | exit if $pid; | ||||||||||||||||||||||||||||
| 190 | setsid | ||||||||||||||||||||||||||||
| 191 | or croak("Fatal:server:daemonize Can't start a new session: $!"); } | ||||||||||||||||||||||||||||
| 192 | *STDERR_SAVED = *STDERR; | ||||||||||||||||||||||||||||
| 193 | *STDERR = *STDOUT | ||||||||||||||||||||||||||||
| 194 | or croak("Fatal:server:daemonize Can't dup stdout: $!"); | ||||||||||||||||||||||||||||
| 195 | return; } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | sub setup_server { | ||||||||||||||||||||||||||||
| 198 | my ($localaddress, $localport) = @_; | ||||||||||||||||||||||||||||
| 199 | my $http_server = HTTP::Daemon->new( | ||||||||||||||||||||||||||||
| 200 | LocalAddr => $localaddress, | ||||||||||||||||||||||||||||
| 201 | LocalPort => $localport, | ||||||||||||||||||||||||||||
| 202 | Listen => SOMAXCONN, | ||||||||||||||||||||||||||||
| 203 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 204 | Reuse => 1, | ||||||||||||||||||||||||||||
| 205 | ); | ||||||||||||||||||||||||||||
| 206 | croak("Fatal:server:init can't setup server on $localaddress:$localport") unless $http_server; | ||||||||||||||||||||||||||||
| 207 | return $http_server; | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | sub croak { | ||||||||||||||||||||||||||||
| 211 | print STDERR $_[0], "\n"; | ||||||||||||||||||||||||||||
| 212 | exit 1; | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | #********************************************************************** | ||||||||||||||||||||||||||||
| 216 | __END__ | ||||||||||||||||||||||||||||