From 29d566dd9bf6f54954bb8bcc00f584178673738e Mon Sep 17 00:00:00 2001 From: Stefan Fritsch Date: Sun, 7 Nov 2010 21:09:19 +0000 Subject: [PATCH] Port "file" function from ssl_expr git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1032393 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/expr.xml | 4 +++- server/util_expr_eval.c | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/docs/manual/expr.xml b/docs/manual/expr.xml index aa81488169..a4d2594aa1 100644 --- a/docs/manual/expr.xml +++ b/docs/manual/expr.xml @@ -290,7 +290,7 @@ listfunction ::= listfuncname "(" word ")" Lookup operating system environment variable env Lookup request environment variable (XXX: will need to be changed for better ssl_expr compat) - lolower + tolower Convert string to lower case toupper Convert string to uppser case @@ -298,6 +298,8 @@ listfunction ::= listfuncname "(" word ")" Escape special characters in %hex encoding unescape Unescape %hex encoded string, leaving URL-special characters encoded (XXX: describe better) + file + Read contents from a file

In addition to string-valued functions, there are also list-valued functions which diff --git a/server/util_expr_eval.c b/server/util_expr_eval.c index 968e1a07e8..153b2467d9 100644 --- a/server/util_expr_eval.c +++ b/server/util_expr_eval.c @@ -749,6 +749,51 @@ static const char *escape_func(ap_expr_eval_ctx *ctx, const char *name, const ch return ap_escape_uri(ctx->p, arg); } +#define MAX_FILE_SIZE 10*1024*1024 +static const char *file_func(ap_expr_eval_ctx *ctx, const char *name, char *arg) +{ + apr_file_t *fp; + char *buf; + apr_off_t offset; + apr_size_t len; + apr_finfo_t finfo; + + if (apr_file_open(&fp, arg, APR_READ|APR_BUFFERED, + APR_OS_DEFAULT, ctx->p) != APR_SUCCESS) { + *ctx->err = apr_psprintf(ctx->p, "Cannot open file %s", arg); + return ""; + } + apr_file_info_get(&finfo, APR_FINFO_SIZE, fp); + if (finfo.size > MAX_FILE_SIZE) { + *ctx->err = apr_psprintf(ctx->p, "File %s too large", arg); + apr_file_close(fp); + return ""; + } + len = (apr_size_t)finfo.size; + if (len == 0) { + apr_file_close(fp); + return ""; + } + else { + if ((buf = (char *)apr_palloc(ctx->p, sizeof(char)*(len+1))) == NULL) { + *ctx->err = "Cannot allocate memory"; + apr_file_close(fp); + return ""; + } + offset = 0; + apr_file_seek(fp, APR_SET, &offset); + if (apr_file_read(fp, buf, &len) != APR_SUCCESS) { + *ctx->err = apr_psprintf(ctx->p, "Cannot read from file %s", arg); + apr_file_close(fp); + return ""; + } + buf[len] = '\0'; + } + apr_file_close(fp); + return buf; +} + + static const char *unescape_func(ap_expr_eval_ctx *ctx, const char *name, const char *arg) { char *result = apr_pstrdup(ctx->p, arg); @@ -988,6 +1033,7 @@ static const struct expr_provider_single string_func_providers[] = { { toupper_func, "toupper" }, { escape_func, "escape" }, { unescape_func, "unescape" }, + { file_func, "file" }, { NULL, NULL} }; /* XXX: base64 encode/decode ? */ -- 2.40.0