Skip to content

fix(mime): GetExtType accepts an extension with or without the dot - #637

Draft
edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/mime-ext-type
Draft

edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/mime-ext-type

Conversation

@edsontmarques

Copy link
Copy Markdown

Problem and change

THorseMimeTypes.GetExtType returns application/octet-stream for an extension such as '.css' on FPC, and for an extension without the dot ('css') on every compiler that uses THorseMimeTypesExt (FPC, and Delphi up to 10.2).

THorseMimeTypesExt.NormalizeExt runs ExtractFileExt on the value it receives:

  • on FPC, ExtractFileExt('.css') is '', because FPC reads .css as a hidden file without extension;
  • ExtractFileExt('css') is '' everywhere.

So a static file served by extension (for example, a caller passing ExtractFileExt(FileName)) gets the wrong type, and the browser refuses the style sheet. Passing the whole file name works, which is why callers have been working around it.

This patch reads a value with no dot after its first character as an extension ('.css', 'css', ' .CSS '). Anything else is a file name, and its extension comes from ExtractFileExt as before ('style.css', 'dir/x.min.js'). The value is trimmed and lower-cased first.

GetFileType now always extracts the extension from the file name, so its result does not change for file names. A hidden file ('.css') and a name without extension ('pdf') still give application/octet-stream.

The System.Net.Mime path (Delphi 10.3 and later) is unchanged.

Validation performed

FPC 3.3.1-22012-g6564bb9b34-unpushed, Linux x86_64.

Check Result
New tests/fpc-mime: extensions with and without the dot, upper case, spaces, file names, hidden file, name without extension, unknown and empty Pass
Same check against the sources at 41c4b1c (this PR's base) Fails on five extension cases: .css, .js, css, .CSS, .CSS
Existing regressions in tests.yml Pass

Reproduce from the repository root:

sh tests/fpc-mime/run-mime-test.sh

The script is registered in .github/workflows/tests.yml, after the URL decoding regression.

On our fork, an end-to-end check serves a static .css through Horse on FPC. It now answers Content-Type: text/css; before, it answered application/octet-stream. Our Delphi 10.2 projects that use Horse also build without errors.

Notes

  • The MIME table defines json twice. The later entry, application/vnd.embarcadero.firedac+json, wins over application/json. vcf is also defined twice. This patch does not change the table: with it, GetExtType('.json') on FPC returns the same type that GetFileType('x.json') already returned. That is left for a separate change.
  • Not run: FPC 3.2.2, Windows, and the Delphi 10.3+ System.Net.Mime path.

🤖 Generated with Claude Code

GetExtType ran ExtractFileExt on the value it received. On FPC
ExtractFileExt('.css') is '' (a hidden file), and ExtractFileExt('css')
is '' on every compiler, so both came back as octet-stream and a static
.css served by extension broke in the browser. A value with no dot after
its first character is now read as an extension; anything else is a file
name. GetFileType always extracts the extension from the file name, so
its result does not change.

tests/fpc-mime fails on five extension cases against the previous source
and passes with the fix; it runs in the tests workflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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