Skip to content

Commit 8b28fc1

Browse files
authored
Merge pull request #89 from nginxinc/NLB-4932-header
NLB-4932: add support for headers-more
2 parents 5f46130 + 938eefb commit 8b28fc1

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

analyze.go

+26
Original file line numberDiff line numberDiff line change
@@ -2725,3 +2725,29 @@ func MatchLua(directive string) ([]uint, bool) {
27252725
masks, matched := LuaDirectives[directive]
27262726
return masks, matched
27272727
}
2728+
2729+
// nginx headers_more directives
2730+
// [https://github.com/openresty/headers-more-nginx-module]
2731+
//
2732+
//nolint:gochecknoglobals
2733+
var headersMoreDirectives = map[string][]uint{
2734+
"more_set_headers": {
2735+
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConf1More,
2736+
},
2737+
"more_clear_headers": {
2738+
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConf1More,
2739+
},
2740+
"more_set_input_headers": {
2741+
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConf1More,
2742+
},
2743+
"more_clear_input_headers": {
2744+
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConf1More,
2745+
},
2746+
}
2747+
2748+
// MatchHeadersMore is a match function for parsing an NGINX config that contains the
2749+
// Headers-More module.
2750+
func MatchHeadersMore(directive string) ([]uint, bool) {
2751+
masks, matched := headersMoreDirectives[directive]
2752+
return masks, matched
2753+
}

analyze_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -2269,3 +2269,86 @@ func TestAnalyze_mgmt(t *testing.T) {
22692269
})
22702270
}
22712271
}
2272+
2273+
//nolint:funlen
2274+
func TestAnalyze_headers_more(t *testing.T) {
2275+
t.Parallel()
2276+
testcases := map[string]struct {
2277+
stmt *Directive
2278+
ctx blockCtx
2279+
wantErr bool
2280+
}{
2281+
"more_set_headers ok": {
2282+
&Directive{
2283+
Directive: "more_set_headers",
2284+
Args: []string{"Server: my_server"},
2285+
Line: 5,
2286+
},
2287+
blockCtx{"http", "location", "location if"},
2288+
false,
2289+
},
2290+
"more_set_headers multiple args ok": {
2291+
&Directive{
2292+
Directive: "more_set_headers",
2293+
Args: []string{"-s", "404", "-s", "500 503", "Foo: bar"},
2294+
Line: 5,
2295+
},
2296+
blockCtx{"http", "location", "location if"},
2297+
false,
2298+
},
2299+
"more_set_headers not ok": {
2300+
&Directive{
2301+
Directive: "more_set_headers",
2302+
Args: []string{"Server: my_server"},
2303+
Line: 5,
2304+
},
2305+
blockCtx{"stream"},
2306+
true,
2307+
},
2308+
"more_clear_headers ok": {
2309+
&Directive{
2310+
Directive: "more_clear_headers",
2311+
Args: []string{"X-Hidden-*"},
2312+
Line: 5,
2313+
},
2314+
blockCtx{"http", "location", "location if"},
2315+
false,
2316+
},
2317+
"more_set_input_headers ok": {
2318+
&Directive{
2319+
Directive: "more_set_input_headers",
2320+
Args: []string{"Authorization: $http_authorization"},
2321+
Line: 5,
2322+
},
2323+
blockCtx{"http", "location", "location if"},
2324+
false,
2325+
},
2326+
"more_clear_input_headers ok": {
2327+
&Directive{
2328+
Directive: "more_set_input_headers",
2329+
Args: []string{"-t", "'text/plain'", "Foo: ", "Baz: "},
2330+
Line: 5,
2331+
},
2332+
blockCtx{"http", "location", "location if"},
2333+
false,
2334+
},
2335+
}
2336+
2337+
for name, tc := range testcases {
2338+
tc := tc
2339+
t.Run(name, func(t *testing.T) {
2340+
t.Parallel()
2341+
err := analyze("nginx.conf", tc.stmt, ";", tc.ctx, &ParseOptions{
2342+
MatchFuncs: []MatchFunc{MatchHeadersMore},
2343+
})
2344+
2345+
if !tc.wantErr && err != nil {
2346+
t.Fatal(err)
2347+
}
2348+
2349+
if tc.wantErr && err == nil {
2350+
t.Fatal("expected error, got nil")
2351+
}
2352+
})
2353+
}
2354+
}

0 commit comments

Comments
 (0)