Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion gixy/directives/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import cached_property

from gixy.directives.directive import Directive, MapDirective
from gixy.core.variable import Variable
from gixy.core.variable import Variable, compile_script
from gixy.core.regexp import Regexp


Expand Down Expand Up @@ -165,6 +165,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)
Expand All @@ -184,6 +185,28 @@ 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 []

boundary = None
compiled_script = compile_script(self.variable)
if len(compiled_script) == 1:
boundary = compiled_script[0].regexp

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=boundary, provider=self)
)
return result

def __str__(self):
return "{name} ({args}) {{".format(name=self.name, args=" ".join(self.args))

Expand Down
2 changes: 1 addition & 1 deletion tests/directives/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/plugins/simply/http_splitting/if_block.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
server {
server_name example.com;

if ($uri ~* ^/echo/(.*)$) {
return 301 $1;
}
}
7 changes: 7 additions & 0 deletions tests/plugins/simply/http_splitting/if_block_fp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
server {
server_name example.com;

if ($request_uri ~* ^/echo/(.*)$) {
return 301 $1;
}
}