-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix broken ws upgrade introduced by using http.TimeoutHandler
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package middleware | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func NewTimeoutHandler(handler http.Handler, timeout time.Duration) http.Handler { | ||
return &timeoutHandler{ | ||
handler: handler, | ||
timeout: timeout, | ||
} | ||
} | ||
|
||
type timeoutHandler struct { | ||
handler http.Handler | ||
timeout time.Duration | ||
} | ||
|
||
func (h *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx, cancel := context.WithTimeout(r.Context(), h.timeout) | ||
defer cancel() | ||
|
||
r = r.WithContext(ctx) | ||
h.handler.ServeHTTP(w, r) | ||
} | ||
|
||
func (h *timeoutHandler) Hijack() (net.Conn, *bufio.ReadWriter, error) { | ||
hijacker, ok := h.handler.(http.Hijacker) | ||
if !ok { | ||
return nil, nil, fmt.Errorf("the handler does not support Hijacker interface") | ||
} | ||
return hijacker.Hijack() | ||
} |
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