Skip to content

Commit 17d3be0

Browse files
blink-so[bot]f0ssel
andcommitted
Fix HTTP request forwarding issues
- Create new URL object instead of mutating original request URL - Add shorter 5-second timeout to prevent hanging - Skip connection-specific headers that cause issues - Use request context for proper cancellation handling - Filter problematic response headers (connection, transfer-encoding) - Should resolve 'Empty reply from server' error Co-authored-by: f0ssel <[email protected]>
1 parent dcb9290 commit 17d3be0

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

proxy/proxy.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,18 @@ func (p *Server) forwardHTTPRequest(w http.ResponseWriter, r *http.Request) {
186186
p.logger.Debug("forwardHTTPRequest called", "method", r.Method, "url", r.URL.String(), "host", r.Host)
187187

188188
// Create a new request to the target server
189-
targetURL := r.URL
190-
if targetURL.Scheme == "" {
191-
targetURL.Scheme = "http"
192-
}
193-
if targetURL.Host == "" {
194-
targetURL.Host = r.Host
189+
targetURL := &url.URL{
190+
Scheme: "http",
191+
Host: r.Host,
192+
Path: r.URL.Path,
193+
RawQuery: r.URL.RawQuery,
195194
}
196195

197196
p.logger.Debug("Target URL constructed", "target", targetURL.String())
198197

199-
// Create HTTP client
198+
// Create HTTP client with shorter timeout
200199
client := &http.Client{
201-
Timeout: 10 * time.Second, // Add timeout to avoid hanging
200+
Timeout: 5 * time.Second,
202201
CheckRedirect: func(req *http.Request, via []*http.Request) error {
203202
return http.ErrUseLastResponse // Don't follow redirects
204203
},
@@ -214,15 +213,19 @@ func (p *Server) forwardHTTPRequest(w http.ResponseWriter, r *http.Request) {
214213

215214
// Copy headers
216215
for name, values := range r.Header {
216+
// Skip connection-specific headers
217+
if strings.ToLower(name) == "connection" || strings.ToLower(name) == "proxy-connection" {
218+
continue
219+
}
217220
for _, value := range values {
218221
req.Header.Add(name, value)
219222
}
220223
}
221224

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

224-
// Make the request
225-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
227+
// Make the request with context
228+
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
226229
defer cancel()
227230
req = req.WithContext(ctx)
228231

@@ -236,8 +239,11 @@ func (p *Server) forwardHTTPRequest(w http.ResponseWriter, r *http.Request) {
236239

237240
p.logger.Debug("Received response", "status", resp.StatusCode, "target", targetURL.String())
238241

239-
// Copy response headers
242+
// Copy response headers (except connection-specific ones)
240243
for name, values := range resp.Header {
244+
if strings.ToLower(name) == "connection" || strings.ToLower(name) == "transfer-encoding" {
245+
continue
246+
}
241247
for _, value := range values {
242248
w.Header().Add(name, value)
243249
}

0 commit comments

Comments
 (0)