Skip to content
Browse files

Merge in upstream changes and remove Capybara references.

  • Loading branch information...
1 parent 99d8f10 commit ab1004abda79a764f85064dc95197abb190b9a64 @tristandunn committed Oct 17, 2011
View
29 src/CommandFactory.cpp
@@ -0,0 +1,29 @@
+#include "CommandFactory.h"
+#include "Visit.h"
+#include "Find.h"
+#include "Command.h"
+#include "Reset.h"
+#include "Node.h"
+#include "Url.h"
+#include "Source.h"
+#include "Evaluate.h"
+#include "Execute.h"
+#include "FrameFocus.h"
+#include "Header.h"
+#include "Render.h"
+#include "Body.h"
+#include "Status.h"
+#include "Headers.h"
+#include "SetCookie.h"
+#include "ClearCookies.h"
+#include "GetCookies.h"
+#include "SetProxy.h"
+
+CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
+ m_page = page;
+}
+
+Command *CommandFactory::createCommand(const char *name) {
+ #include "find_command.h"
+ return NULL;
+}
View
16 src/CommandFactory.h
@@ -0,0 +1,16 @@
+#include <QObject>
+
+class Command;
+class WebPage;
+
+class CommandFactory : public QObject {
+ Q_OBJECT
+
+ public:
+ CommandFactory(WebPage *page, QObject *parent = 0);
+ Command *createCommand(const char *name);
+
+ private:
+ WebPage *m_page;
+};
+
View
68 src/CommandParser.cpp
@@ -0,0 +1,68 @@
+#include "CommandParser.h"
+
+#include <QIODevice>
+
+CommandParser::CommandParser(QIODevice *device, QObject *parent) :
+ QObject(parent) {
+ m_device = device;
+ m_expectingDataSize = -1;
+ connect(m_device, SIGNAL(readyRead()), this, SLOT(checkNext()));
+}
+
+void CommandParser::checkNext() {
+ if (m_expectingDataSize == -1) {
+ if (m_device->canReadLine()) {
+ readLine();
+ checkNext();
+ }
+ } else {
+ if (m_device->bytesAvailable() >= m_expectingDataSize) {
+ readDataBlock();
+ checkNext();
+ }
+ }
+}
+
+void CommandParser::readLine() {
+ char buffer[128];
+ qint64 lineLength = m_device->readLine(buffer, 128);
+ if (lineLength != -1) {
+ buffer[lineLength - 1] = 0;
+ processNext(buffer);
+ }
+}
+
+void CommandParser::readDataBlock() {
+ char *buffer = new char[m_expectingDataSize + 1];
+ m_device->read(buffer, m_expectingDataSize);
+ buffer[m_expectingDataSize] = 0;
+ processNext(buffer);
+ m_expectingDataSize = -1;
+ delete[] buffer;
+}
+
+void CommandParser::processNext(const char *data) {
+ if (m_commandName.isNull()) {
+ m_commandName = data;
+ m_argumentsExpected = -1;
+ } else {
+ processArgument(data);
+ }
+}
+
+void CommandParser::processArgument(const char *data) {
+ if (m_argumentsExpected == -1) {
+ m_argumentsExpected = QString(data).toInt();
+ } else if (m_expectingDataSize == -1) {
+ m_expectingDataSize = QString(data).toInt();
+ } else {
+ m_arguments.append(QString::fromUtf8(data));
+ }
+
+ if (m_arguments.length() == m_argumentsExpected) {
+ emit commandReady(m_commandName, m_arguments);
+ m_commandName = QString();
+ m_arguments.clear();
+ m_argumentsExpected = -1;
+ }
+}
View
29 src/CommandParser.h
@@ -0,0 +1,29 @@
+#include <QObject>
+#include <QStringList>
+
+class QIODevice;
+
+class CommandParser : public QObject {
+ Q_OBJECT
+
+ public:
+ CommandParser(QIODevice *device, QObject *parent = 0);
+
+ public slots:
+ void checkNext();
+
+ signals:
+ void commandReady(QString commandName, QStringList arguments);
+
+ private:
+ void readLine();
+ void readDataBlock();
+ void processNext(const char *line);
+ void processArgument(const char *data);
+ QIODevice *m_device;
+ QString m_commandName;
+ QStringList m_arguments;
+ int m_argumentsExpected;
+ int m_expectingDataSize;
+};
+
View
99 src/Connection.cpp
@@ -1,24 +1,9 @@
#include "Connection.h"
#include "WebPage.h"
#include "UnsupportedContentHandler.h"
-#include "Visit.h"
-#include "Find.h"
+#include "CommandParser.h"
+#include "CommandFactory.h"
#include "Command.h"
-#include "Reset.h"
-#include "Node.h"
-#include "Url.h"
-#include "Source.h"
-#include "Evaluate.h"
-#include "Execute.h"
-#include "FrameFocus.h"
-#include "Header.h"
-#include "Render.h"
-#include "Body.h"
-#include "Status.h"
-#include "Headers.h"
-#include "SetCookie.h"
-#include "ClearCookies.h"
-#include "GetCookies.h"
#include <QTcpSocket>
#include <iostream>
@@ -27,84 +12,39 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) {
m_socket = socket;
m_page = page;
+ m_commandParser = new CommandParser(socket, this);
+ m_commandFactory = new CommandFactory(page, this);
m_command = NULL;
- m_expectingDataSize = -1;
m_pageSuccess = true;
m_commandWaiting = false;
- connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
+ connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
+ connect(m_commandParser, SIGNAL(commandReady(QString, QStringList)), this, SLOT(commandReady(QString, QStringList)));
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
}
-void Connection::checkNext() {
- if (m_expectingDataSize == -1) {
- if (m_socket->canReadLine()) {
- readLine();
- checkNext();
- }
- } else {
- if (m_socket->bytesAvailable() >= m_expectingDataSize) {
- readDataBlock();
- checkNext();
- }
- }
-}
-
-void Connection::readLine() {
- char buffer[128];
- qint64 lineLength = m_socket->readLine(buffer, 128);
- if (lineLength != -1) {
- buffer[lineLength - 1] = 0;
- processNext(buffer);
- }
-}
-
-void Connection::readDataBlock() {
- char *buffer = new char[m_expectingDataSize + 1];
- m_socket->read(buffer, m_expectingDataSize);
- buffer[m_expectingDataSize] = 0;
- processNext(buffer);
- m_expectingDataSize = -1;
- delete[] buffer;
-}
-
-void Connection::processNext(const char *data) {
- if (m_commandName.isNull()) {
- m_commandName = data;
- m_argumentsExpected = -1;
- } else {
- processArgument(data);
- }
-}
-void Connection::processArgument(const char *data) {
- if (m_argumentsExpected == -1) {
- m_argumentsExpected = QString(data).toInt();
- } else if (m_expectingDataSize == -1) {
- m_expectingDataSize = QString(data).toInt();
- } else {
- m_arguments.append(QString::fromUtf8(data));
- }
+void Connection::commandReady(QString commandName, QStringList arguments) {
+ m_commandName = commandName;
+ m_arguments = arguments;
- if (m_arguments.length() == m_argumentsExpected) {
- if (m_page->isLoading())
- m_commandWaiting = true;
- else
- startCommand();
- }
+ if (m_page->isLoading())
+ m_commandWaiting = true;
+ else
+ startCommand();
}
void Connection::startCommand() {
m_commandWaiting = false;
if (m_pageSuccess) {
- m_command = createCommand(m_commandName.toAscii().constData());
+ m_command = m_commandFactory->createCommand(m_commandName.toAscii().constData());
if (m_command) {
connect(m_command,
SIGNAL(finished(Response *)),
this,
SLOT(finishCommand(Response *)));
m_command->start(m_arguments);
} else {
- QString failure = QString("[Capybara WebKit] Unknown command: ") + m_commandName + "\n";
+ QString failure = QString("[WebKitServer] Unknown command: ") + m_commandName + "\n";
writeResponse(new Response(false, failure));
}
m_commandName = QString();
@@ -115,11 +55,6 @@ void Connection::startCommand() {
}
}
-Command *Connection::createCommand(const char *name) {
- #include "find_command.h"
- return NULL;
-}
-
void Connection::pendingLoadFinished(bool success) {
m_pageSuccess = success;
if (m_commandWaiting)
@@ -143,9 +78,5 @@ void Connection::writeResponse(Response *response) {
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
delete response;
-
- m_arguments.clear();
- m_commandName = QString();
- m_argumentsExpected = -1;
}
View
13 src/Connection.h
@@ -5,6 +5,8 @@ class QTcpSocket;
class WebPage;
class Command;
class Response;
+class CommandParser;
+class CommandFactory;
class Connection : public QObject {
Q_OBJECT
@@ -13,26 +15,21 @@ class Connection : public QObject {
Connection(QTcpSocket *socket, WebPage *page, QObject *parent = 0);
public slots:
- void checkNext();
+ void commandReady(QString commandName, QStringList arguments);
void finishCommand(Response *response);
void pendingLoadFinished(bool success);
private:
- void readLine();
- void readDataBlock();
- void processNext(const char *line);
- Command *createCommand(const char *name);
- void processArgument(const char *line);
void startCommand();
void writeResponse(Response *response);
QTcpSocket *m_socket;
QString m_commandName;
Command *m_command;
QStringList m_arguments;
- int m_argumentsExpected;
WebPage *m_page;
- int m_expectingDataSize;
+ CommandParser *m_commandParser;
+ CommandFactory *m_commandFactory;
bool m_pageSuccess;
bool m_commandWaiting;
};
View
2 src/Find.cpp
@@ -7,7 +7,7 @@ Find::Find(WebPage *page, QObject *parent) : Command(page, parent) {
void Find::start(QStringList &arguments) {
QString message;
- QVariant result = page()->invokeCapybaraFunction("find", arguments);
+ QVariant result = page()->invokeWebKitServerFunction("find", arguments);
if (result.isValid()) {
message = result.toString();
View
2 src/Node.cpp
@@ -7,7 +7,7 @@ Node::Node(WebPage *page, QObject *parent) : Command(page, parent) {
void Node::start(QStringList &arguments) {
QStringList functionArguments(arguments);
QString functionName = functionArguments.takeFirst();
- QVariant result = page()->invokeCapybaraFunction(functionName, functionArguments);
+ QVariant result = page()->invokeWebKitServerFunction(functionName, functionArguments);
QString attributeValue = result.toString();
emit finished(new Response(true, attributeValue));
}
View
3 src/Server.cpp
@@ -4,9 +4,10 @@
#include <QTcpServer>
-Server::Server(QObject *parent) : QObject(parent) {
+Server::Server(QObject *parent, bool ignoreSslErrors) : QObject(parent) {
m_tcp_server = new QTcpServer(this);
m_page = new WebPage(this);
+ m_page->setIgnoreSslErrors(ignoreSslErrors);
}
bool Server::start() {
View
2 src/Server.h
@@ -7,7 +7,7 @@ class Server : public QObject {
Q_OBJECT
public:
- Server(QObject *parent = 0);
+ Server(QObject *parent, bool ignoreSslErrors);
bool start();
quint16 server_port() const;
View
24 src/SetProxy.cpp
@@ -0,0 +1,24 @@
+#include "SetProxy.h"
+#include "WebPage.h"
+#include <QNetworkAccessManager>
+#include <QNetworkProxy>
+
+SetProxy::SetProxy(WebPage *page, QObject *parent)
+ : Command(page, parent)
+{ }
+
+void SetProxy::start(QStringList &arguments)
+{
+ // default to empty proxy
+ QNetworkProxy proxy;
+
+ if (arguments.size() > 0)
+ proxy = QNetworkProxy(QNetworkProxy::HttpProxy,
+ arguments[0],
+ (quint16)(arguments[1].toInt()),
+ arguments[2],
+ arguments[3]);
+
+ page()->networkAccessManager()->setProxy(proxy);
+ emit finished(new Response(true));
+}
View
11 src/SetProxy.h
@@ -0,0 +1,11 @@
+#include "Command.h"
+
+class WebPage;
+
+class SetProxy : public Command {
+ Q_OBJECT;
+
+ public:
+ SetProxy(WebPage *page, QObject *parent = 0);
+ virtual void start(QStringList &arguments);
+};
View
1 src/UnsupportedContentHandler.cpp
@@ -27,5 +27,6 @@ void UnsupportedContentHandler::loadUnsupportedContent() {
void UnsupportedContentHandler::finish(bool success) {
connect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
+ m_page->replyFinished(m_reply);
m_page->loadFinished(success);
}
View
35 src/WebPage.cpp
@@ -27,18 +27,19 @@ void WebPage::setCustomNetworkAccessManager() {
manager->setCookieJar(new NetworkCookieJar());
this->setNetworkAccessManager(manager);
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
+ connect(manager, SIGNAL(sslErrors(QNetworkReply *, QList<QSslError>)), this, SLOT(ignoreSslErrors(QNetworkReply *, QList<QSslError>)));
}
void WebPage::loadJavascript() {
- QResource javascript(":/capybara.js");
+ QResource javascript(":/webkit_server.js");
if (javascript.isCompressed()) {
QByteArray uncompressedBytes(qUncompress(javascript.data(), javascript.size()));
- m_capybaraJavascript = QString(uncompressedBytes);
+ m_javascriptString = QString(uncompressedBytes);
} else {
char * javascriptString = new char[javascript.size() + 1];
strcpy(javascriptString, (const char *)javascript.data());
javascriptString[javascript.size()] = 0;
- m_capybaraJavascript = javascriptString;
+ m_javascriptString = javascriptString;
}
}
@@ -67,24 +68,24 @@ void WebPage::frameCreated(QWebFrame * frame) {
void WebPage::injectJavascriptHelpers() {
QWebFrame* frame = qobject_cast<QWebFrame *>(QObject::sender());
- frame->evaluateJavaScript(m_capybaraJavascript);
+ frame->evaluateJavaScript(m_javascriptString);
}
bool WebPage::shouldInterruptJavaScript() {
return false;
}
-QVariant WebPage::invokeCapybaraFunction(const char *name, QStringList &arguments) {
+QVariant WebPage::invokeWebKitServerFunction(const char *name, QStringList &arguments) {
QString qname(name);
- QString objectName("CapybaraInvocation");
+ QString objectName("WebKitServerInvocation");
JavascriptInvocation invocation(qname, arguments);
currentFrame()->addToJavaScriptWindowObject(objectName, &invocation);
- QString javascript = QString("Capybara.invoke()");
+ QString javascript = QString("WebKitServer.invoke()");
return currentFrame()->evaluateJavaScript(javascript);
}
-QVariant WebPage::invokeCapybaraFunction(QString &name, QStringList &arguments) {
- return invokeCapybaraFunction(name.toAscii().data(), arguments);
+QVariant WebPage::invokeWebKitServerFunction(QString &name, QStringList &arguments) {
+ return invokeWebKitServerFunction(name.toAscii().data(), arguments);
}
void WebPage::javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID) {
@@ -174,7 +175,7 @@ bool WebPage::extension(Extension extension, const ExtensionOption *option, Exte
}
QString WebPage::getLastAttachedFileName() {
- return currentFrame()->evaluateJavaScript(QString("Capybara.lastAttachedFile")).toString();
+ return currentFrame()->evaluateJavaScript(QString("WebKitServer.lastAttachedFile")).toString();
}
void WebPage::replyFinished(QNetworkReply *reply) {
@@ -192,6 +193,20 @@ void WebPage::replyFinished(QNetworkReply *reply) {
}
}
+void WebPage::ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &errors) {
+ if (m_ignoreSslErrors)
+ reply->ignoreSslErrors(errors);
+}
+
+void WebPage::setIgnoreSslErrors(bool ignore) {
+ m_ignoreSslErrors = ignore;
+}
+
+bool WebPage::ignoreSslErrors() {
+ return m_ignoreSslErrors;
+}
+
+
int WebPage::getLastStatus() {
return m_lastStatus;
}
View
10 src/WebPage.h
@@ -5,8 +5,8 @@ class WebPage : public QWebPage {
public:
WebPage(QObject *parent = 0);
- QVariant invokeCapybaraFunction(const char *name, QStringList &arguments);
- QVariant invokeCapybaraFunction(QString &name, QStringList &arguments);
+ QVariant invokeWebKitServerFunction(const char *name, QStringList &arguments);
+ QVariant invokeWebKitServerFunction(QString &name, QStringList &arguments);
QString failureString();
QString userAgentForUrl(const QUrl &url ) const;
void setUserAgent(QString userAgent);
@@ -15,6 +15,8 @@ class WebPage : public QWebPage {
void setCustomNetworkAccessManager();
bool render(const QString &fileName);
virtual bool extension (Extension extension, const ExtensionOption *option=0, ExtensionReturn *output=0);
+ void setIgnoreSslErrors(bool ignore);
+ bool ignoreSslErrors();
public slots:
bool shouldInterruptJavaScript();
@@ -25,6 +27,7 @@ class WebPage : public QWebPage {
QString pageHeaders();
void frameCreated(QWebFrame *);
void replyFinished(QNetworkReply *reply);
+ void ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &);
void handleUnsupportedContent(QNetworkReply *reply);
signals:
@@ -38,13 +41,14 @@ class WebPage : public QWebPage {
virtual QString chooseFile(QWebFrame * parentFrame, const QString &suggestedFile);
private:
- QString m_capybaraJavascript;
+ QString m_javascriptString;
QString m_userAgent;
bool m_loading;
QString getLastAttachedFileName();
void loadJavascript();
void setUserStylesheet();
int m_lastStatus;
QString m_pageHeaders;
+ bool m_ignoreSslErrors;
};
View
2 src/find_command.h
@@ -20,3 +20,5 @@ CHECK_COMMAND(Headers)
CHECK_COMMAND(SetCookie)
CHECK_COMMAND(ClearCookies)
CHECK_COMMAND(GetCookies)
+CHECK_COMMAND(Headers)
+CHECK_COMMAND(SetProxy)
View
13 src/main.cpp
@@ -14,17 +14,18 @@ int main(int argc, char **argv) {
#endif
QApplication app(argc, argv);
- app.setApplicationName("capybara-webkit");
- app.setOrganizationName("thoughtbot, inc");
- app.setOrganizationDomain("thoughtbot.com");
+ app.setApplicationName("webkit_server");
- Server server;
+ QStringList args = app.arguments();
+ bool ignoreSslErrors = args.contains("--ignore-ssl-errors");
+
+ Server server(0, ignoreSslErrors);
if (server.start()) {
- std::cout << "Capybara-webkit server started, listening on port: " << server.server_port() << std::endl;
+ std::cout << "webkit_server server started, listening on port: " << server.server_port() << std::endl;
return app.exec();
} else {
- std::cerr << "Couldn't start capybara-webkit server" << std::endl;
+ std::cerr << "Couldn't start webkit_server." << std::endl;
return 1;
}
}
View
4 src/capybara.js → src/webkit_server.js
@@ -1,10 +1,10 @@
-Capybara = {
+window.WebKitServer = {
nextIndex: 0,
nodes: {},
lastAttachedFile: "",
invoke: function () {
- return this[CapybaraInvocation.functionName].apply(this, CapybaraInvocation.arguments);
+ return this[WebKitServerInvocation.functionName].apply(this, WebKitServerInvocation.arguments);
},
find: function (xpath) {
View
7 src/webkit_server.pro
@@ -28,6 +28,9 @@ HEADERS = \
SetCookie.h \
ClearCookies.h \
GetCookies.h \
+ CommandParser.h \
+ CommandFactory.h \
+ SetProxy.h \
SOURCES = \
main.cpp \
@@ -57,9 +60,11 @@ SOURCES = \
SetCookie.cpp \
ClearCookies.cpp \
GetCookies.cpp \
+ CommandParser.cpp \
+ CommandFactory.cpp \
+ SetProxy.cpp \
RESOURCES = webkit_server.qrc
QT += network webkit
CONFIG += console
CONFIG -= app_bundle
-
View
2 src/webkit_server.qrc
@@ -1,5 +1,5 @@
<RCC>
<qresource prefix="/">
- <file>capybara.js</file>
+ <file>webkit_server.js</file>
</qresource>
</RCC>

0 comments on commit ab1004a

Please sign in to comment.
Something went wrong with that request. Please try again.