Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic regex match #12

Open
Roycohen opened this issue Jul 5, 2015 · 3 comments
Open

Dynamic regex match #12

Roycohen opened this issue Jul 5, 2015 · 3 comments

Comments

@Roycohen
Copy link

Roycohen commented Jul 5, 2015

Hi There,
Is it possible to implement dynamic variables after matching a string.
I'll explain: I need to replace a know regex but the value that I need to change is dynamic and comes from the regex.
Example:

replace_filter "[(AV_.*?)]" '${arg_AV_$0}' g;

The above should find all matching regexes for [AV__] and replace it form the nginx arg_AV__.
If, I find [AV_STAT] in the body then it should change it from the {arg_AV_STAT}. And if I have another [AV_DEFINE] then the value should be taken from the {arg_AV_DEFINE}.

I hope I make my self clear.
Is there a way that this supported, and if not can you please guide me where to look in your code or or whether it is possible to modify your code and implement it?

BR,
Roy

@agentzh
Copy link
Member

agentzh commented Jul 5, 2015

@Roycohen It cannot be done in the current implementation though it's certainly possible to modify the current code base to support this. I think your configuration does not quite do what you want. It seems that you really need this instead (untested, of course):

replace_filter "\[(AV_[A-Z]+)\]" "${arg_$1}" g;

You'll need to call ngx_http_get_variable() to query nginx variables on the fly. Also, you need to re-evaluate the replacement string upon every match in the same request. I suggest adding a test to only do this when absolutely necessary so that we won't pay the price when the user does not use this feature.

Patches welcome :)

@Roycohen
Copy link
Author

Roycohen commented Jul 5, 2015

OK @agentzh,
Can you please give me a starting point, where are the main functions that I need to modify? If I'll succeed I'll be glad to add a patch :)

@agentzh
Copy link
Member

agentzh commented Jul 5, 2015

@Roycohen You need to patch the ngx_http_replace_body_filter function for calculating sub (i.e., the replacement string) and also most of the functions in the ngx_http_replace_script.c file for compiling and evaluating the replacement templates. Well, at least these functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants