strange behavior with command-event?

Started by tardigrade, January 26, 2010, 10:30:48 AM

Previous topic - Next topic

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


"GET /foobar.html?this=that"

should be rewritten as


"GET /request.html?method=GET&url=foobar.html"

The preprocessing script looks like this:


(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:


<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



[size=150]Hello[/size]

this=that



Instead of



[size=150]Hello[/size]

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

#1
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:


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:


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


<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">http://www.newlisp.org/environment.cgi



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


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

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