From a7a1322ea12d716559add8f2e46ad6aded5c7f11 Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Sun, 13 Mar 2011 21:35:05 -0700 Subject: [PATCH] Fixed issue #19: option for setting the proxy. Based on the original patch by Clint Berry. --- ChangeLog | 2 ++ src/phantomjs.cpp | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 809c18c2..f07b4cfe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -24,6 +24,8 @@ Version 1.1.0 Added support for CoffeeScript (Ariya Hidayat). + Fixed issue #19: option for setting the proxy (Clint Berry, Ariya Hidayat). + 2011-01-17: Version 1.0.0 Initial launch. diff --git a/src/phantomjs.cpp b/src/phantomjs.cpp index bb41dd85..4c95538c 100644 --- a/src/phantomjs.cpp +++ b/src/phantomjs.cpp @@ -50,8 +50,9 @@ void showUsage() std::cerr << "Options:" << std::endl; std::cerr << "\t--load-images=[yes|no]\t\tLoad all inlined images (default is 'yes')." << std::endl; std::cerr << "\t--load-plugins=[yes|no]\tLoad all plugins (i.e. 'Flash', 'Silverlight', ...) (default is 'no')." << std::endl; + std::cerr << "\t--proxy=address:port\tSet the network proxy." << std::endl; std::cerr << "\t--upload-file fileId=/file/path\tUpload a file by creating a ''\n" - "\t\t\t\tand calling phantom.setFormInputFile(document.getElementById('foo', 'fileId')." << std::endl; + "\t\t\t\tand calling phantom.setFormInputFile(document.getElementById('foo', 'fileId')." << std::endl; } class WebPage: public QWebPage @@ -165,6 +166,8 @@ private slots: private: QString m_scriptFile; QStringList m_args; + QString m_proxyHost; + int m_proxyPort; QString m_loadStatus; WebPage m_page; int m_returnValue; @@ -175,6 +178,7 @@ private: Phantom::Phantom(QObject *parent) : QObject(parent) + , m_proxyPort(1080) , m_returnValue(0) , m_converter(0) { @@ -219,6 +223,18 @@ Phantom::Phantom(QObject *parent) pluginsEnabled = false; continue; } + if (arg.startsWith("--proxy=")) { + m_proxyHost = arg.mid(8).trimmed(); + if (m_proxyHost.lastIndexOf(':') > 0) { + bool ok = true; + int port = m_proxyHost.mid(m_proxyHost.lastIndexOf(':') + 1).toInt(&ok); + if (ok) { + m_proxyHost = m_proxyHost.left(m_proxyHost.lastIndexOf(':')).trimmed(); + m_proxyPort = port; + } + } + continue; + } if (arg.startsWith("--")) { std::cerr << "Unknown option '" << qPrintable(arg) << "'" << std::endl; exit(-1); @@ -234,6 +250,13 @@ Phantom::Phantom(QObject *parent) return; } + if (m_proxyHost.isEmpty()) { + QNetworkProxyFactory::setUseSystemConfiguration(true); + } else { + QNetworkProxy proxy(QNetworkProxy::HttpProxy, m_proxyHost, m_proxyPort); + QNetworkProxy::setApplicationProxy(proxy); + } + // The remaining arguments are available for the script. while (argIterator.hasNext()) { const QString &arg = argIterator.next(); @@ -460,8 +483,6 @@ int main(int argc, char** argv) return 1; } - QNetworkProxyFactory::setUseSystemConfiguration(true); - QApplication app(argc, argv); app.setWindowIcon(QIcon(":/phantomjs-icon.png"));