Skip to content

🔥 feat: Add KeepConnectionHeader option to Proxy middleware#3662

Merged
ReneWerner87 merged 6 commits intomainfrom
2025-08-06-03-29-23
Aug 6, 2025
Merged

🔥 feat: Add KeepConnectionHeader option to Proxy middleware#3662
ReneWerner87 merged 6 commits intomainfrom
2025-08-06-03-29-23

Conversation

@gaby
Copy link
Member

@gaby gaby commented Aug 6, 2025

Summary

  • add KeepConnectionHeader config to proxy middleware (defaults to false)
  • document new option and note in changelog
  • test keeping and dropping Connection header

Fixes #2700

Copilot AI review requested due to automatic review settings August 6, 2025 03:29
@gaby gaby requested a review from a team as a code owner August 6, 2025 03:29
@gaby gaby added the codex label Aug 6, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 6, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

A new boolean option, KeepConnectionHeader, was introduced to the proxy middleware configuration. The code, documentation, and tests were updated to support and describe this option, which controls whether the HTTP Connection header is preserved or dropped when proxying requests and responses. New tests verify both behaviors.

Changes

Cohort / File(s) Change Summary
Proxy Middleware: Config & Logic
middleware/proxy/config.go, middleware/proxy/proxy.go
Added KeepConnectionHeader field to the config struct and updated proxy logic to conditionally remove or retain the Connection header based on this flag.
Proxy Middleware: Tests
middleware/proxy/proxy_test.go
Added tests to verify the behavior of the KeepConnectionHeader option, covering both retention and removal scenarios for the Connection header in proxy requests and responses.
Documentation
docs/middleware/proxy.md, docs/whats_new.md
Updated documentation to describe the new KeepConnectionHeader option, its default value, example usage, and its effect on proxy behavior. Removed an outdated commented example and added a new example showing KeepConnectionHeader usage.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Proxy
    participant Backend

    Client->>Proxy: HTTP request (with Connection header)
    alt KeepConnectionHeader = true
        Proxy->>Backend: Forward request (preserve Connection header)
        Backend->>Proxy: Response (may include Connection header)
        Proxy->>Client: Forward response (preserve Connection header)
    else KeepConnectionHeader = false
        Proxy->>Backend: Forward request (remove Connection header)
        Backend->>Proxy: Response (may include Connection header)
        Proxy->>Client: Forward response (remove Connection header)
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Assessment against linked issues

Objective Addressed Explanation
Provide a way to control deletion of the Connection header in proxy.Do() and middleware (#2700)
Explain or justify why the Connection header is deleted by default in proxying The code now allows preserving it via config.

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes detected.

Suggested reviewers

  • sixcolors
  • ReneWerner87

Poem

🐇 A header hops, a header stays,
Proxy listens to what you say.
Keep it close or let it go,
The choice is yours, now you know!
With docs and tests all set just right,
Proxy’s ready for the flight! ✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 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 d22bdb4 and 8297ef5.

📒 Files selected for processing (1)
  • docs/middleware/proxy.md (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/middleware/proxy.md
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: Compare
  • GitHub Check: Analyse
  • GitHub Check: unit (1.24.x, macos-13)
  • GitHub Check: lint
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: unit (1.24.x, macos-latest)
  • GitHub Check: repeated
✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 2025-08-06-03-29-23

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

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate unit tests to generate unit tests for 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!

I've introduced a new configuration option, KeepConnectionHeader, to the proxy middleware. This option provides users with explicit control over whether the Connection header is retained or dropped when proxying requests and responses, offering greater flexibility for various proxying scenarios.

Highlights

  • New Configuration Option: Introduced KeepConnectionHeader (boolean, defaults to false) in the proxy.Config struct, allowing explicit control over the Connection header.
  • Conditional Header Handling: Modified the proxy logic to conditionally remove the Connection header from both incoming requests and outgoing responses only if KeepConnectionHeader is false.
  • Comprehensive Testing: Added new unit tests to validate both scenarios: when the Connection header is kept and when it is dropped, ensuring the new functionality works as expected.
  • Documentation Updates: Updated the proxy.md middleware documentation to include the new option and added an entry to whats_new.md for changelog purposes.
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 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 or fill out our survey 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 changed the title feat(proxy): add KeepConnectionHeader option 🔥 feat: Add KeepConnectionHeader option to Proxy middleware Aug 6, 2025
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 adds a new KeepConnectionHeader configuration option to the proxy middleware that allows retaining the Connection header instead of dropping it by default (which is the current behavior).

  • Adds KeepConnectionHeader boolean field to proxy config (defaults to false)
  • Conditionally removes Connection header based on this setting in both request and response processing
  • Includes comprehensive test coverage for both keeping and dropping the Connection header

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
middleware/proxy/config.go Adds the new KeepConnectionHeader field to Config struct with default value
middleware/proxy/proxy.go Implements conditional Connection header removal based on the new config option
middleware/proxy/proxy_test.go Adds test cases to verify Connection header handling behavior
docs/middleware/proxy.md Documents the new configuration option in the middleware documentation
docs/whats_new.md Notes the new feature in the changelog

@codecov
Copy link

codecov bot commented Aug 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.84%. Comparing base (059b161) to head (8297ef5).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3662   +/-   ##
=======================================
  Coverage   91.84%   91.84%           
=======================================
  Files         113      113           
  Lines       11443    11444    +1     
=======================================
+ Hits        10510    10511    +1     
  Misses        669      669           
  Partials      264      264           
Flag Coverage Δ
unittests 91.84% <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.

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 introduces a KeepConnectionHeader option to the proxy middleware, allowing the Connection header to be preserved. The implementation for the Balancer is correct and well-tested. My feedback focuses on improving the documentation for this new option, addressing a potential inconsistency with other proxy functions like Forward and Do, and suggesting a refactoring of the new tests for better maintainability.

@gaby gaby added the v3 label Aug 6, 2025
@gaby gaby added this to v3 Aug 6, 2025
@gaby gaby added this to the v3 milestone Aug 6, 2025
@gaby gaby moved this to In Progress in v3 Aug 6, 2025
@gaby
Copy link
Member Author

gaby commented Aug 6, 2025

/gemini review

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 introduces a KeepConnectionHeader option to the proxy middleware, allowing users to preserve the Connection header, which is stripped by default. The implementation is sound, with updates to both the config-based Balancer and the global helpers like Do and Forward. The documentation and changelog have been updated accordingly, and the feature is well-tested. My main feedback is regarding the use of parallel tests on shared global state, which can lead to flaky test runs.

}

func Test_Proxy_Do_KeepConnectionHeader(t *testing.T) {
t.Parallel()
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This test modifies global state (keepConnectionHeader) and is marked to run in parallel. This can create race conditions with other tests that also modify this state (e.g., Test_Proxy_Do_DropConnectionHeader), leading to flaky results. Tests that modify shared global state should not be run in parallel. Please remove t.Parallel() to ensure this test runs serially.

}

func Test_Proxy_Do_DropConnectionHeader(t *testing.T) {
t.Parallel()
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This test modifies global state (keepConnectionHeader) and is marked to run in parallel. This can create race conditions with other tests that also modify this state (e.g., Test_Proxy_Do_KeepConnectionHeader), leading to flaky results. Tests that modify shared global state should not be run in parallel. Please remove t.Parallel() to ensure this test runs serially.

@ReneWerner87
Copy link
Member

@gaby is this test flaky
image

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

🧹 Nitpick comments (1)
docs/middleware/proxy.md (1)

113-119: Optional: Slim down the second Balancer example or give it a subtitle

Back-to-back full balancer snippets (Lines 104-112 and 113-119) differ by just one flag. Consider either:

  1. Adding a short heading/comment (e.g. “// Example with Connection header preserved”) so readers immediately see the purpose, or
  2. Merging the flag into the first example with an inline comment to reduce duplication.

This keeps the docs concise without losing information.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b42632a and d22bdb4.

📒 Files selected for processing (4)
  • docs/middleware/proxy.md (4 hunks)
  • docs/whats_new.md (1 hunks)
  • middleware/proxy/proxy.go (2 hunks)
  • middleware/proxy/proxy_test.go (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • middleware/proxy/proxy.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • docs/whats_new.md
  • middleware/proxy/proxy_test.go
🧰 Additional context used
📓 Path-based instructions (1)
docs/**

📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)

Review and update the contents of the docs folder if necessary when modifying code

Files:

  • docs/middleware/proxy.md
🧠 Learnings (1)
📓 Common learnings
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:20-23
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests for key length enforcement in both `EncryptCookie` and `DecryptCookie` functions have been added to ensure robust validation and prevent potential runtime errors.
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:20-23
Timestamp: 2024-07-01T03:44:03.672Z
Learning: Unit tests for key length enforcement in both `EncryptCookie` and `DecryptCookie` functions have been added to ensure robust validation and prevent potential runtime errors.
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:51-54
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests for key length enforcement in `DecryptCookie` have been added to ensure consistency and security in the encryption processes.
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:51-54
Timestamp: 2024-07-01T03:33:22.283Z
Learning: Unit tests for key length enforcement in `DecryptCookie` have been added to ensure consistency and security in the encryption processes.
🪛 markdownlint-cli2 (0.17.2)
docs/middleware/proxy.md

169-169: Table pipe style
Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe

(MD055, table-pipe-style)


169-169: Table column count
Expected: 4; Actual: 3; Too few cells, row will be missing data

(MD056, table-column-count)


169-169: Tables should be surrounded by blank lines

(MD058, blanks-around-tables)


170-170: Code block style
Expected: fenced; Actual: indented

(MD046, code-block-style)

🪛 GitHub Check: markdownlint
docs/middleware/proxy.md

[failure] 170-170: Code block style
docs/middleware/proxy.md:170 MD046/code-block-style Code block style [Expected: fenced; Actual: indented] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md046.md


[failure] 169-169: Tables should be surrounded by blank lines
docs/middleware/proxy.md:169 MD058/blanks-around-tables Tables should be surrounded by blank lines [Context: "| KeepConnectionHeader | `bool..."] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md058.md


[failure] 169-169: Table column count
docs/middleware/proxy.md:169:118 MD056/table-column-count Table column count [Expected: 4; Actual: 3; Too few cells, row will be missing data] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md056.md


[failure] 169-169: Table pipe style
docs/middleware/proxy.md:169:118 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md

🪛 GitHub Actions: markdownlint
docs/middleware/proxy.md

[error] 169-169: markdownlint MD055/table-pipe-style: Table pipe style [Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe]

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: repeated
  • GitHub Check: Compare

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@ReneWerner87 ReneWerner87 merged commit fcd66f5 into main Aug 6, 2025
19 of 20 checks passed
@ReneWerner87 ReneWerner87 deleted the 2025-08-06-03-29-23 branch August 6, 2025 15:05
@github-project-automation github-project-automation bot moved this from In Progress to Done in v3 Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🤗 [Question]: Why delete "Connection header" at proxy.Do()?

3 participants