Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Code refactoring #3
Browse files Browse the repository at this point in the history
  • Loading branch information
pixiupl committed Jan 15, 2018
1 parent e6250e5 commit 05d8328
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 65 deletions.
10 changes: 3 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@
#include <QtPlugin>
#include <QSslSocket>
#include <QWebSettings>

#include "consts.h"
#include "crashdump.h"
#include "phantom.h"

#if defined(Q_OS_LINUX) && defined(QT_STATIC)
if defined(Q_OS_LINUX) && defined(QT_STATIC)
Q_IMPORT_PLUGIN(PhantomIntegrationPlugin);
#endif
endif

static bool g_printDebugMessages;
void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
Expand Down Expand Up @@ -76,9 +75,7 @@ static int inner_main(int argc, char** argv)

// Registering an alternative Message Handler
qInstallMessageHandler(messageHandler);

QApplication app(argc, argv);

app.setWindowIcon(QIcon(":/phantomjs-icon.png"));
app.setApplicationName("PhantomJS");
app.setOrganizationName("Ofi Labs");
Expand Down Expand Up @@ -159,5 +156,4 @@ int main(int argc, char** argv)
print_crash_message();
return 1;
}
#endif
}
#endif}
15 changes: 1 addition & 14 deletions src/networkaccessmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ NetworkAccessManager::NetworkAccessManager(QObject* parent, const Config* config


connect(this, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), SLOT(provideAuthentication(QNetworkReply*, QAuthenticator*)));

connect(&m_replyTracker, SIGNAL(started(QNetworkReply*, int)), this, SLOT(handleStarted(QNetworkReply*, int)));
connect(&m_replyTracker, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)), this, SLOT(handleSslErrors(QNetworkReply*, const QList<QSslError>&)));
connect(&m_replyTracker, SIGNAL(error(QNetworkReply*, int, QNetworkReply::NetworkError)), this, SLOT(handleNetworkError(QNetworkReply*, int)));
Expand All @@ -205,7 +204,7 @@ void NetworkAccessManager::prepareSslConfiguration(const Config* config)
break;
}
}
// FIXME: actually object to an invalid setting.

if (!setProtocol) {
m_sslConfiguration.setProtocol(QSsl::SecureProtocols);
}
Expand Down Expand Up @@ -318,11 +317,6 @@ void NetworkAccessManager::compileCaptureContentPatterns()
void NetworkAccessManager::setCookieJar(QNetworkCookieJar* cookieJar)
{
QNetworkAccessManager::setCookieJar(cookieJar);
// Remove NetworkAccessManager's ownership of this CookieJar and
// pass it to the PhantomJS Singleton object.
// CookieJar is shared between multiple instances of NetworkAccessManager.
// It shouldn't be deleted when the NetworkAccessManager is deleted, but
// only when close is called on the cookie jar.
cookieJar->setParent(Phantom::instance());
}

Expand Down Expand Up @@ -478,15 +472,8 @@ void NetworkAccessManager::handleStarted(QNetworkReply* reply, int requestId)
data["headers"] = headers;
data["requestHeaders"] = requestHeaders;
data["time"] = QDateTime::currentDateTime();
<<<<<<< HEAD
data["body"] = "";
=======
data["fromCache"] = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool();
>>>>>>> f1f3cd7b5aa0815028f4a18a65a1a7b76c41ade0

QWebFrame *frame = qobject_cast<QWebFrame *>(reply->request().originatingObject());
data["frameName"] = frame->frameName();

emit resourceReceived(data);
}

Expand Down
4 changes: 0 additions & 4 deletions src/networkaccessmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

#ifndef NETWORKACCESSMANAGER_H
#define NETWORKACCESSMANAGER_H

#include <QAuthenticator>
#include <QNetworkAccessManager>
#include <QNetworkReply>
Expand All @@ -42,7 +41,6 @@
class Config;
class QNetworkDiskCache;
class QSslConfiguration;

class TimeoutTimer : public QTimer
{
Q_OBJECT
Expand Down Expand Up @@ -123,15 +121,13 @@ private slots:
bool shouldCaptureResponse(const QString& url);
void compileCaptureContentPatterns();
void prepareSslConfiguration(const Config* config);

int m_idCounter;
QVariant m_lastHttpStatus;
QNetworkDiskCache* m_networkDiskCache;
QVariantMap m_customHeaders;
QStringList m_captureContentPatterns;
QList<QRegExp> m_compiledCaptureContentPatterns;
QSslConfiguration m_sslConfiguration;

NetworkReplyTracker m_replyTracker;
};

Expand Down
13 changes: 2 additions & 11 deletions src/networkreplyproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ NetworkReplyProxy::NetworkReplyProxy(QObject* parent, QNetworkReply* reply,

QString NetworkReplyProxy::body()
{
//TODO converting data to QString in a special way(without charset encoding),
//similar to File::read method in 'filesystem' module.
QString ret(m_data.size(), ' ');
QString ret(m_data.size(), ' ');
for (int i = 0; i < m_data.size(); ++i) {
ret[i] = m_data.at(i);
}

return ret;
return ret;
}

int NetworkReplyProxy::bodySize()
Expand Down Expand Up @@ -120,12 +117,6 @@ void NetworkReplyProxy::ignoreSslErrors()

void NetworkReplyProxy::applyMetaData()
{
/*
We have to store headers and attributes, otherwise
QNetworkReply non-virtual methods (attribute, header, etc.)
would not have data to return.
*/

QList<QByteArray> headers = m_reply->rawHeaderList();
foreach (QByteArray header, headers) {
setRawHeader(header, m_reply->rawHeader(header));
Expand Down
15 changes: 2 additions & 13 deletions src/networkreplytracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,22 @@
#include "networkreplyproxy.h"


NetworkReplyTracker::NetworkReplyTracker(QObject* parent)
: QObject(parent)
NetworkReplyTracker::NetworkReplyTracker(QObject* parent): QObject(parent)
{
// TODO body
// TODO body. Once done, remove TODO marker and test.
}


QNetworkReply* NetworkReplyTracker::trackReply(QNetworkReply* reply, int requestId,
bool shouldCaptureResponseBody)
{
NetworkReplyProxy* proxy = new NetworkReplyProxy(this, reply, shouldCaptureResponseBody);

/*
Tracking link between proxy and proxied reply.
Its possible to get nested reply as a slot argument instead of proxy.
QNetworkAccessManager::createRequest stores original reply internally
and passes it in certain conditions as a signal argument -
authenticationRequire slot, for instance, will receive original reply not the proxy.
*/
m_replies[reply] = proxy;
m_ids[proxy] = requestId;

connect(proxy, SIGNAL(readyRead()), this, SLOT(handleIncomingData()));
connect(proxy, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(handleNetworkError(QNetworkReply::NetworkError)));
connect(proxy, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(handleSslErrors(const QList<QSslError>&)));
connect(proxy, SIGNAL(finished()), SLOT(handleReplyFinished()));

return proxy;
}

Expand Down
17 changes: 1 addition & 16 deletions src/networkreplytracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,13 @@
#include <QSet>
#include <QNetworkReply>


class QSslError;
class NetworkReplyProxy;

/*
This class tracks replies and converts reply signals
to phantomjs specific ones. It uses NetworkReplyProxy to collect
body of the response and passes body as a signal argument.
*/
class NetworkReplyTracker: public QObject
{
Q_OBJECT
public:

NetworkReplyTracker(QObject* parent = 0);


public: NetworkReplyTracker(QObject* parent = 0);
/*
reply - reply to track
requestId - unique request id, used to distinguis replies internally
Expand All @@ -60,25 +50,20 @@ class NetworkReplyTracker: public QObject
QNetworkReply* trackReply(QNetworkReply* reply, int requestId,
bool shouldCaptureResponseBody);


/*
Abort request
status - set custom HTTP status code
statusString - text description of status code
*/
void abort(QNetworkReply* reply, int status, const QString& statusString);


signals:

void started(QNetworkReply* reply, int requestId);
void finished(QNetworkReply* reply, int requestId, int status, const QString& statusText, const QString& body, int bodySize);
void sslErrors(QNetworkReply*, const QList<QSslError>&);
void error(QNetworkReply*, int requestId, QNetworkReply::NetworkError);


private slots:

void handleIncomingData();
void handleReplyFinished();
void handleSslErrors(const QList<QSslError>& errors);
Expand Down

0 comments on commit 05d8328

Please sign in to comment.