Skip to content

Commit ff15230

Browse files
authored
Merge pull request #32 from SGamoff/fix-types-handling
Enable parsing of Types
2 parents 45da0af + f0f89b2 commit ff15230

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

nginx.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ def __init__(self, value, *args):
309309
class Types(Container):
310310
"""Container for MIME type mapping."""
311311

312-
def __init__(self, value, *args):
312+
def __init__(self, *args):
313313
"""Initialize."""
314-
super(Types, self).__init__(value, *args)
314+
super(Types, self).__init__('', *args)
315315
self.name = 'types'
316316

317317

@@ -436,49 +436,56 @@ def loads(data, conf=True):
436436
index += m.end()
437437
continue
438438

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

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

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

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

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

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

481-
m = re.compile(r'^(\s*)#\s*(.*?)\n', re.S).search(data[index:])
481+
m = re.compile(r'^\s*types\s*{', re.S).search(data[index:])
482+
if m:
483+
l = Types()
484+
lopen.insert(0, l)
485+
index += m.end()
486+
continue
487+
488+
m = re.compile(r'^(\s*)#[ \r\t\f]*(.*?)\n', re.S).search(data[index:])
482489
if m:
483490
c = Comment(m.group(2), inline='\n' not in m.group(1))
484491
if lopen and isinstance(lopen[0], Container):

tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@
168168
}
169169
"""
170170

171+
TESTBLOCK_CASE_10 = """
172+
types {
173+
application/CEA cea;
174+
application/cellml+xml cellml cml;
175+
application/clue_info+xml clue;
176+
application/cms cmsc;
177+
}
178+
"""
179+
180+
171181
class TestPythonNginx(unittest.TestCase):
172182
def test_basic_load(self):
173183
self.assertTrue(nginx.loads(TESTBLOCK_CASE_1) is not None)
@@ -272,6 +282,13 @@ def test_semicolon_in_second_key_value(self):
272282
self.assertEqual(location_children[0].name, "add_header")
273283
self.assertEqual(location_children[0].value, 'X-XSS-Protection "1;mode-block"')
274284

285+
def test_types_block(self):
286+
inp_data = nginx.loads(TESTBLOCK_CASE_10)
287+
self.assertEqual(len(inp_data.filter("Types")), 1)
288+
self.assertEqual(len(inp_data.filter("Types")[0].children), 4)
289+
self.assertEqual(len(inp_data.filter("Types")[0].filter("Key")), 4)
290+
data_type = inp_data.filter("Types")[0].filter("Key")[0]
291+
self.assertEqual(data_type.value, "cea")
275292

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

0 commit comments

Comments
 (0)