Skip to content
Merged
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: 16 additions & 9 deletions nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ def __init__(self, value, *args):
class Types(Container):
"""Container for MIME type mapping."""

def __init__(self, value, *args):
def __init__(self, *args):
"""Initialize."""
super(Types, self).__init__(value, *args)
super(Types, self).__init__('', *args)
self.name = 'types'


Expand Down Expand Up @@ -436,49 +436,56 @@ def loads(data, conf=True):
index += m.end()
continue

m = re.compile(r'^\s*location\s*(.*?)\s*{', re.S).search(data[index:])
m = re.compile(r'^\s*location\s*([^;]*?)\s*{', re.S).search(data[index:])
if m:
l = Location(m.group(1))
lopen.insert(0, l)
index += m.end()
continue

m = re.compile(r'^\s*if\s*(.*?)\s*{', re.S).search(data[index:])
m = re.compile(r'^\s*if\s*([^;]*?)\s*{', re.S).search(data[index:])
if m:
ifs = If(m.group(1))
lopen.insert(0, ifs)
index += m.end()
continue

m = re.compile(r'^\s*upstream\s*(.*?)\s*{', re.S).search(data[index:])
m = re.compile(r'^\s*upstream\s*([^;]*?)\s*{', re.S).search(data[index:])
if m:
u = Upstream(m.group(1))
lopen.insert(0, u)
index += m.end()
continue

m = re.compile(r'^\s*geo\s*(.*?)\s*{', re.S).search(data[index:])
m = re.compile(r'^\s*geo\s*([^;]*?)\s*{', re.S).search(data[index:])
if m:
g = Geo(m.group(1))
lopen.insert(0, g)
index += m.end()
continue

m = re.compile(r'^\s*map\s*(.*?)\s*{', re.S).search(data[index:])
m = re.compile(r'^\s*map\s*([^;]*?)\s*{', re.S).search(data[index:])
if m:
g = Map(m.group(1))
lopen.insert(0, g)
index += m.end()
continue

m = re.compile(r'^\s*limit_except\s*(.*?)\s*{', re.S).search(data[index:])
m = re.compile(r'^\s*limit_except\s*([^;]*?)\s*{', re.S).search(data[index:])
if m:
l = LimitExcept(m.group(1))
lopen.insert(0, l)
index += m.end()
continue

m = re.compile(r'^(\s*)#\s*(.*?)\n', re.S).search(data[index:])
m = re.compile(r'^\s*types\s*{', re.S).search(data[index:])
if m:
l = Types()
lopen.insert(0, l)
index += m.end()
continue

m = re.compile(r'^(\s*)#[ \r\t\f]*(.*?)\n', re.S).search(data[index:])
if m:
c = Comment(m.group(2), inline='\n' not in m.group(1))
if lopen and isinstance(lopen[0], Container):
Expand Down
17 changes: 17 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@
}
"""

TESTBLOCK_CASE_10 = """
types {
application/CEA cea;
application/cellml+xml cellml cml;
application/clue_info+xml clue;
application/cms cmsc;
}
"""


class TestPythonNginx(unittest.TestCase):
def test_basic_load(self):
self.assertTrue(nginx.loads(TESTBLOCK_CASE_1) is not None)
Expand Down Expand Up @@ -272,6 +282,13 @@ def test_semicolon_in_second_key_value(self):
self.assertEqual(location_children[0].name, "add_header")
self.assertEqual(location_children[0].value, 'X-XSS-Protection "1;mode-block"')

def test_types_block(self):
inp_data = nginx.loads(TESTBLOCK_CASE_10)
self.assertEqual(len(inp_data.filter("Types")), 1)
self.assertEqual(len(inp_data.filter("Types")[0].children), 4)
self.assertEqual(len(inp_data.filter("Types")[0].filter("Key")), 4)
data_type = inp_data.filter("Types")[0].filter("Key")[0]
self.assertEqual(data_type.value, "cea")

if __name__ == '__main__':
unittest.main()