lua-users home
lua-l archive

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


Hi,

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.

How about the way PHP handles it? PHP has a 'header' function, which receives a string as param. If that strings starts with 'HTTP/', then it set the response status line, otherwise 'header' will add that param as an ordinary header.

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.
That would be nice :) But please note that the 'Location' header is not directly dependant of the 301 status code.


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

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

	Here is my patched send_response if it helps anything:

-- begin of code --
function send_response (req, res)

    if not res.content and not res.sent_headers then
        res.content = ''
    end
    if not res.sent_headers then
        if (type (res.content) == "table") then
            res.content = concat (res.content)
        end
        if (type (res.content) == "string") then
            res.headers["Content-Length"] = len(res.content)
        end
    end

    if  res.headers ["Content-Length"] and
        req.headers ["Connection"] == "Keep-Alive" then

        res.headers ["Connection"] = "Keep-Alive"
        res.keep_alive = true
    else
        res.keep_alive = nil
    end

    if res.content then
        local s = res.content

        -- If you don't want to use this patch, just comment it out.
        -- To use this you will need LuaZLib and require it before
        -- the send_response function (usually it will be at
        -- beggining of the script).

        -- begin gzip pacth
        if find(req.headers['Accept-Encoding'] or '', 'gzip') then
            s = zlib.compress(s, nil, nil, 15 + 16)
            res.headers['Content-Encoding'] = 'gzip'
            res.headers['Content-Length'] = len(s)
        end
        -- end gzip patch
        send_res_data (res, s)
    end
end
-- end of code --


Please note that my application buffer the output, so send_response expects that the full response is in res.content, whether it is a table or a string.

Another note: I usually define all common functions as locals, so 'concat' is table.concat, 'len' is string.len, 'find' is string.find and so on.

--rb