diff --git a/ReactQt/runtime/src/CMakeLists.txt b/ReactQt/runtime/src/CMakeLists.txt index dd3b6c2f8..f3b6fc062 100644 --- a/ReactQt/runtime/src/CMakeLists.txt +++ b/ReactQt/runtime/src/CMakeLists.txt @@ -125,6 +125,7 @@ set( asynclocalstorage.cpp reactitem.cpp rootview.cpp + reactnetworkaccessmanager.cpp mouseeventsinterceptor.cpp testmodule.cpp attachedproperties.cpp diff --git a/ReactQt/runtime/src/networking.cpp b/ReactQt/runtime/src/networking.cpp index edd4aa861..521313687 100644 --- a/ReactQt/runtime/src/networking.cpp +++ b/ReactQt/runtime/src/networking.cpp @@ -87,6 +87,7 @@ void Networking::sendRequest(const QString& method, QNetworkRequest request(url); + request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy); for (const QString& key : headers.keys()) { request.setRawHeader(key.toLocal8Bit(), headers[key].toString().toLocal8Bit()); } diff --git a/ReactQt/runtime/src/reactnetworkaccessmanager.cpp b/ReactQt/runtime/src/reactnetworkaccessmanager.cpp new file mode 100644 index 000000000..83a1c7bec --- /dev/null +++ b/ReactQt/runtime/src/reactnetworkaccessmanager.cpp @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2017-present, Status Research and Development GmbH. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "reactnetworkaccessmanager.h" + +ReactNetworkAccessManager::ReactNetworkAccessManager(QObject* parent) : QNetworkAccessManager(parent) {} + +QNetworkReply* ReactNetworkAccessManager::createRequest(QNetworkAccessManager::Operation op, + const QNetworkRequest& originalReq, + QIODevice* outgoingData) { + QNetworkRequest requestWithHeader = originalReq; + requestWithHeader.setRawHeader("User-Agent", "react-native-desktop"); + return QNetworkAccessManager::createRequest(op, requestWithHeader, outgoingData); +} + +QNetworkAccessManager* ReactNetworkAccessManagerFactory::create(QObject* parent) { + return new ReactNetworkAccessManager(parent); +} + +#include "reactnetworkaccessmanager.moc" diff --git a/ReactQt/runtime/src/reactnetworkaccessmanager.h b/ReactQt/runtime/src/reactnetworkaccessmanager.h new file mode 100644 index 000000000..1c71e6ba4 --- /dev/null +++ b/ReactQt/runtime/src/reactnetworkaccessmanager.h @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2017-present, Status Research and Development GmbH. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#ifndef REACTNETWORKACCESSMANAGER_H +#define REACTNETWORKACCESSMANAGER_H + +#include +#include + +class ReactNetworkAccessManager : public QNetworkAccessManager { + Q_OBJECT + +public: + ReactNetworkAccessManager(QObject* parent = nullptr); + +protected: + virtual QNetworkReply* createRequest(QNetworkAccessManager::Operation op, + const QNetworkRequest& originalReq, + QIODevice* outgoingData = nullptr); +}; + +class ReactNetworkAccessManagerFactory : public QQmlNetworkAccessManagerFactory { +public: + QNetworkAccessManager* create(QObject* parent) override; +}; + +#endif // REACTNETWORKACCESSMANAGER_H diff --git a/ReactQt/runtime/src/rootview.cpp b/ReactQt/runtime/src/rootview.cpp index 893994317..631dfd1d8 100644 --- a/ReactQt/runtime/src/rootview.cpp +++ b/ReactQt/runtime/src/rootview.cpp @@ -21,6 +21,7 @@ #include "bridge.h" #include "eventdispatcher.h" #include "layout/flexbox.h" +#include "reactnetworkaccessmanager.h" #include "rootview.h" #include "uimanager.h" #include "utilities.h" @@ -311,6 +312,9 @@ void RootView::componentComplete() { loadDevMenu(); #endif // RCT_DEV + ReactNetworkAccessManagerFactory* networkManagerFactory = new ReactNetworkAccessManagerFactory(); + qmlEngine(this)->setNetworkAccessManagerFactory(networkManagerFactory); + QTimer::singleShot(0, [=]() { // TODO: setQmlEngine && setNetworkAccessManager to be just setQmlEngine && then internal? d->bridge->setQmlEngine(qmlEngine(this));