Skip to content

Commit b1a4883

Browse files
noamrmoz-wptsync-bot
authored andcommitted
Bug 1805352 [wpt PR 37469] - Use the frame Accept header for prefetches., a=testonly
Automatic update from web-platform-tests 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 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4069726 Reviewed-by: Matt Menke <[email protected]> Commit-Queue: Noam Rosenthal <[email protected]> Auto-Submit: Noam Rosenthal <[email protected]> Cr-Commit-Position: refs/heads/main@{#1083806} -- wpt-commits: 9eb3b189dae1f06fed09df71442227527b860c6b wpt-pr: 37469
1 parent b9c10b1 commit b1a4883

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
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>
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>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
if (p.crossOrigin)
10+
link.setAttribute("crossorigin", p.crossOrigin);
11+
const uid = token();
12+
const params = new URLSearchParams();
13+
params.set("key", uid);
14+
for (const key in p)
15+
params.set(key, p[key]);
16+
const origin = p.origin || '';
17+
link.href = `${origin}/preload/resources/prefetch-info.py?${params.toString()}`;
18+
document.head.appendChild(link);
19+
while (!(await get_prefetch_info(link.href)).length) { }
20+
return {href: link.href, uid};
21+
}
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, value in request.headers.items():
26+
headers[header.decode("utf8")] = value[0].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__)), os.path.basename(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)