Skip to content

Commit

Permalink
fix the problem that HTTP steam request cannot be flushed (#523)
Browse files Browse the repository at this point in the history
* Add check for charset in content-type header in ServeHTTP function

* Add support for chunked transfer encoding in ServeHTTP function

* Fix content-type and transfer-encoding checks in ServeHTTP function

---------

Co-authored-by: taco.wang <taco.wang@anker-in.com>
Co-authored-by: Erik Pellizzon <erikpelli@tutamail.com>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent 5d8f603 commit da3cdee
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"regexp"
"strings"
"sync/atomic"
)

Expand Down Expand Up @@ -193,7 +194,10 @@ func (proxy *ProxyHttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
copyHeaders(w.Header(), resp.Header, proxy.KeepDestinationHeaders)
w.WriteHeader(resp.StatusCode)
var copyWriter io.Writer = w
if w.Header().Get("content-type") == "text/event-stream" {
// Content-Type header may also contain charset definition, so here we need to check the prefix.
// Transfer-Encoding can be a list of comma separated values, so we use Contains() for it.
if strings.HasPrefix(w.Header().Get("content-type"), "text/event-stream") ||
strings.Contains(w.Header().Get("transfer-encoding"), "chunked") {
// server-side events, flush the buffered data to the client.
copyWriter = &flushWriter{w: w}
}
Expand Down

0 comments on commit da3cdee

Please sign in to comment.