Skip to content

Canonical types and aliases - #101

Closed
jeremy wants to merge 1 commit into
mainfrom
aliases
Closed

jeremy wants to merge 1 commit into
mainfrom
aliases

Conversation

@jeremy

@jeremy jeremy commented Mar 6, 2024 •

Copy link
Copy Markdown
Member
  • MIME type aliases are now supported.
  • Aliases are resolved to their canonical type in all APIs.
  • Introduce MimeType.canonicalize type, instead_of: old to override a Tika canonical type with our own, essentially renaming the type and making the old type an alias of the new one. Common scenario with types like WAV with multiple competing historical types, RFCs that aren't actually followed, and browser support trumping them all. This allows us to give preference to browsers' actual MIME type support while keeping Tika's file extensions and magic byte matchers.
  • Warns when extending a type with preexisting extensions, parents, etc. and when extending an aliased type.

@jeremy
jeremy force-pushed the aliases branch 3 times, most recently from 216b5b3 to 075b285 Compare March 6, 2024 23:39
Marcel::MimeType.extend "application/vnd.apple.numbers", parents: "application/zip"
Marcel::MimeType.extend "application/vnd.apple.keynote", parents: "application/zip"

Marcel::MimeType.extend "audio/aac", extensions: %w( aac ), parents: "audio/x-aac"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This style of "overriding" a MIME type results in .aac files resolving to audio/aac but leaves existing magic bytes matches resolving to audio/x-aac.

Replaced with canonicalize "audio/aac", instead_of: "audio/x-aac".

Marcel::MimeType.extend "application/vnd.apple.keynote", extensions: %w( key ), parents: "application/zip"
Marcel::MimeType.extend "application/vnd.apple.pages", parents: "application/zip"
Marcel::MimeType.extend "application/vnd.apple.numbers", parents: "application/zip"
Marcel::MimeType.extend "application/vnd.apple.keynote", parents: "application/zip"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Including the file extensions suggests they're being extended, but they aren't. Omit them to clarify that we're changing the parent.

Marcel::MimeType.extend "audio/ogg", extensions: %w( ogg oga ), magic: [[0, 'OggS', [[29, 'vorbis']]]]
Marcel::MimeType.canonicalize "audio/aac", instead_of: "audio/x-aac"
Marcel::MimeType.canonicalize "audio/flac", instead_of: "audio/x-flac"
Marcel::MimeType.canonicalize "audio/x-wav", instead_of: "audio/vnd.wave"

@jeremy jeremy Mar 6, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Again, change the canonical type rather than introducing misleading MIME subtypes.


Marcel::MimeType.extend "image/vnd.dwg", magic: [[0, "AC10"]]
Marcel::MimeType.extend "audio/mpc", magic: [[0, "MPCKSH"]], extensions: %w( mpc )
Marcel::MimeType.extend "audio/ogg", extensions: %w( ogg oga ), magic: [[0, 'OggS', [[29, 'vorbis']]]]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Really odd one, breaking the MIME hierarchy entirely and using the same magic matcher as audio/vorbis. Leaving this for later.

Marcel::MimeType.extend "image/heic", magic: [[4, "ftypheic"]], extensions: %w( heic )
Marcel::MimeType.extend "image/avif", magic: [[4, "ftypavif"]]
Marcel::MimeType.extend "image/heif", magic: [[4, "ftypmif1"]]
Marcel::MimeType.extend "image/heic", magic: [[4, "ftypheic"]]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These matchers are already in the Tika data. Unclear whether these are simply defunct and can be removed.


Marcel::MimeType.extend "image/x-raw-sony", extensions: %w( arw ), parents: "image/tiff"
Marcel::MimeType.extend "image/x-raw-canon", extensions: %w( cr2 crw ), parents: "image/tiff"
Marcel::MimeType.extend "image/x-raw-canon", parents: "image/tiff"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Drop duplicate extensions.

Marcel::MimeType.extend "video/mp4", magic: [[4, "ftypisom"], [4, "ftypM4V "]], extensions: %w( mp4 m4v )

Marcel::MimeType.extend "audio/flac", magic: [[0, 'fLaC']], extensions: %w( flac ), parents: "audio/x-flac"
Marcel::MimeType.extend "audio/x-wav", magic: [[0, 'RIFF', [[8, 'WAVE']]]], extensions: %w( wav ), parents: "audio/vnd.wav"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Switched to canonicalize.

test "detects #{content_type} given magic bytes from #{name} and aliased type #{aliased}" do
assert_equal content_type, Marcel::MimeType.for(file, declared_type: aliased)
end
end

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Test that declared_type: alias will resolve to the canonical type. This what'll allow browser uploads using alias types to be correctly identified.

Comment thread test/magic_test.rb
Marcel::Magic.add('canonical/child', aliases: 'alias/child', parents: 'canonical/parent')

assert Marcel::Magic.child?('alias/child', 'alias/parent')
end

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Arguments may be alias types, so check that they're resolved to canonicals before checking parentage.

Comment thread lib/marcel/mime_type.rb
existing_magic = Marcel::MAGIC.select { |type, _| type == type }.map(&:last)
if magic.any? && magic == existing_magic
warn "#{type} already has magic matchers #{magic.inspect}"
end

@jeremy jeremy Mar 6, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is all pretty grotesque. Trying to limit the API footprint and not mess with the carefully tuned data tables.

@jeremy jeremy changed the title Respect MIME type aliases Canonical types and aliases Mar 7, 2024
* MIME type aliases are now supported.
* Aliases are resolved to their canonical type in all APIs.
* Introduce `MimeType.canonicalize type, instead_of: old` to override
  a Tika canonical type with our own, essentially renaming the type
  and making the old type an alias of the new one. Common scenario
  with types like WAV with multiple competing historical types, RFCs
  that aren't actually followed, and browser support trumping them all.
  This allows us to give preference to browsers' actual MIME type
  support while keeping Tika's file extensions and magic byte matchers.
* Warns when extending a type with preexisting extensions, parents, etc.
  and when extending an aliased type.
@jeremy

jeremy commented Aug 20, 2026

Copy link
Copy Markdown
Member Author

Superseded by #171 — the same design re-implemented on current main: the merge base here predates the HTML rework (#146/#158), the validating generator (#157), the declared-type validation (#160), and the zip sniffer (#169), so a rebase was no longer meaningful. #171 carries everything over (TYPE_ALIASES table, canonicalize with instead_of:, alias-aware child?, extend warnings) plus fixes surfaced in the port.

@jeremy jeremy closed this Aug 20, 2026
jeremy added a commit that referenced this pull request Aug 20, 2026
Model Tika's type aliases instead of discarding them. The generated
tables gain a TYPE_ALIASES map (152 aliases extracted from the <alias>
elements, downcased to match Marcel's lowercase-normalized lookups),
and alias resolution reaches every API:

* Declared types resolve to their canonical type, so a client-supplied
  application/javascript comes back text/javascript, audio/x-aac comes
  back audio/aac, and so on for the whole alias table.
* Magic.child? resolves aliases on both sides and through the parent
  graph, so parents declared via an alias (as Tika does for X bitmap's
  text/x-c) work without hand-maintained overrides — the custom.xml
  workaround pinning image/x-xbitmap to text/x-csrc is now unnecessary
  and removed, closing that file's standing TODO.
* Magic.canonical/by_type and Magic#canonical expose resolution
  directly. Class-level canonical is allocation-free for normalized
  input (measured: 0 allocations/call lowercase, and child? holds at
  its preexisting 2 — traversal array and visited hash) since child?
  resolves every node it visits.

Tika's alias direction is data, not policy: audited all 152 against
IANA registrations. Where Tika canonicalizes on a deprecated or
private-tree name, the IANA type wins via canonicalize:

* application/yaml over text/x-yaml (RFC 9512; the text/* and x-
  spellings all resolve to it)
* application/vnd.debian.binary-package over application/x-debian-
  package (IANA lists the latter as the deprecated alias)
* application/xliff+xml over application/x-xliff+xml (IANA-registered
  since 2018) — closes #84 with both spellings resolving to the
  standard type
* application/oxps is IANA-registered as its own format and not
  interoperable with Microsoft XPS, so Tika's alias to
  application/vnd.ms-xpsdocument is skipped at generation and
  application/oxps registered distinctly, keeping .xps with the
  Microsoft type and .oxps with OpenXPS

The remaining suspects (bat, matlab, aiff, xcf, java-properties, avi
families) have no IANA registration on either side; Tika's direction
stands. The wav family keeps audio/x-wav by deliberate browser-reality
choice, alongside audio/aac and audio/flac.

Introduce MimeType.canonicalize(type, instead_of:) to make those
overrides: the old type's extensions, magic matchers, parents, and
aliases re-register under the new name, and the old name becomes an
alias. This also replaces the hand-copied matcher/extension/parent
declarations for the audio types and fixes audio/x-wav's dangling
audio/vnd.wav parent (the Tika type is audio/vnd.wave).

Runtime alias registration holds the same invariants as the generator:
Magic.add downcases alias keys, and rejects aliasing a registered type
(canonicalize is the sanctioned path, and it re-points existing
aliases itself) — so alias keys are never registered types, alias
values are never alias keys, and resolution is single-hop by
construction. MimeType.extend on an aliased type redirects to the
canonical type with a warning, so registrations land where lookups
resolve.

MimeType.extend also gains aliases: and warns on redundant extends —
re-declaring extensions, aliases, or parents the tables already have.
Those warnings flushed out definitions that newer Tika data had
absorbed (illustrator parent, iWork/avif/heif/heic/markdown
extensions); they're pruned so Marcel loads warning-free. There is
deliberately no duplicate-magic warning: matcher order determines
precedence, so re-registering an existing matcher legitimately
promotes it ahead of the generated tables, which is exactly how
avif/heif/heic outrank video/quicktime's broad ftyp match.

ALIAS_SKIPS also retains application/x-x509-ca-cert as a distinct
type, reused as the PEM-format certificate type under
application/x-x509-cert;format=pem. The generator rejects aliases that
would shadow a defined type, so the tables can't disagree with
themselves.

Parameterized aliases (image/vnd.dgn;ver=8) are modeled, but declared
types parse down to bare type/subtype, so they resolve through their
base type rather than the alias table.

Supersedes and closes #101. Closes #84.
jeremy added a commit that referenced this pull request Aug 20, 2026
Model Tika's type aliases instead of discarding them. The generated
tables gain a TYPE_ALIASES map (152 aliases extracted from the <alias>
elements, downcased to match Marcel's lowercase-normalized lookups),
and alias resolution reaches every API:

* Declared types resolve to their canonical type, so a client-supplied
  application/javascript comes back text/javascript, audio/x-aac comes
  back audio/aac, and so on for the whole alias table.
* Magic.child? resolves aliases on both sides and through the parent
  graph, so parents declared via an alias (as Tika does for X bitmap's
  text/x-c) work without hand-maintained overrides — the custom.xml
  workaround pinning image/x-xbitmap to text/x-csrc is now unnecessary
  and removed, closing that file's standing TODO.
* Magic.canonical/by_type and Magic#canonical expose resolution
  directly. Class-level canonical is allocation-free for normalized
  input (measured: 0 allocations/call lowercase, and child? holds at
  its preexisting 2 — traversal array and visited hash) since child?
  resolves every node it visits.

Tika's alias direction is data, not policy: audited all 152 against
IANA registrations. Where Tika canonicalizes on a deprecated or
private-tree name, the IANA type wins via canonicalize:

* application/yaml over text/x-yaml (RFC 9512; the text/* and x-
  spellings all resolve to it)
* application/vnd.debian.binary-package over application/x-debian-
  package (IANA lists the latter as the deprecated alias)
* application/xliff+xml over application/x-xliff+xml (IANA-registered
  since 2018) — closes #84 with both spellings resolving to the
  standard type
* application/oxps is IANA-registered as its own format and not
  interoperable with Microsoft XPS. The severance is generation-level:
  ALIAS_SKIPS drops Tika's alias, GLOB_REASSIGNMENTS moves the oxps
  glob to application/oxps (declared in data/custom.xml with its OPC
  parent), and the generator verifies the reassigned glob is claimed.
  Magic-only loading, forward lookup, reverse extension listings, and
  later re-extends of the Microsoft type all agree: .xps is Microsoft
  XPS, .oxps is OpenXPS.

The remaining suspects (bat, matlab, aiff, xcf, java-properties, avi
families) have no IANA registration on either side; Tika's direction
stands. The wav family keeps audio/x-wav by deliberate browser-reality
choice, alongside audio/aac and audio/flac.

Introduce MimeType.canonicalize(type, instead_of:) to make those
overrides: the old type's extensions, magic matchers, parents, and
aliases re-register under the new name, and the old name becomes an
alias. This also replaces the hand-copied matcher/extension/parent
declarations for the audio types and fixes audio/x-wav's dangling
audio/vnd.wav parent (the Tika type is audio/vnd.wave).

Runtime registration holds the generator's invariants, atomically:
Magic.add normalizes and validates the complete registration — alias
keys downcased, aliasing a registered type rejected with a pointer to
canonicalize — before mutating any table, so a rejected registration
leaves every registry untouched. Alias keys are never registered
types, alias values never alias keys, and resolution is single-hop by
construction. MimeType.extend normalizes its type at the mutation
boundary and redirects aliased types to their canonical with a
warning, so registrations land where lookups resolve regardless of
input case.

MimeType.extend also gains aliases: and warns on redundant extends —
re-declaring extensions, aliases, or parents the tables already have.
Those warnings flushed out definitions that newer Tika data had
absorbed (illustrator parent, iWork/avif/heif/heic/markdown
extensions); they're pruned so Marcel loads warning-free. There is
deliberately no duplicate-magic warning: matcher order determines
precedence, so re-registering an existing matcher legitimately
promotes it ahead of the generated tables, which is exactly how
avif/heif/heic outrank video/quicktime's broad ftyp match.

ALIAS_SKIPS also retains application/x-x509-ca-cert as a distinct
type, reused as the PEM-format certificate type under
application/x-x509-cert;format=pem. The generator rejects aliases that
would shadow a defined type, so the tables can't disagree with
themselves.

Parameterized aliases (image/vnd.dgn;ver=8) are modeled, but declared
types parse down to bare type/subtype, so they resolve through their
base type rather than the alias table.

Supersedes and closes #101. Closes #84.
jeremy added a commit that referenced this pull request Aug 20, 2026
Model Tika's type aliases instead of discarding them. The generated
tables gain a TYPE_ALIASES map (152 aliases extracted from the <alias>
elements, downcased to match Marcel's lowercase-normalized lookups),
and alias resolution reaches every API:

* Declared types resolve to their canonical type, so a client-supplied
  application/javascript comes back text/javascript, audio/x-aac comes
  back audio/aac, and so on for the whole alias table.
* Magic.child? resolves aliases on both sides and through the parent
  graph, so parents declared via an alias (as Tika does for X bitmap's
  text/x-c) work without hand-maintained overrides — the custom.xml
  workaround pinning image/x-xbitmap to text/x-csrc is now unnecessary
  and removed, closing that file's standing TODO.
* Magic.canonical/by_type and Magic#canonical expose resolution
  directly. Class-level canonical is allocation-free for normalized
  input (measured: 0 allocations/call lowercase, and child? holds at
  its preexisting 2 — traversal array and visited hash) since child?
  resolves every node it visits.

Tika's alias direction is data, not policy: audited all 152 against
IANA registrations. Where Tika canonicalizes on a deprecated or
private-tree name, the IANA type wins via canonicalize:

* application/yaml over text/x-yaml (RFC 9512; the text/* and x-
  spellings all resolve to it)
* application/vnd.debian.binary-package over application/x-debian-
  package (IANA lists the latter as the deprecated alias)
* application/xliff+xml over application/x-xliff+xml (IANA-registered
  since 2018) — closes #84 with both spellings resolving to the
  standard type
* application/oxps is IANA-registered as its own format and not
  interoperable with Microsoft XPS. The severance is generation-level:
  ALIAS_SKIPS drops Tika's alias, GLOB_REASSIGNMENTS moves the oxps
  glob to application/oxps (declared in data/custom.xml with its OPC
  parent), and the generator verifies the reassigned glob is claimed.
  Magic-only loading, forward lookup, reverse extension listings, and
  later re-extends of the Microsoft type all agree: .xps is Microsoft
  XPS, .oxps is OpenXPS.

The remaining suspects (bat, matlab, aiff, xcf, java-properties, avi
families) have no IANA registration on either side; Tika's direction
stands. The wav family keeps audio/x-wav by deliberate browser-reality
choice, alongside audio/aac and audio/flac.

Introduce MimeType.canonicalize(type, instead_of:) to make those
overrides: the old type's extensions, magic matchers, parents, and
aliases re-register under the new name, and the old name becomes an
alias. This also replaces the hand-copied matcher/extension/parent
declarations for the audio types and fixes audio/x-wav's dangling
audio/vnd.wav parent (the Tika type is audio/vnd.wave).

Runtime registration holds the generator's invariants, atomically:
Magic.add normalizes and validates the complete registration — alias
keys downcased, aliasing a registered type rejected with a pointer to
canonicalize — before mutating any table, so a rejected registration
leaves every registry untouched. Alias keys are never registered
types, alias values never alias keys, and resolution is single-hop by
construction. MimeType.extend normalizes its type at the mutation
boundary and redirects aliased types to their canonical with a
warning, so registrations land where lookups resolve regardless of
input case.

MimeType.extend also gains aliases: and warns on redundant extends —
re-declaring extensions, aliases, or parents the tables already have.
Those warnings flushed out definitions that newer Tika data had
absorbed (illustrator parent, iWork/avif/heif/heic/markdown
extensions); they're pruned so Marcel loads warning-free. There is
deliberately no duplicate-magic warning: matcher order determines
precedence, so re-registering an existing matcher legitimately
promotes it ahead of the generated tables, which is exactly how
avif/heif/heic outrank video/quicktime's broad ftyp match.

ALIAS_SKIPS also retains application/x-x509-ca-cert as a distinct
type, reused as the PEM-format certificate type under
application/x-x509-cert;format=pem. The generator rejects aliases that
would shadow a defined type, so the tables can't disagree with
themselves.

Parameterized aliases (image/vnd.dgn;ver=8) are modeled, but declared
types parse down to bare type/subtype, so they resolve through their
base type rather than the alias table.

Supersedes and closes #101. Closes #84.
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.

1 participant