Skip to content

🐛 bug: Fix Range parsing of bytes unit #3541

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

gaby
Copy link
Member

@gaby gaby commented Jun 23, 2025

Summary

  • enforce recognized bytes unit and trim whitespace when parsing Range header
  • document Range behavior in API reference
  • add regression tests for whitespace and unrecognized units
  • improve rfc 9110 compliance.

@gaby gaby added the codex label Jun 23, 2025 — with ChatGPT Connector
@gaby gaby changed the title Fix Range parsing 🐛 bug: Fix Range parsing of bytes unit Jun 23, 2025
Copy link
Contributor

coderabbitai bot commented Jun 23, 2025

Walkthrough

Whitespace handling and validation in the Range method of DefaultCtx were tightened to strictly accept only the "bytes" unit and to ignore extraneous spaces within the Range header. Two new test cases were added to verify these behaviors, and documentation was updated to clarify the method's compliance with RFC 9110.

Changes

File(s) Change Summary
ctx.go Improved parsing and validation in Range method: trims whitespace, strictly checks for "bytes".
ctx_test.go Added tests for whitespace handling and non-"bytes" unit in Range header.
docs/api/ctx.md Updated documentation to clarify "bytes" unit enforcement and whitespace handling in Range parsing.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Ctx
    participant RangeParser

    Client->>Ctx: Send request with Range header
    Ctx->>RangeParser: Parse Range header (trim spaces, check unit)
    RangeParser-->>Ctx: Return parsed ranges or error
    Ctx-->>Client: Respond with range or error
Loading

Suggested labels

📒 Documentation

Suggested reviewers

  • sixcolors
  • ReneWerner87

Poem

In the land of HTTP bytes,
Whitespace now brings no frights.
Only "bytes" shall pass the gate,
All else meets a stricter fate.
Tests and docs join in the cheer,
RFC rules are crystal clear!
🐇✨

✨ 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 focuses on improving the Range header parsing functionality within the Ctx context. My aim was to make the parsing more resilient to various input formats, specifically by handling and trimming extraneous whitespace, and to enforce strict compliance with the HTTP/1.1 specification (RFC 9110) by only recognizing the 'bytes' unit. These changes lead to more predictable and robust behavior when processing content range requests.

Highlights

  • Improved Range Header Parsing Robustness: The Range header parsing logic has been enhanced to be more robust against malformed inputs by consistently trimming optional whitespace from various parts of the header value, including the overall string, the unit type, and individual range specifiers.
  • Enforcement of 'bytes' Unit Compliance: The parser now strictly enforces that only the canonical 'bytes' unit is recognized for Range headers. Any other unit will result in a malformed range error, aligning the implementation more closely with RFC 9110 for byte range requests.
  • Enhanced Test Coverage for Edge Cases: New regression tests have been added to specifically cover scenarios involving whitespace in Range headers and the rejection of unrecognized range units, ensuring the new parsing behavior is correctly validated and preventing future regressions.
  • Updated API Documentation for Clarity: The API reference for the Range method has been updated to explicitly document that only the 'bytes' unit is recognized and that whitespace around range specifiers will be ignored, providing clearer guidance for developers using the API.
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 23, 2025
@gaby gaby added this to the v3 milestone Jun 23, 2025
@gaby gaby moved this to In Progress in v3 Jun 23, 2025
@gaby gaby added the v3 label Jun 23, 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

The pull request enhances the Range header parsing logic by enforcing the recognition of the bytes unit, trimming whitespace, and adding regression tests. The changes improve the robustness and correctness of the Range header parsing, aligning with RFC 9110. The addition of documentation and tests further enhances the quality of the code.

Copy link

codecov bot commented Jun 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.71%. Comparing base (a258661) to head (e39e346).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3541   +/-   ##
=======================================
  Coverage   90.71%   90.71%           
=======================================
  Files         110      110           
  Lines       10954    10959    +5     
=======================================
+ Hits         9937     9942    +5     
  Misses        767      767           
  Partials      250      250           
Flag Coverage Δ
unittests 90.71% <100.00%> (+<0.01%) ⬆️

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 marked this pull request as ready for review June 23, 2025 13:41
@gaby gaby requested a review from a team as a code owner June 23, 2025 13:41
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (2)
ctx.go (1)

1285-1288: Excellent: Strict "bytes" unit validation enhances RFC 9110 compliance.

The combination of trimming whitespace around the range type and enforcing strict "bytes" unit validation properly implements RFC 9110 requirements. The early return prevents unnecessary processing of invalid range types.

This aligns with the previous review comment about the performance benefits of early return for non-"bytes" types.

ctx_test.go (1)

2845-2845: Duplicate comment: Unit validation test case already reviewed.

This test case appropriately validates that non-bytes units are rejected, supporting the stricter enforcement mentioned in the PR objectives.

🧹 Nitpick comments (2)
ctx.go (1)

1273-1343: Consider optimizing multiple trim operations for performance.

While the comprehensive whitespace trimming is correct and improves RFC compliance, the multiple utils.Trim calls could potentially be optimized. However, given that Range header parsing is not typically a high-frequency operation, the current approach prioritizes correctness and readability over micro-optimizations.

If performance becomes a concern, consider:

  • Pre-processing the entire range string to remove all unnecessary whitespace once
  • Using a single regex-based approach for whitespace normalization
  • Benchmarking to determine if optimization is warranted
docs/api/ctx.md (1)

1216-1218: Consider adding a whitespace-handling example. Illustrating the trimming behavior—e.g.,

// Range: bytes= 500-700 , 700-900

—would help users understand the new parsing rules.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a258661 and e39e346.

📒 Files selected for processing (3)
  • ctx.go (2 hunks)
  • ctx_test.go (1 hunks)
  • docs/api/ctx.md (1 hunks)
🔇 Additional comments (7)
ctx.go (5)

1279-1279: Good: Initial whitespace trimming improves header parsing robustness.

Trimming whitespace from the entire Range header value at the start helps handle headers with leading/trailing spaces, which improves RFC compliance.


1289-1289: Good: Consistent whitespace handling for ranges section.

Trimming whitespace from the ranges part (after the '=') ensures consistent parsing regardless of spacing around the ranges specification.


1299-1299: Good: Proper whitespace handling in range splitting.

Trimming whitespace when processing comma-separated ranges ensures that spaces around commas don't interfere with individual range parsing.


1304-1304: Good: Individual range whitespace normalization.

Adding whitespace trimming for each individual range specification ensures clean parsing of range boundaries.


1313-1314: Good: Granular whitespace trimming for range components.

Trimming whitespace from both start and end components of each range specification provides comprehensive whitespace handling that aligns with RFC 9110's flexibility requirements.

ctx_test.go (1)

2844-2844: LGTM! Good test coverage for whitespace handling.

This test case correctly verifies that Range headers with whitespace after the equals sign (bytes= 0-1) are properly parsed and trimmed, which aligns with the RFC 9110 compliance improvements mentioned in the PR objectives.

docs/api/ctx.md (1)

1216-1218: Documentation accurately updated for Range parsing. The description now correctly enforces that only the canonical "bytes" unit is recognized and that optional whitespace around range specifiers is ignored, in alignment with RFC 9110.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

1 participant