Skip to content

Commit

Permalink
feat: force encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
abemedia committed May 22, 2023
1 parent 2cdddb1 commit c5fb773
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type Config struct {
// DisableNoContent controls whether a nil or zero value response should
// automatically return 204 No Content with an empty body.
DisableNoContent bool

ForceDefaultEncoding bool
}

// New creates a new API instance.
Expand Down Expand Up @@ -126,14 +128,18 @@ func (r *API) RequestHandler() fasthttp.RequestHandler {
}

return func(ctx *fasthttp.RequestCtx) {
ct := ctx.Request.Header.ContentType()
if len(ct) == 0 || bytes.HasPrefix(ct, anyEncoding) {
if r.config.ForceDefaultEncoding {
ctx.Request.Header.SetContentType(r.config.DefaultEncoding)
}

ac := ctx.Request.Header.Peek(fasthttp.HeaderAccept)
if len(ac) == 0 || bytes.HasPrefix(ac, anyEncoding) {
ctx.Request.Header.Set(fasthttp.HeaderAccept, r.config.DefaultEncoding)
} else {
contentType := ctx.Request.Header.ContentType()
if len(contentType) == 0 || bytes.HasPrefix(contentType, anyEncoding) {
ctx.Request.Header.SetContentType(r.config.DefaultEncoding)
}
accept := ctx.Request.Header.Peek(fasthttp.HeaderAccept)
if len(accept) == 0 || bytes.HasPrefix(accept, anyEncoding) {
ctx.Request.Header.Set(fasthttp.HeaderAccept, r.config.DefaultEncoding)
}
}

h(ctx)
Expand Down

0 comments on commit c5fb773

Please sign in to comment.