lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sunday 31 July 2005 10:00 pm, William Trenker wrote:
> 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?

there has been some discussions about how to make the statusline visible to 
CGILua.  the main obstacle is that we don't want to add Xavante-only features 
to the API.

> 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.

that suggestion seems very logical.  it's not as powerful as a full statusline 
access, but it's certainly the most common usage, and certainly an omission 
of my part.  i'm sure that if i had tried to use the SAPI.Response.redirect() 
call, i'd added a res.statusline="HTTP/1.1 301 Moved Permanently" line right 
there.

feel free to add it yourself; if on the next version it's not there (or a 
better (and documented) way to do the same), drop a line to wake us!

> 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"?

after a handler returns, the send_response() function sends the statusline, 
headers and any unsent content (if needed).  because of the way CGILua 
translates luapages into CGILua scripts, it's possible that cgilua.put() is 
called one or more times even before the first command of the script is 
executed.  but we want to delay sending the headers as long as passible, so 
that the script can set any header it wants.

in other words, don't remove that line, or you could find it difficult to set 
your own headers.

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

 res.content = " " line shouldn't be necessary... i'll review the 
send_response() function.

> Thanks,

thanks to you, and keep those reports coming!

-- 
Javier

Attachment: pgp6EM0tfLOK7.pgp
Description: PGP signature