strange behavior with command-event?

Q&A's, tips, howto's
Locked
tardigrade
Posts: 3
Joined: Tue Jan 26, 2010 4:11 pm

strange behavior with command-event?

Post by tardigrade »

I'm a newcomer to newLISP. I've been playing around with url-rewriting using command-event, and ran into a behavior I wasn't expecting. My intention was to redirect all http requests to "request.html", parsing the http method and the url (minus the query string, if any) out of the original request and putting them into a query string in the new request. Thus

Code: Select all

"GET /foobar.html?this=that"
should be rewritten as

Code: Select all

"GET /request.html?method=GET&url=foobar.html"
The preprocessing script looks like this:

Code: Select all

(command-event (fn (str)
  (local (method url page)
    (begin
      (map set '(method url) (0 2 (parse str )))
      (if (find "?" url) (set 'page ((parse url "?") 0)))
      (join (list "GET /request.html?method=" method "&page=" page) "")))))
while request.html is just this little snippet of JavaScript:

Code: Select all

<html>
  <body>
    <h1>Hello</h1>
    <script type="text/javascript">
      query = document.location.href.split("?")[1]
      vars = query.split("&")
      for( i = 0; i < vars.length; i++ )
        document.write( vars[i] + "<br>" )
    </script>
  </body>
</html>
Strangely, when I start up newLISP as an HTTP daemon and point my browser to "foobar.html?this=that", I see

Hello
this=that

Instead of

Hello
method=GET
page=foobar.html

as I expect. In other words, the base url is correctly translated from "foobar.html" to "request.html", but the original query parameters are being passed instead of the new ones.

Can anyone explain:
(1) Why does command-event behave like this?
(2) How can I rewrite the query parameters?

I'd be most grateful for any advice.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: strange behavior with command-event?

Post by Lutz »

I can replicate what you are seeing, but think that it is JavaScript geting the "this=that" from the browser input; it does not come from newLISP server, which is behaving correctly.

In a debugging version of newLISP server I see this coming in as request:

Code: Select all

request.html?method=GET&page=/foobar.html
The command-event function in http-conf.lsp is working as expected. This is the header and body sent back:

Code: Select all

HTTP/1.0 200 OK
Server: newLISP v.10110 (OSX)
Content-length: 269
Content-type: text/html

Code: Select all

<html>
  <body>
    <h1>Hello</h1>
    <script type="text/javascript">
      query = document.location.href.split("?")[1]
      vars = query.split("&")
      for( i = 0; i < vars.length; i++ )
        document.write( vars[i] + "<br>" )
    </script>
  </body>
</html>
I then changed request.html to environment.cgi in commmand-event. The environment.cgi program you can find here:

http://www.newlisp.org/environment.cgi

then started newlisp server using the changed command-event function. In the environment.cgi output I saw correctly:

Code: Select all

QUERY_STRING	method=GET&page=/foobar.html
So newLISP server sends back the correct header and html and sets the enviroment QUERY_STRING correctly too.

I assume what you are seeing is, javaScript somehow taking "this=that" from the browser input, but it does not come from newLISP server.


ps: welcome to newLISP

tardigrade
Posts: 3
Joined: Tue Jan 26, 2010 4:11 pm

Re: strange behavior with command-event?

Post by tardigrade »

Lutz - Thanks for your quick response! It was very helpful.

Locked