Skip to content

Commit

Permalink
Added default user-agent to get requests (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkjr authored Dec 26, 2018
1 parent 2a76c43 commit 564b9dc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions ReactQt/runtime/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ set(
asynclocalstorage.cpp
reactitem.cpp
rootview.cpp
reactnetworkaccessmanager.cpp
mouseeventsinterceptor.cpp
testmodule.cpp
attachedproperties.cpp
Expand Down
1 change: 1 addition & 0 deletions ReactQt/runtime/src/networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
27 changes: 27 additions & 0 deletions ReactQt/runtime/src/reactnetworkaccessmanager.cpp
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 34 additions & 0 deletions ReactQt/runtime/src/reactnetworkaccessmanager.h
Original file line number Diff line number Diff line change
@@ -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 <QNetworkAccessManager>
#include <QQmlNetworkAccessManagerFactory>

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
4 changes: 4 additions & 0 deletions ReactQt/runtime/src/rootview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 564b9dc

Please sign in to comment.