Bug 658949 patch 1: Teach nsDataHandler::ParseURI about #ref, and transfer 'isRefValid' flag in nsSimpleURI::Clone. r=bz

This commit is contained in:
Daniel Holbert 2011-05-24 03:37:54 -07:00
parent 2d246fbdb2
commit 9c8854da80
4 changed files with 29 additions and 10 deletions

View File

@ -115,8 +115,10 @@ nsSimpleURI::Read(nsIObjectInputStream* aStream)
mIsRefValid = isRefValid;
if (isRefValid) {
rv = aStream->ReadCString(mRef);
if (NS_FAILED(rv)) return rv;
rv = aStream->ReadCString(mRef);
if (NS_FAILED(rv)) return rv;
} else {
mRef.Truncate(); // invariant: mRef should be empty when it's not valid
}
return NS_OK;
@ -166,6 +168,7 @@ nsSimpleURI::Read(const IPC::Message *aMsg, void **aIter)
if (mIsRefValid) {
return ReadParam(aMsg, aIter, &mRef);
}
mRef.Truncate(); // invariant: mRef should be empty when it's not valid
return PR_TRUE;
}
@ -191,6 +194,8 @@ nsSimpleURI::GetSpec(nsACString &result)
result = mScheme + NS_LITERAL_CSTRING(":") + mPath;
if (mIsRefValid) {
result += NS_LITERAL_CSTRING("#") + mRef;
} else {
NS_ABORT_IF_FALSE(mRef.IsEmpty(), "mIsRefValid/mRef invariant broken");
}
return NS_OK;
}
@ -368,6 +373,7 @@ nsSimpleURI::SetPath(const nsACString &path)
PRInt32 hashPos = path.FindChar('#');
if (hashPos < 0) {
mIsRefValid = PR_FALSE;
mRef.Truncate(); // invariant: mRef should be empty when it's not valid
mPath = path;
return NS_OK;
}
@ -380,6 +386,7 @@ NS_IMETHODIMP
nsSimpleURI::GetRef(nsACString &result)
{
if (!mIsRefValid) {
NS_ABORT_IF_FALSE(mRef.IsEmpty(), "mIsRefValid/mRef invariant broken");
result.Truncate();
} else {
result = mRef;
@ -398,7 +405,7 @@ nsSimpleURI::SetRef(const nsACString &aRef)
if (aRef.IsEmpty()) {
// Empty string means to remove ref completely.
mIsRefValid = PR_FALSE;
mRef.Truncate();
mRef.Truncate(); // invariant: mRef should be empty when it's not valid
return NS_OK;
}
@ -503,6 +510,7 @@ nsSimpleURI::CloneInternal(nsSimpleURI::RefHandlingEnum refHandlingMode,
url->mPath = mPath;
if (refHandlingMode == eHonorRef) {
url->mRef = mRef;
url->mIsRefValid = mIsRefValid;
}
url.forget(result);

View File

@ -62,10 +62,10 @@ nsDataChannel::OpenContentStream(PRBool async, nsIInputStream **result,
rv = URI()->GetAsciiSpec(spec);
if (NS_FAILED(rv)) return rv;
nsCString contentType, contentCharset, dataBuffer;
nsCString contentType, contentCharset, dataBuffer, hashRef;
PRBool lBase64;
rv = nsDataHandler::ParseURI(spec, contentType, contentCharset,
lBase64, dataBuffer);
lBase64, dataBuffer, hashRef);
NS_UnescapeURL(dataBuffer);

View File

@ -114,9 +114,9 @@ nsDataHandler::NewURI(const nsACString &aSpec,
rv = uri->SetRef(spec);
} else {
// Otherwise, we'll assume |spec| is a fully-specified data URI
nsCAutoString contentType, contentCharset, dataBuffer;
nsCAutoString contentType, contentCharset, dataBuffer, hashRef;
PRBool base64;
rv = ParseURI(spec, contentType, contentCharset, base64, dataBuffer);
rv = ParseURI(spec, contentType, contentCharset, base64, dataBuffer, hashRef);
if (NS_FAILED(rv))
return rv;
@ -171,7 +171,8 @@ nsDataHandler::ParseURI(nsCString& spec,
nsCString& contentType,
nsCString& contentCharset,
PRBool& isBase64,
nsCString& dataBuffer) {
nsCString& dataBuffer,
nsCString& hashRef) {
isBase64 = PR_FALSE;
// move past "data:"
@ -230,7 +231,16 @@ nsDataHandler::ParseURI(nsCString& spec,
contentType.StripWhitespace();
contentCharset.StripWhitespace();
dataBuffer.Assign(comma + 1);
// Split encoded data from terminal "#ref" (if present)
char *data = comma + 1;
char *hash = strchr(data, '#');
if (!hash) {
dataBuffer.Assign(data);
hashRef.Truncate();
} else {
dataBuffer.Assign(data, hash - data);
hashRef.Assign(hash);
}
return NS_OK;
}

View File

@ -63,7 +63,8 @@ public:
nsCString& contentType,
nsCString& contentCharset,
PRBool& isBase64,
nsCString& dataBuffer);
nsCString& dataBuffer,
nsCString& hashRef);
};
#endif /* nsDataHandler_h___ */