mirror of https://github.com/yandex/gixy
Improved regex for "if" directive condition capturing
parent
92f2c01831
commit
c5df3f073e
|
@ -57,15 +57,15 @@ class RawParser(object):
|
||||||
Keyword("=") |
|
Keyword("=") |
|
||||||
Keyword("~*") | Keyword("~") |
|
Keyword("~*") | Keyword("~") |
|
||||||
(Literal("-") + (Literal("f") | Literal("d") | Literal("e") | Literal("x")))))
|
(Literal("-") + (Literal("f") | Literal("d") | Literal("e") | Literal("x")))))
|
||||||
|
# This ugly workaround needed to parse unquoted regex with nested parentheses
|
||||||
|
# so we capture all content between parentheses and then parse it :(
|
||||||
|
# TODO(buglloc): may be use something better?
|
||||||
condition_body = (
|
condition_body = (
|
||||||
(if_modifier + Optional(space) + value) |
|
(if_modifier + Optional(space) + value) |
|
||||||
(variable + Optional(space + if_modifier + Optional(space) + value))
|
(variable + Optional(space + if_modifier + Optional(space) + value))
|
||||||
)
|
)
|
||||||
# This ugly workaround needed to parse unquoted regex with nested parentheses
|
condition = Regex(r'\((?:[^();\n\r\\]|(?:\(.*\))|(?:\\.))+?\)')\
|
||||||
# pyparsing.nestedExpr doesn't work in some rare cases like: ($http_user_agent ~* \( )
|
.setParseAction(lambda s, l, t: condition_body.parseString(t[0][1:-1]))
|
||||||
# so we capture all content between parentheses and then parse it:)
|
|
||||||
# TODO(buglloc): may be use something better?
|
|
||||||
condition = Regex(r'\(.*\)').setParseAction(lambda s, l, t: condition_body.parseString(t[0][1:-1]))
|
|
||||||
|
|
||||||
# rules
|
# rules
|
||||||
include = (
|
include = (
|
||||||
|
|
|
@ -234,6 +234,8 @@ if ($host ~* (lori|rage2)\.yandex\.(ru|ua|com|com\.tr)) {
|
||||||
|
|
||||||
if ($request_filename ~* ^.*?/(\d+_)([^/]+)$) {
|
if ($request_filename ~* ^.*?/(\d+_)([^/]+)$) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($foo = "BAR") { rewrite ^(.*)$ /bar; }
|
||||||
'''
|
'''
|
||||||
|
|
||||||
expected = [
|
expected = [
|
||||||
|
@ -262,6 +264,9 @@ if ($request_filename ~* ^.*?/(\d+_)([^/]+)$) {
|
||||||
['set', '$x_frame_options', 'ALLOW']
|
['set', '$x_frame_options', 'ALLOW']
|
||||||
]],
|
]],
|
||||||
['if', ['$request_filename', '~*', '^.*?/(\d+_)([^/]+)$'], [
|
['if', ['$request_filename', '~*', '^.*?/(\d+_)([^/]+)$'], [
|
||||||
|
]],
|
||||||
|
['if', ['$foo', '=', 'BAR'], [
|
||||||
|
['rewrite', '^(.*)$', '/bar']
|
||||||
]]
|
]]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue