History

Merge pull request #4569 from ctrlaltdavid/20431

CR for Job #20431 - Remove XMLHttpRequest file reading and model uploading

Brad Hefta-Gaub committed 11 years ago

Commit 1de15272 parents 92c55f9 edaa4cd5 files · +27 −925Browse files

Showing the changes to libraries/script-engine/src/XMLHttpRequestClass.cpp (1 of 5 files). Show all

Unified · Split

libraries/script-engine/src/XMLHttpRequestClass.cpp+27−133
@@ -13,15 +13,14 @@ · 12 unmodified lines
13//13//
14 14
15#include <QEventLoop>15#include <QEventLoop>
16#include <QFile>
17#include <qurlquery.h>16#include <qurlquery.h>
18 17
18#include <AccountManager.h>
19#include <NetworkAccessManager.h>19#include <NetworkAccessManager.h>
20#include <NetworkingConstants.h>20#include <NetworkingConstants.h>
21 21
22#include <AccountManager.h>
23#include "XMLHttpRequestClass.h"
24#include "ScriptEngine.h"22#include "ScriptEngine.h"
23#include "XMLHttpRequestClass.h"
25 24
26const QString METAVERSE_API_URL = NetworkingConstants::METAVERSE_SERVER_URL.toString() + "/api/";25const QString METAVERSE_API_URL = NetworkingConstants::METAVERSE_SERVER_URL.toString() + "/api/";
27 26
@@ -42,7 +41,6 @@ XMLHttpRequestClass::XMLHttpRequestClass(QScriptEngine* engine) : · 14 unmodified lines
42 _onReadyStateChange(QScriptValue::NullValue),41 _onReadyStateChange(QScriptValue::NullValue),
43 _readyState(XMLHttpRequestClass::UNSENT),42 _readyState(XMLHttpRequestClass::UNSENT),
44 _errorCode(QNetworkReply::NoError),43 _errorCode(QNetworkReply::NoError),
45 _file(NULL),
46 _timeout(0),44 _timeout(0),
47 _timer(this),45 _timer(this),
48 _numRedirects(0) {46 _numRedirects(0) {
@@ -63,22 +61,6 @@ QScriptValue XMLHttpRequestClass::getStatus() const { · 14 unmodified lines
63 if (_reply) {61 if (_reply) {
64 return QScriptValue(_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());62 return QScriptValue(_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
65 } 63 }
66 if(_url.isLocalFile()) {
67 switch (_errorCode) {
68 case QNetworkReply::NoError:
69 return QScriptValue(200);
70 case QNetworkReply::ContentNotFoundError:
71 return QScriptValue(404);
72 case QNetworkReply::ContentConflictError:
73 return QScriptValue(409);
74 case QNetworkReply::TimeoutError:
75 return QScriptValue(408);
76 case QNetworkReply::ContentOperationNotPermittedError:
77 return QScriptValue(501);
78 default:
79 break;
80 }
81 }
82 return QScriptValue(0);64 return QScriptValue(0);
83}65}
84 66
@@ -86,22 +68,6 @@ QString XMLHttpRequestClass::getStatusText() const { · 1 unmodified line
86 if (_reply) {68 if (_reply) {
87 return _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();69 return _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
88 }70 }
89 if (_url.isLocalFile()) {
90 switch (_errorCode) {
91 case QNetworkReply::NoError:
92 return "OK";
93 case QNetworkReply::ContentNotFoundError:
94 return "Not Found";
95 case QNetworkReply::ContentConflictError:
96 return "Conflict";
97 case QNetworkReply::TimeoutError:
98 return "Timeout";
99 case QNetworkReply::ContentOperationNotPermittedError:
100 return "Not Implemented";
101 default:
102 break;
103 }
104 }
105 return "";71 return "";
106}72}
107 73
@@ -147,13 +113,6 @@ QScriptValue XMLHttpRequestClass::getAllResponseHeaders() const { · 39 unmodified lines
147 }113 }
148 return QString(headers.data());114 return QString(headers.data());
149 }115 }
150 if (_url.isLocalFile()) {
151 QString headers = QString("Content-Type: application/octet-stream\n");
152 headers.append("Content-Length: ");
153 headers.append(QString("%1").arg(_rawResponseData.length()));
154 headers.append("\n");
155 return headers;
156 }
157 return QScriptValue("");116 return QScriptValue("");
158}117}
159 118
@@ -161,14 +120,6 @@ QScriptValue XMLHttpRequestClass::getResponseHeader(const QString& name) const { · 1 unmodified line
161 if (_reply && _reply->hasRawHeader(name.toLatin1())) {120 if (_reply && _reply->hasRawHeader(name.toLatin1())) {
162 return QScriptValue(QString(_reply->rawHeader(name.toLatin1())));121 return QScriptValue(QString(_reply->rawHeader(name.toLatin1())));
163 }122 }
164 if (_url.isLocalFile()) {
165 if (name.toLower() == "content-type") {
166 return QString("application/octet-stream");
167 }
168 if (name.toLower() == "content-length") {
169 return QString("%1").arg(_rawResponseData.length());
170 }
171 }
172 return QScriptValue::NullValue;123 return QScriptValue::NullValue;
173}124}
174 125
@@ -188,47 +139,24 @@ void XMLHttpRequestClass::open(const QString& method, const QString& url, bool a · 13 unmodified lines
188 _url.setUrl(url);139 _url.setUrl(url);
189 _async = async;140 _async = async;
190 141
191 if (_url.isLocalFile()) {142 if (url.toLower().left(METAVERSE_API_URL.length()) == METAVERSE_API_URL) {
192 if (_method.toUpper() == "GET" && !_async && username.isEmpty() && password.isEmpty()) {143 AccountManager& accountManager = AccountManager::getInstance();
193 _file = new QFile(_url.toLocalFile());
194 if (!_file->exists()) {
195 qDebug() << "Can't find file " << _url.fileName();
196 abortRequest();
197 _errorCode = QNetworkReply::ContentNotFoundError;
198 setReadyState(DONE);
199 emit requestComplete();
200 } else if (!_file->open(QIODevice::ReadOnly)) {
201 qDebug() << "Can't open file " << _url.fileName();
202 abortRequest();
203 _errorCode = QNetworkReply::ContentConflictError;
204 setReadyState(DONE);
205 emit requestComplete();
206 } else {
207 setReadyState(OPENED);
208 }
209 } else {
210 notImplemented();
211 }
212 } else {
213 if (url.toLower().left(METAVERSE_API_URL.length()) == METAVERSE_API_URL) {
214 AccountManager& accountManager = AccountManager::getInstance();
215
216 if (accountManager.hasValidAccessToken()) {
217 QUrlQuery urlQuery(_url.query());
218 urlQuery.addQueryItem("access_token", accountManager.getAccountInfo().getAccessToken().token);
219 _url.setQuery(urlQuery);
220 }
221 144
145 if (accountManager.hasValidAccessToken()) {
146 QUrlQuery urlQuery(_url.query());
147 urlQuery.addQueryItem("access_token", accountManager.getAccountInfo().getAccessToken().token);
148 _url.setQuery(urlQuery);
222 }149 }
223 if (!username.isEmpty()) {150
224 _url.setUserName(username);151 }
225 }152 if (!username.isEmpty()) {
226 if (!password.isEmpty()) {153 _url.setUserName(username);
227 _url.setPassword(password);154 }
228 }155 if (!password.isEmpty()) {
229 _request.setUrl(_url);156 _url.setPassword(password);
230 setReadyState(OPENED);
231 }157 }
158 _request.setUrl(_url);
159 setReadyState(OPENED);
232 }160 }
233}161}
234 162
@@ -239,23 +167,18 @@ void XMLHttpRequestClass::send() { · 4 unmodified lines
239void XMLHttpRequestClass::send(const QScriptValue& data) {167void XMLHttpRequestClass::send(const QScriptValue& data) {
240 if (_readyState == OPENED && !_reply) {168 if (_readyState == OPENED && !_reply) {
241 if (!data.isNull()) {169 if (!data.isNull()) {
242 if (_url.isLocalFile()) {170 _sendData = new QBuffer(this);
243 notImplemented();171 if (data.isObject()) {
244 return;172 QByteArray ba = qscriptvalue_cast<QByteArray>(data);
173 _sendData->setData(ba);
245 } else {174 } else {
246 _sendData = new QBuffer(this);175 _sendData->setData(data.toString().toUtf8());
247 if (data.isObject()) {
248 QByteArray ba = qscriptvalue_cast<QByteArray>(data);
249 _sendData->setData(ba);
250 } else {
251 _sendData->setData(data.toString().toUtf8());
252 }
253 }176 }
254 }177 }
255 178
256 doSend();179 doSend();
257 180
258 if (!_async && !_url.isLocalFile()) {181 if (!_async) {
259 QEventLoop loop;182 QEventLoop loop;
260 connect(this, SIGNAL(requestComplete()), &loop, SLOT(quit()));183 connect(this, SIGNAL(requestComplete()), &loop, SLOT(quit()));
261 loop.exec();184 loop.exec();
@@ -265,23 +188,13 @@ void XMLHttpRequestClass::send(const QScriptValue& data) { · 3 unmodified lines
265 188
266void XMLHttpRequestClass::doSend() {189void XMLHttpRequestClass::doSend() {
267 190
268 if (!_url.isLocalFile()) {191 _reply = NetworkAccessManager::getInstance().sendCustomRequest(_request, _method.toLatin1(), _sendData);
269 _reply = NetworkAccessManager::getInstance().sendCustomRequest(_request, _method.toLatin1(), _sendData);192 connectToReply(_reply);
270 connectToReply(_reply);
271 }
272 193
273 if (_timeout > 0) {194 if (_timeout > 0) {
274 _timer.start(_timeout);195 _timer.start(_timeout);
275 connect(&_timer, SIGNAL(timeout()), this, SLOT(requestTimeout()));196 connect(&_timer, SIGNAL(timeout()), this, SLOT(requestTimeout()));
276 }197 }
277
278 if (_url.isLocalFile()) {
279 setReadyState(HEADERS_RECEIVED);
280 setReadyState(LOADING);
281 _rawResponseData = _file->readAll();
282 _file->close();
283 requestFinished();
284 }
285}198}
286 199
287void XMLHttpRequestClass::requestTimeout() {200void XMLHttpRequestClass::requestTimeout() {
@@ -300,16 +213,10 @@ void XMLHttpRequestClass::requestError(QNetworkReply::NetworkError code) { · 12 unmodified lines
300void XMLHttpRequestClass::requestFinished() {213void XMLHttpRequestClass::requestFinished() {
301 disconnect(&_timer, SIGNAL(timeout()), this, SLOT(requestTimeout()));214 disconnect(&_timer, SIGNAL(timeout()), this, SLOT(requestTimeout()));
302 215
303 if (!_url.isLocalFile()) {216 _errorCode = _reply->error();
304 _errorCode = _reply->error();
305 } else {
306 _errorCode = QNetworkReply::NoError;
307 }
308 217
309 if (_errorCode == QNetworkReply::NoError) {218 if (_errorCode == QNetworkReply::NoError) {
310 if (!_url.isLocalFile()) {219 _rawResponseData.append(_reply->readAll());
311 _rawResponseData.append(_reply->readAll());
312 }
313 220
314 if (_responseType == "json") {221 if (_responseType == "json") {
315 _responseData = _engine->evaluate("(" + QString(_rawResponseData.data()) + ")");222 _responseData = _engine->evaluate("(" + QString(_rawResponseData.data()) + ")");
@@ -338,19 +245,6 @@ void XMLHttpRequestClass::abortRequest() { · 22 unmodified lines
338 _reply->deleteLater();245 _reply->deleteLater();
339 _reply = NULL;246 _reply = NULL;
340 }247 }
341
342 if (_file != NULL) {
343 _file->close();
344 _file = NULL;
345 }
346}
347
348void XMLHttpRequestClass::notImplemented() {
349 abortRequest();
350 //_errorCode = QNetworkReply::OperationNotImplementedError; TODO: Use this status code when update to Qt 5.3
351 _errorCode = QNetworkReply::ContentOperationNotPermittedError;
352 setReadyState(DONE);
353 emit requestComplete();
354}248}
355 249
356void XMLHttpRequestClass::connectToReply(QNetworkReply* reply) {250void XMLHttpRequestClass::connectToReply(QNetworkReply* reply) {