From 693dda7eabbf277533985c1547d4325281db8a1d Mon Sep 17 00:00:00 2001 From: Mark Ellzey Date: Sat, 18 Mar 2023 13:44:17 -0400 Subject: [PATCH] Deal with non-RFC compliant servers There are hundreds of thousands of servers on the internet where the "INTENT" is to be an HTTP service, but in reality they do not properly conform to the HTTP RFC. In many cases, these servers will send back a simple: HTTP/1.1 404 Not Found With no `Connection: close` header, and terminates the established connection. While it's understood these are not RFC compliant, I would still like to see the servers that are intending to be HTTP servers. --- lib/http/client.go | 8 +++++--- lib/http/transport.go | 5 ++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/http/client.go b/lib/http/client.go index 029b797f..b21f346b 100644 --- a/lib/http/client.go +++ b/lib/http/client.go @@ -831,9 +831,11 @@ func (b *cancelTimerBody) Read(p []byte) (n int, err error) { } func (b *cancelTimerBody) Close() error { - err := b.rc.Close() - b.stop() - return err + defer b.stop() + if b.rc != nil { + return b.rc.Close() + } + return nil } func shouldCopyHeaderOnRedirect(headerKey string, initial, dest *url.URL) bool { diff --git a/lib/http/transport.go b/lib/http/transport.go index 019f86dd..c34d5679 100644 --- a/lib/http/transport.go +++ b/lib/http/transport.go @@ -1503,7 +1503,7 @@ func (pc *persistConn) readLoop() { closeErr = err } - if err != nil { + if err != nil && (!pc.sawEOF || resp == nil) { if pc.readLimit <= 0 { err = fmt.Errorf("net/http: server response headers exceeded %d bytes; aborted", pc.maxHeaderResponseSize()) } @@ -1657,6 +1657,9 @@ func (pc *persistConn) readResponse(rc requestAndChan, trace *httptrace.ClientTr } resp, err = ReadResponse(pc.br, rc.req) if err != nil { + if err == io.ErrUnexpectedEOF { + pc.sawEOF = true + } return } if rc.continueCh != nil {