Fwd: svn commit: r1377475 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml docs/manual/style/scripts/prettify.js modules/lua/lua_vmprep.h modules/lua/mod_lua.c modules/lua/mod_lua.h
Original Message Subject:svn commit: r1377475 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml docs/manual/style/scripts/prettify.js modules/lua/lua_vmprep.h modules/lua/mod_lua.c modules/lua/mod_lua.h Date: Sun, 26 Aug 2012 18:39:59 GMT From: humbed...@apache.org Author: humbedooh Date: Sun Aug 26 18:39:58 2012 New Revision: 1377475 URL: http://svn.apache.org/viewvc?rev=1377475view=rev Log: Add new directives, LuaInputFilter/LuaOutputFilter for creating content filters using Lua. Modified: httpd/httpd/trunk/CHANGES httpd/httpd/trunk/docs/manual/mod/mod_lua.xml httpd/httpd/trunk/docs/manual/style/scripts/prettify.js httpd/httpd/trunk/modules/lua/lua_vmprep.h httpd/httpd/trunk/modules/lua/mod_lua.c httpd/httpd/trunk/modules/lua/mod_lua.h Modified: httpd/httpd/trunk/modules/lua/mod_lua.c URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/mod_lua.c?rev=1377475r1=1377474r2=1377475view=diff == --- httpd/httpd/trunk/modules/lua/mod_lua.c (original) +++ httpd/httpd/trunk/modules/lua/mod_lua.c Sun Aug 26 18:39:58 2012 @@ -55,6 +55,14 @@ typedef struct { apr_hash_t *lua_authz_providers; +typedef struct +{ +apr_bucket_brigade *tmpBucket; +lua_State *L; +ap_lua_vm_spec *spec; +int broken; +} lua_filter_ctx; + /** * error reporting if lua has an error. @@ -290,6 +298,281 @@ static int lua_handler(request_rec *r) } +/* --- Input/output content filters --- */ + + +static apr_status_t lua_setup_filter_ctx(ap_filter_t* f, request_rec* r, lua_filter_ctx** c) { +apr_pool_t *pool; +ap_lua_vm_spec *spec; +int n, rc; +lua_State *L; +lua_filter_ctx *ctx; +ap_lua_server_cfg *server_cfg = ap_get_module_config(r-server-module_config, +lua_module); +const ap_lua_dir_cfg *cfg = ap_get_module_config(r-per_dir_config, +lua_module); + +ctx = apr_pcalloc(r-pool, sizeof(lua_filter_ctx)); +ctx-broken = 0; +*c = ctx; +/* Find the filter that was called */ +for (n = 0; n cfg-mapped_filters-nelts; n++) { +ap_lua_filter_handler_spec *hook_spec = +((ap_lua_filter_handler_spec **) cfg-mapped_filters-elts)[n]; + +if (hook_spec == NULL) { +continue; +} +if (!strcasecmp(hook_spec-filter_name, f-frec-name)) { +spec = create_vm_spec(pool, r, cfg, server_cfg, +hook_spec-file_name, +NULL, +0, +hook_spec-function_name, +filter); +L = ap_lua_get_lua_state(pool, spec, r); +if (L) { +L = lua_newthread(L); +} + +if (!L) { +ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01477) +lua: Failed to obtain lua interpreter for %s %s, +hook_spec-function_name, hook_spec-file_name); +ap_lua_release_state(L, spec, r); +return APR_EGENERAL; +} +if (hook_spec-function_name != NULL) { +lua_getglobal(L, hook_spec-function_name); +if (!lua_isfunction(L, -1)) { +ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01478) +lua: Unable to find function %s in %s, +hook_spec-function_name, +hook_spec-file_name); +ap_lua_release_state(L, spec, r); +return APR_EGENERAL; +} + +ap_lua_run_lua_request(L, r); +} +else { +int t; +ap_lua_run_lua_request(L, r); + +t = lua_gettop(L); +lua_setglobal(L, r); +lua_settop(L, t); +} +ctx-L = L; +ctx-spec = spec; + +/* If a Lua filter is interested in filtering a request, it must first do a yield, + * otherwise we'll assume that it's not interested and pretend we didn't find it. + */ +rc = lua_resume(L, 1); +if (rc == LUA_YIELD) { +return OK; +} +else { +ap_lua_release_state(L, spec, r); +return APR_ENOENT; +} +} +} +return APR_ENOENT; +} + +static apr_status_t lua_output_filter_handle(ap_filter_t *f, apr_bucket_brigade *pbbIn) { +apr_bucket *e; +request_rec *r = f-r; +int rc; +lua_State *L; +lua_filter_ctx* ctx; +conn_rec *c = r-connection; +apr_bucket *pbktIn; +apr_bucket_brigade *pbbOut = NULL; + +/* Set up the initial filter context and acquire the
Re: Fwd: svn commit: r1377475 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml docs/manual/style/scripts/prettify.js modules/lua/lua_vmprep.h modules/lua/mod_lua.c modules/lua/mod_lua.h
On 08/27/2012 09:41 AM, Rüdiger Plüm wrote: +/* Clean up and pass on the brigade to the next filter in the chain */ Where do we do a cleanup here? +return APR_SUCCESS; +} + snip Regards Rüdiger Heh, I believe that's what is called a copypasto. Both cleanups and passing on content is done earlier in the chain, the specific comment is simply a remnant of creating the input filter based on the code used for the output filter (you'll see the same comment in the output filter code, where it actually makes sense). I'll remove the comment. With regards and thanks, Daniel.
RE: Fwd: svn commit: r1377475 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml docs/manual/style/scripts/prettify.js modules/lua/lua_vmprep.h modules/lua/mod_lua.c modules/lua/mod_lua.h
-Original Message- From: Daniel Gruno [mailto:rum...@cord.dk] Sent: Montag, 27. August 2012 10:45 To: dev@httpd.apache.org Subject: Re: Fwd: svn commit: r1377475 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml docs/manual/style/scripts/prettify.js modules/lua/lua_vmprep.h modules/lua/mod_lua.c modules/lua/mod_lua.h On 08/27/2012 09:41 AM, Rüdiger Plüm wrote: +/* Clean up and pass on the brigade to the next filter in the chain */ Where do we do a cleanup here? +return APR_SUCCESS; +} + snip Regards Rüdiger Heh, I believe that's what is called a copypasto. Both cleanups and passing on content is done earlier in the chain, the specific comment is simply a remnant of creating the input filter based on the code used for the output filter (you'll see the same comment in the output filter code, where it actually makes sense). I'll remove the comment. Thanks. I guess you noticed that I found more important issues with the code than this comment :-) Regards Rüdiger
Re: Fwd: svn commit: r1377475 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml docs/manual/style/scripts/prettify.js modules/lua/lua_vmprep.h modules/lua/mod_lua.c modules/lua/mod_lua.h
On 08/27/2012 11:59 AM, Plüm, Rüdiger, Vodafone Group wrote: Thanks. I guess you noticed that I found more important issues with the code than this comment :-) Regards Rüdiger Haha, yeah, but that's a big mess to clean up (I'm not a programmer by trade, so I'm a bit slow in that regard ;) ), so it will take me a while to get through all that, but I will get through it...eventually. With regards, Daniel.
Re: Fwd: svn commit: r1377475 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml docs/manual/style/scripts/prettify.js modules/lua/lua_vmprep.h modules/lua/mod_lua.c modules/lua/mod_lua.h
On 08/27/2012 11:59 AM, Plüm, Rüdiger, Vodafone Group wrote: Thanks. I guess you noticed that I found more important issues with the code than this comment :-) Regards Rüdiger I've tried my best to correct the errors you mentioned, but I'm having some trouble figuring out how the input filter should pass along data to the next in the chain. What I have currently is this: /* Get the output from Lua's yield() as a string */ const char* output = lua_tolstring(L, 1, olen); /* Make a bucket for it */ pbktOut = apr_bucket_heap_create(output, olen, 0, c-bucket_alloc); /* Add it to the brigade */ APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut); /* Do something more here involving cleanups or..?? */ But then what? Am I supposed to make a call to some ap_*_brigade function? I've checked some of the example filter modules, but I can't seem to find any suitable way of doing this other than finishing up the brigade and returning APR_SUCCESS in the end. Any pointers (or some code) would be much appreciated. I have plenty imagination, but my httpd C API knowledge can be somewhat lacking at certain points. With regards, Daniel.
RE: Fwd: svn commit: r1377475 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml docs/manual/style/scripts/prettify.js modules/lua/lua_vmprep.h modules/lua/mod_lua.c modules/lua/mod_lua.h
-Original Message- From: Daniel Gruno [mailto:rum...@cord.dk] Sent: Montag, 27. August 2012 14:06 To: dev@httpd.apache.org Subject: Re: Fwd: svn commit: r1377475 - in /httpd/httpd/trunk: CHANGES docs/manual/mod/mod_lua.xml docs/manual/style/scripts/prettify.js modules/lua/lua_vmprep.h modules/lua/mod_lua.c modules/lua/mod_lua.h On 08/27/2012 11:59 AM, Plüm, Rüdiger, Vodafone Group wrote: Thanks. I guess you noticed that I found more important issues with the code than this comment :-) Regards Rüdiger I've tried my best to correct the errors you mentioned, but I'm having You should try to leave the loop if ap_pass_brigade does not return APR_SUCCESS and should return this (!= APR_SUCCESS) to the caller. It is always a bad sign if the return code of ap_pass_brigade gets lost unhandled or unpassed. some trouble figuring out how the input filter should pass along data to the next in the chain. What I have currently is this: /* Get the output from Lua's yield() as a string */ const char* output = lua_tolstring(L, 1, olen); /* Make a bucket for it */ pbktOut = apr_bucket_heap_create(output, olen, 0, c-bucket_alloc); /* Add it to the brigade */ APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut); /* Do something more here involving cleanups or..?? */ But then what? Am I supposed to make a call to some ap_*_brigade function? I've checked some of the example filter modules, but I can't seem to find any suitable way of doing this other than finishing up the brigade and returning APR_SUCCESS in the end. Keep the input brigade you fetched in the context and return to the caller. You will be called again if more data is needed. Then you can continue processing the data in your context input brigade first and fetch further data on your own if you run out of data. Regards Rüdiger