-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Read response when client closes connection #1232 * Fix edge case were client responds with invalid header * Follow linter suggestions for tests * Changes after review * Reafactor error check after review * Handle connection reset on windows * Remove format string from test where not needed * Run connection reset tests not on Windows
- Loading branch information
Showing
6 changed files
with
177 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package fasthttp | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// See issue #1232 | ||
func TestRstConnResponseWhileSending(t *testing.T) { | ||
const expectedStatus = http.StatusTeapot | ||
const payload = "payload" | ||
|
||
srv, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer srv.Close() | ||
|
||
go func() { | ||
for { | ||
conn, err := srv.Accept() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Read at least one byte of the header | ||
// Otherwise we would have an unsolicited response | ||
_, err = ioutil.ReadAll(io.LimitReader(conn, 1)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Respond | ||
_, err = conn.Write([]byte("HTTP/1.1 418 Teapot\r\n\r\n")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Forcefully close connection | ||
err = conn.(*net.TCPConn).SetLinger(0) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
conn.Close() | ||
} | ||
}() | ||
|
||
svrUrl := "http://" + srv.Addr().String() | ||
client := HostClient{Addr: srv.Addr().String()} | ||
|
||
for i := 0; i < 100; i++ { | ||
req := AcquireRequest() | ||
defer ReleaseRequest(req) | ||
resp := AcquireResponse() | ||
defer ReleaseResponse(resp) | ||
|
||
req.Header.SetMethod("POST") | ||
req.SetBodyStream(strings.NewReader(payload), len(payload)) | ||
req.SetRequestURI(svrUrl) | ||
|
||
err = client.Do(req, resp) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if expectedStatus != resp.StatusCode() { | ||
t.Fatalf("Expected %d status code, but got %d", expectedStatus, resp.StatusCode()) | ||
} | ||
} | ||
} | ||
|
||
// See issue #1232 | ||
func TestRstConnClosedWithoutResponse(t *testing.T) { | ||
const payload = "payload" | ||
|
||
srv, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer srv.Close() | ||
|
||
go func() { | ||
for { | ||
conn, err := srv.Accept() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Read at least one byte of the header | ||
// Otherwise we would have an unsolicited response | ||
_, err = ioutil.ReadAll(io.LimitReader(conn, 1)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Respond with incomplete header | ||
_, err = conn.Write([]byte("Http")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Forcefully close connection | ||
err = conn.(*net.TCPConn).SetLinger(0) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
conn.Close() | ||
} | ||
}() | ||
|
||
svrUrl := "http://" + srv.Addr().String() | ||
client := HostClient{Addr: srv.Addr().String()} | ||
|
||
for i := 0; i < 100; i++ { | ||
req := AcquireRequest() | ||
defer ReleaseRequest(req) | ||
resp := AcquireResponse() | ||
defer ReleaseResponse(resp) | ||
|
||
req.Header.SetMethod("POST") | ||
req.SetBodyStream(strings.NewReader(payload), len(payload)) | ||
req.SetRequestURI(svrUrl) | ||
|
||
err = client.Do(req, resp) | ||
|
||
if !isConnectionReset(err) { | ||
t.Fatal("Expected connection reset error") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package fasthttp | ||
|
||
import ( | ||
"errors" | ||
"syscall" | ||
) | ||
|
||
func isConnectionReset(err error) bool { | ||
return errors.Is(err, syscall.ECONNRESET) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package fasthttp | ||
|
||
import ( | ||
"errors" | ||
"syscall" | ||
) | ||
|
||
func isConnectionReset(err error) bool { | ||
return errors.Is(err, syscall.WSAECONNRESET) | ||
} |