| 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 | BEGIN { | ||||||||||||||||||||||||||||
| 22 | 1 | ▶ | $FindBin::RealBin =~ /^([^\0]+)\z/; # Valid Unix path TODO: Windows, revisit regexp | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 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 | 1 | ▶ | 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 | my $address = $g_opts->get('address') || 'localhost'; | ||||||||||||||||||||||||||||
| 48 | my $port = $g_opts->get('port') || '3334'; | ||||||||||||||||||||||||||||
| 49 | my $server = setup_server($address, $port); | ||||||||||||||||||||||||||||
| 50 | my $server_port = $server->sockport(); | ||||||||||||||||||||||||||||
| 51 | print STDERR "LaTeXML server listening on $server_port\n"; | ||||||||||||||||||||||||||||
| 52 | #********************************************************************** | ||||||||||||||||||||||||||||
| 53 | # Daemonize | ||||||||||||||||||||||||||||
| 54 | daemonize(); | ||||||||||||||||||||||||||||
| 55 | #********************************************************************** | ||||||||||||||||||||||||||||
| 56 | # Set up the processing. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Install signal-handlers | ||||||||||||||||||||||||||||
| 59 | local $SIG{'ALRM'} = 'timeout_handler'; | ||||||||||||||||||||||||||||
| 60 | local $SIG{'TERM'} = 'term_handler'; | ||||||||||||||||||||||||||||
| 61 | local $SIG{'INT'} = 'term_handler'; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | alarm($g_opts->get('expire')); #Timeout if inactive for too long... | ||||||||||||||||||||||||||||
| 64 | #********************************************************************** | ||||||||||||||||||||||||||||
| 65 | # Process files on demand: | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | while (my $client = $server->accept()) { | ||||||||||||||||||||||||||||
| 68 | while (my $request = $client->get_request) { | ||||||||||||||||||||||||||||
| 69 | if (!$request->content) { | ||||||||||||||||||||||||||||
| 70 | $client->send_response(HTTP::Response->new(400, 'Fatal:http:request Empty request?!')); | ||||||||||||||||||||||||||||
| 71 | $client->close; | ||||||||||||||||||||||||||||
| 72 | next; | ||||||||||||||||||||||||||||
| 73 | } elsif ($request->method ne 'POST') { | ||||||||||||||||||||||||||||
| 74 | $client->send_response(HTTP::Response->new(405, 'Fatal:http:request Only POST allowed')); | ||||||||||||||||||||||||||||
| 75 | $client->close; | ||||||||||||||||||||||||||||
| 76 | next; | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | # Bookkeeping... | ||||||||||||||||||||||||||||
| 79 | alarm(0); # We are processing, ignore timeout here | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Thaw request into options: | ||||||||||||||||||||||||||||
| 82 | my $opts = LaTeXML::Common::Config->new(); | ||||||||||||||||||||||||||||
| 83 | my $config_build_return = eval { | ||||||||||||||||||||||||||||
| 84 | # Split, and make sure keys with no values get an empty string stub | ||||||||||||||||||||||||||||
| 85 | my $keyvals = [ | ||||||||||||||||||||||||||||
| 86 | map { $$_[0], $$_[1] ? decode('utf-8',uri_unescape($$_[1])) : '' } | ||||||||||||||||||||||||||||
| 87 | map { [split(/=/,$_,2)] } # Split key=values | ||||||||||||||||||||||||||||
| 88 | map { split(/\&/,$_)} # Split URI options | ||||||||||||||||||||||||||||
| 89 | $request->content ]; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | $opts->read_keyvals($keyvals,silent=>1); }; | ||||||||||||||||||||||||||||
| 92 | if (!$config_build_return || $@) { | ||||||||||||||||||||||||||||
| 93 | # Wrong options, report error. | ||||||||||||||||||||||||||||
| 94 | $@ = "See 'latexmlc --help' for the full options specification" unless $@; | ||||||||||||||||||||||||||||
| 95 | $client->send_response(HTTP::Response->new(400, "Fatal:http:request You have used illegal or ill-formed options in your request.\n Details:$@")); | ||||||||||||||||||||||||||||
| 96 | $client->close; | ||||||||||||||||||||||||||||
| 97 | next; | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | # Local if peerhost is localhost: | ||||||||||||||||||||||||||||
| 100 | $opts->set('local', $client->peerhost eq '127.0.0.1'); | ||||||||||||||||||||||||||||
| 101 | $opts->set('source', $opts->get('tex')) if (!($opts->get('source')) && $opts->get('tex')); | ||||||||||||||||||||||||||||
| 102 | $opts->delete('tex'); | ||||||||||||||||||||||||||||
| 103 | # Prepare TeX source | ||||||||||||||||||||||||||||
| 104 | my $source = $opts->get('source'); | ||||||||||||||||||||||||||||
| 105 | $opts->delete('source'); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | my $base = $opts->get('base'); | ||||||||||||||||||||||||||||
| 108 | if ($base && !pathname_is_url($base)) { | ||||||||||||||||||||||||||||
| 109 | my $canonical_base = pathname_canonical($base); | ||||||||||||||||||||||||||||
| 110 | if ($canonical_base ne pathname_cwd()) { | ||||||||||||||||||||||||||||
| 111 | chdir $canonical_base | ||||||||||||||||||||||||||||
| 112 | or croak("Fatal:server:chdir Can't chdir to $canonical_base: $!"); | ||||||||||||||||||||||||||||
| 113 | $LaTeXML::Util::Pathname::cached_cwd = $canonical_base; | ||||||||||||||||||||||||||||
| 114 | # Prevent spurious warning; we may not use the cached_cwd, but we really want to have it around | ||||||||||||||||||||||||||||
| 115 | $LaTeXML::Util::Pathname::cached_cwd if 0; | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # We now have a LaTeXML options object - $opts. | ||||||||||||||||||||||||||||
| 120 | # Make sure the g_opts are included as defaults | ||||||||||||||||||||||||||||
| 121 | foreach ($g_opts->keys) { | ||||||||||||||||||||||||||||
| 122 | $opts->set($_, $g_opts->get($_)) unless defined $$opts{opts}{$_}; | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | # Print to regular STDERR if debugging is requested | ||||||||||||||||||||||||||||
| 125 | *STDERR = *STDERR_SAVED if ($LaTeXML::DEBUG); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Boot/find a daemon: | ||||||||||||||||||||||||||||
| 128 | my $converter = LaTeXML->get_converter($opts); | ||||||||||||||||||||||||||||
| 129 | #Override/extend with session-specific options in $opt: | ||||||||||||||||||||||||||||
| 130 | $converter->prepare_session($opts); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | #Send a request: | ||||||||||||||||||||||||||||
| 133 | $g_opts->set('input_counter', ($g_opts->get('input_counter') || 0) + 1); | ||||||||||||||||||||||||||||
| 134 | my $response = $converter->convert($source); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Send back the serialized XML, log and status: | ||||||||||||||||||||||||||||
| 137 | $client->send_response(HTTP::Response->new(200, 'OK', undef, encode_json($response))); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | #Flush daemon, every $input_limit files: | ||||||||||||||||||||||||||||
| 140 | if ($g_opts->get('input_limit') && | ||||||||||||||||||||||||||||
| 141 | (($g_opts->get('input_counter') || 0) >= $g_opts->get('input_limit'))) { | ||||||||||||||||||||||||||||
| 142 | $client->close; | ||||||||||||||||||||||||||||
| 143 | exec("$RealBin_safe/latexmls", @$ARGV_SAVED) | ||||||||||||||||||||||||||||
| 144 | or croak("Fatal:server:restart Daemon autoflush Failed!"); | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # Terminate immediately if requested | ||||||||||||||||||||||||||||
| 148 | timeout_handler() if ($opts->get('expire') == -1); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Important performance enhancement: if the conversion ended with a Fatal that threatened state, | ||||||||||||||||||||||||||||
| 151 | # a second initialization is needed, and we should do it *before* we accept the next job. | ||||||||||||||||||||||||||||
| 152 | $converter->initialize_session unless $$converter{ready}; | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # Continue looking for a new request, timeout if inactive for too long... | ||||||||||||||||||||||||||||
| 155 | alarm($opts->get('expire')); | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | $client->close; | ||||||||||||||||||||||||||||
| 158 | undef($client); | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub timeout_handler { | ||||||||||||||||||||||||||||
| 162 | print STDERR "Daemon timed out after inactivity.\n"; | ||||||||||||||||||||||||||||
| 163 | print STDERR "Self destructing...\n"; | ||||||||||||||||||||||||||||
| 164 | term_handler(); | ||||||||||||||||||||||||||||
| 165 | return; | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | sub term_handler { | ||||||||||||||||||||||||||||
| 169 | print STDERR "Exiting...\n"; | ||||||||||||||||||||||||||||
| 170 | exit; | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # TODO: Think about if the croaks can't be done better, so that the client | ||||||||||||||||||||||||||||
| 174 | # receives the error/log message? or is that overkill? | ||||||||||||||||||||||||||||
| 175 | sub daemonize { | ||||||||||||||||||||||||||||
| 176 | #This causes more problems than it solves (e.g. sTeX setup) | ||||||||||||||||||||||||||||
| 177 | #chdir '/' or croak "Can't chdir to /: $!"; | ||||||||||||||||||||||||||||
| 178 | open STDIN, '<', '/dev/null' | ||||||||||||||||||||||||||||
| 179 | or croak("Fatal:server:daemonize Can't read /dev/null: $!"); | ||||||||||||||||||||||||||||
| 180 | open STDOUT, '>', '/dev/null' | ||||||||||||||||||||||||||||
| 181 | or croak("Fatal:server:daemonize Can't write to /dev/null: $!"); | ||||||||||||||||||||||||||||
| 182 | defined(my $pid = fork) | ||||||||||||||||||||||||||||
| 183 | or croak("Fatal:server:daemonize Can't fork: $!"); | ||||||||||||||||||||||||||||
| 184 | exit if $pid; | ||||||||||||||||||||||||||||
| 185 | setsid | ||||||||||||||||||||||||||||
| 186 | or croak("Fatal:server:daemonize Can't start a new session: $!"); | ||||||||||||||||||||||||||||
| 187 | *STDERR_SAVED = *STDERR; | ||||||||||||||||||||||||||||
| 188 | *STDERR = *STDOUT | ||||||||||||||||||||||||||||
| 189 | or croak("Fatal:server:daemonize Can't dup stdout: $!"); | ||||||||||||||||||||||||||||
| 190 | return; | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | sub setup_server { | ||||||||||||||||||||||||||||
| 194 | my ($localaddress, $localport) = @_; | ||||||||||||||||||||||||||||
| 195 | my $http_server = HTTP::Daemon->new( | ||||||||||||||||||||||||||||
| 196 | LocalAddr => $localaddress, | ||||||||||||||||||||||||||||
| 197 | LocalPort => $localport, | ||||||||||||||||||||||||||||
| 198 | Listen => SOMAXCONN, | ||||||||||||||||||||||||||||
| 199 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 200 | Reuse => 1, | ||||||||||||||||||||||||||||
| 201 | ); | ||||||||||||||||||||||||||||
| 202 | croak("Fatal:server:init can't setup server") unless $http_server; | ||||||||||||||||||||||||||||
| 203 | return $http_server; | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | sub croak { | ||||||||||||||||||||||||||||
| 207 | print STDERR $_[0], "\n"; | ||||||||||||||||||||||||||||
| 208 | exit 1; | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | #********************************************************************** | ||||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||