Skip to content

refactor: replace filterFlags with mime.ParseMediaType - #4758

Open
wanghaolong613 wants to merge 1 commit into
gin-gonic:masterfrom
wanghaolong613:refactor/filterFlags
Open

refactor: replace filterFlags with mime.ParseMediaType#4758
wanghaolong613 wants to merge 1 commit into
gin-gonic:masterfrom
wanghaolong613:refactor/filterFlags

Conversation

@wanghaolong613

@wanghaolong613 wanghaolong613 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Pull Request Checklist

Please ensure your pull request meets the following requirements:

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions).
  • Tests are added or modified as needed to cover code changes.
  • If the pull request introduces a new feature, the feature is documented in the docs/doc.md.

Changes

Replace handmade filterFlags simple string truncation logic with standard library mime.ParseMediaType to parse Content-Type header.

Purpose

  • Fix multiple parsing defects of filterFlags: leading whitespace, case sensitivity, incomplete whitespace handling
  • Follow RFC standard for MIME type parsing
  • Unify media type to lowercase automatically
  • Gain better compatibility with malformed HTTP request headers from clients

Testing

Added test cases to verify parsing behavior for:

  • Leading / trailing whitespaces
  • Case-insensitive media types
  • Various irregular whitespace layouts
  • Malformed and incomplete Content-Type headers

@rashmi-tondare rashmi-tondare left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good direction since mime.ParseMediaType fixes real defects in the old scan (leading whitespace, case sensitivity). One thing to check though: ParseMediaType can return a non-empty mediatype and a non-nil error (ErrInvalidMediaParameter) for headers with a valid base type but a malformed param. The current err != nil fallback doesn't handle that correctly.

Also, could use some table-driven tests in utils_test.go for the cases this is meant to fix (whitespace, case, missing space before ;), plus a couple of malformed-header cases to pin down the fallback behavior.

@wanghaolong613

Copy link
Copy Markdown
Contributor Author

Good direction since mime.ParseMediaType fixes real defects in the old scan (leading whitespace, case sensitivity). One thing to check though: ParseMediaType can return a non-empty mediatype and a non-nil error (ErrInvalidMediaParameter) for headers with a valid base type but a malformed param. The current err != nil fallback doesn't handle that correctly.

Also, could use some table-driven tests in utils_test.go for the cases this is meant to fix (whitespace, case, missing space before ;), plus a couple of malformed-header cases to pin down the fallback behavior.

OK, I'll fix it

@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.33%. Comparing base (3dc1cd6) to head (8060370).
⚠️ Report is 289 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4758      +/-   ##
==========================================
- Coverage   99.21%   98.33%   -0.89%     
==========================================
  Files          42       48       +6     
  Lines        3182     3174       -8     
==========================================
- Hits         3157     3121      -36     
- Misses         17       43      +26     
- Partials        8       10       +2     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.31% <100.00%> (?)
-tags go_json 98.25% <100.00%> (?)
-tags nomsgpack 98.30% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.25 98.33% <100.00%> (?)
go-1.26 98.33% <100.00%> (?)
macos-latest 98.33% <100.00%> (-0.89%) ⬇️
ubuntu-latest 98.33% <100.00%> (-0.89%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 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.

@rashmi-tondare

Copy link
Copy Markdown

Changes look good to me! Just one small thing, would be nice to add the pull request template in the PR description and tick off appropriate items from the checklist.

@wanghaolong613

Copy link
Copy Markdown
Contributor Author

Changes look good to me! Just one small thing, would be nice to add the pull request template in the PR description and tick off appropriate items from the checklist.

Thanks for the feedback! I'll add the PR template to the description and tick off the relevant checklist items right away.

@rashmi-tondare rashmi-tondare left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM!
@appleboy this fix looks sound to me. It contains tests for the fixed issue as well and I believe would be a good addition to the lib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants