🔧 Enhance streaming response handling with headers and flushing support
This commit is contained in:
@@ -179,6 +179,19 @@ func isStreaming(requestId string, resp *http.Response) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleStream(requestId string, w io.Writer, body io.Reader) {
|
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)
|
reader := bufio.NewReader(body)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -193,13 +206,25 @@ func handleStream(requestId string, w io.Writer, body io.Reader) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
line = strings.TrimRight(line, "\r\n")
|
// Write the line as-is without trimming
|
||||||
if line == "" {
|
_, writeErr := rw.Write([]byte(line))
|
||||||
continue
|
if writeErr != nil {
|
||||||
|
log.Println(requestId, "Write error:", writeErr)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(requestId, "Process chunk:", line)
|
if len(line) == 1 && line[len(line)-1] == 10 {
|
||||||
w.Write([]byte(line + "\r\n"))
|
// 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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user