Skip to content

Add MIME Type Exclusions for Transparent Output Compression#91

Open
stfast wants to merge 4 commits into
kjdev:masterfrom
stfast:mime-exclude
Open

Add MIME Type Exclusions for Transparent Output Compression#91
stfast wants to merge 4 commits into
kjdev:masterfrom
stfast:mime-exclude

Conversation

@stfast

@stfast stfast commented Jun 8, 2026

Copy link
Copy Markdown

Add MIME Type Exclusions for Transparent Output Compression

Summary

This patch adds a new INI directive:

brotli.output_compression_exclude_types

The directive allows administrators to exclude selected MIME types from transparent Brotli output compression.

Examples

brotli.output_compression_exclude_types="image/*,video/*,audio/*"
brotli.output_compression_exclude_types="application/pdf,application/zip"

Supported Matching

  • Exact MIME matches

    • application/pdf
    • application/zip
  • Wildcard family matches

    • image/*
    • video/*
    • audio/*

Motivation

Many binary formats are already compressed and provide little or no benefit from additional Brotli compression while still consuming CPU resources.

This feature allows administrators to keep transparent output compression enabled while excluding selected content types.

Changes

  • Added brotli.output_compression_exclude_types INI directive
  • Added MIME matching logic to output handler
  • Added documentation and configuration examples to README
  • No changes to existing behavior when the directive is unset

Summary by CodeRabbit

  • New Features

    • Added brotli.output_compression_exclude_types configuration to exclude specific MIME types from transparent Brotli output compression. Accepts comma-separated values with exact MIME matches and type/* wildcard family patterns (case-insensitive).
  • Documentation

    • Expanded configuration docs with detailed syntax, examples, and guidance for the new exclusion option.

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ae7cdd79-6043-491c-82a9-b326471ac0a8

📥 Commits

Reviewing files that changed from the base of the PR and between fd69609 and aa799e8.

📒 Files selected for processing (1)
  • brotli.c
🚧 Files skipped from review as they are similar to previous changes (1)
  • brotli.c

📝 Walkthrough

Walkthrough

Adds a new INI option brotli.output_compression_exclude_types and implements MIME-type matching to skip Brotli output compression for configured types; updates module globals, output handler logic, INI registration, and README documentation.

Changes

MIME Type Exclusion Configuration

Layer / File(s) Summary
Module global and INI configuration
php_brotli.h, brotli.c
New module global output_compression_exclude_types is declared, initialized to NULL, and registered as the INI entry brotli.output_compression_exclude_types (string, default "") with OnUpdateString.
Exclusion matching and handler integration
brotli.c
php_brotli_output_mimetype_excluded() extracts the current SAPI mimetype and checks it against the comma-separated exclusion list with case-insensitive exact matches and type/* wildcard prefix matches; php_brotli_output_handler checks this at PHP_OUTPUT_HANDLER_START and returns FAILURE to skip compression when matched.
Configuration documentation
README.md
Adds the new option to the "Output handler option" table and a detailed section describing comma-separated MIME-type patterns, exact and type/* wildcard matching, and example ini values.

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I sniff the config, soft and spry,
I hop where MIME types hide and lie,
Fonts and images, spared the squeeze,
Wildcards dance upon the breeze,
Brotli rests — the rabbit's shy.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding MIME type exclusions for transparent output compression, which aligns with the core feature introduced across all modified files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
brotli.c

brotli.c:5:10: fatal error: 'php.h' file not found
5 | #include <php.h>
| ^~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/aa799e80bcb9664f9a5ece59611c6bae2602ee50-ee952d2e15e0324e/tmp/clang_command_.tmp.b0f5c5.txt
++Contents of '/tmp/coderabbit-infer/aa799e80bcb9664f9a5ece59611c6bae2602ee50-ee952d2e15e0324e/tmp/clang_command_.tmp.b0f5c5.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknown-linux-gnu" "-emit-obj" "-mrelax-all" "-disable-free"
"-clear-ast-before-backend" "-disabl

... [truncated 640 characters] ...

tem"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/lib/clang/18/include"
"-internal-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/ee952d2e15e0324e/file.o" "-x" "c" "brotli.c" "-O0"
"-fno-builtin" "-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"


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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
brotli.c (1)

384-432: ⚡ Quick win

Add PHPT coverage for exact/wildcard exclusions with parameterized Content-Type.

This new path should have explicit tests for: exact match (application/pdf), wildcard (image/*), and exact match with parameters (text/html; charset=UTF-8) to prevent regressions in handler start logic.

Also applies to: 554-557

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@brotli.c` around lines 384 - 432, Add PHPT tests exercising
php_brotli_output_mimetype_excluded for (1) exact match (e.g.
BROTLI_G(output_compression_exclude_types)="application/pdf" with
SG(sapi_headers).mimetype="application/pdf"), (2) wildcard match (e.g. "image/*"
matching "image/png"), and (3) exact match with parameters (e.g.
SG(sapi_headers).mimetype="text/html; charset=UTF-8" matching "text/html");
ensure tests set BROTLI_G(output_compression_exclude_types) and
SG(sapi_headers).mimetype and assert the function returns true for excluded
types and false for non-matching types to prevent regressions in handler start
logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@brotli.c`:
- Around line 395-424: The code currently uses mimetype_len = strlen(mimetype)
so comparisons against entries in exclude (e.g. "text/html") fail when the
Content-Type has parameters like "text/html; charset=UTF-8"; change the logic to
compute mimetype_len as the length of the media type only (stop at the first ';'
or trailing whitespace), then use that trimmed mimetype_len for the existing
comparisons (including the wildcard branch that computes prefix_len and the
exact token_len match), updating any uses of mimetype_len in the block that
checks p/token_len and strncasecmp to compare only the media type portion.

---

Nitpick comments:
In `@brotli.c`:
- Around line 384-432: Add PHPT tests exercising
php_brotli_output_mimetype_excluded for (1) exact match (e.g.
BROTLI_G(output_compression_exclude_types)="application/pdf" with
SG(sapi_headers).mimetype="application/pdf"), (2) wildcard match (e.g. "image/*"
matching "image/png"), and (3) exact match with parameters (e.g.
SG(sapi_headers).mimetype="text/html; charset=UTF-8" matching "text/html");
ensure tests set BROTLI_G(output_compression_exclude_types) and
SG(sapi_headers).mimetype and assert the function returns true for excluded
types and false for non-matching types to prevent regressions in handler start
logic.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9de13b90-f53b-4373-be4a-049c35285bf6

📥 Commits

Reviewing files that changed from the base of the PR and between 5cd25a6 and fd69609.

📒 Files selected for processing (3)
  • README.md
  • brotli.c
  • php_brotli.h

Comment thread brotli.c Outdated

@kjdev kjdev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please add a test.

Comment thread brotli.c
}


static int php_brotli_output_mimetype_excluded(void)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The indentation is set to tabs, so please change it to 4 spaces.

Comment thread brotli.c
return BROTLI_G(compression_coding);
}


Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Extra line breaks (removed)

Comment thread brotli.c
return FAILURE;
}


Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Extra line breaks (removed)

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