tusd/pkg/handler/context.go

37 lines
828 B
Go
Raw Normal View History

package handler
import (
"context"
"net/http"
)
// httpContext is wrapper around context.Context that also carries the
// corresponding HTTP request and response writer, as well as an
// optional body reader
// TODO: Consider including HTTPResponse as well
type httpContext struct {
context.Context
2022-06-18 07:16:57 +00:00
parentCtx context.Context
res http.ResponseWriter
req *http.Request
body *bodyReader
}
func newContext(w http.ResponseWriter, r *http.Request) *httpContext {
return &httpContext{
2022-06-18 07:16:57 +00:00
Context: context.Background(),
parentCtx: r.Context(),
res: w,
req: r,
body: nil, // body can be filled later for PATCH requests
}
}
2022-06-18 07:16:57 +00:00
func (hctx *httpContext) Value(key interface{}) interface{} {
if v := hctx.Context.Value(key); v != nil {
return v
}
return hctx.parentCtx.Value(key)
}