mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-13 22:51:08 -05:00
header: match
subdirective for response matching (#6765)
This commit is contained in:
parent
1f927d6b07
commit
e48b75843b
3 changed files with 46 additions and 0 deletions
|
@ -12,10 +12,14 @@
|
|||
@images path /images/*
|
||||
header @images {
|
||||
Cache-Control "public, max-age=3600, stale-while-revalidate=86400"
|
||||
match {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
header {
|
||||
+Link "Foo"
|
||||
+Link "Bar"
|
||||
match status 200
|
||||
}
|
||||
header >Set Defer
|
||||
header >Replace Deferred Replacement
|
||||
|
@ -42,6 +46,11 @@
|
|||
{
|
||||
"handler": "headers",
|
||||
"response": {
|
||||
"require": {
|
||||
"status_code": [
|
||||
200
|
||||
]
|
||||
},
|
||||
"set": {
|
||||
"Cache-Control": [
|
||||
"public, max-age=3600, stale-while-revalidate=86400"
|
||||
|
@ -136,6 +145,11 @@
|
|||
"Foo",
|
||||
"Bar"
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"status_code": [
|
||||
200
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -99,6 +99,16 @@ func parseCaddyfile(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
|
|||
handler.Response.Deferred = true
|
||||
continue
|
||||
}
|
||||
if field == "match" {
|
||||
responseMatchers := make(map[string]caddyhttp.ResponseMatcher)
|
||||
err := caddyhttp.ParseNamedResponseMatcher(h.NewFromNextSegment(), responseMatchers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
matcher := responseMatchers["match"]
|
||||
handler.Response.Require = &matcher
|
||||
continue
|
||||
}
|
||||
if hasArgs {
|
||||
return nil, h.Err("cannot specify headers in both arguments and block") // because it would be weird
|
||||
}
|
||||
|
|
|
@ -143,6 +143,28 @@ func TestHandler(t *testing.T) {
|
|||
"Cache-Control": []string{"no-cache"},
|
||||
},
|
||||
},
|
||||
{ // same as above, but checks that response headers are left alone when "Require" conditions are unmet
|
||||
handler: Handler{
|
||||
Response: &RespHeaderOps{
|
||||
Require: &caddyhttp.ResponseMatcher{
|
||||
Headers: http.Header{
|
||||
"Cache-Control": nil,
|
||||
},
|
||||
},
|
||||
HeaderOps: &HeaderOps{
|
||||
Add: http.Header{
|
||||
"Cache-Control": []string{"no-cache"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
respHeader: http.Header{
|
||||
"Cache-Control": []string{"something"},
|
||||
},
|
||||
expectedRespHeader: http.Header{
|
||||
"Cache-Control": []string{"something"},
|
||||
},
|
||||
},
|
||||
{
|
||||
handler: Handler{
|
||||
Response: &RespHeaderOps{
|
||||
|
|
Loading…
Add table
Reference in a new issue