diff options
author | Stefan Fritsch <sf@apache.org> | 2010-11-07 22:09:19 +0100 |
---|---|---|
committer | Stefan Fritsch <sf@apache.org> | 2010-11-07 22:09:19 +0100 |
commit | 29d566dd9bf6f54954bb8bcc00f584178673738e (patch) | |
tree | 6e264fc0a1c014e468637c4b6e46c63aefc73a12 /server | |
parent | mark connection for close after the return from (diff) | |
download | apache2-29d566dd9bf6f54954bb8bcc00f584178673738e.tar.xz apache2-29d566dd9bf6f54954bb8bcc00f584178673738e.zip |
Port "file" function from ssl_expr
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1032393 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'server')
-rw-r--r-- | server/util_expr_eval.c | 46 |
1 files changed, 46 insertions, 0 deletions
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 ? */ |