Skip to content

Commit

Permalink
chore: update unauthorized callback calling order (zeromicro#1469)
Browse files Browse the repository at this point in the history
* chore: update unauthorized callback calling order

* chore: add comments
  • Loading branch information
kevwan authored Jan 20, 2022
1 parent df0f8ed commit fdc7f64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion core/limit/tokenlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func (lim *TokenLimiter) reserveN(now time.Time, n int) bool {
// Lua boolean false -> r Nil bulk reply
if err == redis.Nil {
return false
} else if err != nil {
}
if err != nil {
logx.Errorf("fail to use rate limiter: %s, use in-process limiter for rescue", err)
lim.startMonitor()
return lim.rescueLimiter.AllowN(now, n)
Expand Down
4 changes: 3 additions & 1 deletion core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,9 @@ func getValueWithChainedKeys(m Valuer, keys []string) (interface{}, bool) {
if len(keys) == 1 {
v, ok := m.Value(keys[0])
return v, ok
} else if len(keys) > 1 {
}

if len(keys) > 1 {
if v, ok := m.Value(keys[0]); ok {
if nextm, ok := v.(map[string]interface{}); ok {
return getValueWithChainedKeys(MapValuer(nextm), keys[1:])
Expand Down
6 changes: 4 additions & 2 deletions rest/handler/authhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ func unauthorized(w http.ResponseWriter, r *http.Request, err error, callback Un
detailAuthLog(r, noDetailReason)
}

writer.WriteHeader(http.StatusUnauthorized)

// let callback go first, to make sure we respond with user-defined HTTP header
if callback != nil {
callback(writer, r, err)
}

// if user not setting HTTP header, we set header with 401
writer.WriteHeader(http.StatusUnauthorized)
}

type guardedResponseWriter struct {
Expand Down

0 comments on commit fdc7f64

Please sign in to comment.