From 02302c904987f38fa84856cccb206a7a16ffdc15 Mon Sep 17 00:00:00 2001 From: Stefan Fritsch Date: Tue, 18 Oct 2011 22:02:26 +0000 Subject: [PATCH] regex related cleanups: - use AP_MAX_REG_MATCH where appropriate - in mod_proxy_ftp, compile the "ls -s1" regex only once instead of once per request - add some coments git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1185898 13f79535-47bb-0310-9956-ffa450edef68 --- modules/filters/mod_include.c | 5 +---- modules/proxy/mod_proxy_ftp.c | 17 +++++++++-------- server/util_expr_eval.c | 8 ++++---- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/modules/filters/mod_include.c b/modules/filters/mod_include.c index fdbde51b92..e2563bb2d5 100644 --- a/modules/filters/mod_include.c +++ b/modules/filters/mod_include.c @@ -674,12 +674,9 @@ static const char *get_include_var(const char *var, include_ctx_t *ctx) return NULL; } else if (re->match[idx].rm_so < 0 || re->match[idx].rm_eo < 0) { - /* I don't think this can happen if have_match is true. - * But let's not risk a regression by dropping this - */ + /* This particular subpattern was not used by the regex */ return NULL; } - else { val = apr_pstrmemdup(ctx->dpool, re->source + re->match[idx].rm_so, re->match[idx].rm_eo - re->match[idx].rm_so); diff --git a/modules/proxy/mod_proxy_ftp.c b/modules/proxy/mod_proxy_ftp.c index aa1cf24c49..a48213470d 100644 --- a/modules/proxy/mod_proxy_ftp.c +++ b/modules/proxy/mod_proxy_ftp.c @@ -363,6 +363,7 @@ typedef struct { /* fallback regex for ls -s1; ($0..$2) == 3 */ #define LS_REG_PATTERN "^ *([0-9]+) +([^ ]+)$" #define LS_REG_MATCH 3 +ap_regex_t *ls_regex = NULL; static apr_status_t proxy_send_dir_filter(ap_filter_t *f, apr_bucket_brigade *in) @@ -524,14 +525,8 @@ static apr_status_t proxy_send_dir_filter(ap_filter_t *f, char *filename; int found = 0; int eos = 0; - - ap_regex_t *re = NULL; ap_regmatch_t re_result[LS_REG_MATCH]; - /* Compile the output format of "ls -s1" as a fallback for non-unix ftp listings */ - re = ap_pregcomp(p, LS_REG_PATTERN, AP_REG_EXTENDED); - ap_assert(re != NULL); - /* get a complete line */ /* if the buffer overruns - throw data away */ while (!found && !APR_BRIGADE_EMPTY(ctx->in)) { @@ -654,8 +649,11 @@ static apr_status_t proxy_send_dir_filter(ap_filter_t *f, } } /* Try a fallback for listings in the format of "ls -s1" */ - else if (0 == ap_regexec(re, ctx->buffer, LS_REG_MATCH, re_result, 0)) { - + else if (0 == ap_regexec(ls_regex, ctx->buffer, LS_REG_MATCH, re_result, 0)) { + /* + * We don't need to check for rm_eo == rm_so == -1 here since ls_regex + * is such that $2 cannot be unset if we have a match. + */ filename = apr_pstrndup(p, &ctx->buffer[re_result[2].rm_so], re_result[2].rm_eo - re_result[2].rm_so); str = apr_pstrcat(p, ap_escape_html(p, apr_pstrndup(p, ctx->buffer, re_result[2].rm_so)), @@ -2016,6 +2014,9 @@ static void ap_proxy_ftp_register_hook(apr_pool_t *p) /* filters */ ap_register_output_filter("PROXY_SEND_DIR", proxy_send_dir_filter, NULL, AP_FTYPE_RESOURCE); + /* Compile the output format of "ls -s1" as a fallback for non-unix ftp listings */ + ls_regex = ap_pregcomp(p, LS_REG_PATTERN, AP_REG_EXTENDED); + ap_assert(ls_regex != NULL); } static const command_rec proxy_ftp_cmds[] = diff --git a/server/util_expr_eval.c b/server/util_expr_eval.c index 0530463723..3dd5863ae8 100644 --- a/server/util_expr_eval.c +++ b/server/util_expr_eval.c @@ -749,7 +749,7 @@ AP_DECLARE(int) ap_expr_exec_re(request_rec *r, const ap_expr_info_t *info, ap_expr_eval_ctx_t ctx; int dont_vary = (info->flags & AP_EXPR_FLAG_DONT_VARY); const char *tmp_source = NULL, *vary_this = NULL; - ap_regmatch_t tmp_pmatch[10]; + ap_regmatch_t tmp_pmatch[AP_MAX_REG_MATCH]; AP_DEBUG_ASSERT((info->flags & AP_EXPR_FLAG_STRING_RESULT) == 0); @@ -766,7 +766,7 @@ AP_DECLARE(int) ap_expr_exec_re(request_rec *r, const ap_expr_info_t *info, ctx.data = NULL; if (!pmatch) { - ctx.re_nmatch = 10; + ctx.re_nmatch = AP_MAX_REG_MATCH; ctx.re_pmatch = tmp_pmatch; ctx.re_source = &tmp_source; } @@ -784,7 +784,7 @@ AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r, ap_expr_eval_ctx_t ctx; int dont_vary, rc; const char *tmp_source = NULL, *vary_this = NULL; - ap_regmatch_t tmp_pmatch[10]; + ap_regmatch_t tmp_pmatch[AP_MAX_REG_MATCH]; const char *result; AP_DEBUG_ASSERT(info->flags & AP_EXPR_FLAG_STRING_RESULT); @@ -811,7 +811,7 @@ AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r, ctx.result_string = &result; if (!pmatch) { - ctx.re_nmatch = 10; + ctx.re_nmatch = AP_MAX_REG_MATCH; ctx.re_pmatch = tmp_pmatch; ctx.re_source = &tmp_source; } -- 2.40.0