Skip to content

🐛 bug: Fix Accept-Language matching per RFC 4647#3553

Merged
ReneWerner87 merged 2 commits intomainfrom
2025-06-29-13-35-23
Jun 30, 2025
Merged

🐛 bug: Fix Accept-Language matching per RFC 4647#3553
ReneWerner87 merged 2 commits intomainfrom
2025-06-29-13-35-23

Conversation

@gaby
Copy link
Member

@gaby gaby commented Jun 29, 2025

Summary

  • follow RFC 4647 basic filtering for Accept-Language
  • add tests for invalid and multi-language scenarios

Related #3383

Copilot AI review requested due to automatic review settings June 29, 2025 13:35
@gaby gaby requested a review from a team as a code owner June 29, 2025 13:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the Accept-Language matching logic to follow RFC 4647 basic filtering and enhances its test coverage and documentation.

  • Modified the acceptsOffer function to implement case-insensitive prefix matching for language tags.
  • Added unit tests in helpers_test.go and ctx_test.go for valid, invalid, and case-insensitive language scenarios.
  • Updated documentation in docs/api/ctx.md to explain the Basic Filtering algorithm.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
helpers_test.go Added tests to validate Accept-Language filtering behavior.
helpers.go Adjusted acceptsOffer to implement RFC 4647 basic filtering with case-insensitivity.
docs/api/ctx.md Enhanced documentation to describe Basic Filtering for Accept-Language.
ctx_test.go Added tests covering multi-header and case-insensitive scenarios.
Comments suppressed due to low confidence (1)

helpers.go:142

  • The implementation supports a trailing '*' wildcard by immediately returning true, but the documentation in ctx.md does not mention this behavior, which diverges from RFC 4647 basic filtering. Consider updating the documentation to reflect the wildcard support or revising the implementation to strictly follow the RFC.
func acceptsOffer(spec, offer string, _ headerParams) bool {

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 29, 2025

"""

Walkthrough

This change updates the language tag matching logic in the codebase to follow RFC 4647 Basic Filtering for the Accept-Language header. It refines the internal matching algorithm, adds new and extended test cases to ensure correct behavior, and updates documentation to clarify the matching semantics.

Changes

File(s) Summary of Changes
helpers.go Refined acceptsOffer function to use case-insensitive equality and trailing '*' wildcard only; added acceptsLanguageOffer for RFC 4647 Basic Filtering.
helpers_test.go Added and extended test cases for language tag matching, case sensitivity, and subtag handling in Test_Utils_GetOffer.
ctx.go Modified AcceptsLanguages method to use acceptsLanguageOffer instead of acceptsOffer for language matching.
ctx_test.go Added two new test functions for AcceptsLanguages: one for basic filtering and one for case-insensitivity.
docs/api/ctx.md Updated documentation to clarify Basic Filtering algorithm and matching rules for Accept-Language.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server (Fiber)
    participant Ctx

    Client->>Server (Fiber): Sends request with Accept-Language header
    Server (Fiber)->>Ctx: Parses Accept-Language header
    Ctx->>Ctx: Calls AcceptsLanguages(...)
    Ctx->>Ctx: Uses acceptsLanguageOffer() for language tag matching
    Ctx-->>Server (Fiber): Returns preferred language
    Server (Fiber)-->>Client: Responds using selected language
Loading

Suggested reviewers

  • sixcolors
  • efectn
  • ReneWerner87

Poem

🐇 Through headers wild and free,
I hop to match each language key.
Hyphens, cases, all aligned,
RFC rules in code defined.
Tests now dance in joyful spree,
A perfect match for you and me! ✨
🥕📜
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1ac5620 and f9d2744.

📒 Files selected for processing (3)
  • ctx.go (1 hunks)
  • helpers.go (1 hunks)
  • helpers_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • helpers_test.go
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: ReneWerner87
PR: gofiber/fiber#0
File: :0-0
Timestamp: 2024-12-01T10:28:36.011Z
Learning: Feature request #3224 has been created to add support for square bracket notation and comma-separated values in multipart form data in Fiber, while maintaining binary data transfer capabilities. This would bring parity with the existing form-urlencoded functionality.
ctx.go (2)
Learnt from: gaby
PR: gofiber/fiber#3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.
Learnt from: gaby
PR: gofiber/fiber#3193
File: middleware/cache/cache_test.go:897-897
Timestamp: 2024-11-08T04:10:42.990Z
Learning: In the Fiber framework, `Context()` is being renamed to `RequestCtx()`, and `UserContext()` to `Context()` to improve clarity and align with Go's context conventions.
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Analyse
  • GitHub Check: unit (1.24.x, macos-13)
  • GitHub Check: unit (1.24.x, ubuntu-latest)
  • GitHub Check: unit (1.24.x, macos-latest)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: Compare
  • GitHub Check: repeated
  • GitHub Check: lint
🔇 Additional comments (3)
helpers.go (2)

138-147: LGTM! Addresses the previous review concern.

The simplification of acceptsOffer to only support trailing '*' wildcard and case-insensitive exact matching correctly addresses the previous review concern about inappropriate prefix matching for AcceptsCharsets and AcceptsEncodings.


149-164: Excellent RFC 4647 Basic Filtering implementation.

The new acceptsLanguageOffer function correctly implements RFC 4647 Basic Filtering for language tags:

  • Supports '*' wildcard matching
  • Handles exact case-insensitive matches
  • Implements prefix matching with hyphen delimiter as required by the RFC

This provides the proper separation of concerns between generic offer matching and language-specific negotiation.

ctx.go (1)

231-231: Perfect integration of RFC 4647 Basic Filtering.

The update to use acceptsLanguageOffer ensures that AcceptsLanguages now correctly follows RFC 4647 Basic Filtering semantics while maintaining the existing API.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 enhances the accuracy and compliance of Accept-Language header processing by implementing RFC 4647 Basic Filtering. This change ensures that the application correctly interprets user language preferences according to web standards, leading to more reliable content negotiation. The updated logic is thoroughly tested and documented to reflect this improved behavior.

Highlights

  • RFC 4647 Basic Filtering for Accept-Language: The core logic for processing Accept-Language headers has been updated to strictly follow RFC 4647's Basic Filtering algorithm. This ensures that language tag matching (e.g., 'en' matching 'en-US' but not vice-versa) is handled correctly and case-insensitively.
  • Enhanced Test Coverage: New, comprehensive test cases have been added to ctx_test.go and helpers_test.go to validate the updated Accept-Language parsing. These tests cover various scenarios, including basic filtering rules, multi-language requests, invalid formats, and case-insensitivity, ensuring the new behavior is robust.
  • API Documentation Update: The docs/api/ctx.md file has been updated to clearly document that Fiber now uses RFC 4647 Basic Filtering for Accept-Language headers, providing clarity for developers on how language negotiation is performed.
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

  1. 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.

@gaby gaby added this to v3 Jun 29, 2025
@gaby gaby moved this to In Progress in v3 Jun 29, 2025
@gaby gaby added this to the v3 milestone Jun 29, 2025
@gaby gaby changed the title Fix Accept-Language prefix matching 🐛 bug: Fix Accept-Language prefix matching Jun 29, 2025
@gaby gaby changed the title 🐛 bug: Fix Accept-Language prefix matching 🐛 bug: Fix Accept-Language prefix matching per RFC 4647 Jun 29, 2025
@codecov
Copy link

codecov bot commented Jun 29, 2025

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.97%. Comparing base (55818fb) to head (f9d2744).
⚠️ Report is 365 commits behind head on main.

Files with missing lines Patch % Lines
helpers.go 87.50% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3553      +/-   ##
==========================================
- Coverage   91.02%   90.97%   -0.05%     
==========================================
  Files         110      110              
  Lines       10955    10961       +6     
==========================================
  Hits         9972     9972              
- Misses        731      735       +4     
- Partials      252      254       +2     
Flag Coverage Δ
unittests 90.97% <88.88%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gaby gaby changed the title 🐛 bug: Fix Accept-Language prefix matching per RFC 4647 🐛 bug: Fix Accept-Language matching per RFC 4647 Jun 29, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request aims to fix the Accept-Language prefix matching to follow RFC 4647 for basic filtering. The changes include updating the matching logic, adding relevant tests, and documenting the new behavior.

Overall, the intent is good and the implementation for language matching is correct. However, the current approach of modifying the shared acceptsOffer function introduces a critical bug, as this function is also used for other headers like Accept-Charset and Accept-Encoding where this specific prefix matching is not applicable.

My review includes a critical comment to refactor the logic to avoid this side effect, along with a corresponding update to the tests.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 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: 1ac5620 Previous: 55818fb Ratio
Benchmark_SlashRecognition/forEach 5.81 ns/op 0 B/op 0 allocs/op 3.712 ns/op 0 B/op 0 allocs/op 1.57
Benchmark_SlashRecognition/forEach - ns/op 5.81 ns/op 3.712 ns/op 1.57

This comment was automatically generated by workflow using github-action-benchmark.

@ReneWerner87 ReneWerner87 merged commit 55e7b1e into main Jun 30, 2025
13 of 14 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in v3 Jun 30, 2025
@gaby gaby deleted the 2025-06-29-13-35-23 branch June 30, 2025 11:34
@gaby gaby added the 📜 RFC Compliance Feature, implementation, or contribution adheres to relevant RFC standards. label Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☢️ Bug codex 📜 RFC Compliance Feature, implementation, or contribution adheres to relevant RFC standards. v3

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants