Skip to content

Commit

Permalink
reverse proxy: Support more h2 stream scenarios (caddyserver#3556)
Browse files Browse the repository at this point in the history
  • Loading branch information
masknu committed Jul 8, 2020
1 parent 0bf2565 commit ba62ee2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions modules/caddyhttp/reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,13 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, di Dia

rw.WriteHeader(res.StatusCode)

// for some apps which need a correct response header to start http2 streaming,
// it's important to explicit flush headers to the client before copy streaming data.
if req.ProtoMajor == 2 && res.ContentLength == -1 {
if wf, ok := rw.(http.Flusher); ok {
wf.Flush()
}
}
err = h.copyResponse(rw, res.Body, h.flushInterval(req, res))
res.Body.Close() // close now, instead of defer, to populate res.Trailer
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions modules/caddyhttp/reverseproxy/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func (h Handler) flushInterval(req *http.Request, res *http.Response) time.Durat
return -1 // negative means immediately
}

// for h2 and h2c upstream streaming data to client
if req.ProtoMajor == 2 && res.ContentLength == -1 {
return -1
}

// TODO: more specific cases? e.g. res.ContentLength == -1? (this TODO is from the std lib)
return time.Duration(h.FlushInterval)
}
Expand Down

0 comments on commit ba62ee2

Please sign in to comment.