forked from speakeasy-api/speakeasy-auth-test-service
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreset_injector.go
More file actions
38 lines (28 loc) · 883 Bytes
/
reset_injector.go
File metadata and controls
38 lines (28 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package middleware
import (
"net"
"net/http"
"github.com/lingrino/go-fault"
)
var _ fault.Injector = (*ConnectionResetInjector)(nil)
// Injects a connection error by closing the connection immediately while also
// simulating a TCP RST via setting SO_LINGER to 0.
type ConnectionResetInjector struct{}
func (i *ConnectionResetInjector) Handler(_ http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "connection hijacking not supported", http.StatusInternalServerError)
return
}
conn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, "failed to hijack connection", http.StatusInternalServerError)
return
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
_ = tcpConn.SetLinger(0) // Best effort RST on close
}
conn.Close()
})
}