[Modules] [PATCH] Add environment variables to the params set for mod_transform
Vincent Bray
noodlet at gmail.com
Sun May 27 17:43:17 EDT 2007
Hi,
Attached is a patch which sends r->subprocess_env to the params
argument of xsltApplyStylesheet() for the 0.6.0 release of
mod_transform, as requested by a user on #apache. It's my first
attempt at doing anything useful with C but I've got thick skin so if
it's crap, say so :-)
--
noodl
-------------- next part --------------
Index: include/mod_transform_private.h
===================================================================
--- include/mod_transform_private.h (revision 92)
+++ include/mod_transform_private.h (working copy)
@@ -115,6 +115,14 @@
}
transform_xmlio_input_ctx;
+typedef struct
+{
+ int counter;
+ request_rec *r;
+ const char **params;
+}
+params_ctx;
+
apr_status_t transform_apachefs_filter(ap_filter_t * f,
apr_bucket_brigade * bb);
Index: src/mod_transform.c
===================================================================
--- src/mod_transform.c (revision 92)
+++ src/mod_transform.c (working copy)
@@ -61,6 +61,52 @@
return NULL;
}
+/* apr_table_do callback for populating the xslt params array */
+static int populate_params(void *rec, const char *key, const char *value)
+{
+ params_ctx *ctx = rec;
+ ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, ctx->r,
+ "mod_transform: populating array with: (%d) %s - %s",
+ ctx->counter, key, value);
+ ctx->params[ctx->counter++] = key;
+ ctx->params[ctx->counter++] = value;
+ return 1;
+}
+
+/* Convert subprocess_env to the format libxslt expects for params */
+static char *env_to_params(request_rec *r)
+{
+ int params_cnt, env_cnt;
+ const char **params;
+ params_ctx *ctx;
+
+ /* Get the number of entries in the env table. */
+ env_cnt = apr_table_elts(r->subprocess_env)->nelts;
+ ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+ "mod_transform: Env var count: %d", env_cnt);
+
+ /**
+ * Create the params array large enough to hold a flattened, null terminated
+ * subprocess_env. The format is:
+ * const char *feh[] = {"key", "val", "key", "val", NULL};
+ */
+ params_cnt = env_cnt * 2 + 1;
+ params = apr_palloc(r->pool, sizeof(char *) * params_cnt);
+
+ /* Use this simple struct to keep state in the callback */
+ ctx = apr_palloc(r->pool, sizeof(params_ctx));
+ ctx->r = r;
+ ctx->counter = 0;
+ ctx->params = params;
+
+ /* Populate params */
+ apr_table_do(populate_params, ctx, r->subprocess_env);
+ /* Terminate the array */
+ ctx->params[params_cnt - 1] = NULL;
+
+ return ctx->params;
+}
+
static apr_status_t transform_run(ap_filter_t * f, xmlDocPtr doc)
{
size_t length;
@@ -77,6 +123,7 @@
&transform_module);
svr_cfg *sconf = ap_get_module_config(f->r->server->module_config,
&transform_module);
+ const char **params;
if (!doc) {
return pass_failure(f, "XSLT: Couldn't parse XML Document", notes);
@@ -128,7 +175,9 @@
return pass_failure(f, "XSLT: Loading of the XSLT File has failed", notes);
}
- result = xsltApplyStylesheet(transform, doc, 0);
+ params = env_to_params(f->r);
+
+ result = xsltApplyStylesheet(transform, doc, params);
if (!result) {
if (!stylesheet_is_cached) {
xsltFreeStylesheet(transform);
More information about the Modules
mailing list