1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#!/usr/bin/env python3
import hashlib
import importlib.util
from pathlib import Path
import tempfile
spec = importlib.util.spec_from_file_location('prefix', Path(__file__).resolve().parents[2] / 'web/prefix.py')
p = importlib.util.module_from_spec(spec)
spec.loader.exec_module(p)
prefix = '/sites/fixture'
original = '''<!DOCTYPE html><html><head><style>
/* url(/comment) */
a[href="/blog/"]{background:url('/assets/a.png');content:"/ordinary"}
@font-face{src:url(/fonts/a.woff2)}
@import "/main.css";
.svg{background:url("data:image/svg+xml,%3Csvg viewBox='0 0 10 10'%3E")}
</style></head><body>
<a href="/blog/post?q=1&x=2#part">go</a><img src="/img.png" srcset="/one.png 1x, /two.png 2x">
<a href="//elsewhere/foo">external</a><a href="https://other/x">external</a>
<a href="#part">fragment</a><img src="data:image/png;base64,abcd">
<a href="../page.html">relative</a><div style="background:url(/img.png)"></div>
<code>href="/example"</code><!-- <a href="/comment"> -->
<script>const s = '/assets/unmodified.js'; fetch('/api/pokes');</script>
<script type="application/json">{"url":"/unmodified"}</script>
</body></html>'''
rewritten = p.html(original, prefix)
assert 'srcset=" /sites/fixture/a.png 1x, /sites/fixture/b.png 2x"' in p.html('<img srcset=" /a.png 1x, /b.png 2x">', prefix)
assert 'url("/sites/fixture/a.png")' in p.html('<div style="background:url("/a.png")"></div>', prefix)
assert p.srcset('data:image/svg+xml,/untouched 1x, /a.png 2x', prefix) == 'data:image/svg+xml,/untouched 1x, /sites/fixture/a.png 2x'
for piece in ['href="/sites/fixture/blog/post?q=1&x=2#part"',
'srcset="/sites/fixture/one.png 1x, /sites/fixture/two.png 2x"',
'url(/sites/fixture/fonts/a.woff2)', 'a[href="/sites/fixture/blog/"]',
'@import "/sites/fixture/main.css"']:
assert piece in rewritten, piece
for piece in ['/* url(/comment) */', 'content:"/ordinary"', '<code>href="/example"</code>',
"fetch('/api/pokes')", "const s = '/assets/unmodified.js'", '{"url":"/unmodified"}',
'<!-- <a href="/comment"> -->', 'href="../page.html"', 'href="#part"',
'href="//elsewhere/foo"', 'href="https://other/x"', 'src="data:image/png;base64,abcd"',
'url("data:image/svg+xml,%3Csvg viewBox=\'0 0 10 10\'%3E")']:
assert piece in rewritten, piece
for invalid in ['', '/', '//host', '/a/../b', '/a?b', '/a#b', 'http://a']:
try:
p.check_prefix(invalid)
raise AssertionError(invalid)
except ValueError:
pass
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
source, out = root/'normal', root/'preview'
source.mkdir()
(source/'index.html').write_text(original)
(source/'app.js').write_text("fetch('/api/pokes')")
def hashes():
return {str(f.relative_to(source)): hashlib.sha256(f.read_bytes()).hexdigest() for f in source.rglob('*') if f.is_file()}
before = hashes()
p.publish(source, out, prefix)
assert hashes() == before
assert out.is_symlink() and (out/'index.html').read_text() == rewritten
assert (out/'app.js').read_bytes() == (source/'app.js').read_bytes()
previous = out.resolve()
(source/'app.js').unlink()
p.publish(source, out, prefix)
assert out.resolve() != previous and previous.exists()
assert not (out/'app.js').exists(), 'stale files must not leak into next generation'
(source/'escape').symlink_to('/etc/hostname')
current = out.resolve()
try:
p.publish(source, out, prefix)
raise AssertionError('symlink accepted')
except ValueError:
pass
assert out.resolve() == current, 'failed generation changed published output'
print('prefix: scoped HTML/CSS, unchanged JS/text/source, atomic publication and failure preservation passed')
|