Re: mod_lua headers_out
On Mon, Jan 12, 2009 at 5:23 AM, Brian McCallister bri...@skife.org wrote: Bertrand, I just send in a patch on the message with subject patch for handling headers_in and headers_out as tables in mod_lua which takes knowledge of setting headers_in, headers_out off the request which, with that patch, can just push the apr_table_t as a boxed pointer allowing lua-style [] access and modification. Does that work? Yes, it will work, thank you :) The advantage of having a function instead of a table for setting headers_in/headers_out is that you can use luaL_checkstring() to make sure the given parameters are of the correct type. I don't know what the patch will return if someone tries to set a table like in headers_out['Foo'] = {Bar = 'baz'} Maybe it is possible to set a metatable for headers_in/headers_out that would check on __index and __newindex if the value validates with luaL_checkstring (just thinking out loud).
Re: mod_lua headers_out
On Mon, Jan 12, 2009 at 2:07 AM, Bertrand Mansion bmans...@mamasam.net wrote: .. snip .. Does that work? Yes, it will work, thank you :) The advantage of having a function instead of a table for setting headers_in/headers_out is that you can use luaL_checkstring() to make sure the given parameters are of the correct type. I don't know what the patch will return if someone tries to set a table like in headers_out['Foo'] = {Bar = 'baz'} Maybe it is possible to set a metatable for headers_in/headers_out that would check on __index and __newindex if the value validates with luaL_checkstring (just thinking out loud). I believe it does -- header_in and headers_out are boxed apr_table_t instances and the only thing they have in their metatable are __index and __newindex. If they are not checking for strings, that is a bug as apr_table_t is string only. -Brian
mod_lua headers_out
Hi, My name is Bertrand Mansion, I live in Paris, France, I have been mostly programming in PHP for the last few years and I am very excited about mod_lua since I consider Lua is superior in many ways. I have followed the development of mod_wombat and noticed that you changed the way headers_in and headers_out are accessed, they were previously tables, now only headers_in can be accessed through a headers_in() method. I have written a method to have access to headers_out as well. I am not sure it is perfect since I haven't coded with Lua and apr C APIs before and many you thought already about something more clever. Anyway, with this patch, you can do: r:headers_out('Location', 'http://www.example.com') -- set the Location header r:headers_out('Location) -- return the current value for header Location r:headers_out('Location', nil) -- unset the Location header There might be other options I haven't implemented for example: r.headers_out['Location'] = 'http://www.example.com' -- like in previous versions of mod_wombat or r:headers_out{Location = 'http://www.example.com} -- but then it becomes harder to unset a header (?) or r.headers_out:set('Location', 'http://www.example.com') r.headers_out:get('Location') r.headers_out:unset('Location') HTH - Bertrand Mansion Mamasam --- lua_request.c +++ (clipboard) @@ -447,6 +447,41 @@ return 1; } +static int req_headers_out(lua_State *L) +{ +const char *key; +const char *value; + +request_rec *r = apl_check_request_rec(L, 1); + +key = luaL_checkstring(L, 2); + +if (lua_isnone(L, 3) == 1) { +/* getting a value from headers_out */ +value = apr_table_get(r-headers_out, key); +if (value) { +lua_pushstring(L, value); +} +else { +lua_pushnil(L); +} +} +else { +if (lua_isnil(L, 3) == 1) { +value = apr_table_get(r-headers_out, key); +if (value) { +apr_table_unset(r-headers_out, key); +} +} +else { +value = luaL_checkstring(L, 3); +apr_table_set(r-headers_out, key, value); +} +} + +return 1; +} + /* handle r.status = 201 */ static int req_newindex(lua_State *L) { @@ -604,6 +639,8 @@ apr_hash_set(dispatch, headers_in, APR_HASH_KEY_STRING, makefun(req_headers_in, APL_REQ_FUNTYPE_LUACFUN, p)); +apr_hash_set(dispatch, headers_out, APR_HASH_KEY_STRING, + makefun(req_headers_out, APL_REQ_FUNTYPE_LUACFUN, p)); lua_pushlightuserdata(L, dispatch); lua_setfield(L, LUA_REGISTRYINDEX, Apache2.Request.dispatch); @@ -668,4 +705,4 @@ lua_boxpointer(L, r); luaL_getmetatable(L, Apache2.Request); lua_setmetatable(L, -2); -} \ No newline at end of file +}
Re: mod_lua headers_out
On Sun, Jan 11, 2009 at 4:21 AM, Bertrand Mansion bmans...@mamasam.net wrote: .. snip .. I have followed the development of mod_wombat and noticed that you changed the way headers_in and headers_out are accessed, they were previously tables I would actually prefer to make them tables again -- in particular, to put the apr_table_t instance directly in as a boxed userdata. Paul, any particular reason for making it a function rather than userdata? Anyway, with this patch, you can do: r:headers_out('Location', 'http://www.example.com') -- set the Location header r:headers_out('Location) -- return the current value for header Location r:headers_out('Location', nil) -- unset the Location header There might be other options I haven't implemented for example: r.headers_out['Location'] = 'http://www.example.com' -- like in previous versions of mod_wombat or r:headers_out{Location = 'http://www.example.com} -- but then it becomes harder to unset a header (?) or r.headers_out:set('Location', 'http://www.example.com') r.headers_out:get('Location') r.headers_out:unset('Location') HTH - Bertrand Mansion Mamasam
Re: mod_lua headers_out
Bertrand, I just send in a patch on the message with subject patch for handling headers_in and headers_out as tables in mod_lua which takes knowledge of setting headers_in, headers_out off the request which, with that patch, can just push the apr_table_t as a boxed pointer allowing lua-style [] access and modification. Does that work? -Brian On Sun, Jan 11, 2009 at 4:21 AM, Bertrand Mansion bmans...@mamasam.net wrote: Hi, My name is Bertrand Mansion, I live in Paris, France, I have been mostly programming in PHP for the last few years and I am very excited about mod_lua since I consider Lua is superior in many ways. I have followed the development of mod_wombat and noticed that you changed the way headers_in and headers_out are accessed, they were previously tables, now only headers_in can be accessed through a headers_in() method. I have written a method to have access to headers_out as well. I am not sure it is perfect since I haven't coded with Lua and apr C APIs before and many you thought already about something more clever. Anyway, with this patch, you can do: r:headers_out('Location', 'http://www.example.com') -- set the Location header r:headers_out('Location) -- return the current value for header Location r:headers_out('Location', nil) -- unset the Location header There might be other options I haven't implemented for example: r.headers_out['Location'] = 'http://www.example.com' -- like in previous versions of mod_wombat or r:headers_out{Location = 'http://www.example.com} -- but then it becomes harder to unset a header (?) or r.headers_out:set('Location', 'http://www.example.com') r.headers_out:get('Location') r.headers_out:unset('Location') HTH - Bertrand Mansion Mamasam