File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed
caddytest/integration/caddyfile_adapt Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 12
12
@images path /images/*
13
13
header @images {
14
14
Cache-Control "public, max-age=3600, stale-while-revalidate=86400"
15
+ match {
16
+ status 200
17
+ }
15
18
}
16
19
header {
17
20
+Link "Foo"
18
21
+Link "Bar"
22
+ match status 200
19
23
}
20
24
header >Set Defer
21
25
header >Replace Deferred Replacement
42
46
{
43
47
"handler": "headers",
44
48
"response": {
49
+ "require": {
50
+ "status_code": [
51
+ 200
52
+ ]
53
+ },
45
54
"set": {
46
55
"Cache-Control": [
47
56
"public, max-age=3600, stale-while-revalidate=86400"
136
145
"Foo",
137
146
"Bar"
138
147
]
148
+ },
149
+ "require": {
150
+ "status_code": [
151
+ 200
152
+ ]
139
153
}
140
154
}
141
155
},
Original file line number Diff line number Diff line change @@ -99,6 +99,16 @@ func parseCaddyfile(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
99
99
handler .Response .Deferred = true
100
100
continue
101
101
}
102
+ if field == "match" {
103
+ responseMatchers := make (map [string ]caddyhttp.ResponseMatcher )
104
+ err := caddyhttp .ParseNamedResponseMatcher (h .NewFromNextSegment (), responseMatchers )
105
+ if err != nil {
106
+ return nil , err
107
+ }
108
+ matcher := responseMatchers ["match" ]
109
+ handler .Response .Require = & matcher
110
+ continue
111
+ }
102
112
if hasArgs {
103
113
return nil , h .Err ("cannot specify headers in both arguments and block" ) // because it would be weird
104
114
}
Original file line number Diff line number Diff line change @@ -143,6 +143,28 @@ func TestHandler(t *testing.T) {
143
143
"Cache-Control" : []string {"no-cache" },
144
144
},
145
145
},
146
+ { // same as above, but checks that response headers are left alone when "Require" conditions are unmet
147
+ handler : Handler {
148
+ Response : & RespHeaderOps {
149
+ Require : & caddyhttp.ResponseMatcher {
150
+ Headers : http.Header {
151
+ "Cache-Control" : nil ,
152
+ },
153
+ },
154
+ HeaderOps : & HeaderOps {
155
+ Add : http.Header {
156
+ "Cache-Control" : []string {"no-cache" },
157
+ },
158
+ },
159
+ },
160
+ },
161
+ respHeader : http.Header {
162
+ "Cache-Control" : []string {"something" },
163
+ },
164
+ expectedRespHeader : http.Header {
165
+ "Cache-Control" : []string {"something" },
166
+ },
167
+ },
146
168
{
147
169
handler : Handler {
148
170
Response : & RespHeaderOps {
You can’t perform that action at this time.
0 commit comments