Skip to content

Commit

Permalink
Fix WS connection hijack (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview authored May 31, 2024
1 parent 27e60e6 commit 43d1fde
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
package api

import (
"bufio"
"errors"
"net"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -34,6 +37,21 @@ func (m *metricsResponseWriter) WriteHeader(code int) {
m.ResponseWriter.WriteHeader(code)
}

// Hijack complies the writer with WS subscriptions interface
// Hijack lets the caller take over the connection.
// After a call to Hijack the HTTP server library
// will not do anything else with the connection.
//
// It becomes the caller's responsibility to manage
// and close the connection.
func (m *metricsResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := m.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("hijack not supported")
}
return h.Hijack()
}

// metricsMiddleware is a middleware that records metrics for each request.
func metricsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 43d1fde

Please sign in to comment.