Skip to content

Commit 9880a38

Browse files
committed
fix: add geo directive to the payload without further parsing
1 parent 0847e95 commit 9880a38

File tree

5 files changed

+341
-3
lines changed

5 files changed

+341
-3
lines changed

parse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co
260260
}
261261
}
262262

263-
// if inside map block - add contents to payload, but do not parse further
264-
if len(ctx) > 0 && (ctx[len(ctx)-1] == "map" || ctx[len(ctx)-1] == "charset_map") {
263+
// if inside map or geo block - add contents to payload, but do not parse further
264+
if len(ctx) > 0 && (ctx[len(ctx)-1] == "map" || ctx[len(ctx)-1] == "charset_map" || ctx[len(ctx)-1] == "geo") {
265265
mapErr := analyzeMapContents(parsing.File, stmt, t.Value)
266266
if mapErr != nil && p.options.StopParsingOnError {
267267
return nil, mapErr
@@ -445,7 +445,7 @@ func analyzeMapContents(fname string, stmt *Directive, term string) error {
445445
}
446446
if len(stmt.Args) != 1 {
447447
return &ParseError{
448-
What: "invalid number of the map parameters",
448+
What: "invalid number of parameters",
449449
File: &fname,
450450
Line: &stmt.Line,
451451
}

parse_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,100 @@ var parseFixtures = []parseFixture{
10791079
},
10801080
},
10811081
}},
1082+
{"geo", "", ParseOptions{ErrorOnUnknownDirectives: true}, Payload{
1083+
Status: "ok",
1084+
Config: []Config{
1085+
{
1086+
File: getTestConfigPath("directive-with-space", "nginx.conf"),
1087+
Status: "ok",
1088+
Parsed: Directives{
1089+
{
1090+
Directive: "events",
1091+
Args: []string{},
1092+
Line: 1,
1093+
Block: Directives{
1094+
{
1095+
Directive: "worker_connections",
1096+
Args: []string{"1024"},
1097+
Line: 2,
1098+
},
1099+
},
1100+
},
1101+
{
1102+
Directive: "http",
1103+
Args: []string{},
1104+
Line: 5,
1105+
Block: Directives{
1106+
{
1107+
Directive: "geo",
1108+
Args: []string{"$geo"},
1109+
Line: 6,
1110+
Block: Directives{
1111+
{
1112+
Directive: "default",
1113+
Args: []string{"0"},
1114+
Line: 7,
1115+
Block: Directives{},
1116+
},
1117+
{
1118+
Directive: "192.168.1.0/24",
1119+
Args: []string{"1"},
1120+
Line: 8,
1121+
Block: Directives{},
1122+
},
1123+
{
1124+
Directive: "127.0.0.1",
1125+
Args: []string{"2"},
1126+
Line: 9,
1127+
Block: Directives{},
1128+
},
1129+
},
1130+
},
1131+
{
1132+
Directive: "server",
1133+
Args: []string{},
1134+
Line: 11,
1135+
Block: Directives{
1136+
{
1137+
Directive: "listen",
1138+
Args: []string{"127.0.0.1:8080"},
1139+
Line: 12,
1140+
Block: Directives{},
1141+
},
1142+
{
1143+
Directive: "server_name",
1144+
Args: []string{"default_server"},
1145+
Line: 13,
1146+
Block: Directives{},
1147+
},
1148+
{
1149+
Directive: "location",
1150+
Args: []string{"/"},
1151+
Line: 14,
1152+
Block: Directives{
1153+
{
1154+
Directive: "if",
1155+
Args: []string{"$geo", "=", "2"},
1156+
Line: 15,
1157+
Block: Directives{
1158+
{
1159+
Directive: "return",
1160+
Args: []string{"403"},
1161+
Line: 16,
1162+
Block: Directives{},
1163+
},
1164+
},
1165+
},
1166+
},
1167+
},
1168+
},
1169+
},
1170+
},
1171+
},
1172+
},
1173+
},
1174+
},
1175+
}},
10821176
}
10831177

10841178
func TestParse(t *testing.T) {

testdata/configs/geo/nginx.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
events {
2+
worker_connections 1024;
3+
}
4+
5+
http {
6+
geo $geo {
7+
default 0;
8+
192.168.1.0/24 1;
9+
127.0.0.1 2;
10+
}
11+
server {
12+
listen 127.0.0.1:8080;
13+
server_name default_server;
14+
location / {
15+
if ( $geo = 2 ) {
16+
return 403;
17+
}
18+
}
19+
}
20+
}

testdata/configs/geo/nginx.json

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"status": "ok",
3+
"errors": [],
4+
"config": [
5+
{
6+
"file": "testdata/configs/geo/nginx.conf",
7+
"status": "ok",
8+
"errors": [],
9+
"parsed": [
10+
{
11+
"directive": "events",
12+
"line": 1,
13+
"args": [],
14+
"block": [
15+
{
16+
"directive": "worker_connections",
17+
"line": 2,
18+
"args": [
19+
"1024"
20+
]
21+
}
22+
]
23+
},
24+
{
25+
"directive": "http",
26+
"line": 5,
27+
"args": [],
28+
"block": [
29+
{
30+
"directive": "geo",
31+
"line": 6,
32+
"args": [
33+
"$geo"
34+
],
35+
"block": [
36+
{
37+
"directive": "default",
38+
"line": 7,
39+
"args": [
40+
"0"
41+
]
42+
},
43+
{
44+
"directive": "192.168.1.0/24",
45+
"line": 8,
46+
"args": [
47+
"1"
48+
]
49+
},
50+
{
51+
"directive": "127.0.0.1",
52+
"line": 9,
53+
"args": [
54+
"2"
55+
]
56+
}
57+
]
58+
},
59+
{
60+
"directive": "server",
61+
"line": 11,
62+
"args": [],
63+
"block": [
64+
{
65+
"directive": "listen",
66+
"line": 12,
67+
"args": [
68+
"127.0.0.1:8080"
69+
]
70+
},
71+
{
72+
"directive": "server_name",
73+
"line": 13,
74+
"args": [
75+
"default_server"
76+
]
77+
},
78+
{
79+
"directive": "location",
80+
"line": 14,
81+
"args": [
82+
"/"
83+
],
84+
"block": [
85+
{
86+
"directive": "if",
87+
"line": 15,
88+
"args": [
89+
"$geo",
90+
"=",
91+
"2"
92+
],
93+
"block": [
94+
{
95+
"directive": "return",
96+
"line": 16,
97+
"args": [
98+
"403"
99+
]
100+
}
101+
]
102+
}
103+
]
104+
}
105+
]
106+
}
107+
]
108+
}
109+
]
110+
}
111+
]
112+
}

testdata/configs/geo/xp.json

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"status": "ok",
3+
"errors": [],
4+
"config": [
5+
{
6+
"file": "testdata/configs/geo/nginx.conf",
7+
"status": "ok",
8+
"errors": [],
9+
"parsed": [
10+
{
11+
"directive": "events",
12+
"line": 1,
13+
"args": [],
14+
"block": [
15+
{
16+
"directive": "worker_connections",
17+
"line": 2,
18+
"args": [
19+
"1024"
20+
]
21+
}
22+
]
23+
},
24+
{
25+
"directive": "http",
26+
"line": 5,
27+
"args": [],
28+
"block": [
29+
{
30+
"directive": "geo",
31+
"line": 6,
32+
"args": [
33+
"$geo"
34+
],
35+
"block": [
36+
{
37+
"directive": "default",
38+
"line": 7,
39+
"args": [
40+
"0"
41+
]
42+
},
43+
{
44+
"directive": "192.168.1.0/24",
45+
"line": 8,
46+
"args": [
47+
"1"
48+
]
49+
},
50+
{
51+
"directive": "127.0.0.1",
52+
"line": 9,
53+
"args": [
54+
"2"
55+
]
56+
}
57+
]
58+
},
59+
{
60+
"directive": "server",
61+
"line": 11,
62+
"args": [],
63+
"block": [
64+
{
65+
"directive": "listen",
66+
"line": 12,
67+
"args": [
68+
"127.0.0.1:8080"
69+
]
70+
},
71+
{
72+
"directive": "server_name",
73+
"line": 13,
74+
"args": [
75+
"default_server"
76+
]
77+
},
78+
{
79+
"directive": "location",
80+
"line": 14,
81+
"args": [
82+
"/"
83+
],
84+
"block": [
85+
{
86+
"directive": "if",
87+
"line": 15,
88+
"args": [
89+
"$geo",
90+
"=",
91+
"2"
92+
],
93+
"block": [
94+
{
95+
"directive": "return",
96+
"line": 16,
97+
"args": [
98+
"403"
99+
]
100+
}
101+
]
102+
}
103+
]
104+
}
105+
]
106+
}
107+
]
108+
}
109+
]
110+
}
111+
]
112+
}

0 commit comments

Comments
 (0)