newLISP Fan Club

Forum => newLISP and the O.S. => Topic started by: abbat on November 03, 2009, 12:44:53 PM

Title: Calling newlisp from Javascript
Post by: abbat on November 03, 2009, 12:44:53 PM
I use newLISP as local http server on Windows platform:

  newlisp.exe -c -d 1235



It is possible send some string (like "(+ 2 3)") from Javascript to newLISP session

for evaluate and get back answer?
Title: Re: Calling newlisp from Javascript
Post by: cormullion on November 03, 2009, 02:08:27 PM
Don't know. Wouldn't that be a security problem?
Title: Re: Calling newlisp from Javascript
Post by: Lutz on November 03, 2009, 08:40:41 PM
It should be possible to do this from JavaScript. Connect to port 1235 and send the string "(+ 3 4)n",  then wait for an answer which should be "7n" (7 plus line-feed). And yes, there is a big potential for security problems; so carefully check each string received for evaluation.
Title: Re: Calling newlisp from Javascript
Post by: abbat on November 04, 2009, 08:12:30 AM
Thanks you for reply.

I am beginner in newLisp and Javascritp.

Please, give me an example.

I try run newlisp:

  newlisp.exe. -c -d 8080

and open in browser this document:

 

<html>
<body>
<h3>newLISP server and JS demo</h3>

<script type="text/javascript" language="javascript">

if (window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function newLispEval(expr) {
  xmlHttp.open("GET", "http://localhost:8080", false);
  try {
    xmlHttp.send(expr);
  } catch (e) { alert(e.message); }
  return xmlHttp.responseText;
}

var answer = newLispEval("(+ 3 4)n");

document.write(answer);
document.write("<BR>ok.");
</script>
</body>
</html>
<!-- end of file -->


but get NS_ERROR_FAILURE for "xmlHttp.send(expr);"
Title: Re: Calling newlisp from Javascript
Post by: abbat on November 04, 2009, 08:13:54 AM
Sorry, misprint:

I run newLISP with line:

newlisp.exe -c -d
Title: Re: Calling newlisp from Javascript
Post by: abbat on November 04, 2009, 08:14:58 AM
Sorry again :)

newlisp.exe -c -d 8080
Title: Re: Calling newlisp from Javascript
Post by: Lutz on November 04, 2009, 09:49:57 AM
newLISP when started: newlisp -c -d 8080, answers to two different protocols. What I was demonstrating is the simple command line protocol, but  xmlHttp.open() is using the HTTP protocol.



So with: newlisp -c -d 8080, the newLISP server handles the HTTP protocol too. But you would need to initiate some kind of CGI process and pass the expression to evaluate as a parameter in a GET or POST request.



Cormullion is doing this here:



http://unbalanced-parentheses.nfshost.com/lambdalator/index



If your emphasis is on asynchronous update then look at this example:



http://www.newlisp.org/index.cgi?page=AJAX_and_newLISP



You could merge both approaches passing the expression to evaluate as parameter in the xmlHttp GET request.
Title: Re: Calling newlisp from Javascript
Post by: abbat on November 07, 2009, 09:17:22 AM
Sorry for long silent.



I create file "eval.cgi":

#!/usr/bin/newlisp

(define (url-translate str)
   (replace "%([0-9A-F][0-9A-F])" str (format "%c" (int (append "0x" $1))) 1))

(print "Content-type: text/htmlrnrn")
(print (eval-string (url-translate (env "QUERY_STRING"))))
(exit)

# end of file


and file "test1.html":

<html>
<body>
<h3>newLISP server and JS demo</h3>

<script type="text/javascript" language="javascript">

if (window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function newLispEval(expr) {
  xmlHttp.open("GET", "eval.cgi?" + expr, false);
  try {
    xmlHttp.send(null);
  } catch (e) { alert(e.message); }
  return xmlHttp.responseText;
}

var answer;
answer = newLispEval("(+ 3 4)");
document.write(answer);
document.write("<BR>ok.");
</script>
</body>
</html>
<!-- end of file -->


then I run
newlisp.exe -c -d 1235
and open browser for location:
http://localhost:1235//tmp/1/test1.html
and... it's work!

Thanks you.



But I still have some small problems:

1. Newlisp session not saved between requests:

after newLispEval("(set 'x 3)");
evaluation of newLispEval("(+ 'x 4)");
give me error "ERR: value expected in function + : x".

It is possible work form Javascript with one newlisp session?



2. Newlisp in server mode not support names with spaces?

Location like:
Quotehttp://localhost:1235//my projects/q1/test1.html

not work for me.



3. It is possible explicit specify Windows drive letter in location?

Location like:
Quotehttp://localhost:1235//D/tmp/1/test1.html

not work for me.
Title: Re: Calling newlisp from Javascript
Post by: abbat on November 07, 2009, 09:19:35 AM
Sorry, misprint:

instead

"newLispEval("(+ 'x 4)");"

must be

"newLispEval("(+ x 4)");"
Title: Re: Calling newlisp from Javascript
Post by: Lutz on November 07, 2009, 11:26:17 AM
Quote1. Newlisp session not saved between requests:


You could save the session doing a (save "session.lsp") in the CGI program after evaluating and doing (load "session.lsp") at the beginning of each CGI process.



But a better/faster solution would be to start a second newlisp server process, which stays in memory, and use 'net-eval' to connect from it from the CGI process. newLISP processes use very little memory, so starting just another one is the preferred solution. It is also the more secured solution, because you could preload the serve with statements disabling all dangerous commands. E.g:


(constant 'delete-file (fn () "not allowed"))

Quote2. Newlisp in server mode not support names with spaces?


Currently not supported. newLISP server mode is not meant to be a full featured web server, it doesn't do any URL back translation.


Quote3. It is possible explicit specify Windows drive letter in location?


Drive switching works like this on Windows:


http://localhost:1235/D:/tmp/1/test1.html

If D would be the current drive, you could do:


http://localhost:1235//tmp/1/test1.html
Title: Re: Calling newlisp from Javascript
Post by: abbat on November 07, 2009, 11:38:59 AM
This is suitable for me.

Many thanks!