Skip to content

Commit e17ff61

Browse files
committed
Switch to mime-db as source of extension => MIME map
The Apache MIME type map is not actively maintained anymore, so this switches to jshttp/mime-db, which seems to be the de-facto standard in this area now. This avoid the need to patch in our own MIME types over time. The preference algorithm is based on: https://github.com/jshttp/mime-types/blob/47b62ac45e9b176a2af35532d0eea4968bb9eb6d/index.js#L154 Closes GH-5764.
1 parent cdc4e49 commit e17ff61

File tree

2 files changed

+210
-28
lines changed

2 files changed

+210
-28
lines changed

sapi/cli/generate_mime_type_map.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,42 @@
33

44
// Check if we are being given a mime.types file or if we should use the
55
// default URL.
6-
$source = count($_SERVER['argv']) > 1 ? $_SERVER['argv'][1] : 'https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types';
6+
$source = count($argv) > 1 ? $argv[1] : 'https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.44.0/db.json';
77

88
// See if we can actually load it.
9-
$types = @file($source);
10-
if ($types === false) {
9+
$data = @file_get_contents($source);
10+
if ($data === false) {
1111
fprintf(STDERR, "Error: unable to read $source\n");
1212
exit(1);
1313
}
1414

15-
// Remove comments and flip into an extensions array.
15+
// Source preference from lowest to highest
16+
$prefs = ['nginx' => 1, 'apache' => 2, 'custom' => 3, 'iana' => 4];
17+
1618
$extensions = [];
17-
array_walk($types, function ($line) use (&$extensions) {
18-
$line = trim($line);
19-
if ($line && $line[0] != '#') {
20-
$fields = preg_split('/\s+/', $line);
21-
if (count($fields) > 1) {
22-
$mime = array_shift($fields);
23-
foreach ($fields as $extension) {
24-
$extensions[$extension] = $mime;
19+
$types = json_decode($data, true);
20+
foreach ($types as $mime => $info) {
21+
$source = $info['source'] ?? 'custom';
22+
$pref = $prefs[$source];
23+
foreach ($info['extensions'] ?? [] as $extension) {
24+
if (isset($extensions[$extension])) {
25+
// Use same preference rules as jshttp/mime-types
26+
[$oldMime, $oldPref] = $extensions[$extension];
27+
if ($oldMime !== 'application/octet-stream'
28+
&& ($oldPref > $pref
29+
|| ($oldPref === $pref && substr($oldMime, 0, 12) === 'application/'))) {
30+
continue;
2531
}
2632
}
33+
34+
$extensions[$extension] = [$mime, $pref];
2735
}
28-
});
36+
}
37+
38+
// Only keep the MIME type.
39+
$extensions = array_map(function($x) { return $x[0]; }, $extensions);
2940

3041
$additional_mime_maps = [
31-
"map" => "application/json", // from commit: a0d62f08ae8cbebc88e5c92e08fca8d0cdc7309d
3242
"jsm" => "application/javascript",
3343
];
3444

0 commit comments

Comments
 (0)