Skip to content

Commit

Permalink
Fix #66. If user does not define headerValues when configuring Tollbo…
Browse files Browse the repository at this point in the history
…oth,

Create individual bucket by using request header’s own value,

so that we can rate-limit individually.

Also fix a regression where we didn’t pass BasicAuth username on one of the possible path of key building.
  • Loading branch information
didip committed Jul 23, 2019
1 parent be0cf69 commit 9a45a30
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tollbooth.go
Expand Up @@ -59,10 +59,10 @@ func BuildKeys(lmt *limiter.Limiter, r *http.Request) [][]string {
if libstring.StringInSlice(lmtMethods, r.Method) {
for headerKey, headerValues := range lmtHeaders {
if (headerValues == nil || len(headerValues) <= 0) && r.Header.Get(headerKey) != "" {
// If header values are empty, rate-limit all request with headerKey.
// If header values are empty, rate-limit all request containing headerKey.
username, _, ok := r.BasicAuth()
if ok && libstring.StringInSlice(lmtBasicAuthUsers, username) {
sliceKeys = append(sliceKeys, []string{remoteIP, path, r.Method, headerKey, username})
sliceKeys = append(sliceKeys, []string{remoteIP, path, r.Method, headerKey, r.Header.Get(headerKey), username})
}

} else if len(headerValues) > 0 && r.Header.Get(headerKey) != "" {
Expand All @@ -71,7 +71,7 @@ func BuildKeys(lmt *limiter.Limiter, r *http.Request) [][]string {
if r.Header.Get(headerKey) == headerValue {
username, _, ok := r.BasicAuth()
if ok && libstring.StringInSlice(lmtBasicAuthUsers, username) {
sliceKeys = append(sliceKeys, []string{remoteIP, path, r.Method, headerKey, headerValue})
sliceKeys = append(sliceKeys, []string{remoteIP, path, r.Method, headerKey, headerValue, username})
}
break
}
Expand All @@ -86,7 +86,7 @@ func BuildKeys(lmt *limiter.Limiter, r *http.Request) [][]string {
for headerKey, headerValues := range lmtHeaders {
if (headerValues == nil || len(headerValues) <= 0) && r.Header.Get(headerKey) != "" {
// If header values are empty, rate-limit all request with headerKey.
sliceKeys = append(sliceKeys, []string{remoteIP, path, r.Method, headerKey})
sliceKeys = append(sliceKeys, []string{remoteIP, path, r.Method, headerKey, r.Header.Get(headerKey)})

} else if len(headerValues) > 0 && r.Header.Get(headerKey) != "" {
// We are only limiting if request's header value is defined inside `headerValues`.
Expand Down Expand Up @@ -120,7 +120,7 @@ func BuildKeys(lmt *limiter.Limiter, r *http.Request) [][]string {
for headerKey, headerValues := range lmtHeaders {
if (headerValues == nil || len(headerValues) <= 0) && r.Header.Get(headerKey) != "" {
// If header values are empty, rate-limit all request with headerKey.
sliceKeys = append(sliceKeys, []string{remoteIP, path, headerKey})
sliceKeys = append(sliceKeys, []string{remoteIP, path, headerKey, r.Header.Get(headerKey)})

} else if len(headerValues) > 0 && r.Header.Get(headerKey) != "" {
// If header values are not empty, rate-limit all request with headerKey and headerValues.
Expand Down

0 comments on commit 9a45a30

Please sign in to comment.