2021-07-09 03:09:02 +02:00
|
|
|
package api
|
2021-01-23 00:14:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2021-07-09 03:09:02 +02:00
|
|
|
type contextKey int
|
2021-01-23 00:14:12 +02:00
|
|
|
|
2021-07-09 03:09:02 +02:00
|
|
|
const (
|
|
|
|
httpConnContextKey contextKey = iota
|
|
|
|
sessionContextKey
|
|
|
|
)
|
2021-01-23 00:14:12 +02:00
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// SetContextConn stores the connection in the request context.
|
2021-01-23 00:14:12 +02:00
|
|
|
func SetContextConn(ctx context.Context, c net.Conn) context.Context {
|
2021-07-09 03:09:02 +02:00
|
|
|
return context.WithValue(ctx, httpConnContextKey, c)
|
2021-01-23 00:14:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// GetContextConn gets the stored connection from the request context.
|
2021-01-23 00:14:12 +02:00
|
|
|
func GetContextConn(r *http.Request) net.Conn {
|
2021-07-09 03:09:02 +02:00
|
|
|
value := r.Context().Value(httpConnContextKey)
|
2021-01-23 00:14:12 +02:00
|
|
|
if value == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.(net.Conn)
|
|
|
|
}
|