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