[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: xavante / cgilua.redirect proposed changes
- From: William Trenker <wtrenker@...>
- Date: Sun, 31 Jul 2005 23:00:23 -0400
I've been running Xavante with great success and have got several
Cgiua scripts working nicely. I now want to implement an HTTP
redirect in a Cgilua script. Cgilua.redirect lets me set the
"Location:" header. But to actually make the browser go to that
location it must receive an HTTP status line with something like
"HTTP/1.1 301 Moved Permanently" -- RFC2616. (Similar to what is done
in xavante/filehandler.lua for listing directories.)
How do I set the HTTP status line from within a cgilua .lua or .lp
file? Looking at send_res_headers() in xavante/httpd.lua it appears
that I have to somehow set res.statusline. Is there a way to get at
the underlying response table at the Cgilua level?
Probably better, in the spirit of the API, it might make sense to
consider having SAPI.Response.redirect set the HTTP statusline value
in addition to setting the Location: header. It seems that if one is
explicitly setting the Location: header they intend to force an HTTP
redirect and so setting the HTTP statusline appropriately would be
essential.
There is another small catch as well. Looking into
xavante/httpd.lua:send_res_data(res, data) it appears that the headers
(including the HTTP status line) are not sent if there is no data
(e.g. cgilua.put has never been called, as expected for a redirect
where there is no entity-body -- no response content):
-------- lua code from httpd.lua -----------------
local function send_res_data (res, data)
if not data then
return -- no redirect headers are sent
end
if not res.sent_headers then
send_res_headers (res)
end
if data then
res.socket:send (data)
end
end
------------------- end of code ------------------------
However, the headers with status line must be sent if
SAPI.Response.redirect() has been previously called. What would be
the side-effects of removing the "if not data then return end"?
Anyway, for the time being, I've simply modified (aka kludged) the
definition of SAPI.Response.redirect like this:
------------ lua code ------------
SAPI.Response.redirect = function (s)
res.headers ["Location"] = s
res.statusline = "HTTP/1.1 301 Moved Permanently\r\n"
res.content = " " --one kludgy blank to force output
end
----------- end of code ---------
Doing it this way, the following one-line cgilua script works as expected:
------------ redir.lua --------------
SAPI.Response.redirect("http://lua.org")
-------- end of code ------------
Thanks,
Bill