diff --git a/main.go b/main.go index 7ceba3e..c50abc0 100644 --- a/main.go +++ b/main.go @@ -179,6 +179,19 @@ func isStreaming(requestId string, resp *http.Response) bool { } func handleStream(requestId string, w io.Writer, body io.Reader) { + // Cast to http.ResponseWriter to access Header and Flush methods + rw, ok := w.(http.ResponseWriter) + if !ok { + log.Println(requestId, "Error: ResponseWriter is not an http.ResponseWriter") + return + } + + // // Set headers for streaming + // rw.Header().Set("Content-Type", "text/event-stream") + // rw.Header().Set("Cache-Control", "no-cache") + // rw.Header().Set("Connection", "keep-alive") + // rw.Header().Set("Transfer-Encoding", "chunked") + reader := bufio.NewReader(body) for { @@ -193,13 +206,25 @@ func handleStream(requestId string, w io.Writer, body io.Reader) { break } - line = strings.TrimRight(line, "\r\n") - if line == "" { - continue + // Write the line as-is without trimming + _, writeErr := rw.Write([]byte(line)) + if writeErr != nil { + log.Println(requestId, "Write error:", writeErr) + break } - log.Println(requestId, "Process chunk:", line) - w.Write([]byte(line + "\r\n")) + if len(line) == 1 && line[len(line)-1] == 10 { + // SKIP empty line + } else { + log.Println(requestId, "Process chunk:", fmt.Sprintf("%d bytes", len(line)), strings.TrimSpace(line)) + } + + // Flush the response to ensure the chunk is sent immediately + if flusher, ok := rw.(http.Flusher); ok { + flusher.Flush() + } else { + log.Println(requestId, "Warning: ResponseWriter does not support flushing") + } } }