Bug 1126815 - Implement Response.finalURL. r=bkelly,baku

This commit is contained in:
Nikhil Marathe 2015-02-17 10:23:34 -08:00
parent 8a8fbb3c0c
commit 6f344d4c40
8 changed files with 65 additions and 3 deletions

View File

@ -71,3 +71,4 @@ MSG_DEF(MSG_INVALID_ZOOMANDPAN_VALUE_ERROR, 0, JSEXN_RANGEERR, "Invalid zoom and
MSG_DEF(MSG_INVALID_TRANSFORM_ANGLE_ERROR, 0, JSEXN_RANGEERR, "Invalid transform angle.")
MSG_DEF(MSG_INVALID_RESPONSE_STATUSCODE_ERROR, 0, JSEXN_RANGEERR, "Invalid response status code.")
MSG_DEF(MSG_INVALID_REDIRECT_STATUSCODE_ERROR, 0, JSEXN_RANGEERR, "Invalid redirect status code.")
MSG_DEF(MSG_RESPONSE_URL_IS_NULL, 0, JSEXN_TYPEERR, "Cannot set Response.finalURL when Response.url is null.")

View File

@ -517,9 +517,11 @@ already_AddRefed<InternalResponse>
FetchDriver::BeginAndGetFilteredResponse(InternalResponse* aResponse)
{
MOZ_ASSERT(aResponse);
nsAutoCString reqURL;
mRequest->GetURL(reqURL);
aResponse->SetUrl(reqURL);
if (!aResponse->FinalURL()) {
nsAutoCString reqURL;
mRequest->GetURL(reqURL);
aResponse->SetUrl(reqURL);
}
// FIXME(nsm): Handle mixed content check, step 7 of fetch.

View File

@ -14,6 +14,7 @@ namespace dom {
InternalResponse::InternalResponse(uint16_t aStatus, const nsACString& aStatusText)
: mType(ResponseType::Default)
, mFinalURL(false)
, mStatus(aStatus)
, mStatusText(aStatusText)
, mHeaders(new InternalHeaders(HeadersGuardEnum::Response))
@ -26,6 +27,7 @@ InternalResponse::InternalResponse(const InternalResponse& aOther)
: mType(aOther.mType)
, mTerminationReason(aOther.mTerminationReason)
, mURL(aOther.mURL)
, mFinalURL(aOther.mFinalURL)
, mStatus(aOther.mStatus)
, mStatusText(aOther.mStatusText)
, mBody(aOther.mBody)

View File

@ -76,6 +76,18 @@ public:
mURL.Assign(aURL);
}
bool
FinalURL() const
{
return mFinalURL;
}
void
SetFinalURL(bool aFinalURL)
{
mFinalURL = aFinalURL;
}
uint16_t
GetStatus() const
{
@ -120,6 +132,7 @@ private:
ResponseType mType;
nsCString mTerminationReason;
nsCString mURL;
bool mFinalURL;
const uint16_t mStatus;
const nsCString mStatusText;
nsRefPtr<InternalHeaders> mHeaders;

View File

@ -215,5 +215,18 @@ Response::Headers_()
return mHeaders;
}
void
Response::SetFinalURL(bool aFinalURL, ErrorResult& aRv)
{
nsCString url;
mInternalResponse->GetUrl(url);
if (url.IsEmpty()) {
aRv.ThrowTypeError(MSG_RESPONSE_URL_IS_NULL);
return;
}
mInternalResponse->SetFinalURL(aFinalURL);
}
} // namespace dom
} // namespace mozilla

View File

@ -56,6 +56,15 @@ public:
aUrl.AsAString() = NS_ConvertUTF8toUTF16(url);
}
bool
GetFinalURL(ErrorResult& aRv) const
{
return mInternalResponse->FinalURL();
}
void
SetFinalURL(bool aFinalURL, ErrorResult& aRv);
uint16_t
Status() const
{

View File

@ -18,6 +18,8 @@ interface Response {
readonly attribute ResponseType type;
readonly attribute USVString url;
[Throws]
attribute boolean finalURL;
readonly attribute unsigned short status;
readonly attribute boolean ok;
readonly attribute ByteString statusText;

View File

@ -66,6 +66,25 @@ function testOk() {
ok(!r4.ok, "Response with status 302 should have ok false");
}
// It is not possible to test setting finalURL until we have ServiceWorker
// interception. This is because synthetic Responses do not have a url, the url
// is set based on the request, so a SW could initiate a fetch() on behalf of
// a client and set the resulting Response's finalURL before returning it to
// the client, in which case the "set response's url to request's url" from the
// client's point of view would not happen. A test for this will be added by
// Bug 1134352.
function testFinalURL() {
var r1 = new Response();
ok(!r1.finalURL, "Response.finalURL is false by default.");
try {
r1.finalURL = true;
ok(false, "Setting Response.finalURL of Response with null url should fail.");
} catch(e) {
ok(true, "Setting Response.finalURL of Response with null url should fail.");
}
}
function testBodyUsed() {
var res = new Response("Sample body");
ok(!res.bodyUsed, "bodyUsed is initially false.");
@ -153,6 +172,7 @@ onmessage = function() {
testClone();
testRedirect();
testOk();
testFinalURL();
Promise.resolve()
.then(testBodyCreation)