package csp

import (
	"context"
	"net/http"
)

type cspKey struct{}

func Csp(r *http.Request) (*http.Request, *Builder) {
	csp, ok := r.Context().Value(cspKey{}).(*Builder)
	if !ok {
		csp = &Builder{
			request: r,
		}
		r = r.WithContext(context.WithValue(r.Context(), cspKey{}, csp))
	}
	return r, csp
}

func (csp *Builder) Write() {
	csp.request.Header.Set("Content-Security-Policy", csp.Build())
}
