| 1 |
#!/usr/bin/env python |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import argparse |
| 5 |
import http.client |
| 6 |
import re |
| 7 |
from contextlib import closing |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
module_dir = Path(__file__).parent.resolve() |
| 11 |
media_types_py = module_dir / "../src/whitenoise/media_types.py" |
| 12 |
|
| 13 |
|
| 14 |
def main() -> int: |
| 15 |
parser = argparse.ArgumentParser() |
| 16 |
parser.add_argument("--check", action="store_true") |
| 17 |
args = parser.parse_args() |
| 18 |
|
| 19 |
func_str = get_default_types_function() |
| 20 |
text = media_types_py.read_text() |
| 21 |
new_text = re.sub( |
| 22 |
r"def default_types.*\}", |
| 23 |
func_str, |
| 24 |
text, |
| 25 |
flags=re.DOTALL, |
| 26 |
) |
| 27 |
if new_text != text: |
| 28 |
if args.check: |
| 29 |
print("Would write changes") |
| 30 |
return 1 |
| 31 |
else: |
| 32 |
print(f"Writing {media_types_py}") |
| 33 |
media_types_py.write_text(new_text) |
| 34 |
return 0 |
| 35 |
|
| 36 |
|
| 37 |
EXTRA_MIMETYPES = { |
| 38 |
# nginx file uses application/javascript, but HTML specification recommends |
| 39 |
# text/javascript: |
| 40 |
".js": "text/javascript", |
| 41 |
".md": "text/markdown", |
| 42 |
".mjs": "text/javascript", |
| 43 |
".woff": "application/font-woff", |
| 44 |
".woff2": "font/woff2", |
| 45 |
"apple-app-site-association": "application/pkc7-mime", |
| 46 |
# Adobe Products - see: |
| 47 |
# https://www.adobe.com/devnet-docs/acrobatetk/tools/AppSec/xdomain.html#policy-file-host-basics |
| 48 |
"crossdomain.xml": "text/x-cross-domain-policy", |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
FUNCTION_TEMPLATE = '''\ |
| 53 |
def default_types() -> dict[str, str]: |
| 54 |
""" |
| 55 |
We use our own set of default media types rather than the system-supplied |
| 56 |
ones. This ensures consistent media type behaviour across varied |
| 57 |
environments. The defaults are based on those shipped with nginx, with |
| 58 |
some custom additions. |
| 59 |
|
| 60 |
(Auto-generated by scripts/generate_default_media_types.py) |
| 61 |
""" |
| 62 |
return {{ |
| 63 |
{entries} |
| 64 |
}}''' |
| 65 |
|
| 66 |
|
| 67 |
def get_default_types_function() -> str: |
| 68 |
types_map = get_types_map() |
| 69 |
lines = [ |
| 70 |
f' "{suffix}": "{media_type}",' # noqa: B028 |
| 71 |
for suffix, media_type in types_map.items() |
| 72 |
] |
| 73 |
return FUNCTION_TEMPLATE.format(entries="\n".join(lines)) |
| 74 |
|
| 75 |
|
| 76 |
def get_types_map() -> dict[str, str]: |
| 77 |
nginx_data = get_nginx_data() |
| 78 |
matches = re.findall(r"(\w+/.*?)\s+(.*?);", nginx_data) |
| 79 |
types_map = {} |
| 80 |
for match in matches: |
| 81 |
media_type = match[0] |
| 82 |
# This is the default media type anyway, no point specifying |
| 83 |
# it explicitly |
| 84 |
if media_type == "application/octet-stream": |
| 85 |
continue |
| 86 |
|
| 87 |
extensions = match[1].split() |
| 88 |
for extension in extensions: |
| 89 |
types_map[f".{extension}"] = media_type |
| 90 |
types_map.update(EXTRA_MIMETYPES) |
| 91 |
return dict(sorted(types_map.items())) |
| 92 |
|
| 93 |
|
| 94 |
def get_nginx_data() -> str: |
| 95 |
conn = http.client.HTTPSConnection("raw.githubusercontent.com") |
| 96 |
with closing(conn): |
| 97 |
conn.request("GET", "/nginx/nginx/master/conf/mime.types") |
| 98 |
response = conn.getresponse() |
| 99 |
assert response.status == 200 |
| 100 |
return response.read().decode() |
| 101 |
|
| 102 |
|
| 103 |
if __name__ == "__main__": |
| 104 |
raise SystemExit(main()) |