Bug 464848: XMLHttpRequest doesn't send the right content-type for non-document request bodies. r/sr=bz

This commit is contained in:
Jonas Sicking 2009-01-13 22:53:43 -08:00
parent 8ffe1c08e4
commit 2dc8a2f1eb
9 changed files with 231 additions and 31 deletions

View File

@ -2453,6 +2453,7 @@ nsXMLHttpRequest::Send(nsIVariant *aBody)
nsXPIDLString serial;
nsCOMPtr<nsIInputStream> postDataStream;
nsCAutoString charset(NS_LITERAL_CSTRING("UTF-8"));
nsCAutoString defaultContentType(NS_LITERAL_CSTRING("text/plain"));
PRUint16 dataType;
rv = aBody->GetDataType(&dataType);
@ -2474,6 +2475,8 @@ nsXMLHttpRequest::Send(nsIVariant *aBody)
// document?
nsCOMPtr<nsIDOMDocument> doc(do_QueryInterface(supports));
if (doc) {
defaultContentType.AssignLiteral("application/xml");
nsCOMPtr<nsIDOMSerializer> serializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID, &rv));
if (NS_FAILED(rv)) return rv;
@ -2481,9 +2484,7 @@ nsXMLHttpRequest::Send(nsIVariant *aBody)
if (dom3doc) {
nsAutoString inputEncoding;
dom3doc->GetInputEncoding(inputEncoding);
if (DOMStringIsNull(inputEncoding)) {
charset.AssignLiteral("UTF-8");
} else {
if (!DOMStringIsNull(inputEncoding)) {
CopyUTF16toUTF8(inputEncoding, charset);
}
}
@ -2525,15 +2526,10 @@ nsXMLHttpRequest::Send(nsIVariant *aBody)
case nsIDataType::VTYPE_EMPTY:
// Makes us act as if !aBody, don't upload anything
break;
case nsIDataType::VTYPE_EMPTY_ARRAY:
case nsIDataType::VTYPE_ARRAY:
// IE6 throws error here, so we do that as well
return NS_ERROR_INVALID_ARG;
default:
// try variant string
rv = aBody->GetAsWString(getter_Copies(serial));
if (NS_FAILED(rv))
return rv;
NS_ENSURE_SUCCESS(rv, rv);
break;
}
@ -2562,7 +2558,7 @@ nsXMLHttpRequest::Send(nsIVariant *aBody)
GetRequestHeader(NS_LITERAL_CSTRING("Content-Type"),
contentType)) ||
contentType.IsEmpty()) {
contentType = NS_LITERAL_CSTRING("application/xml");
contentType = defaultContentType;
}
// We don't want to set a charset for streams.
@ -2573,25 +2569,21 @@ nsXMLHttpRequest::Send(nsIVariant *aBody)
rv = NS_ExtractCharsetFromContentType(contentType, specifiedCharset,
&haveCharset, &charsetStart,
&charsetEnd);
if (NS_FAILED(rv)) {
contentType.AssignLiteral("application/xml");
specifiedCharset.Truncate();
charsetStart = charsetEnd = contentType.Length();
}
// If the content-type the page set already has a charset parameter,
// and it's the same charset, up to case, as |charset|, just send the
// page-set content-type header. Apparently at least
// google-web-toolkit is broken and relies on the exact case of its
// charset parameter, which makes things break if we use |charset|
// (which is always a fully resolved charset per our charset alias
// table, hence might be differently cased).
if (!specifiedCharset.Equals(charset,
nsCaseInsensitiveCStringComparator())) {
nsCAutoString newCharset("; charset=");
newCharset.Append(charset);
contentType.Replace(charsetStart, charsetEnd - charsetStart,
newCharset);
if (NS_SUCCEEDED(rv)) {
// If the content-type the page set already has a charset parameter,
// and it's the same charset, up to case, as |charset|, just send the
// page-set content-type header. Apparently at least
// google-web-toolkit is broken and relies on the exact case of its
// charset parameter, which makes things break if we use |charset|
// (which is always a fully resolved charset per our charset alias
// table, hence might be differently cased).
if (!specifiedCharset.Equals(charset,
nsCaseInsensitiveCStringComparator())) {
nsCAutoString newCharset("; charset=");
newCharset.Append(charset);
contentType.Replace(charsetStart, charsetEnd - charsetStart,
newCharset);
}
}
}

View File

@ -274,6 +274,10 @@ _TEST_FILES = test_bug5141.html \
bug455629-helper.svg \
test_bug473162-1.html \
test_bug473162-2.html \
test_XHRSendData.html \
file_XHRSendData.sjs \
file_XHRSendData_doc.xml \
file_XHRSendData_doc.xml^headers^ \
$(NULL)
# Disabled for now. Mochitest isn't reliable enough for these.

View File

@ -57,6 +57,8 @@ window.addEventListener('message', function(e) {
if (req.withCred)
xhr.withCredentials = true;
if (req.body)
sendData = req.body;
res.events.push("opening");
xhr.open(req.method, req.url, true);

View File

@ -1,3 +1,8 @@
const CC = Components.Constructor;
const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream");
function handleRequest(request, response)
{
var query = {};
@ -8,12 +13,28 @@ function handleRequest(request, response)
var isPreflight = request.method == "OPTIONS";
var bodyStream = new BinaryInputStream(request.bodyInputStream);
var bodyBytes = [];
while ((bodyAvail = bodyStream.available()) > 0)
Array.prototype.push.apply(bodyBytes, bodyStream.readByteArray(bodyAvail));
var body = decodeURIComponent(
escape(String.fromCharCode.apply(null, bodyBytes)));
// Check that request was correct
if (!isPreflight && query.body && body != query.body) {
sendHttp500(response, "Wrong body. Expected " + query.body + " got " +
body);
return;
}
if (!isPreflight && "headers" in query) {
headers = eval(query.headers);
for(headerName in headers) {
if (request.getHeader(headerName) != headers[headerName]) {
// Content-Type is changed if there was a body
if (!(headerName == "Content-Type" && body) &&
request.getHeader(headerName) != headers[headerName]) {
sendHttp500(response,
"Header " + headerName + " had wrong value. Expected " +
headers[headerName] + " got " + request.getHeader(headerName));

View File

@ -0,0 +1,22 @@
const CC = Components.Constructor;
const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream");
function handleRequest(request, response)
{
if (request.hasHeader("Content-Type"))
response.setHeader("Result-Content-Type",
request.getHeader("Content-Type"));
response.setHeader("Content-Type", "text/plain; charset=ISO-8859-1");
var body = new BinaryInputStream(request.bodyInputStream);
var avail;
var bytes = [];
while ((avail = body.available()) > 0)
Array.prototype.push.apply(bytes, body.readByteArray(avail));
var data = String.fromCharCode.apply(null, bytes);
response.write(data);
}

View File

@ -0,0 +1,2 @@
<!-- comment -->
<out>hi</out>

View File

@ -0,0 +1 @@
Content-Type: application/xml; charset=ISO-8859-1

View File

@ -199,21 +199,29 @@ function runTest() {
"Accept-Language": "sv-SE" },
},
{ method: "POST",
headers: { "Content-Type": "text/plain" },
body: "hi there",
noAllowPreflight: 1,
},
{ method: "POST",
},
{ method: "POST",
body: "hi there",
headers: { "Content-Type": "text/plain" },
noAllowPreflight: 1,
},
{ method: "POST",
body: "hi there",
headers: { "Content-Type": "foo/bar" },
},
{ method: "POST",
body: "hi there",
headers: { "Content-Type": "text/plain",
"Accept": "foo/bar",
"Accept-Language": "sv-SE" },
noAllowPreflight: 1,
},
{ method: "POST",
body: "hi there",
headers: { "Accept": "foo/bar",
"Accept-Language": "sv-SE",
"x-my-header": "myValue" },
@ -225,6 +233,7 @@ function runTest() {
allowHeaders: "x-my-header",
},
{ method: "POST",
body: "hi there",
headers: { "Content-Type": "foo/bar",
"x-my-header": "myValue" },
allowHeaders: "x-my-header",
@ -234,6 +243,7 @@ function runTest() {
allowHeaders: "x-my-header",
},
{ method: "POST",
body: "hi there",
headers: { "x-my-header": "myValue" },
allowHeaders: "x-my-header, $_%",
},
@ -262,10 +272,12 @@ function runTest() {
allowMethods: " ,, PUT ,, , , XXDELETE , ,",
},
{ method: "POST",
body: "hi there",
headers: { "Content-Type": "text/plain" },
uploadProgress: "uploadprogress",
},
{ method: "POST",
body: "hi there",
headers: { "Content-Type": "text/plain" },
uploadProgress: "progress",
},
@ -310,6 +322,7 @@ function runTest() {
noAllowPreflight: 1,
},
{ method: "POST",
body: "hi there",
headers: { "Content-Type": "foo/bar" },
noAllowPreflight: 1,
},
@ -349,11 +362,13 @@ function runTest() {
allowMethods: "put",
},
{ method: "POST",
body: "hi there",
headers: { "Content-Type": "text/plain" },
noAllowPreflight: 1,
uploadProgress: "uploadprogress",
},
{ method: "POST",
body: "hi there",
headers: { "Content-Type": "text/plain" },
noAllowPreflight: 1,
uploadProgress: "progress",
@ -368,6 +383,7 @@ function runTest() {
method: test.method,
headers: test.headers,
uploadProgress: test.uploadProgress,
body: test.body,
};
if (test.noAllowPreflight)
@ -408,8 +424,13 @@ function runTest() {
method: test.method,
headers: test.headers,
uploadProgress: test.uploadProgress,
body: test.body,
};
if (test.body) {
req.url += "&body=" + escape(test.body);
}
if (test.noAllowPreflight)
req.url += "&noAllowPreflight";

View File

@ -0,0 +1,135 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=464848
-->
<head>
<title>XMLHttpRequest send data and headers</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/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=464848">Mozilla Bug 464848</a>
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.8">
xhr = new XMLHttpRequest();
xhr.open("GET", "file_XHRSendData_doc.xml", false);
xhr.send();
testDoc1 = xhr.responseXML;
is(testDoc1.inputEncoding, "ISO-8859-1", "wrong encoding");
testDoc2 = document.implementation.createDocument("", "", null);
testDoc2.appendChild(testDoc2.createComment(" doc 2 "));
testDoc2.appendChild(testDoc2.createElement("res"));
testDoc2.documentElement.appendChild(testDoc2.createTextNode("text"));
is(testDoc2.inputEncoding, null, "wrong encoding");
tests = [{ body: null,
resBody: "",
},
{ body: undefined,
resBody: "",
},
{ body: "hi",
resBody: "hi",
resContentType: "text/plain; charset=UTF-8",
},
{ body: "r\xe4ksm\xf6rg\xe5s",
resBody: "r\xc3\xa4ksm\xc3\xb6rg\xc3\xa5s",
resContentType: "text/plain; charset=UTF-8",
},
{ body: "hi",
contentType: "",
resBody: "hi",
resContentType: "text/plain; charset=UTF-8",
},
{ body: "hi",
contentType: "foo/bar",
resBody: "hi",
resContentType: "foo/bar; charset=UTF-8",
},
{ body: "hi",
contentType: "foo/bar; baz=bin",
resBody: "hi",
resContentType: "foo/bar; charset=UTF-8; baz=bin",
},
{ body: "hi",
contentType: "foo/bar; charset=ascii; baz=bin",
resBody: "hi",
resContentType: "foo/bar; charset=UTF-8; baz=bin",
},
{ body: "hi",
contentType: "foo/bar; charset=uTf-8",
resBody: "hi",
resContentType: "foo/bar; charset=uTf-8",
},
{ body: testDoc1,
resBody: "<!-- comment -->\n<out>hi</out>",
resContentType: "application/xml; charset=ISO-8859-1",
},
{ body: testDoc1,
contentType: "foo/bar",
resBody: "<!-- comment -->\n<out>hi</out>",
resContentType: "foo/bar; charset=ISO-8859-1",
},
{ body: testDoc1,
contentType: "foo/bar; charset=ascii; baz=bin",
resBody: "<!-- comment -->\n<out>hi</out>",
resContentType: "foo/bar; charset=ISO-8859-1; baz=bin",
},
{ body: testDoc1,
contentType: "foo/bar; charset=IsO-8859-1",
resBody: "<!-- comment -->\n<out>hi</out>",
resContentType: "foo/bar; charset=IsO-8859-1",
},
{ body: testDoc2,
resBody: "<!-- doc 2 -->\n<res>text</res>",
resContentType: "application/xml; charset=UTF-8",
},
{ body: testDoc2,
contentType: "foo/bar",
resBody: "<!-- doc 2 -->\n<res>text</res>",
resContentType: "foo/bar; charset=UTF-8",
},
{ body: testDoc2,
contentType: "foo/bar; charset=ascii; baz=bin",
resBody: "<!-- doc 2 -->\n<res>text</res>",
resContentType: "foo/bar; charset=UTF-8; baz=bin",
},
{ body: testDoc2,
contentType: "foo/bar; charset=uTf-8",
resBody: "<!-- doc 2 -->\n<res>text</res>",
resContentType: "foo/bar; charset=uTf-8",
},
];
for each(test in tests) {
xhr = new XMLHttpRequest;
xhr.open("POST", "file_XHRSendData.sjs", false);
if (test.contentType)
xhr.setRequestHeader("Content-Type", test.contentType);
xhr.send(test.body);
if (test.resContentType) {
is(xhr.getResponseHeader("Result-Content-Type"), test.resContentType,
"Wrong Content-Type sent");
}
else {
is(xhr.getResponseHeader("Result-Content-Type"), null);
}
is(xhr.responseText, test.resBody, "Wrong body");
}
</script>
</pre>
</body>
</html>