Skip to content

Commit 564d3fe

Browse files
blink-so[bot]f0ssel
andcommitted
Fix HTTP forwarding by making it fully synchronous
- Remove async context timeout that was causing premature completion - Make HTTP request and response copying fully synchronous - Add response flushing to ensure data reaches client - Add detailed logging for bytes written and completion - Should resolve the race condition causing empty replies Co-authored-by: f0ssel <[email protected]>
1 parent 4c750e2 commit 564d3fe

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

proxy/proxy.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,7 @@ func (p *Server) forwardHTTPRequest(w http.ResponseWriter, r *http.Request) {
224224

225225
p.logger.Debug("About to make HTTP request", "target", targetURL.String())
226226

227-
// Make the request with context
228-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
229-
defer cancel()
230-
req = req.WithContext(ctx)
231-
227+
// Make the request synchronously - this is the key fix
232228
resp, err := client.Do(req)
233229
if err != nil {
234230
p.logger.Error("Failed to make forward request", "error", err, "target", targetURL.String(), "error_type", fmt.Sprintf("%T", err))
@@ -252,13 +248,20 @@ func (p *Server) forwardHTTPRequest(w http.ResponseWriter, r *http.Request) {
252248
// Copy status code
253249
w.WriteHeader(resp.StatusCode)
254250

255-
// Copy response body
256-
_, copyErr := io.Copy(w, resp.Body)
251+
// Copy response body - ensure this completes
252+
bytesWritten, copyErr := io.Copy(w, resp.Body)
257253
if copyErr != nil {
258-
p.logger.Debug("Error copying response body", "error", copyErr)
254+
p.logger.Error("Error copying response body", "error", copyErr, "bytes_written", bytesWritten)
255+
http.Error(w, "Failed to copy response", http.StatusBadGateway)
259256
} else {
260-
p.logger.Debug("Successfully forwarded HTTP response")
257+
p.logger.Debug("Successfully forwarded HTTP response", "bytes_written", bytesWritten, "status", resp.StatusCode)
258+
}
259+
260+
// Ensure response is flushed
261+
if flusher, ok := w.(http.Flusher); ok {
262+
flusher.Flush()
261263
}
264+
p.logger.Debug("forwardHTTPRequest completed")
262265
}
263266

264267
// forwardHTTPSRequest forwards an HTTPS request

0 commit comments

Comments
 (0)