Skip to content

Commit 409e129

Browse files
committed
fsthttp: support sending StatusEarlyHints
1 parent 2befc18 commit 409e129

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
@@ -274,6 +274,8 @@ func (resp *responseWriter) Header() Header {
274274
return resp.header
275275
}
276276

277+
var excludeHeadersNoBody = map[string]bool{"Content-Length": true, "Transfer-Encoding": true}
278+
277279
func (resp *responseWriter) WriteHeader(code int) {
278280
if resp.wroteHeaders {
279281
println("fsthttp: multiple calls to WriteHeader")
@@ -282,11 +284,26 @@ func (resp *responseWriter) WriteHeader(code int) {
282284

283285
resp.abiResp.SetFramingHeadersMode(resp.ManualFramingMode)
284286
resp.abiResp.SetStatusCode(code)
287+
288+
var skip map[string]bool
289+
if code == StatusEarlyHints {
290+
skip = excludeHeadersNoBody
291+
}
292+
285293
for _, key := range resp.header.Keys() {
294+
// don't send body headers if we're sending early hints
295+
if skip[key] {
296+
continue
297+
}
286298
resp.abiResp.SetHeaderValues(key, resp.header.Values(key))
287299
}
288300
resp.abiResp.SendDownstream(resp.abiBody, true)
289301

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

@@ -309,6 +326,9 @@ func (resp *responseWriter) SetManualFramingMode(mode bool) {
309326
}
310327

311328
func (resp *responseWriter) Append(other io.ReadCloser) error {
329+
if !resp.wroteHeaders {
330+
resp.WriteHeader(200)
331+
}
312332
otherAbiBody, ok := other.(*fastly.HTTPBody)
313333
if !ok {
314334
return fmt.Errorf("non-Response Body passed to ResponseWriter.Append")

0 commit comments

Comments
 (0)