From 2629743ed4c545622bdba65266bb189f363b91a1 Mon Sep 17 00:00:00 2001 From: Joshua Rogers Date: Thu, 8 May 2025 17:57:47 +0200 Subject: [PATCH 1/7] Parse capture groups in if-blocks. Fixes #50. --- gixy/directives/block.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/gixy/directives/block.py b/gixy/directives/block.py index 9abf05b..a2f4130 100644 --- a/gixy/directives/block.py +++ b/gixy/directives/block.py @@ -149,6 +149,7 @@ def needs_anchor(self): class IfBlock(Block): nginx_name = "if" self_context = False + provide_variables = True def __init__(self, name, args): super(IfBlock, self).__init__(name, args) @@ -168,6 +169,25 @@ def __init__(self, name, args): else: raise Exception('Unknown "if" definition, args: {0!r}'.format(args)) + @property + def is_regex(self): + return self.operand and self.operand in ("~", "~*", '!~', '!~*') + + @cached_property + def variables(self): + if not self.is_regex: + return [] + + regexp = Regexp(self.value, case_sensitive=self.operand in ["~", '!~']) + result = [] + for name, group in regexp.groups.items(): + result.append( + Variable(name=name, value=group, boundary=None, provider=self) + ) + return result + + + def __str__(self): return "{name} ({args}) {{".format(name=self.name, args=" ".join(self.args)) From 07100cd4e900a0d3b75061a487c356352db7e972 Mon Sep 17 00:00:00 2001 From: Joshua Rogers Date: Fri, 9 May 2025 13:31:37 +0200 Subject: [PATCH 2/7] update test --- tests/directives/test_block.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/directives/test_block.py b/tests/directives/test_block.py index a9d0b85..5b69a27 100644 --- a/tests/directives/test_block.py +++ b/tests/directives/test_block.py @@ -103,7 +103,7 @@ def test_if(): assert isinstance(directive, IfBlock) assert directive.is_block assert not directive.self_context - assert not directive.provide_variables + assert directive.provide_variables assert directive.variable == '$some' assert directive.operand is None assert directive.value is None From e2786cc058c0d81dd7d16ceb9e29b7259b480e86 Mon Sep 17 00:00:00 2001 From: Joshua Rogers Date: Mon, 19 May 2025 01:56:31 +0200 Subject: [PATCH 3/7] add boundary for if-block. fixes #56 --- gixy/directives/block.py | 11 +++++++---- tests/plugins/simply/http_splitting/if_block_fp.conf | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 tests/plugins/simply/http_splitting/if_block_fp.conf diff --git a/gixy/directives/block.py b/gixy/directives/block.py index a2f4130..0bdf6ee 100644 --- a/gixy/directives/block.py +++ b/gixy/directives/block.py @@ -4,7 +4,7 @@ from functools import cached_property from gixy.directives.directive import Directive -from gixy.core.variable import Variable +from gixy.core.variable import Variable, compile_script from gixy.core.regexp import Regexp @@ -178,16 +178,19 @@ def variables(self): if not self.is_regex: return [] + boundary = None + compiled_script = compile_script(self.variable) + if len(compiled_script) == 1: + boundary = compiled_script[0].value + regexp = Regexp(self.value, case_sensitive=self.operand in ["~", '!~']) result = [] for name, group in regexp.groups.items(): result.append( - Variable(name=name, value=group, boundary=None, provider=self) + Variable(name=name, value=group, boundary=boundary, provider=self) ) return result - - def __str__(self): return "{name} ({args}) {{".format(name=self.name, args=" ".join(self.args)) diff --git a/tests/plugins/simply/http_splitting/if_block_fp.conf b/tests/plugins/simply/http_splitting/if_block_fp.conf new file mode 100644 index 0000000..469eca8 --- /dev/null +++ b/tests/plugins/simply/http_splitting/if_block_fp.conf @@ -0,0 +1,7 @@ +server { + server_name example.com; + + if ($request_uri ~* ^/echo/(.*)$) { + return 301 $1; + } +} From 69ed9ecb92051dd838541193c3db502b6ddb8196 Mon Sep 17 00:00:00 2001 From: Joshua Rogers Date: Mon, 19 May 2025 01:58:53 +0200 Subject: [PATCH 4/7] add if_block test --- tests/plugins/simply/http_splitting/if_block.conf | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/plugins/simply/http_splitting/if_block.conf diff --git a/tests/plugins/simply/http_splitting/if_block.conf b/tests/plugins/simply/http_splitting/if_block.conf new file mode 100644 index 0000000..413615d --- /dev/null +++ b/tests/plugins/simply/http_splitting/if_block.conf @@ -0,0 +1,7 @@ +server { + server_name example.com; + + if ($uri ~* ^/echo/(.*)$) { + return 301 $1; + } +} From dbc502714e3c34af6a611bd6a36dab64de2569fd Mon Sep 17 00:00:00 2001 From: Joshua Rogers Date: Mon, 30 Jun 2025 03:10:20 +0200 Subject: [PATCH 5/7] boundaries are only regexp --- gixy/directives/block.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gixy/directives/block.py b/gixy/directives/block.py index 0bdf6ee..7431316 100644 --- a/gixy/directives/block.py +++ b/gixy/directives/block.py @@ -181,7 +181,7 @@ def variables(self): boundary = None compiled_script = compile_script(self.variable) if len(compiled_script) == 1: - boundary = compiled_script[0].value + boundary = compiled_script[0].regexp regexp = Regexp(self.value, case_sensitive=self.operand in ["~", '!~']) result = [] From 3fa81c8334d4969c8e36d38b86d02406ab8f20b2 Mon Sep 17 00:00:00 2001 From: Joshua Rogers Date: Tue, 30 Sep 2025 16:10:16 +0200 Subject: [PATCH 6/7] Update gixy/directives/block.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- gixy/directives/block.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gixy/directives/block.py b/gixy/directives/block.py index 7431316..bd6d687 100644 --- a/gixy/directives/block.py +++ b/gixy/directives/block.py @@ -171,7 +171,7 @@ def __init__(self, name, args): @property def is_regex(self): - return self.operand and self.operand in ("~", "~*", '!~', '!~*') + return self.operand and self.operand in {"~", "~*", "!~", "!~*"} @cached_property def variables(self): From e6ed3ee09394c7aa2fc7cbbe83d028b324caacbe Mon Sep 17 00:00:00 2001 From: Joshua Rogers Date: Tue, 30 Sep 2025 16:10:30 +0200 Subject: [PATCH 7/7] Update gixy/directives/block.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- gixy/directives/block.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gixy/directives/block.py b/gixy/directives/block.py index bd6d687..baa7c08 100644 --- a/gixy/directives/block.py +++ b/gixy/directives/block.py @@ -183,7 +183,7 @@ def variables(self): if len(compiled_script) == 1: boundary = compiled_script[0].regexp - regexp = Regexp(self.value, case_sensitive=self.operand in ["~", '!~']) + regexp = Regexp(self.value, case_sensitive=self.operand in {"~", '!~'}) result = [] for name, group in regexp.groups.items(): result.append(