Keep XMLHttpRequest response data after receiving a server error
authorTopi Reinio <topi.reinio@nokia.com>
Wed, 18 Apr 2012 12:55:02 +0000 (14:55 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 1 May 2012 04:06:51 +0000 (06:06 +0200)
Fix XMLHttpRequest.responseText being set to empty string whenever a
server status code other than '200/OK' is received. XMLHttpRequest
specification says that response entity body is to be returned unless
the error flag is set, and the flag is set only in case of abort() or
network error. This change enables clients to receive additional error
information the server may return in the response body.

http://www.w3.org/TR/XMLHttpRequest/#the-responsetext-attribute

Task-number: QTBUG-21706

Change-Id: I7e44f481494dc7eddea3868d6f92ee45d7ab0c69
Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
Reviewed-by: Bea Lam <bea.lam@nokia.com>
src/qml/qml/qqmlxmlhttprequest.cpp
tests/auto/qml/qqmlxmlhttprequest/data/status.400.reply
tests/auto/qml/qqmlxmlhttprequest/data/status.qml
tests/auto/qml/qqmlxmlhttprequest/data/statusText.qml
tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp

index 94ee7c7..e829b8b 100644 (file)
@@ -1281,14 +1281,11 @@ void QQmlXMLHttpRequest::error(QNetworkReply::NetworkError error)
 {
     v8::HandleScope handle_scope;
 
-    Q_UNUSED(error)
     m_status =
         m_network->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
     m_statusText =
         QString::fromUtf8(m_network->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray());
 
-    m_responseEntityBody = QByteArray();
-
     m_request = QNetworkRequest();
     m_data.clear();
     destroyNetwork();
@@ -1310,6 +1307,7 @@ void QQmlXMLHttpRequest::error(QNetworkReply::NetworkError error)
         if (tc.HasCaught()) printError(tc.Message());
     } else {
         m_errorFlag = true;
+        m_responseEntityBody = QByteArray();
     } 
 
     m_state = Done;
index f5e10d7..5feac17 100644 (file)
@@ -48,11 +48,7 @@ QtObject {
                 if (x.status == expectedStatus)
                     done = true;
 
-                if (expectedStatus != 200) {
-                    dataOK = (x.responseText == "");
-                } else {
-                    dataOK = (x.responseText == "QML Rocks!\n");
-                }
+                dataOK = (x.responseText == "QML Rocks!\n");
 
                 x.open("GET", url);
                 x.setRequestHeader("Accept-Language", "en-US");
index e7f658f..3c74efc 100644 (file)
@@ -48,11 +48,7 @@ QtObject {
                 if (x.statusText == expectedStatus)
                     done = true;
 
-                if (expectedStatus != "OK") {
-                    dataOK = (x.responseText == "");
-                } else {
-                    dataOK = (x.responseText == "QML Rocks!\n");
-                }
+                dataOK = (x.responseText == "QML Rocks!\n");
 
                 x.open("GET", url);
                 x.setRequestHeader("Accept-Language", "en-US");
index f8d74c3..7a65308 100644 (file)
@@ -937,8 +937,8 @@ void tst_qqmlxmlhttprequest::responseText_data()
 
     QTest::newRow("OK") << testFileUrl("status.200.reply") << testFileUrl("testdocument.html") << "QML Rocks!\n";
     QTest::newRow("empty body") << testFileUrl("status.200.reply") << QUrl() << "";
-    QTest::newRow("Not Found") << testFileUrl("status.404.reply") << testFileUrl("testdocument.html") << "";
-    QTest::newRow("Bad Request") << testFileUrl("status.404.reply") << testFileUrl("testdocument.html") << "";
+    QTest::newRow("Not Found") << testFileUrl("status.404.reply") << testFileUrl("testdocument.html") << "QML Rocks!\n";
+    QTest::newRow("Bad Request") << testFileUrl("status.400.reply") << testFileUrl("testdocument.html") << "QML Rocks!\n";
 }
 
 void tst_qqmlxmlhttprequest::nonUtf8()