Skip to content

Commit 9b080c3

Browse files
authored
Merge pull request #66 from nginxinc/main
fix: update parsing logic to handle variables with braces
2 parents f19f054 + f6c1c2f commit 9b080c3

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

lex.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,11 @@ func tokenize(reader io.Reader, tokenCh chan NgxToken) {
198198
token.WriteString(la)
199199

200200
case inVar:
201+
token.WriteString(la)
201202
// this is using the same logic as the exiting lexer, but this is wrong since it does not terminate on token boundary
202203
if !strings.HasSuffix(token.String(), "}") && !isSpace(la) {
203-
token.WriteString(la)
204204
continue
205205
}
206-
token.WriteString(la)
207206
lexState = inWord
208207

209208
case inQuote:

lex_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ var lexFixtures = []lexFixture{
160160
{"/status.html", 18},
161161
{"{", 18},
162162
{"try_files", 19},
163-
{"/abc/${uri} /abc/${uri}.html", 19},
163+
{"/abc/${uri}", 19},
164+
{"/abc/${uri}.html", 19},
164165
{"=404", 19},
165166
{";", 19},
166167
{"}", 20},

parse_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,70 @@ var parseFixtures = []parseFixture{
491491
},
492492
},
493493
}},
494+
{"simple-variable-with-braces", "-ignore-directives-1", ParseOptions{IgnoreDirectives: []string{"listen", "server_name"}}, Payload{
495+
Status: "ok",
496+
Config: []Config{
497+
{
498+
File: getTestConfigPath("simple-variable-with-braces", "nginx.conf"),
499+
Status: "ok",
500+
Parsed: Directives{
501+
{
502+
Directive: "events",
503+
Args: []string{},
504+
Line: 1,
505+
Block: Directives{
506+
{
507+
Directive: "worker_connections",
508+
Args: []string{"1024"},
509+
Line: 2,
510+
},
511+
},
512+
},
513+
{
514+
Directive: "http",
515+
Args: []string{},
516+
Line: 5,
517+
Block: Directives{
518+
{
519+
Directive: "server",
520+
Args: []string{},
521+
Line: 6,
522+
Block: Directives{
523+
{
524+
Directive: "location",
525+
Args: []string{"/proxy"},
526+
Line: 9,
527+
Block: Directives{
528+
{
529+
Directive: "set",
530+
Args: []string{"$backend_protocol", "http"},
531+
Line: 10,
532+
},
533+
{
534+
Directive: "set",
535+
Args: []string{"$backend_host", "bar"},
536+
Line: 11,
537+
},
538+
{
539+
Directive: "set",
540+
Args: []string{"$foo", ""},
541+
Line: 12,
542+
},
543+
{
544+
Directive: "proxy_pass",
545+
Args: []string{"$backend_protocol://$backend_host${foo}"},
546+
Line: 13,
547+
},
548+
},
549+
},
550+
},
551+
},
552+
},
553+
},
554+
},
555+
},
556+
},
557+
}},
494558
{"with-comments", "-true", ParseOptions{ParseComments: true}, Payload{
495559
Status: "ok",
496560
Config: []Config{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
events {
2+
worker_connections 1024;
3+
}
4+
5+
http {
6+
server {
7+
listen 127.0.0.1:8080;
8+
server_name default_server;
9+
location /proxy {
10+
set $backend_protocol http;
11+
set $backend_host bar;
12+
set $foo '';
13+
proxy_pass $backend_protocol://$backend_host${foo};
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)