Improved regex for "if" directive condition capturing

pull/46/head
Andrew Krasichkov 2017-05-14 14:33:38 +03:00
parent 92f2c01831
commit c5df3f073e
2 changed files with 10 additions and 5 deletions

View File

@ -57,15 +57,15 @@ class RawParser(object):
Keyword("=") |
Keyword("~*") | Keyword("~") |
(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 = (
(if_modifier + Optional(space) + value) |
(variable + Optional(space + if_modifier + Optional(space) + value))
)
# This ugly workaround needed to parse unquoted regex with nested parentheses
# pyparsing.nestedExpr doesn't work in some rare cases like: ($http_user_agent ~* \( )
# 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]))
condition = Regex(r'\((?:[^();\n\r\\]|(?:\(.*\))|(?:\\.))+?\)')\
.setParseAction(lambda s, l, t: condition_body.parseString(t[0][1:-1]))
# rules
include = (

View File

@ -234,6 +234,8 @@ if ($host ~* (lori|rage2)\.yandex\.(ru|ua|com|com\.tr)) {
if ($request_filename ~* ^.*?/(\d+_)([^/]+)$) {
}
if ($foo = "BAR") { rewrite ^(.*)$ /bar; }
'''
expected = [
@ -262,6 +264,9 @@ if ($request_filename ~* ^.*?/(\d+_)([^/]+)$) {
['set', '$x_frame_options', 'ALLOW']
]],
['if', ['$request_filename', '~*', '^.*?/(\d+_)([^/]+)$'], [
]],
['if', ['$foo', '=', 'BAR'], [
['rewrite', '^(.*)$', '/bar']
]]
]