Bug 1417463 - Default accept header should follow the fetch spec, r=mayhemer

For navigation: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
For images: image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5
for style: text/css,*/*;q=0.1
Anything else: */*
This commit is contained in:
Andrea Marchesini 2019-01-08 23:36:16 +01:00
Родитель 049a2340a5
Коммит 5752cef527
9 изменённых файлов: 67 добавлений и 43 удалений

Просмотреть файл

@ -1739,9 +1739,6 @@ pref("network.http.request.max-start-delay", 10);
// If a connection is reset, we will retry it max-attempts times.
pref("network.http.request.max-attempts", 10);
// Headers
pref("network.http.accept.default", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
// Prefs allowing granular control of referers
// 0=don't send any, 1=send only on clicks, 2=send on image requests as well
pref("network.http.sendRefererHeader", 2);

Просмотреть файл

@ -682,8 +682,14 @@ class WellKnownChecker {
nsILoadInfo *loadInfo) {
uint64_t channelId;
nsLoadFlags flags;
nsContentPolicyType contentPolicyType =
loadInfo ? loadInfo->GetExternalContentPolicyType()
: nsIContentPolicy::TYPE_OTHER;
if (NS_FAILED(gHttpHandler->NewChannelId(channelId)) ||
NS_FAILED(chan->Init(uri, caps, nullptr, 0, nullptr, channelId)) ||
NS_FAILED(chan->Init(uri, caps, nullptr, 0, nullptr, channelId,
contentPolicyType)) ||
NS_FAILED(chan->SetAllowAltSvc(false)) ||
NS_FAILED(chan->SetRedirectMode(
nsIHttpChannelInternal::REDIRECT_MODE_ERROR)) ||

Просмотреть файл

@ -329,7 +329,8 @@ void HttpBaseChannel::SetFlashPluginState(
nsresult HttpBaseChannel::Init(nsIURI* aURI, uint32_t aCaps,
nsProxyInfo* aProxyInfo,
uint32_t aProxyResolveFlags, nsIURI* aProxyURI,
uint64_t aChannelId) {
uint64_t aChannelId,
nsContentPolicyType aContentPolicyType) {
LOG1(("HttpBaseChannel::Init [this=%p]\n", this));
MOZ_ASSERT(aURI, "null uri");
@ -376,7 +377,8 @@ nsresult HttpBaseChannel::Init(nsIURI* aURI, uint32_t aCaps,
rv = mRequestHead.SetHeader(nsHttp::Host, hostLine);
if (NS_FAILED(rv)) return rv;
rv = gHttpHandler->AddStandardRequestHeaders(&mRequestHead, isHTTPS);
rv = gHttpHandler->AddStandardRequestHeaders(&mRequestHead, isHTTPS,
aContentPolicyType);
if (NS_FAILED(rv)) return rv;
nsAutoCString type;

Просмотреть файл

@ -128,7 +128,8 @@ class HttpBaseChannel : public nsHashPropertyBag,
virtual MOZ_MUST_USE nsresult Init(nsIURI *aURI, uint32_t aCaps,
nsProxyInfo *aProxyInfo,
uint32_t aProxyResolveFlags,
nsIURI *aProxyURI, uint64_t aChannelId);
nsIURI *aProxyURI, uint64_t aChannelId,
nsContentPolicyType aContentPolicyType);
// nsIRequest
NS_IMETHOD GetName(nsACString &aName) override;

Просмотреть файл

@ -236,10 +236,6 @@ nsresult InterceptedHttpChannel::RedirectForResponseURL(
mResponseHead, mBodyReader, bodyCallback, mChannelCreationTime,
mChannelCreationTimestamp, mAsyncOpenTime);
rv = newChannel->Init(aResponseURI, mCaps,
static_cast<nsProxyInfo*>(mProxyInfo.get()),
mProxyResolveFlags, mProxyURI, mChannelId);
// If the response has been redirected, propagate all the URLs to content.
// Thus, the exact value of the redirect flag does not matter as long as it's
// not REDIRECT_INTERNAL.
@ -248,6 +244,15 @@ nsresult InterceptedHttpChannel::RedirectForResponseURL(
nsCOMPtr<nsILoadInfo> redirectLoadInfo =
CloneLoadInfoForRedirect(aResponseURI, flags);
nsContentPolicyType contentPolicyType =
redirectLoadInfo ? redirectLoadInfo->GetExternalContentPolicyType()
: nsIContentPolicy::TYPE_OTHER;
rv = newChannel->Init(
aResponseURI, mCaps, static_cast<nsProxyInfo*>(mProxyInfo.get()),
mProxyResolveFlags, mProxyURI, mChannelId, contentPolicyType);
newChannel->SetLoadInfo(redirectLoadInfo);
NS_ENSURE_SUCCESS(rv, rv);

Просмотреть файл

@ -376,9 +376,10 @@ void nsHttpChannel::ReleaseMainThreadOnlyReferences() {
nsresult nsHttpChannel::Init(nsIURI *uri, uint32_t caps, nsProxyInfo *proxyInfo,
uint32_t proxyResolveFlags, nsIURI *proxyURI,
uint64_t channelId) {
uint64_t channelId,
nsContentPolicyType aContentPolicyType) {
nsresult rv = HttpBaseChannel::Init(uri, caps, proxyInfo, proxyResolveFlags,
proxyURI, channelId);
proxyURI, channelId, aContentPolicyType);
if (NS_FAILED(rv)) return rv;
LOG1(("nsHttpChannel::Init [this=%p]\n", this));
@ -9562,9 +9563,13 @@ nsresult nsHttpChannel::RedirectToInterceptedChannel() {
InterceptedHttpChannel::CreateForInterception(
mChannelCreationTime, mChannelCreationTimestamp, mAsyncOpenTime);
nsresult rv = intercepted->Init(mURI, mCaps,
static_cast<nsProxyInfo *>(mProxyInfo.get()),
mProxyResolveFlags, mProxyURI, mChannelId);
nsContentPolicyType type = mLoadInfo
? mLoadInfo->GetExternalContentPolicyType()
: nsIContentPolicy::TYPE_OTHER;
nsresult rv = intercepted->Init(
mURI, mCaps, static_cast<nsProxyInfo *>(mProxyInfo.get()),
mProxyResolveFlags, mProxyURI, mChannelId, type);
nsCOMPtr<nsILoadInfo> redirectLoadInfo =
CloneLoadInfoForRedirect(mURI, nsIChannelEventSink::REDIRECT_INTERNAL);

Просмотреть файл

@ -138,11 +138,10 @@ class nsHttpChannel final : public HttpBaseChannel,
nsHttpChannel();
virtual MOZ_MUST_USE nsresult Init(nsIURI *aURI, uint32_t aCaps,
nsProxyInfo *aProxyInfo,
uint32_t aProxyResolveFlags,
nsIURI *aProxyURI,
uint64_t aChannelId) override;
virtual MOZ_MUST_USE nsresult
Init(nsIURI *aURI, uint32_t aCaps, nsProxyInfo *aProxyInfo,
uint32_t aProxyResolveFlags, nsIURI *aProxyURI, uint64_t aChannelId,
nsContentPolicyType aContentPolicyType) override;
MOZ_MUST_USE nsresult OnPush(const nsACString &uri,
Http2PushedStream *pushedStream);

Просмотреть файл

@ -115,6 +115,12 @@
#define TCP_FAST_OPEN_STALLS_TIMEOUT \
"network.tcp.tcp_fastopen_http_stalls_timeout"
#define ACCEPT_HEADER_NAVIGATION \
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
#define ACCEPT_HEADER_IMAGE "image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"
#define ACCEPT_HEADER_STYLE "text/css,*/*;q=0.1"
#define ACCEPT_HEADER_ALL "*/*"
#define UA_PREF(_pref) UA_PREF_PREFIX _pref
#define HTTP_PREF(_pref) HTTP_PREF_PREFIX _pref
#define BROWSER_PREF(_pref) BROWSER_PREF_PREFIX _pref
@ -617,8 +623,9 @@ nsresult nsHttpHandler::InitConnectionMgr() {
return rv;
}
nsresult nsHttpHandler::AddStandardRequestHeaders(nsHttpRequestHead *request,
bool isSecure) {
nsresult nsHttpHandler::AddStandardRequestHeaders(
nsHttpRequestHead *request, bool isSecure,
nsContentPolicyType aContentPolicyType) {
nsresult rv;
// Add the "User-Agent" header
@ -630,7 +637,20 @@ nsresult nsHttpHandler::AddStandardRequestHeaders(nsHttpRequestHead *request,
// Add the "Accept" header. Note, this is set as an override because the
// service worker expects to see it. The other "default" headers are
// hidden from service worker interception.
rv = request->SetHeader(nsHttp::Accept, mAccept, false,
nsAutoCString accept;
if (aContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT ||
aContentPolicyType == nsIContentPolicy::TYPE_SUBDOCUMENT) {
accept.Assign(ACCEPT_HEADER_NAVIGATION);
} else if (aContentPolicyType == nsIContentPolicy::TYPE_IMAGE ||
aContentPolicyType == nsIContentPolicy::TYPE_IMAGESET) {
accept.Assign(ACCEPT_HEADER_IMAGE);
} else if (aContentPolicyType == nsIContentPolicy::TYPE_STYLESHEET) {
accept.Assign(ACCEPT_HEADER_STYLE);
} else {
accept.Assign(ACCEPT_HEADER_ALL);
}
rv = request->SetHeader(nsHttp::Accept, accept, false,
nsHttpHeaderArray::eVarietyRequestOverride);
if (NS_FAILED(rv)) return rv;
@ -1358,15 +1378,6 @@ void nsHttpHandler::PrefsChanged(const char *pref) {
if (NS_SUCCEEDED(rv)) mQoSBits = (uint8_t)clamped(val, 0, 0xff);
}
if (PREF_CHANGED(HTTP_PREF("accept.default"))) {
nsAutoCString accept;
rv = Preferences::GetCString(HTTP_PREF("accept.default"), accept);
if (NS_SUCCEEDED(rv)) {
rv = SetAccept(accept.get());
MOZ_ASSERT(NS_SUCCEEDED(rv));
}
}
if (PREF_CHANGED(HTTP_PREF("accept-encoding"))) {
nsAutoCString acceptEncodings;
rv = Preferences::GetCString(HTTP_PREF("accept-encoding"), acceptEncodings);
@ -1962,11 +1973,6 @@ nsresult nsHttpHandler::SetAcceptLanguages() {
return rv;
}
nsresult nsHttpHandler::SetAccept(const char *aAccept) {
mAccept = aAccept;
return NS_OK;
}
nsresult nsHttpHandler::SetAcceptEncodings(const char *aAcceptEncodings,
bool isSecure) {
if (isSecure) {
@ -2108,8 +2114,12 @@ nsHttpHandler::NewProxiedChannel2(nsIURI *uri, nsIProxyInfo *givenProxyInfo,
rv = NewChannelId(channelId);
NS_ENSURE_SUCCESS(rv, rv);
nsContentPolicyType contentPolicyType =
aLoadInfo ? aLoadInfo->GetExternalContentPolicyType()
: nsIContentPolicy::TYPE_OTHER;
rv = httpChannel->Init(uri, caps, proxyInfo, proxyResolveFlags, proxyURI,
channelId);
channelId, contentPolicyType);
if (NS_FAILED(rv)) return rv;
// set the loadInfo on the new channel

Просмотреть файл

@ -64,8 +64,9 @@ class nsHttpHandler final : public nsIHttpProtocolHandler,
static already_AddRefed<nsHttpHandler> GetInstance();
MOZ_MUST_USE nsresult AddStandardRequestHeaders(nsHttpRequestHead *,
bool isSecure);
MOZ_MUST_USE nsresult
AddStandardRequestHeaders(nsHttpRequestHead *, bool isSecure,
nsContentPolicyType aContentPolicyType);
MOZ_MUST_USE nsresult AddConnectionHeader(nsHttpRequestHead *,
uint32_t capabilities);
bool IsAcceptableEncoding(const char *encoding, bool isSecure);
@ -411,7 +412,6 @@ class nsHttpHandler final : public nsIHttpProtocolHandler,
void InitUserAgentComponents();
void PrefsChanged(const char *pref);
MOZ_MUST_USE nsresult SetAccept(const char *);
MOZ_MUST_USE nsresult SetAcceptLanguages();
MOZ_MUST_USE nsresult SetAcceptEncodings(const char *, bool mIsSecure);
@ -498,7 +498,6 @@ class nsHttpHandler final : public nsIHttpProtocolHandler,
bool mEnforceAssocReq;
nsCString mAccept;
nsCString mAcceptLanguages;
nsCString mHttpAcceptEncodings;
nsCString mHttpsAcceptEncodings;