Skip to content

Commit 6c3d18d

Browse files
authored
Merge pull request #11 from lelik9/feature
Fixing
2 parents dce3724 + 108e02b commit 6c3d18d

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

nginx.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,19 +415,19 @@ def loads(data, conf=True):
415415
lopen = []
416416
for line in data.split('\n'):
417417
line_outside_quotes = re.sub(r'"([^"]+)"|\'([^\']+)\'|\\S+', '', line)
418-
if re.match(r'\s*server\s*{', line):
418+
if re.match(r'\s*server\s*({.*)?$', line):
419419
s = Server()
420420
lopen.insert(0, s)
421421
if re.match(r'\s*location.*{', line):
422422
lpath = re.match(r'\s*location\s*(.*\S+)\s*{', line).group(1)
423423
l = Location(lpath)
424424
lopen.insert(0, l)
425-
if re.match(r'\s*if.*{', line):
426-
ifs = re.match('\s*if\s*(.*\S+)\s*{', line).group(1)
425+
if re.match(r'\s*if.*({.*)?$', line):
426+
ifs = re.match('\s*if\s*(.*\s+)\s*', line).group(1)
427427
ifs = If(ifs)
428428
lopen.insert(0, ifs)
429-
if re.match(r'\s*upstream.*{', line):
430-
ups = re.match(r'\s*upstream\s*(.*\S+)\s*{', line).group(1)
429+
if re.match(r'\s*upstream.*({.*)?$', line):
430+
ups = re.match(r'\s*upstream\s*(.*\S+)\s*[^{]', line).group(1)
431431
u = Upstream(ups)
432432
lopen.insert(0, u)
433433
if re.match(r'\s*geo\s*\$.*\s{', line):
@@ -445,7 +445,7 @@ def loads(data, conf=True):
445445
if "#" not in kname:
446446
k = Key(kname, kval)
447447
lopen[0].add(k)
448-
if re.match(r'.*}', line_outside_quotes):
448+
if re.match(r'(^(?!#)([^#]*[}]{1}\s*)$)|(\s*{$)', line_outside_quotes):
449449
closenum = len(re.findall('}', line_outside_quotes))
450450
while closenum > 0:
451451
if isinstance(lopen[0], Server):

tests.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,41 @@
2929
}
3030
"""
3131

32+
SECONDTESTBLOCK = """
33+
upstream php
34+
{
35+
server unix:/tmp/php-fcgi.socket;
36+
}
37+
server
38+
{
39+
listen 80; # This comment should be present;
40+
# And this one
41+
server_name localhost 127.0.0.1;
42+
root /srv/http; # And also this one
43+
mykey "myvalue; #notme myothervalue";
44+
# This one too
45+
index index.php;
46+
if (!-e $request_filename)
47+
{
48+
rewrite ^(.+)$ /index.php?q=$1 last;
49+
}
50+
51+
if (!-e $request_filename) {
52+
rewrite ^(.+)$ /index.php?q=$1 last;
53+
}
54+
location ~ \.php(?:$|/) {
55+
fastcgi_pass php;
56+
}
57+
58+
# location from the issue #10
59+
location / {
60+
return 301 $scheme://$host:$server_port${request_uri}bitbucket/;
61+
}
62+
}
63+
"""
64+
65+
66+
3267
MESSYBLOCK = """
3368
# This is an example of a messy config
3469
upstream php { server unix:/tmp/php-cgi.socket; }
@@ -62,6 +97,16 @@ def test_key_parse(self):
6297
self.assertEqual(thirdKey.name, 'mykey')
6398
self.assertEqual(thirdKey.value, '"myvalue; #notme myothervalue"')
6499

100+
def test_key_parse_testblock2(self):
101+
data = nginx.loads(SECONDTESTBLOCK)
102+
self.assertEqual(len(data.server.keys), 5)
103+
firstKey = data.server.keys[0]
104+
thirdKey = data.server.keys[3]
105+
self.assertEqual(firstKey.name, 'listen')
106+
self.assertEqual(firstKey.value, '80')
107+
self.assertEqual(thirdKey.name, 'mykey')
108+
self.assertEqual(thirdKey.value, '"myvalue; #notme myothervalue"')
109+
65110
def test_location_parse(self):
66111
data = nginx.loads(TESTBLOCK)
67112
self.assertEqual(len(data.server.locations), 1)

0 commit comments

Comments
 (0)