Skip to content

Commit af4d657

Browse files
authored
Fix http request handling (#642)
* Avoid to call filterResponse twice for the same request * Let the client close the connection when it specifies the Connection: close header * Remove explicit set of Close to false, since it's unnecessary
1 parent d49d216 commit af4d657

3 files changed

Lines changed: 2 additions & 31 deletions

File tree

http.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
func (proxy *ProxyHttpServer) handleHttp(w http.ResponseWriter, r *http.Request) {
1111
ctx := &ProxyCtx{Req: r, Session: atomic.AddInt64(&proxy.sess, 1), Proxy: proxy}
1212

13-
var err error
1413
ctx.Logf("Got request %v %v %v %v", r.URL.Path, r.Host, r.Method, r.URL.String())
1514
if !r.URL.IsAbs() {
1615
proxy.NonproxyHandler.ServeHTTP(w, r)
@@ -25,17 +24,14 @@ func (proxy *ProxyHttpServer) handleHttp(w http.ResponseWriter, r *http.Request)
2524
proxy.serveWebsocket(ctx, conn, r)
2625
}
2726
}
28-
2927
if !proxy.KeepHeader {
3028
RemoveProxyHeaders(ctx, r)
3129
}
30+
31+
var err error
3232
resp, err = ctx.RoundTrip(r)
3333
if err != nil {
3434
ctx.Error = err
35-
resp = proxy.filterResponse(nil, ctx)
36-
}
37-
if resp != nil {
38-
ctx.Logf("Received response %v", resp.Status)
3935
}
4036
}
4137

https.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,6 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
210210
return
211211
}
212212

213-
// Take the original value before filtering the request
214-
closeConn := req.Close
215-
216213
if requestOk := func(req *http.Request) bool {
217214
// Since we handled the request parsing by our own, we manually
218215
// need to set a cancellable context when we finished the request
@@ -265,11 +262,6 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
265262
}(req); !requestOk {
266263
break
267264
}
268-
269-
if closeConn {
270-
ctx.Logf("Non-persistent connection; closing")
271-
break
272-
}
273265
}
274266
case ConnectMitm:
275267
_, _ = proxyClient.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
@@ -322,9 +314,6 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
322314
req.URL, err = url.Parse("https://" + r.Host + req.URL.String())
323315
}
324316

325-
// Take the original value before filtering the request
326-
closeConn := req.Close
327-
328317
if continueLoop := func(req *http.Request) bool {
329318
// Since we handled the request parsing by our own, we manually
330319
// need to set a cancellable context when we finished the request
@@ -459,11 +448,6 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
459448
}(req); !continueLoop {
460449
return
461450
}
462-
463-
if closeConn {
464-
ctx.Logf("Non-persistent connection; closing")
465-
return
466-
}
467451
}
468452
ctx.Logf("Exiting on EOF")
469453
}()

proxy.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,6 @@ func RemoveProxyHeaders(ctx *ProxyCtx, r *http.Request) {
110110
// options that are desired for that particular connection and MUST NOT
111111
// be communicated by proxies over further connections.
112112

113-
// When server reads http request it sets req.Close to true if
114-
// "Connection" header contains "close".
115-
// https://github.com/golang/go/blob/master/src/net/http/request.go#L1080
116-
// Later, transfer.go adds "Connection: close" back when req.Close is true
117-
// https://github.com/golang/go/blob/master/src/net/http/transfer.go#L275
118-
// That's why tests that checks "Connection: close" removal fail
119-
if r.Header.Get("Connection") == "close" {
120-
r.Close = false
121-
}
122113
r.Header.Del("Connection")
123114
}
124115

0 commit comments

Comments
 (0)