1

Hi guys im trying to build a module for nginx and need to match a substring here is what im using to try and match

int match_chan(ngx_http_request_t *r, ngx_pool_t *temp_pool, ngx_str_t *body, ngx_str_t *channel) {
    u_char errstr[NGX_MAX_CONF_ERRSTR];
    ngx_regex_compile_t *rc;
    int captures[2];
    if ((rc = ngx_pcalloc(temp_pool, sizeof(ngx_regex_compile_t))) == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "unable to allocate memory to compile agent patterns");
        return 0;
    }
    //ngx_memzero(rc, sizeof(ngx_regex_compile_t));
    ngx_str_t pat = ngx_string("test(:|%3[Aa])([a-zA-Z0-9]+)");
    rc->pattern = pat;
    rc->pool = temp_pool;
    rc->err.len = NGX_MAX_CONF_ERRSTR;
    rc->err.data = errstr;
    if (ngx_regex_compile(rc) != NGX_OK) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "unable to compile regex pattern %V", rc->pattern);
        return 0;
    }
    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%V, %V", &pat, body);
    if (ngx_regex_exec(rc->regex, body, captures, 2) >= 0) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "It Matched");
        //ngx_memcpy(channel->data, body->data + captures[0], body->len);
        return 1;
    }
    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "It did not match");
    return 0;
}

ngx_str_t *channel = NULL;
if(match_chan(r, temp_pool, aux, channel)) {
  //ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, " match: %c", match);
}

and the message that is passed looks like this

2014/07/04 13:28:49 [error] 10695#0: *38 test:([a-z0-9]+), MSG%0Atest%3Ahello%0A%0A%0Awins%00
2014/07/04 13:28:49 [error] 10695#0: *38 It did not match

taken from nginx log

ive tested the regex in a pure C app and that worked fine i thought nginx was similar but i guess it has its differences

ive looked all over google and ive tried looking at nginx modules with still no luck please help me :)

Thanks Dave

2
  • I know somone is going to reply to me saying its because the string is encoded but how do i accout for that regex101.com/r/yV1xW1/1 Commented Jul 4, 2014 at 13:51
  • well i got it to match with ngx_str_t pat = ngx_string("test%3A([a-zA-Z0-9]+)"); but it seems to end the thread on every message recieved so im still going wrong somewhere Commented Jul 4, 2014 at 15:09

1 Answer 1

1

The problem is that the string you are trying to match is URL-encoded, and due to this it doesn't match the pattern provided. There are two options:

  • Construct a regular expression so it will match encoded string as well ("test(:|%3[Aa])([a-zA-Z0-9]+)" will match both unescaped and escaped forms);

  • Unescape the string you are matching. In nginx, this is done with the ngx_unescape_uri() function.

Sign up to request clarification or add additional context in comments.

1 Comment

OK thanks so much for taking the time to reply so ive rebuilt nginx with the changes it matches ok but the thread is still exiting i have no idea why could you help with that ill edit the post above with what i have now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.