2021-01-22 23:14:12 +01:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type contextKey struct {
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
var connContextKey = &contextKey{"http-conn"}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// SetContextConn stores the connection in the request context.
|
2021-01-22 23:14:12 +01:00
|
|
|
func SetContextConn(ctx context.Context, c net.Conn) context.Context {
|
|
|
|
return context.WithValue(ctx, connContextKey, c)
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// GetContextConn gets the stored connection from the request context.
|
2021-01-22 23:14:12 +01:00
|
|
|
func GetContextConn(r *http.Request) net.Conn {
|
|
|
|
value := r.Context().Value(connContextKey)
|
|
|
|
if value == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.(net.Conn)
|
|
|
|
}
|