Esempio n. 1
0
RegexSearch::RegexSearch(const std::string& regex,
                         Report* report,
                         const std::wstring& file_path)
    : regex_(regex),
      report_(report),
      file_path_(file_path),
      block_offset_(0),
      match_offset_(0),
      sre_pool_(nullptr),
      sre_ctx_(nullptr) {
  sre_pool_ = sre_create_pool(4096);
  assert(sre_pool_);
  sre_uint_t ncaps;
  sre_int_t err_offset;
  sre_regex_t* sre_regex = sre_regex_parse(
      sre_pool_,
      const_cast<sre_char*>(reinterpret_cast<const sre_char*>(regex_.c_str())),
      &ncaps, 0, &err_offset);
  assert(sre_regex);
  sre_program_t* sre_program = sre_regex_compile(sre_pool_, sre_regex);
  assert(sre_program);
  matches_.resize(2 * (ncaps + 1));
  sre_ctx_ = sre_vm_pike_create_ctx(sre_pool_, sre_program, matches_.data(),
                                    matches_.size() * sizeof(sre_int_t));
  assert(sre_ctx_);
}
static char *
ngx_http_replace_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
    u_char         **value;
    sre_int_t        err_offset, err_regex_id;
    ngx_str_t        prefix, suffix;
    sre_pool_t      *ppool; /* parser pool */
    sre_regex_t     *re;
    sre_program_t   *prog;

    ngx_http_replace_main_conf_t    *rmcf;

    ngx_http_replace_loc_conf_t *prev = parent;
    ngx_http_replace_loc_conf_t *conf = child;

    ngx_conf_merge_size_value(conf->max_buffered_size,
                              prev->max_buffered_size,
                              8192);

    ngx_conf_merge_uint_value(conf->last_modified,
                              prev->last_modified,
                              NGX_HTTP_REPLACE_CLEAR_LAST_MODIFIED);

    if (ngx_http_merge_types(cf, &conf->types_keys, &conf->types,
                             &prev->types_keys, &prev->types,
                             ngx_http_html_default_types)
        != NGX_OK)
    {
        return NGX_CONF_ERROR;
    }

    if (conf->skip == NULL) {
        conf->skip = prev->skip;
    }

    if (conf->regexes.nelts > 0 && conf->program == NULL) {

        dd("parsing and compiling %d regexes", (int) conf->regexes.nelts);

        ppool = sre_create_pool(1024);
        if (ppool == NULL) {
            return NGX_CONF_ERROR;
        }

        value = conf->regexes.elts;

        re = sre_regex_parse_multi(ppool, value, conf->regexes.nelts,
                                   &conf->ncaps, conf->multi_flags.elts,
                                   &err_offset, &err_regex_id);

        if (re == NULL) {

            if (err_offset >= 0) {
                prefix.data = value[err_regex_id];
                prefix.len = err_offset;

                suffix.data = value[err_regex_id] + err_offset;
                suffix.len = ngx_strlen(value[err_regex_id]) - err_offset;

                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                   "failed to parse regex at offset %i: "
                                   "syntax error; marked by <-- HERE in "
                                   "\"%V <-- HERE %V\"",
                                   (ngx_int_t) err_offset, &prefix, &suffix);

            } else {

                if (err_regex_id >= 0) {
                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                       "failed to parse regex \"%s\"",
                                       value[err_regex_id]);

                } else {
                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                       "failed to parse regex \"%s\" "
                                       "and its siblings",
                                       value[0]);
                }
            }

            sre_destroy_pool(ppool);
            return NGX_CONF_ERROR;
        }

        rmcf = ngx_http_conf_get_module_main_conf(cf,
                                              ngx_http_replace_filter_module);

        prog = sre_regex_compile(rmcf->compiler_pool, re);

        sre_destroy_pool(ppool);

        if (prog == NULL) {
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                               "failed to compile regex \"%s\" and its "
                               "siblings", value[0]);

            return NGX_CONF_ERROR;
        }

        conf->program = prog;
        conf->ovecsize = 2 * (conf->ncaps + 1) * sizeof(sre_int_t);

    } else {

        conf->regexes       = prev->regexes;
        conf->multi_once    = prev->multi_once;
        conf->multi_flags   = prev->multi_flags;
        conf->multi_replace = prev->multi_replace;
        conf->parse_buf     = prev->parse_buf;
        conf->verbatim      = prev->verbatim;
        conf->program       = prev->program;
        conf->ncaps         = prev->ncaps;
        conf->ovecsize      = prev->ovecsize;
        conf->seen_once     = prev->seen_once;
        conf->seen_global   = prev->seen_global;
    }

    return NGX_CONF_OK;
}