-
-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(cache): improve hasDirective function to handle various cases and… #4181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -907,23 +907,16 @@ func New(config ...Config) fiber.Handler { | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // hasDirective checks if a cache-control header contains a directive (case-insensitive) | ||||||||||||||||||||||||||||||||||
| func hasDirective(cc, directive string) bool { | ||||||||||||||||||||||||||||||||||
| ccLen := len(cc) | ||||||||||||||||||||||||||||||||||
| dirLen := len(directive) | ||||||||||||||||||||||||||||||||||
| for i := 0; i <= ccLen-dirLen; i++ { | ||||||||||||||||||||||||||||||||||
| if !utils.EqualFold(cc[i:i+dirLen], directive) { | ||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| if i > 0 { | ||||||||||||||||||||||||||||||||||
| prev := cc[i-1] | ||||||||||||||||||||||||||||||||||
| if prev != ' ' && prev != ',' { | ||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| if i+dirLen == ccLen || cc[i+dirLen] == ',' { | ||||||||||||||||||||||||||||||||||
| for _, part := range strings.Split(cc, ",") { | ||||||||||||||||||||||||||||||||||
| part = strings.TrimSpace(part) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| key, _, _ := strings.Cut(part, "=") | ||||||||||||||||||||||||||||||||||
| key = strings.TrimSpace(key) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if strings.EqualFold(key, directive) { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+910
to
+916
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoted commas can produce false directive matches The current comma-split approach can misparse valid quoted directive values and report a directive that only appears inside a quoted string. Consider reusing Suggested fix func hasDirective(cc, directive string) bool {
- for _, part := range strings.Split(cc, ",") {
- part = strings.TrimSpace(part)
-
- key, _, _ := strings.Cut(part, "=")
- key = strings.TrimSpace(key)
-
- if strings.EqualFold(key, directive) {
- return true
- }
- }
- return false
+ found := false
+ parseCacheControlDirectives(utils.UnsafeBytes(cc), func(key, _ []byte) {
+ if !found && utils.EqualFold(utils.UnsafeString(key), directive) {
+ found = true
+ }
+ })
+ return found
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better performance and to reduce allocations, consider using
strings.SplitSeqinstead ofstrings.Split.strings.SplitSeqprovides an iterator over the substrings, avoiding the allocation of a slice to hold all of them at once. This is more memory-efficient, especially forCache-Controlheaders with many directives.This change would also be consistent with the implementation of
parseVaryin this file, which already usesstrings.SplitSeq.