🐛 bug: Fix Cache-Control header parsing#3534
Conversation
WalkthroughThis change updates cache-control directive handling to be case-insensitive throughout the codebase. It refactors internal logic for detecting "no-cache" directives, updates related tests to cover uppercase variants, and amends a documentation comment to reference an updated RFC. No exported APIs or control flows are altered. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Middleware
participant Cache
participant Handler
Client->>Middleware: Send request with Cache-Control (any case)
Middleware->>Cache: Check for cached response
alt hasRequestDirective (case-insensitive)
Middleware->>Handler: Bypass cache, process request
Handler-->>Middleware: Return fresh response
Middleware-->>Client: Send fresh response
else No directive
alt Cache hit
Cache-->>Middleware: Return cached response
Middleware-->>Client: Send cached response
else Cache miss
Middleware->>Handler: Process request
Handler-->>Middleware: Return response
Middleware->>Cache: Store response
Middleware-->>Client: Send response
end
end
Possibly related PRs
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (8)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Summary of Changes
Hello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly improves the robustness of Cache-Control header parsing by implementing case-insensitivity for directives like no-cache and no-store. This change aligns the application's caching logic with modern HTTP specifications, ensuring directives are correctly interpreted regardless of their casing, and is backed by expanded test coverage.
Highlights
- Case-Insensitive Cache-Control Parsing: The core change introduces case-insensitive parsing for
Cache-Controlheader directives, specificallyno-cacheand other request directives, by utilizingutils.EqualFoldandutils.ToLower. This makes the application more compliant with HTTP/1.1 and HTTP/2 specifications where header field names are case-insensitive. - Updated RFC Reference: The RFC reference for
Cache-Control: no-cacheinctx.gohas been updated from RFC 2616 to the more current RFC 9111, ensuring documentation points to the latest standard. - Enhanced Test Coverage: New test cases have been added across
helpers_test.goandmiddleware/cache/cache_test.goto validate the case-insensitive parsing ofno-cacheandno-storedirectives, improving the robustness of the caching middleware.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3534 +/- ##
==========================================
+ Coverage 90.53% 90.56% +0.02%
==========================================
Files 110 110
Lines 10949 10957 +8
==========================================
+ Hits 9913 9923 +10
+ Misses 777 776 -1
+ Partials 259 258 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request improves the parsing of Cache-Control headers by making it case-insensitive, which is a great enhancement. The changes in isNoCache and the added test cases are solid.
However, I've identified a critical issue in the implementation of hasRequestDirective. It uses strings.Contains, which can lead to incorrect behavior due to partial matches of directives. I've provided a detailed comment with a suggested fix to ensure directives are matched as whole tokens.
Overall, a good direction, and with the suggested fix, it will be a solid improvement.
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.
| Benchmark suite | Current: 4ca5ba4 | Previous: e1a7c11 | Ratio |
|---|---|---|---|
Benchmark_Ctx_SendString_B |
15.19 ns/op 0 B/op 0 allocs/op |
9.374 ns/op 0 B/op 0 allocs/op |
1.62 |
Benchmark_Ctx_SendString_B - ns/op |
15.19 ns/op |
9.374 ns/op |
1.62 |
Benchmark_Utils_IsNoCache |
77.17 ns/op 0 B/op 0 allocs/op |
40.8 ns/op 0 B/op 0 allocs/op |
1.89 |
Benchmark_Utils_IsNoCache - ns/op |
77.17 ns/op |
40.8 ns/op |
1.89 |
This comment was automatically generated by workflow using github-action-benchmark.
There was a problem hiding this comment.
Pull Request Overview
Improves Cache-Control header parsing to handle directives case-insensitively and adds uppercase test coverage for no-cache and no-store.
- Refactor directive checks in
hasRequestDirectiveandisNoCacheto useEqualFoldwith precise token boundaries - Extend cache tests to cover uppercase
NO-CACHEandNO-STOREscenarios and update expected responses - Update RFC reference in
Fresh()comment to the current spec
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| middleware/cache/cache.go | Refactor hasRequestDirective for case-insensitive matching and boundaries |
| middleware/cache/cache_test.go | Add uppercase NO-CACHE/NO-STORE tests and adjust expected body assertions |
| helpers.go | Refactor isNoCache to use EqualFold and correct iteration bounds |
| helpers_test.go | Add uppercase NO-CACHE test cases |
| ctx.go | Update RFC link in Fresh() method comment |
Comments suppressed due to low confidence (3)
middleware/cache/cache_test.go:263
- The new uppercase
NO-CACHEETag test asserts status and header but does not read and verify the response body. Consider readingnoCacheRespUpper.Bodyand asserting it equals[]byte("3").
require.Equal(t, fiber.StatusOK, noCacheRespUpper.StatusCode)
middleware/cache/cache_test.go:313
- The uppercase
NO-STOREtest checks the body but omits an assertion on theX-Cacheheader. Addrequire.Equal(t, cacheMiss, noStoreRespUpper.Header.Get("X-Cache"))to verify caching behavior.
require.Equal(t, []byte("3"), noStoreBodyUpper)
middleware/cache/cache.go:313
- Current boundary check requires the next character to be a comma or end-of-string, but does not allow optional whitespace before the comma. Consider trimming or skipping OWS so directives like
"no-cache , public"are still detected.
if i+dLen == n || cc[i+dLen] == ',' {
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Summary
no-cache.no-cache.Related #3383