Bug 1244764 P1 Make Cache .add()/.addAll() fail if a Response.ok() is false. r=ehsan

This commit is contained in:
Ben Kelly 2016-02-04 07:59:52 -08:00
parent b6adf15a82
commit 1fb29fb6d4
3 changed files with 23 additions and 2 deletions

View File

@ -89,3 +89,4 @@ MSG_DEF(MSG_IS_NOT_PROMISE, 1, JSEXN_TYPEERR, "{0} is not a Promise")
MSG_DEF(MSG_SW_INSTALL_ERROR, 2, JSEXN_TYPEERR, "ServiceWorker script at {0} for scope {1} encountered an error during installation.")
MSG_DEF(MSG_SW_SCRIPT_THREW, 2, JSEXN_TYPEERR, "ServiceWorker script at {0} for scope {1} threw an exception during script evaluation.")
MSG_DEF(MSG_TYPEDARRAY_IS_SHARED, 1, JSEXN_TYPEERR, "{0} can't be a typed array on SharedArrayBuffer")
MSG_DEF(MSG_CACHE_ADD_FAILED_RESPONSE, 3, JSEXN_TYPEERR, "Cache got {0} response with bad status {1} while trying to add request {2}")

20
dom/cache/Cache.cpp vendored
View File

@ -158,6 +158,26 @@ public:
return;
}
// Do not allow the convenience methods .add()/.addAll() to store failed
// responses. A consequence of this is that these methods cannot be
// used to store opaque or opaqueredirect responses since they always
// expose a 0 status value.
if (!response->Ok()) {
uint32_t t = static_cast<uint32_t>(response->Type());
NS_ConvertASCIItoUTF16 type(ResponseTypeValues::strings[t].value,
ResponseTypeValues::strings[t].length);
nsAutoString status;
status.AppendInt(response->Status());
nsAutoString url;
mRequestList[i]->GetUrl(url);
ErrorResult rv;
rv.ThrowTypeError<MSG_CACHE_ADD_FAILED_RESPONSE>(type, status, url);
// TODO: abort the fetch requests we have running (bug 1157434)
mPromise->MaybeReject(rv);
return;
}
responseList.AppendElement(Move(response));
}

View File

@ -50,11 +50,11 @@ public:
}
void
GetUrl(DOMString& aUrl) const
GetUrl(nsAString& aUrl) const
{
nsCString url;
mInternalResponse->GetUrl(url);
aUrl.AsAString() = NS_ConvertUTF8toUTF16(url);
CopyUTF8toUTF16(url, aUrl);
}
uint16_t