Skip to content

Commit

Permalink
refactor: naming and simplify since we have no after usecase atm
Browse files Browse the repository at this point in the history
  • Loading branch information
malud committed Apr 8, 2024
1 parent 0eba9f9 commit 5e33b54
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
4 changes: 2 additions & 2 deletions handler/middleware/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func NewCORSHandler(opts *CORSOptions, nextHandler http.Handler) http.Handler {

func (c *CORS) ServeNextHTTP(rw http.ResponseWriter, nextHandler http.Handler, req *http.Request) {
if response, ok := rw.(*writer.Response); ok {
response.RegisterHeaderModifier(func(header http.Header) {
response.AddHeaderModifier(func(header http.Header) {
c.setCorsRespHeaders(header, req)
}, false)
})
}

if c.isCorsPreflightRequest(req) {
Expand Down
47 changes: 19 additions & 28 deletions server/writer/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type writer interface {

type modifier interface {
AddModifier(...hcl.Body)
AddHeaderModifier(HeaderModifier)
}

var (
Expand All @@ -47,11 +48,10 @@ type Response struct {
statusCode int
rawBytesWritten int
bytesWritten int
// modifier
evalCtx *eval.Context
modifier []hcl.Body
headerModifiersAfter []HeaderModifier
headerModifiersBefore []HeaderModifier
// modifiers
evalCtx *eval.Context
modifiers []hcl.Body
headerModifiers []HeaderModifier
// security
addPrivateCC bool
}
Expand All @@ -64,7 +64,7 @@ func NewResponseWriter(rw http.ResponseWriter, secureCookies string) *Response {
}
}

// WithEvalContext sets the eval context for the response modifier.
// WithEvalContext sets the eval context for the response modifiers.
func (r *Response) WithEvalContext(ctx *eval.Context) *Response {
r.evalCtx = ctx
return r
Expand Down Expand Up @@ -145,11 +145,10 @@ func (r *Response) WriteHeader(statusCode int) {
}

r.configureHeader()
r.applyHeaderModifiers(false)
r.applyModifier()
r.applyHeaderModifiers(true)
r.applyHeaderModifiers()
r.applyModifiers() // hcl body modifiers

// !!! Execute after modifier !!!
// execute after modifiers
if r.addPrivateCC {
r.Header().Add("Cache-Control", "private")
}
Expand Down Expand Up @@ -206,36 +205,28 @@ func (r *Response) AddPrivateCC() {
}

func (r *Response) AddModifier(modifier ...hcl.Body) {
r.modifier = append(r.modifier, modifier...)
r.modifiers = append(r.modifiers, modifier...)
}

func (r *Response) applyModifier() {
if r.evalCtx == nil || r.modifier == nil {
// applyModifiers applies the hcl body modifiers to the response.
func (r *Response) applyModifiers() {
if r.evalCtx == nil || r.modifiers == nil {
return
}

hctx := r.evalCtx.HCLContextSync()
for _, body := range r.modifier {
for _, body := range r.modifiers {
_ = eval.ApplyResponseHeaderOps(hctx, body, r.Header())
}
}

func (r *Response) RegisterHeaderModifier(headerModifier HeaderModifier, afterModifierAttributes bool) {
if afterModifierAttributes {
r.headerModifiersAfter = append(r.headerModifiersAfter, headerModifier)
} else {
r.headerModifiersBefore = append(r.headerModifiersBefore, headerModifier)
}
func (r *Response) AddHeaderModifier(headerModifier HeaderModifier) {
r.headerModifiers = append(r.headerModifiers, headerModifier)
}

func (r *Response) applyHeaderModifiers(afterModifierAttributes bool) {
var headerModifiers []HeaderModifier
if afterModifierAttributes {
headerModifiers = r.headerModifiersAfter
} else {
headerModifiers = r.headerModifiersBefore
}
for _, modifierFn := range headerModifiers {
// applyHeaderModifiers applies the http.Header modifiers to the response.
func (r *Response) applyHeaderModifiers() {
for _, modifierFn := range r.headerModifiers {
modifierFn(r.Header())
}
}

0 comments on commit 5e33b54

Please sign in to comment.