Skip to content

Commit 941b18f

Browse files
committed
fsthttp: support sending StatusEarlyHints
1 parent a8c5c4a commit 941b18f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

fsthttp/response.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ func (resp *responseWriter) Header() Header {
276276
return resp.header
277277
}
278278

279+
var excludeHeadersNoBody = map[string]bool{CanonicalHeaderKey("Content-Length"): true, CanonicalHeaderKey("Transfer-Encoding"): true}
280+
279281
func (resp *responseWriter) WriteHeader(code int) {
280282
if resp.wroteHeaders {
281283
println("fsthttp: multiple calls to WriteHeader")
@@ -284,11 +286,26 @@ func (resp *responseWriter) WriteHeader(code int) {
284286

285287
resp.abiResp.SetFramingHeadersMode(resp.ManualFramingMode)
286288
resp.abiResp.SetStatusCode(code)
289+
290+
var skip map[string]bool
291+
if code == StatusEarlyHints {
292+
skip = excludeHeadersNoBody
293+
}
294+
287295
for _, key := range resp.header.Keys() {
296+
// don't send body headers if we're sending early hints
297+
if skip[key] {
298+
continue
299+
}
288300
resp.abiResp.SetHeaderValues(key, resp.header.Values(key))
289301
}
290302
resp.abiResp.SendDownstream(resp.abiBody, true)
291303

304+
if code == StatusEarlyHints {
305+
// For early hints, don't mark the headers as "sent" so we can send them again next time.
306+
return
307+
}
308+
292309
resp.wroteHeaders = true
293310
}
294311

@@ -315,6 +332,9 @@ func (resp *responseWriter) SetManualFramingMode(mode bool) {
315332
}
316333

317334
func (resp *responseWriter) Append(other io.ReadCloser) error {
335+
if !resp.wroteHeaders {
336+
resp.WriteHeader(200)
337+
}
318338
otherAbiBody, ok := other.(*fastly.HTTPBody)
319339
if !ok {
320340
return fmt.Errorf("non-Response Body passed to ResponseWriter.Append")

0 commit comments

Comments
 (0)