From 042e5e8033813623731144ddc7ede07f68c01de8 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 12 Apr 2017 00:21:46 +0200 Subject: [PATCH] multiple fixes for http proxy --- proxy/http/server.go | 17 ++++++++++++----- proxy/http/server_test.go | 8 ++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/proxy/http/server.go b/proxy/http/server.go index cf24c4519..285ca9369 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -68,15 +68,21 @@ func parseHost(rawHost string, defaultPort v2net.Port) (v2net.Destination, error return v2net.TCPDestination(v2net.DomainAddress(host), port), nil } +func isTimeout(err error) bool { + nerr, ok := err.(net.Error) + return ok && nerr.Timeout() +} + func (s *Server) Process(ctx context.Context, network v2net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error { + reader := bufio.NewReaderSize(conn, 8192) + Start: - conn.SetReadDeadline(time.Now().Add(time.Second * 8)) - reader := bufio.NewReaderSize(conn, 2048) + conn.SetReadDeadline(time.Now().Add(time.Second * 16)) request, err := http.ReadRequest(reader) if err != nil { trace := newError("failed to read http request").Base(err) - if errors.Cause(err) != io.EOF { + if errors.Cause(err) != io.EOF && !isTimeout(errors.Cause(err)) { trace.AtWarning() } return trace @@ -186,13 +192,13 @@ func StripHopByHopHeaders(header http.Header) { header.Del("Upgrade") connections := header.Get("Connection") + header.Del("Connection") if len(connections) == 0 { return } for _, h := range strings.Split(connections, ",") { header.Del(strings.TrimSpace(h)) } - header.Del("Connection") } var errWaitAnother = newError("keep alive") @@ -246,8 +252,9 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, rea if err == nil { StripHopByHopHeaders(response.Header) response.Header.Set("Proxy-Connection", "keep-alive") - response.Header.Set("Connection", "Keep-Alive") + response.Header.Set("Connection", "keep-alive") response.Header.Set("Keep-Alive", "timeout=4") + response.Close = false } else { log.Trace(newError("failed to read response").Base(err).AtWarning()) response = &http.Response{ diff --git a/proxy/http/server_test.go b/proxy/http/server_test.go index de07658ab..2bc370a2e 100644 --- a/proxy/http/server_test.go +++ b/proxy/http/server_test.go @@ -40,8 +40,8 @@ Accept-Language: de,en;q=0.7,en-us;q=0.3 StripHopByHopHeaders(req.Header) assert.String(req.Header.Get("Connection")).IsEmpty() - assert.String(req.Header.Get("Foo")).Equals("") - assert.String(req.Header.Get("Bar")).Equals("") - assert.String(req.Header.Get("Proxy-Connection")).Equals("") - assert.String(req.Header.Get("Proxy-Authenticate")).Equals("") + assert.String(req.Header.Get("Foo")).IsEmpty() + assert.String(req.Header.Get("Bar")).IsEmpty() + assert.String(req.Header.Get("Proxy-Connection")).IsEmpty() + assert.String(req.Header.Get("Proxy-Authenticate")).IsEmpty() }