Skip to content

Truly independent web browser

3.3 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script>
    asyncTest(async done => {
        const server = httpTestServer();
        const run = crypto.randomUUID();
        const firstGate = `${run}-first`;
        const secondGate = `${run}-second`;
        const path = name => `/speculative-html-parser-base-${run}-${name}`;
        const javascriptHeaders = { "Content-Type": "text/javascript" };
        const imageHeaders = { "Content-Type": "image/png" };

        const firstBlockerURL = await server.createEcho("GET", path("first-blocker.js"), {
            status: 200,
            headers: javascriptHeaders,
            body: "",
            wait_for_unblock: firstGate,
        });
        const secondBlockerURL = await server.createEcho("GET", path("second-blocker.js"), {
            status: 200,
            headers: javascriptHeaders,
            body: "",
            wait_for_unblock: secondGate,
        });
        const oldImageURL = await server.createEcho("GET", path("old/photo.png"), { status: 200, headers: imageHeaders, body: "" });
        const newImageURL = await server.createEcho("GET", path("new/photo.png"), { status: 200, headers: imageHeaders, body: "" });
        const oldBase = new URL(".", oldImageURL).href;
        const newBase = new URL(".", newImageURL).href;

        // While the parser waits for the first script, the speculative parser finds the image and resolves its URL
        // against the document's base URL. The script after it changes that base URL, so the speculative parser started
        // for the second script resolves the image's URL against the new one.
        const scriptStartTag = "<scr" + "ipt";
        const scriptEndTag = "</scr" + "ipt>";
        const pageBody = `<!DOCTYPE html>
            <base href="${oldBase}">
            ${scriptStartTag} src="${firstBlockerURL}">${scriptEndTag}
            ${scriptStartTag}>document.querySelector("base").href = "${newBase}";${scriptEndTag}
            ${scriptStartTag} src="${secondBlockerURL}">${scriptEndTag}
            <img src="photo.png">`;
        const pageURL = await server.createEcho("GET", path("page.html"), {
            status: 200,
            headers: { "Content-Type": "text/html" },
            body: pageBody,
        });

        const hasRecordedRequest = async url => {
            try {
                const response = await fetch(server.recordedRequestHeadersURL(url));
                return response.ok;
            } catch {
                return false;
            }
        };

        const frame = document.createElement("iframe");
        const frameLoaded = new Promise(resolve => (frame.onload = resolve));
        frame.src = pageURL;
        document.body.appendChild(frame);

        while (!(await hasRecordedRequest(oldImageURL))) {}
        println("PASS: speculative parser fetched the image against the first base URL while the first script was held");

        await fetch(`${server.baseURL}/unblock/${firstGate}`);

        // Only the speculative parser can request the image against the new base URL while the second script is held.
        while (!(await hasRecordedRequest(newImageURL))) {}
        println("PASS: speculative parser fetched the image against the new base URL while the second script was held");

        await fetch(`${server.baseURL}/unblock/${secondGate}`);
        await frameLoaded;
        done();
    });
</script>