Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http'),
  2. sys = require('sys');
  3. url = require('url')
  4. spawn = require('child_process').spawn;
  5. fs = require('fs');
  6.  
  7. var handleRequests = function(response, data){
  8.     //Create a req
  9.     var crypto = require("crypto");
  10.     var fs = require('fs');
  11.     var f = fs.openSync('/dev/random', 'r', mode=0666);
  12.     var rand = fs.readSync(f, 10,0, encoding='base64');
  13.     var key = rand[0] ;
  14.     var hmacdata = '1234567890' ;
  15.  
  16.     var hmac = crypto.createHmac("sha1", key);
  17.     var hash2 = hmac.update(hmacdata);
  18.     var filename = hmac.digest(encoding="hex");
  19.     //Create a file by this name and stream to it...
  20.     var latex    = spawn('latex',['-jobname='+filename]);
  21.     //TODO:Write to a system log
  22.     latex.stdout.on('data', function (data) {
  23.         sys.print('***Latex stdout: ' + data);
  24.     });
  25.     //On closing the stdout?? I dont quite understand
  26.     latex.stdout.on('close', function(data_temp){
  27.         console.log('***CLOSED LATEX STREAM...');
  28.        
  29.         dvi2svg = spawn('dvisvgm',['-s', filename])
  30.         response.writeHead(200, {'Content-Type': 'text/html'});
  31.         response.write('<HTML><HEAD></HEAD>');
  32.        
  33.         dvi2svg.stdout.on('data',
  34.             function(data) {
  35.                 console.log('***DVISVGM stdout detected.'+data );
  36.                 //Stream the o/p
  37.                 response.write(data);
  38.            
  39.         })
  40.        
  41.         dvi2svg.stdout.on('end',function(data){
  42.                 console.log('***DVISVG Data...'+data)
  43.                 response.end('</HTML>');
  44.                 spawn('rm',[filename+'.dvi',filename+'.aux',filename+'.log']).stderr.on('data', function(data){
  45.                     console.log('Deleted temp files...'+data)
  46.                 });
  47.         });
  48.     })
  49.        
  50.     latex.stderr.on('data', function (data) {
  51.         sys.print('***Latex stdout: ' + data);
  52.     });
  53.     //TODO: Write to Sockets and read from there...Write Latex O/P to a socket using -ipc option.
  54.     //Create a socket and read and write from it
  55.     //mkfifo is for making named pipes
  56.     latex.stdin.write('\\documentclass{article} \\usepackage{amsmath} \\usepackage{amssymb} \\usepackage{amsfonts} \\thispagestyle{empty} \\begin{document} \\[ '+data+' \\] \\end{document}')
  57.     //TO DO: USE DVI
  58.     latex.stdin.end();
  59.     //TODO: Read from socket
  60.    
  61. }
  62. server = http.createServer(function(req, response){
  63.     // your normal server code
  64.    
  65.  
  66.     //Attempt to return a latex SVG image...
  67.        var qs = require('querystring');
  68.        var querystring = url.parse(req.url).query;
  69.        var queryobject = qs.parse(querystring);
  70.        console.log(querystring);
  71.        if('latex' in queryobject){
  72.            var data = queryobject['latex'];
  73.            handleRequests(response, data);
  74.            
  75.        }else{
  76.            //Serve the normal HTML index file...
  77.            response.writeHead(200, {'Content-Type': 'text/html'});
  78.            fs.readFile('index.html', encoding='utf8', function (err, data) {
  79.               if (err) throw err;
  80.               response.write(data);
  81.               response.end();
  82.             });
  83.            
  84.            
  85.        }
  86.      
  87.  
  88. });
  89. server.listen(80);
  90. process.setuid('sid')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement