Skip to content

Commit

Permalink
Merge pull request #386 from zmap/ecubit/http-fix
Browse files Browse the repository at this point in the history
http: ignore EOF when reading headers
  • Loading branch information
elliotcubit authored Aug 28, 2023
2 parents 92d29ac + 7e1db3b commit 5988ada
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions lib/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ func readResponse(tc *TeeConn, req *Request) (*Response, error) {
// Parse the response headers.
mimeHeader, err := tp.ReadMIMEHeader()
if err != nil {
// Ignore EOF, so long as we got a valid status line
if err == io.EOF {
err = io.ErrUnexpectedEOF
return resp, nil
}
return resp, err
}
Expand Down
2 changes: 1 addition & 1 deletion lib/http/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ func TestReadResponseErrors(t *testing.T) {

tests := []testCase{
{"", "", nil, io.ErrUnexpectedEOF},
{"", "HTTP/1.1 301 Moved Permanently\r\nFoo: bar", nil, io.ErrUnexpectedEOF},
{"", "HTTP/1.1 404 Not Found", nil, nil},
{"", "HTTP/1.1", nil, "malformed HTTP response"},
{"", "HTTP/2.0", nil, "malformed HTTP response"},
status("20X Unknown", true),
Expand Down

0 comments on commit 5988ada

Please sign in to comment.