summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/reverseproxy/streaming.go
diff options
context:
space:
mode:
authorKevin Lin <masknu@users.noreply.github.com>2020-08-04 10:50:38 +0800
committerGitHub <noreply@github.com>2020-08-03 20:50:38 -0600
commit904f149e5b2f0ae9eff09dacb4f1ec41c4a76298 (patch)
tree52b831c3c70da560c62b5bda9132b11beee4c989 /modules/caddyhttp/reverseproxy/streaming.go
parent8b80a3201fcddda3fcf125116d33555cb385a803 (diff)
reverse_proxy: fix bidirectional streams with encodings (fix #3606) (#3620)
* reverse_proxy: fix bi-h2stream breaking gzip encode handle(#3606). * reverse_proxy: check http version of both sides to avoid affecting non-h2 upstream. * Minor cleanup; apply review suggestions Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
Diffstat (limited to 'modules/caddyhttp/reverseproxy/streaming.go')
-rw-r--r--modules/caddyhttp/reverseproxy/streaming.go24
1 files changed, 21 insertions, 3 deletions
diff --git a/modules/caddyhttp/reverseproxy/streaming.go b/modules/caddyhttp/reverseproxy/streaming.go
index 105ff32..127c0f0 100644
--- a/modules/caddyhttp/reverseproxy/streaming.go
+++ b/modules/caddyhttp/reverseproxy/streaming.go
@@ -96,15 +96,33 @@ 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 (issue #3556)
- if req.ProtoMajor == 2 && res.ContentLength == -1 {
+ // for h2 and h2c upstream streaming data to client (issues #3556 and #3606)
+ if h.isBidirectionalStream(req, res) {
return -1
}
- // TODO: more specific cases? e.g. res.ContentLength == -1? (this TODO is from the std lib)
+ // TODO: more specific cases? e.g. res.ContentLength == -1? (this TODO is from the std lib, but
+ // strangely similar to our isBidirectionalStream function that we implemented ourselves)
return time.Duration(h.FlushInterval)
}
+// isBidirectionalStream returns whether we should work in bi-directional stream mode.
+//
+// See https://github.com/caddyserver/caddy/pull/3620 for discussion of nuances.
+func (h Handler) isBidirectionalStream(req *http.Request, res *http.Response) bool {
+ // We have to check the encoding here; only flush headers with identity encoding.
+ // Non-identity encoding might combine with "encode" directive, and in that case,
+ // if body size larger than enc.MinLength, upper level encode handle might have
+ // Content-Encoding header to write.
+ // (see https://github.com/caddyserver/caddy/issues/3606 for use case)
+ ae := req.Header.Get("Accept-Encoding")
+
+ return req.ProtoMajor == 2 &&
+ res.ProtoMajor == 2 &&
+ res.ContentLength == -1 &&
+ (ae == "identity" || ae == "")
+}
+
func (h Handler) copyResponse(dst io.Writer, src io.Reader, flushInterval time.Duration) error {
if flushInterval != 0 {
if wf, ok := dst.(writeFlusher); ok {