flush.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package dpcore
  2. import (
  3. "mime"
  4. "net/http"
  5. "time"
  6. )
  7. // Auto sniff of flush interval from header
  8. func (p *ReverseProxy) getFlushInterval(req *http.Request, res *http.Response) time.Duration {
  9. contentType := req.Header.Get("Content-Type")
  10. if actualContentType, _, _ := mime.ParseMediaType(contentType); actualContentType == "text/event-stream" {
  11. return -1
  12. }
  13. if req.ContentLength == -1 || p.isBidirectionalStream(req, res) {
  14. return -1
  15. }
  16. //Cannot sniff anything. Use default value
  17. return p.FlushInterval
  18. }
  19. // Check for bidirectional stream, copy from Caddy :D
  20. func (p *ReverseProxy) isBidirectionalStream(req *http.Request, res *http.Response) bool {
  21. // We have to check the encoding here; only flush headers with identity encoding.
  22. // Non-identity encoding might combine with "encode" directive, and in that case,
  23. // if body size larger than enc.MinLength, upper level encode handle might have
  24. // Content-Encoding header to write.
  25. // (see https://github.com/caddyserver/caddy/issues/3606 for use case)
  26. ae := req.Header.Get("Accept-Encoding")
  27. return req.ProtoMajor == 2 &&
  28. res.ProtoMajor == 2 &&
  29. res.ContentLength == -1 &&
  30. (ae == "identity" || ae == "")
  31. }