Permalink
Browse files

Update test5.php to use utf8 instead of unsupported content type for IE

Fix CORS code for IE that was broken by header change
Modify all users of responseText to properly guard against IE exceptions
Clarify header parsing code

Enyo-DCO-1.1-Signed-Off-By: Ben Combee (ben.combee@palm.com)
  • Loading branch information...
1 parent a78630b commit 9fbb08e819504e83754fbd2e1e4d65ad138d35ea @unwiredben unwiredben committed Nov 14, 2012
Showing with 40 additions and 23 deletions.
  1. +16 −5 source/ajax/Ajax.js
  2. +11 −3 source/ajax/xhr.js
  3. +3 −1 tools/test/ajax/php/test5.php
  4. +10 −14 tools/test/ajax/tests/AjaxTest.js
View
@@ -68,6 +68,11 @@ enyo.kind({
}
}
enyo.mixin(xhr_headers, this.headers);
+ // don't pass in headers structure if there are no headers defined as this messes
+ // up CORS code for IE8-9
+ if (enyo.keys(xhr_headers).length === 0) {
+ xhr_headers = undefined;
+ }
//
try {
this.xhr = enyo.xhr.request({
@@ -91,10 +96,14 @@ enyo.kind({
},
receive: function(inText, inXhr) {
if (!this.failed && !this.destroyed) {
+ var text;
+ if (typeof inXhr.responseText === "string") {
+ text = inXhr.responseText;
+ }
this.xhrResponse = {
status: inXhr.status,
headers: enyo.Ajax.parseResponseHeaders(inXhr),
- body: inXhr.response
+ body: text
};
if (this.isFailure(inXhr)) {
this.fail(inXhr.status);
@@ -179,11 +188,13 @@ enyo.kind({
},
parseResponseHeaders: function(xhr) {
var headers = {};
- var headersStr = xhr.getAllResponseHeaders()
- .split('\u000a'); // .split('\n');
+ var headersStr = [];
+ if (xhr.getAllResponseHeaders) {
+ headersStr = xhr.getAllResponseHeaders().split('\r\n');
+ }
for (var i = 0; i < headersStr.length; i++) {
- var headerStr = headersStr[i].replace(/\u000d*\u000a*$/, ''); // .replace(/\r*\n*/,'');
- var index = headerStr.indexOf('\u003a\u0020' /*': '*/);
+ var headerStr = headersStr[i];
+ var index = headerStr.indexOf(': ');
if (index > 0) {
var key = headerStr.substring(0, index);
var val = headerStr.substring(index + 2);
View
@@ -67,12 +67,20 @@ enyo.xhr = {
makeReadyStateHandler: function(inXhr, inCallback) {
if (window.XDomainRequest && inXhr instanceof XDomainRequest) {
inXhr.onload = function() {
- inCallback.apply(null, [inXhr.responseText, inXhr]);
+ var text;
+ if (typeof inXhr.responseText === "string") {
+ text = inXhr.responseText;
+ }
+ inCallback.apply(null, [text, inXhr]);
};
}
inXhr.onreadystatechange = function() {
if (inXhr.readyState == 4) {
- inCallback.apply(null, [inXhr.responseText, inXhr]);
+ var text;
+ if (typeof inXhr.responseText === "string") {
+ text = inXhr.responseText;
+ }
+ inCallback.apply(null, [text, inXhr]);
}
};
},
@@ -94,7 +102,7 @@ enyo.xhr = {
// only use XDomainRequest when it exists, no extra headers were set, and the
// target URL maps to a domain other than the document origin.
if (enyo.platform.ie < 10 && window.XDomainRequest && !inParams.headers &&
- !this.inOrigin(inParams.inUrl) && !/^file:\/\//.test(window.location.href)) {
+ !this.inOrigin(inParams.url) && !/^file:\/\//.test(window.location.href)) {
return new XDomainRequest();
}
} catch(e) {}
@@ -8,7 +8,9 @@
//http_response_code(500); // php >= 5.4
header('X-PHP-Response-Code: 500', true, 500); // php >= 4.3
- header('Content-Type: text/plain; charset=x-user-unparseable');
+ // can't use odd charset due to IE exception throwing
+ //header('Content-Type: text/plain; charset=x-user-unparseable');
+ header('Content-Type: text/plain; charset=utf-8');
echo "my error description";
break;
@@ -76,7 +76,7 @@ enyo.kind({
var contentType = "application/x-www-form-urlencoded";
this._testAjax({url: "php/test4.php", method: "POST", postBody: "data"}, null, function(inValue) {
var status = (inValue.ctype.indexOf(contentType) === 0);
- if (status) {
+ if (!status) {
enyo.log("Bad CT: " + inValue.ctype + " expected: " + contentType);
}
return status;
@@ -88,9 +88,9 @@ enyo.kind({
formData.append('token', "data");
var contentType = "multipart/form-data";
this._testAjax({url: "php/test4.php", method: "POST", postBody: formData}, null, function(inValue) {
- var status = (inValue.ctype.indexOf(contentType) === 0)
- && (inValue.ctype.indexOf("boundary=--") > 10);
- if ( ! status) {
+ var status = (inValue.ctype.indexOf(contentType) === 0) &&
+ (inValue.ctype.indexOf("boundary=--") > 10);
+ if (!status) {
enyo.log("Bad CT: " + inValue.ctype + " expected: " + contentType);
}
return status;
@@ -142,19 +142,15 @@ enyo.kind({
},
// expected to fail
testErrorResponse: function() {
- enyo.log("testErrorResponse: starting");
var req = this._testAjax({url: "php/test5.php"}, null, function(inValue) {
- enyo.log("testErrorResponse: FAIL inValue=", inValue);
+ // getting success means server sent wrong response
return false;
}, function(inError) {
- enyo.log("testErrorResponse: inError=", inError);
- enyo.log(inError === 500);
- enyo.log("testErrorResponse: req.xhrResponse=", req.xhrResponse);
- enyo.log(req.xhrResponse.status === 500);
- enyo.log("Content-Type='"+ req.xhrResponse.headers['Content-Type'] + "'");
- enyo.log(req.xhrResponse.headers['Content-Type'] === "text/plain; charset=x-user-unparseable");
- enyo.log(req.xhrResponse.body === "my error description");
- return (inError === 500) && req.xhrResponse && (req.xhrResponse.status === 500) && (req.xhrResponse.headers['Content-Type'] === "text/plain; charset=x-user-unparseable") && (req.xhrResponse.body === "my error description");
+ return (inError === 500) &&
+ req.xhrResponse &&
+ (req.xhrResponse.status === 500) &&
+ (req.xhrResponse.headers['Content-Type'] === "text/plain; charset=utf-8") &&
+ (req.xhrResponse.body === "my error description");
});
}
});

0 comments on commit 9fbb08e

Please sign in to comment.