Bug 784367 - Encode/decode document.cookie as UTF-8 per HTML5 spec. r=bz

This commit is contained in:
Masatoshi Kimura 2012-09-04 21:01:57 -04:00
Родитель 6ae700c29b
Коммит a13f21b796
5 изменённых файлов: 66 добавлений и 9 удалений

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

@ -3598,21 +3598,39 @@ nsContentUtils::ConvertStringFromCharset(const nsACString& aCharset,
return rv;
nsPromiseFlatCString flatInput(aInput);
int32_t srcLen = flatInput.Length();
int32_t dstLen;
rv = decoder->GetMaxLength(flatInput.get(), srcLen, &dstLen);
int32_t length = flatInput.Length();
int32_t outLen;
rv = decoder->GetMaxLength(flatInput.get(), length, &outLen);
if (NS_FAILED(rv))
return rv;
PRUnichar *ustr = (PRUnichar *)nsMemory::Alloc((dstLen + 1) *
PRUnichar *ustr = (PRUnichar *)nsMemory::Alloc((outLen + 1) *
sizeof(PRUnichar));
if (!ustr)
return NS_ERROR_OUT_OF_MEMORY;
rv = decoder->Convert(flatInput.get(), &srcLen, ustr, &dstLen);
if (NS_SUCCEEDED(rv)) {
const char* data = flatInput.get();
aOutput.Truncate();
for (;;) {
int32_t srcLen = length;
int32_t dstLen = outLen;
rv = decoder->Convert(data, &srcLen, ustr, &dstLen);
// Convert will convert the input partially even if the status
// indicates a failure.
ustr[dstLen] = 0;
aOutput.Assign(ustr, dstLen);
aOutput.Append(ustr, dstLen);
if (rv != NS_ERROR_ILLEGAL_INPUT) {
break;
}
// Emit a decode error manually because some decoders
// do not support kOnError_Recover (bug 638379)
if (srcLen == -1) {
decoder->Reset();
} else {
data += srcLen + 1;
length -= srcLen + 1;
aOutput.Append(static_cast<PRUnichar>(0xFFFD));
}
}
nsMemory::Free(ustr);

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

@ -1246,7 +1246,10 @@ nsHTMLDocument::GetCookie(nsAString& aCookie)
nsXPIDLCString cookie;
service->GetCookieString(codebaseURI, mChannel, getter_Copies(cookie));
CopyASCIItoUTF16(cookie, aCookie);
// CopyUTF8toUTF16 doesn't handle error
// because it assumes that the input is valid.
nsContentUtils::ConvertStringFromCharset(NS_LITERAL_CSTRING("utf-8"),
cookie, aCookie);
}
return NS_OK;
@ -1285,7 +1288,7 @@ nsHTMLDocument::SetCookie(const nsAString& aCookie)
return NS_OK;
}
NS_LossyConvertUTF16toASCII cookie(aCookie);
NS_ConvertUTF16toUTF8 cookie(aCookie);
service->SetCookieString(codebaseURI, prompt, cookie.get(), mChannel);
}

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

@ -72,6 +72,8 @@ MOCHITEST_FILES = test_bug1682.html \
test_bug677495.html \
test_bug677495-1.html \
test_bug741266.html \
test_non-ascii-cookie.html \
test_non-ascii-cookie.html^headers^ \
$(NULL)
ifneq (mobile,$(MOZ_BUILD_APP))

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

@ -0,0 +1,33 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=784367
-->
<head>
<meta charset="utf-8">
<title>Test for non-ASCII document.cookie</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=784367">Mozilla Bug 784367</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for non-ASCII document.cookie **/
var c = document.cookie;
is(document.cookie, 'abc=012©ABC\ufffdDEF', "document.cookie should be decoded as UTF-8");
var newCookie = 'def=∼≩≭≧∯≳≲≣∽≸≸∺≸∠≯≮≥≲≲≯≲∽≡≬≥≲≴∨∱∩∾';
document.cookie = newCookie;
is(document.cookie, c + '; ' + newCookie, "document.cookie should be encoded as UTF-8");
var date1 = new Date();
date1.setTime(0);
document.cookie = newCookie + 'def=;expires=' + date1.toGMTString();
</script>
</pre>
</body>
</html>

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

@ -0,0 +1 @@
Set-Cookie: abc=012©ABC©DEF