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

Commit

Permalink
Fix #12979
Browse files Browse the repository at this point in the history
  • Loading branch information
pixiuPL committed Jan 8, 2018
2 parents 66102e5 + f5ba243 commit f94b7e5
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 995 deletions.
259 changes: 191 additions & 68 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
#include <QWebFrame>
#include <QNetworkProxy>
#include "terminal.h"
#include "qcommandline.h"
#include "utils.h"
#include "consts.h"
#include <iostream>


<<<<<<< HEAD

This comment has been minimized.

Copy link
@yan12125

yan12125 Jan 9, 2018

This is a broken commit

static const struct QCommandLineConfigEntry flags[] =
{
{ QCommandLine::Option, '\0', "cookies-file", "Sets the file name to store the persistent cookies", QCommandLine::Optional },
Expand Down Expand Up @@ -77,17 +77,64 @@ static const struct QCommandLineConfigEntry flags[] =
{ QCommandLine::Switch, 'h', "help", "Shows this message and quits", QCommandLine::Optional },
{ QCommandLine::Switch, 'v', "version", "Prints out PhantomJS version", QCommandLine::Optional },
QCOMMANDLINE_CONFIG_ENTRY_END
=======
static const QCommandLineOption cookiesFileOption("cookies-file", "Sets the file name to store the persistent cookies", "cookies-file");
static const QCommandLineOption configOption("config", "Specifies JSON-formatted configuration file", "config");
static const QCommandLineOption debugOption("debug", "Prints additional warning and debug message: 'true' or 'false' (default)", "debug");
static const QCommandLineOption diskCacheOption("disk-cache", "Enables disk cache: 'true' or 'false' (default)", "disk-cache");
static const QCommandLineOption ignoreSslErrorsOption("ignore-ssl-errors", "Ignores SSL errors (expired/self-signed certificate errors): 'true' or 'false' (default)", "ignore-ssl-errors");
static const QCommandLineOption loadImagesOption("load-images", "Loads all inlined images: 'true' (default) or 'false'", "load-images");
static const QCommandLineOption localStoragePathOption("local-storage-path", "Specifies the location for offline local storage", "local-storage-path");
static const QCommandLineOption localStorageQuotaOption("local-storage-quota", "Sets the maximum size of the offline local storage (in KB)", "local-storage-quota");
static const QCommandLineOption localUrlAccessOption("local-url-access", "Allows use of 'file:///' URLs: 'true' (default) or 'false'", "local-url-access");
static const QCommandLineOption localToRemoteUrlAccessOption("local-to-remote-url-access", "Allows local content to access remote URL: 'true' or 'false' (default)", "local-to-remote-url-access");
static const QCommandLineOption maxDiskCacheSizeOption("max-disk-cache-size", "Limits the size of the disk cache (in KB)", "maxDiskCacheSizeOption");
static const QCommandLineOption outputEncodingOption("output-encoding", "Sets the encoding for the terminal output, default is 'utf8'", "output-encoding");
static const QCommandLineOption remoteDebuggerPortOption("remote-debugger-port", "Starts the script in a debug harness and listens on the specified port", "remote-debugger-port");
static const QCommandLineOption remoteDebuggerAutorunOption("remote-debugger-autorun", "Runs the script in the debugger immediately: 'true' or 'false' (default)", "remote-debugger-autorun");
static const QCommandLineOption proxyOption("proxy", "Sets the proxy server, e.g. '--proxy=http://proxy.company.com:8080'", "proxy");
static const QCommandLineOption proxyAuthOption("proxy-auth", "Provides authentication information for the proxy, e.g. ''-proxy-auth=username:password'", "proxy-auth");
static const QCommandLineOption proxyTypeOption("proxy-type", "Specifies the proxy type, 'http' (default), 'none' (disable completely), or 'socks5'", "proxy-type");
static const QCommandLineOption scriptEncodingOption("script-encoding", "Sets the encoding used for the starting script, default is 'utf8'", "script-encoding");
static const QCommandLineOption scriptLanguageOption("script-language", "Sets the script language instead of detecting it: 'javascript'", "script-language");
static const QCommandLineOption webSecurityOption("web-security", "Enables web security, 'true' (default) or 'false'", "web-security");
static const QCommandLineOption sslProtocolOption("ssl-protocol", "Selects a specific SSL protocol version to offer. Values (case insensitive): TLSv1.2, TLSv1.1, TLSv1.0, TLSv1 (same as v1.0), SSLv3, or ANY. Default is to offer all that Qt thinks are secure (SSLv3 and up). Not all values may be supported, depending on the system OpenSSL library.", "ssl-protocol");
static const QCommandLineOption sslCiphersOption("ssl-ciphers", "Sets supported TLS/SSL ciphers. Argument is a colon-separated list of OpenSSL cipher names (macros like ALL, kRSA, etc. may not be used). Default matches modern browsers.", "ssl-ciphers");
static const QCommandLineOption sslCertificatesPathOption("ssl-certificates-path", "Sets the location for custom CA certificates (if none set, uses system default)", "ssl-certificates-path");
static const QCommandLineOption webdriverLogfileOption("webdriver-logfile", "File where to write the WebDriver's Log (default 'none') (NOTE: needs '--webdriver') ", "webdriver-logfile");
static const QCommandLineOption webdriverLogLevelOption("webdriver-loglevel", "WebDriver Logging Level: (supported: 'ERROR', 'WARN', 'INFO', 'DEBUG') (default 'INFO') (NOTE: needs '--webdriver') ", "webdriver-loglever");
static const QCommandLineOption webdriverSeleniumGridHubOption("webdriver-selenium-grid-hub", "URL to the Selenium Grid HUB: 'URL_TO_HUB' (default 'none') (NOTE: needs '--webdriver') ", "webdriver-selenium-grid-hub");
static const QCommandLineOption webdriverAddressOption("webdriver-address", "Set the 'Remote WebDriver mode' address '[[<IP>:]<PORT>]' (default '127.0.0.1:8910') ", "webdriver-address", "127.0.0.1:8910");

static const QCommandLineOption scriptOption("script", "Script", "script");
static const QCommandLineOption scriptArgumentOption("scriptArgument", "Script argument", "scriptArgument");

static const QCommandLineOption helpOption({ "h", "help" }, "Shows this message and quits");
static const QCommandLineOption versionOption({ "v", "version" }, "Prints out PhantomJS version");
static const QCommandLineOption webdriverOption({ "w", "wd", "webdriver" }, "Will start PhantomJS in 'Remote WebDriver mode'");


struct UnknownOptionException {
const QString &m_optionName;
explicit UnknownOptionException(const QString &optionName)
:m_optionName(optionName){}
>>>>>>> 20da5b0a41b28bcc3b94ab761150338a59f5b84e
};



Config::Config(QObject *parent)
: QObject(parent)
{
<<<<<<< HEAD
m_cmdLine = new QCommandLine;

// We will handle --help and --version ourselves in phantom.cpp
m_cmdLine->enableHelp(false);
m_cmdLine->enableVersion(false);

=======
>>>>>>> 20da5b0a41b28bcc3b94ab761150338a59f5b84e
resetToDefaults();
}

Expand All @@ -104,14 +151,52 @@ void Config::init(const QStringList *const args)

void Config::processArgs(const QStringList &args)
{
connect(m_cmdLine, SIGNAL(switchFound(const QString &)), this, SLOT(handleSwitch(const QString &)));
connect(m_cmdLine, SIGNAL(optionFound(const QString &, const QVariant &)), this, SLOT(handleOption(const QString &, const QVariant &)));
connect(m_cmdLine, SIGNAL(paramFound(const QString &, const QVariant &)), this, SLOT(handleParam(const QString &, const QVariant &)));
connect(m_cmdLine, SIGNAL(parseError(const QString &)), this, SLOT(handleError(const QString &)));

m_cmdLine->setArguments(args);
m_cmdLine->setConfig(flags);
m_cmdLine->parse();
m_parser.addOption(helpOption);
m_parser.addOption(versionOption);
m_parser.addOption(cookiesFileOption);
m_parser.addOption(configOption);
m_parser.addOption(debugOption);
m_parser.addOption(diskCacheOption);
m_parser.addOption(ignoreSslErrorsOption);
m_parser.addOption(loadImagesOption);
m_parser.addOption(localStoragePathOption);
m_parser.addOption(localStorageQuotaOption);
m_parser.addOption(localUrlAccessOption);
m_parser.addOption(localToRemoteUrlAccessOption);
m_parser.addOption(maxDiskCacheSizeOption);
m_parser.addOption(outputEncodingOption);
m_parser.addOption(remoteDebuggerPortOption);
m_parser.addOption(remoteDebuggerAutorunOption);
m_parser.addOption(proxyOption);
m_parser.addOption(proxyAuthOption);
m_parser.addOption(proxyTypeOption);
m_parser.addOption(scriptEncodingOption);
m_parser.addOption(scriptLanguageOption);
m_parser.addOption(webSecurityOption);
m_parser.addOption(sslProtocolOption);
m_parser.addOption(sslCiphersOption);
m_parser.addOption(sslCertificatesPathOption);
m_parser.addOption(webdriverOption);
m_parser.addOption(webdriverAddressOption);
m_parser.addOption(webdriverLogfileOption);
m_parser.addOption(webdriverLogLevelOption);
m_parser.addOption(webdriverSeleniumGridHubOption);


if (!m_parser.parse(args)) {
handleError(m_parser.errorText());
return;
}

try {
handleSwitches();
handleOptions();
handleParams();
}
catch (const UnknownOptionException& unknownOptionException) {
setUnknownOption(QString("Invalid values for '%1' option.").arg(unknownOptionException.m_optionName));
return;
}

// Inject command line parameters to be picked up by GhostDriver
if (isWebdriverMode()) {
Expand Down Expand Up @@ -174,7 +259,7 @@ void Config::loadJsonFile(const QString &filePath)

QString Config::helpText() const
{
return m_cmdLine->help();
return m_parser.helpText();
}

bool Config::autoLoadImages() const
Expand Down Expand Up @@ -605,20 +690,26 @@ void Config::setPrintDebugMessages(const bool value)
m_printDebugMessages = value;
}

void Config::handleSwitch(const QString &sw)
void Config::handleSwitches()
{
setHelpFlag(sw == "help");
setVersionFlag(sw == "version");
setHelpFlag(m_parser.isSet(helpOption));
setVersionFlag(m_parser.isSet(versionOption));

if (sw == "wd") {
if (m_parser.isSet(webdriverOption)) {
setWebdriver(DEFAULT_WEBDRIVER_CONFIG);
}
}

void Config::handleOption(const QString &option, const QVariant &value)
bool Config::readBooleanValue(const QString& option, const QString &value) const
{
bool boolValue = false;
if ((value != "true") && (value != "yes") && (value != "false") && (value != "no")) {
throw UnknownOptionException(option);
}
return (value == "true") || (value == "yes");

}

<<<<<<< HEAD
QStringList booleanFlags;
booleanFlags << "debug";
booleanFlags << "disk-cache";
Expand All @@ -633,115 +724,147 @@ void Config::handleOption(const QString &option, const QVariant &value)
return;
}
boolValue = (value == "true") || (value == "yes");
=======
void Config::handleOptions()
{
if (m_parser.isSet(cookiesFileOption)) {
setCookiesFile(m_parser.value(cookiesFileOption));
>>>>>>> 20da5b0a41b28bcc3b94ab761150338a59f5b84e
}

if (option == "cookies-file") {
setCookiesFile(value.toString());
if (m_parser.isSet(configOption)) {
loadJsonFile(m_parser.value(configOption));
}

if (option == "config") {
loadJsonFile(value.toString());
if (m_parser.isSet(debugOption)) {
setPrintDebugMessages(readBooleanValue(debugOption.valueName(), m_parser.value(debugOption)));
}

if (option == "debug") {
setPrintDebugMessages(boolValue);
if (m_parser.isSet(diskCacheOption)) {
setDiskCacheEnabled(readBooleanValue(diskCacheOption.valueName(), m_parser.value(diskCacheOption)));
}

if (m_parser.isSet(ignoreSslErrorsOption)) {
setIgnoreSslErrors(readBooleanValue(ignoreSslErrorsOption.valueName(), m_parser.value(ignoreSslErrorsOption)));
}

if (option == "disk-cache") {
setDiskCacheEnabled(boolValue);
if (m_parser.isSet(loadImagesOption)) {
setAutoLoadImages(readBooleanValue(loadImagesOption.valueName(), m_parser.value(loadImagesOption)));
}

if (option == "ignore-ssl-errors") {
setIgnoreSslErrors(boolValue);
if (m_parser.isSet(localStoragePathOption)) {
setOfflineStoragePath(m_parser.value(localStoragePathOption));
}

if (option == "load-images") {
setAutoLoadImages(boolValue);
if (m_parser.isSet(localStorageQuotaOption)) {
setOfflineStorageDefaultQuota(m_parser.value(localStorageQuotaOption).toInt());
}

if (option == "local-storage-path") {
setOfflineStoragePath(value.toString());
if (m_parser.isSet(localUrlAccessOption)) {
setLocalUrlAccessEnabled(readBooleanValue(localUrlAccessOption.valueName(), m_parser.value(localUrlAccessOption)));
}

if (option == "local-storage-quota") {
setOfflineStorageDefaultQuota(value.toInt());
if (m_parser.isSet(localToRemoteUrlAccessOption)) {
setLocalToRemoteUrlAccessEnabled(readBooleanValue(localToRemoteUrlAccessOption.valueName(), m_parser.value(localToRemoteUrlAccessOption)));
}

<<<<<<< HEAD
if (option == "local-to-remote-url-access") {
setLocalToRemoteUrlAccessEnabled(boolValue);
=======
if (m_parser.isSet(maxDiskCacheSizeOption)) {
setMaxDiskCacheSize(m_parser.value(maxDiskCacheSizeOption).toInt());
}

if (option == "max-disk-cache-size") {
setMaxDiskCacheSize(value.toInt());
if (m_parser.isSet(outputEncodingOption)) {
setOutputEncoding(m_parser.value(outputEncodingOption));
>>>>>>> 20da5b0a41b28bcc3b94ab761150338a59f5b84e
}

if (option == "output-encoding") {
setOutputEncoding(value.toString());
if (m_parser.isSet(remoteDebuggerAutorunOption)) {
setRemoteDebugAutorun(readBooleanValue(remoteDebuggerAutorunOption.valueName(), m_parser.value(remoteDebuggerAutorunOption)));
}

if (option == "remote-debugger-autorun") {
setRemoteDebugAutorun(boolValue);
if (m_parser.isSet(remoteDebuggerPortOption)) {
setDebug(true);
setRemoteDebugPort(m_parser.value(remoteDebuggerPortOption).toInt());
}

if (option == "remote-debugger-port") {
setDebug(true);
setRemoteDebugPort(value.toInt());
if (m_parser.isSet(proxyOption)) {
setProxy(m_parser.value(proxyOption));
}

if (m_parser.isSet(proxyTypeOption)) {
setProxyType(m_parser.value(proxyTypeOption));
}

if (option == "proxy") {
setProxy(value.toString());
if (m_parser.isSet(proxyAuthOption)) {
setProxyAuth(m_parser.value(proxyAuthOption));
}

if (option == "proxy-type") {
setProxyType(value.toString());
if (m_parser.isSet(scriptEncodingOption)) {
setScriptEncoding(m_parser.value(scriptEncodingOption));
}

if (option == "proxy-auth") {
setProxyAuth(value.toString());
if (m_parser.isSet(scriptLanguageOption)) {
setScriptLanguage(m_parser.value(scriptLanguageOption));
}

if (option == "script-encoding") {
setScriptEncoding(value.toString());
if (m_parser.isSet(webSecurityOption)) {
setWebSecurityEnabled(readBooleanValue(webSecurityOption.valueName(), m_parser.value(webSecurityOption)));
}

if (option == "script-language") {
setScriptLanguage(value.toString());
if (m_parser.isSet(sslProtocolOption)) {
setSslProtocol(m_parser.value(sslProtocolOption));
}

if (option == "web-security") {
setWebSecurityEnabled(boolValue);
if (m_parser.isSet(sslCiphersOption)) {
setSslCiphers(m_parser.value(sslCiphersOption));
}
<<<<<<< HEAD
if (option == "ssl-protocol") {
setSslProtocol(value.toString());
}
if (option == "ssl-certificates-path") {
setSslCertificatesPath(value.toString());
=======

if (m_parser.isSet(sslCertificatesPathOption)) {
setSslCertificatesPath(m_parser.value(sslCertificatesPathOption));
>>>>>>> 20da5b0a41b28bcc3b94ab761150338a59f5b84e
}
if (option == "webdriver") {
setWebdriver(value.toString().length() > 0 ? value.toString() : DEFAULT_WEBDRIVER_CONFIG);

if (m_parser.isSet(webdriverAddressOption) ) {
setWebdriver(m_parser.value(webdriverAddressOption));
}
if (option == "webdriver-logfile") {
setWebdriverLogFile(value.toString());

if (m_parser.isSet(webdriverLogfileOption)) {
setWebdriverLogFile(m_parser.value(webdriverLogfileOption));
}
if (option == "webdriver-loglevel") {
setWebdriverLogLevel(value.toString());

if (m_parser.isSet(webdriverLogLevelOption)) {
setWebdriverLogLevel(m_parser.value(webdriverLogLevelOption));
}
if (option == "webdriver-selenium-grid-hub") {
setWebdriverSeleniumGridHub(value.toString());

if (m_parser.isSet(webdriverSeleniumGridHubOption)) {
setWebdriverSeleniumGridHub(m_parser.value(webdriverSeleniumGridHubOption));
}
if (option == "webdriver-selenium-grid-remote-proxy-class") {
setWebdriverSeleniumGridRemoteProxyClass(value.toString());
}
}

void Config::handleParam(const QString& param, const QVariant &value)
void Config::handleParams()
{
Q_UNUSED(param);
QStringList positionalArguments = m_parser.positionalArguments();

if (m_scriptFile.isEmpty())
m_scriptFile = value.toString();
else
m_scriptArgs += value.toString();
if (!positionalArguments.isEmpty()) {
m_scriptFile = positionalArguments.takeFirst();
}

if (!positionalArguments.isEmpty()) {
m_scriptArgs += positionalArguments;
}
}

void Config::handleError(const QString &error)
Expand Down Expand Up @@ -774,7 +897,7 @@ void Config::setSslCertificatesPath(const QString& sslCertificatesPath)
m_sslCertificatesPath = sslCertificatesPath + "/*";
} else {
m_sslCertificatesPath = sslCertificatesPath;
}
}
}

void Config::setWebdriverSeleniumGridRemoteProxyClass(const QString& webdriverSeleniumGridRemoteProxyClass)
Expand Down
Loading

0 comments on commit f94b7e5

Please sign in to comment.