Skip to content

Commit b95560e

Browse files
noamrchromium-wpt-export-bot
authored andcommitted
Use the frame Accept header for prefetches.
Special-casing prefetches makes it so that `Vary: Accept` makes the browser erroneously reject the prefetched response. Also changed the SXG ?q= value for the frame accept header, as it was done for prefetch (and should have been done for the frame accept header). For now CORS prefetches remain untouched, as changing them to the >128bytes frame accept headers would cause incompatibility due to a CORS-preflight requirement. This was previously unspecified, See whatwg/fetch#1485 Bug: 626081 Change-Id: I2c99f4f1abd2556fdf456d877588b346a22fd677
1 parent c29a034 commit b95560e

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

preload/prefetch-accept.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<title>Ensures that prefetch works with documents</title>
3+
<script src="/resources/testharness.js"></script>
4+
<script src="/resources/testharnessreport.js"></script>
5+
<script src="/common/utils.js"></script>
6+
<script src="/common/dispatcher/dispatcher.js"></script>
7+
<script src="resources/prefetch-helper.js"></script>
8+
<body>
9+
<script>
10+
11+
promise_test(async t => {
12+
const {href, uid} = await prefetch({
13+
file: "prefetch-exec.html",
14+
type: "text/html",
15+
origin: document.origin});
16+
const popup = window.open(href + "&cache_bust=" + token());
17+
const remoteContext = new RemoteContext(uid);
18+
t.add_cleanup(() => popup.close());
19+
await remoteContext.execute_script(() => "OK");
20+
const results = await get_prefetch_info(href);
21+
assert_equals(results.length, 2);
22+
assert_equals(results[0].headers.accept, results[1].headers.accept);
23+
}, "Document prefetch should send the exact Accept header as navigation")
24+
25+
</script>
26+
</body>

preload/resources/prefetch-exec.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>Message BC</title>
4+
<script src="/common/dispatcher/dispatcher.js"></script>
5+
<script>
6+
"use strict";
7+
const params = new URLSearchParams(location.search);
8+
window.executor = new Executor(params.get("key"));
9+
</script>

preload/resources/prefetch-helper.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
async function get_prefetch_info(href) {
2+
const response = await fetch(`${href}&mode=info`, {mode: "cors"});
3+
return await response.json();
4+
}
5+
6+
async function prefetch(p = {}, t) {
7+
const link = document.createElement("link");
8+
link.rel = "prefetch";
9+
link.as = p.as;
10+
if (p.crossOrigin)
11+
link.setAttribute("crossorigin", p.crossOrigin);
12+
const uid = token();
13+
const params = new URLSearchParams();
14+
params.set("key", uid);
15+
for (const key in p)
16+
params.set(key, p[key]);
17+
const origin = p.origin || '';
18+
link.href = `${origin}/preload/resources/prefetch-info.py?${params.toString()}`;
19+
document.head.appendChild(link);
20+
while (!(await get_prefetch_info(link.href)).length) { }
21+
return {href: link.href, uid};
22+
}
23+

preload/resources/prefetch-info.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
from wptserve.utils import isomorphic_encode
3+
from json import dumps, loads
4+
5+
def main(request, response):
6+
key = request.GET.first(b"key").decode("utf8")
7+
mode = request.GET.first(b"mode", "content")
8+
status = int(request.GET.first(b"status", b"200"))
9+
stash = request.server.stash
10+
cors = request.GET.first(b"cors", "true")
11+
if cors == "true" or mode == b"info":
12+
response.headers.set(b"Access-Control-Allow-Origin", b"*")
13+
14+
response.status = status
15+
with stash.lock:
16+
requests = loads(stash.take(key) or '[]')
17+
if mode == b"info":
18+
response.headers.set(b"Content-Type", "application/json")
19+
json_reqs = dumps(requests)
20+
response.content = json_reqs
21+
stash.put(key, json_reqs)
22+
return
23+
else:
24+
headers = {}
25+
for header in request.headers:
26+
headers[header.decode("utf8")] = request.headers[header].decode("utf8")
27+
path = request.url
28+
requests.append({"headers": headers, "url": request.url})
29+
stash.put(key, dumps(requests))
30+
31+
response.headers.set(b"Content-Type", request.GET.first(b"type", "text/plain"))
32+
response.headers.set(b"Cache-Control", request.GET.first(b"cache-control", b"max-age: 604800"))
33+
if b"file" in request.GET:
34+
path = os.path.join(os.path.dirname(isomorphic_encode(__file__)), request.GET.first(b"file"));
35+
response.content = open(path, mode=u'rb').read()
36+
else:
37+
return request.GET.first(b"content", "123")

0 commit comments

Comments
 (0)