Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch: send 'sec-purpose' header for prefetch requests #3694

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1449,14 +1449,21 @@ async function httpNetworkOrCacheFetch (
// 13. Append the Fetch metadata headers for httpRequest. [FETCH-METADATA]
appendFetchMetadata(httpRequest)

// 14. If httpRequest’s header list does not contain `User-Agent`, then
// 14. If httpRequest’s initiator is "prefetch", then set a structured
// field value given (`Sec-Purpose`, the token 'prefetch') in httpRequest’s
// header list.
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
if (httpRequest.initiator === 'prefetch') {
httpRequest.headersList.append('sec-purpose', 'prefetch', true)
}

// 15. If httpRequest’s header list does not contain `User-Agent`, then
// user agents should append `User-Agent`/default `User-Agent` value to
// httpRequest’s header list.
if (!httpRequest.headersList.contains('user-agent', true)) {
httpRequest.headersList.append('user-agent', defaultUserAgent, true)
}

// 15. If httpRequest’s cache mode is "default" and httpRequest’s header
// 16. If httpRequest’s cache mode is "default" and httpRequest’s header
// list contains `If-Modified-Since`, `If-None-Match`,
// `If-Unmodified-Since`, `If-Match`, or `If-Range`, then set
// httpRequest’s cache mode to "no-store".
Expand All @@ -1471,7 +1478,7 @@ async function httpNetworkOrCacheFetch (
httpRequest.cache = 'no-store'
}

// 16. If httpRequest’s cache mode is "no-cache", httpRequest’s prevent
// 17. If httpRequest’s cache mode is "no-cache", httpRequest’s prevent
// no-cache cache-control header modification flag is unset, and
// httpRequest’s header list does not contain `Cache-Control`, then append
// `Cache-Control`/`max-age=0` to httpRequest’s header list.
Expand All @@ -1483,7 +1490,7 @@ async function httpNetworkOrCacheFetch (
httpRequest.headersList.append('cache-control', 'max-age=0', true)
}

// 17. If httpRequest’s cache mode is "no-store" or "reload", then:
// 18. If httpRequest’s cache mode is "no-store" or "reload", then:
if (httpRequest.cache === 'no-store' || httpRequest.cache === 'reload') {
// 1. If httpRequest’s header list does not contain `Pragma`, then append
// `Pragma`/`no-cache` to httpRequest’s header list.
Expand All @@ -1498,13 +1505,13 @@ async function httpNetworkOrCacheFetch (
}
}

// 18. If httpRequest’s header list contains `Range`, then append
// 19. If httpRequest’s header list contains `Range`, then append
// `Accept-Encoding`/`identity` to httpRequest’s header list.
if (httpRequest.headersList.contains('range', true)) {
httpRequest.headersList.append('accept-encoding', 'identity', true)
}

// 19. Modify httpRequest’s header list per HTTP. Do not append a given
// 20. Modify httpRequest’s header list per HTTP. Do not append a given
// header if httpRequest’s header list contains that header’s name.
// TODO: https://github.com/whatwg/fetch/issues/1285#issuecomment-896560129
if (!httpRequest.headersList.contains('accept-encoding', true)) {
Expand All @@ -1517,7 +1524,7 @@ async function httpNetworkOrCacheFetch (

httpRequest.headersList.delete('host', true)

// 20. If includeCredentials is true, then:
// 21. If includeCredentials is true, then:
if (includeCredentials) {
// 1. If the user agent is not configured to block cookies for httpRequest
// (see section 7 of [COOKIES]), then:
Expand All @@ -1526,20 +1533,20 @@ async function httpNetworkOrCacheFetch (
// TODO: credentials
}

// 21. If there’s a proxy-authentication entry, use it as appropriate.
// 22. If there’s a proxy-authentication entry, use it as appropriate.
// TODO: proxy-authentication

// 22. Set httpCache to the result of determining the HTTP cache
// 23. Set httpCache to the result of determining the HTTP cache
// partition, given httpRequest.
// TODO: cache

// 23. If httpCache is null, then set httpRequest’s cache mode to
// 24. If httpCache is null, then set httpRequest’s cache mode to
// "no-store".
if (httpCache == null) {
httpRequest.cache = 'no-store'
}

// 24. If httpRequest’s cache mode is neither "no-store" nor "reload",
// 25. If httpRequest’s cache mode is neither "no-store" nor "reload",
// then:
if (httpRequest.cache !== 'no-store' && httpRequest.cache !== 'reload') {
// TODO: cache
Expand Down
Loading