Changed the wire protocol so that newlines in arguments don't break everything

This commit is contained in:
Joe Ferris 2011-02-26 13:38:10 -05:00
parent 13426ba4c1
commit cfb5babae5
5 changed files with 46 additions and 21 deletions

View File

@ -31,15 +31,17 @@ class Capybara::Driver::Webkit
def command(name, *args)
@socket.puts name
@socket.puts args.size.to_s
args.each { |arg| @socket.puts arg }
@socket.puts args.size
args.each do |arg|
@socket.puts arg.bytesize
@socket.print arg
end
check
read_response
end
def evaluate_script(script)
json = command('Evaluate', script)
puts "Got JSON: #{json}"
JSON.parse("[#{json}]").first
end

View File

@ -110,5 +110,10 @@ describe Capybara::Driver::Webkit do
result = subject.evaluate_script(%<'"'>)
result.should === "\""
end
it "evaluates Javascript with multiple lines" do
result = subject.evaluate_script("[1,\n2]")
result.should == [1, 2]
end
end

View File

@ -16,31 +16,48 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
m_socket = socket;
m_page = page;
m_command = NULL;
m_expectingDataSize = -1;
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
}
void Connection::checkNext() {
while (m_socket->canReadLine()) {
readNext();
if (m_expectingDataSize == -1) {
if (m_socket->canReadLine()) {
readLine();
checkNext();
}
} else {
if (m_socket->bytesAvailable() >= m_expectingDataSize) {
readDataBlock();
checkNext();
}
}
}
void Connection::readNext() {
char buffer[1024];
qint64 lineLength = m_socket->readLine(buffer, 1024);
void Connection::readLine() {
char buffer[128];
qint64 lineLength = m_socket->readLine(buffer, 128);
if (lineLength != -1) {
buffer[lineLength - 1] = 0;
processLine(buffer);
processNext(buffer);
}
}
void Connection::processLine(const char *line) {
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_command) {
continueCommand(line);
continueCommand(data);
} else {
m_command = createCommand(line);
m_command = createCommand(data);
if (m_command) {
std::cout << "Starting command: " << line << std::endl;
startCommand();
} else {
m_socket->write("bad command\n");
@ -61,11 +78,13 @@ void Connection::startCommand() {
SLOT(finishCommand(bool, QString &)));
}
void Connection::continueCommand(const char *line) {
void Connection::continueCommand(const char *data) {
if (m_argumentsExpected == -1) {
m_argumentsExpected = QString(line).toInt();
m_argumentsExpected = QString(data).toInt();
} else if (m_expectingDataSize == -1) {
m_expectingDataSize = QString(data).toInt();
} else {
m_arguments.append(line);
m_arguments.append(data);
}
if (m_arguments.length() == m_argumentsExpected) {
@ -77,7 +96,6 @@ void Connection::finishCommand(bool success, QString &response) {
m_command->deleteLater();
m_command = NULL;
m_arguments.clear();
std::cout << "Finished command" << std::endl;
if (success) {
m_socket->write("ok\n");
} else {

View File

@ -16,8 +16,9 @@ class Connection : public QObject {
void finishCommand(bool success, QString &response);
private:
void readNext();
void processLine(const char *line);
void readLine();
void readDataBlock();
void processNext(const char *line);
Command *createCommand(const char *name);
void startCommand();
void continueCommand(const char *line);
@ -27,5 +28,6 @@ class Connection : public QObject {
QStringList m_arguments;
int m_argumentsExpected;
WebPage *m_page;
int m_expectingDataSize;
};

View File

@ -16,7 +16,6 @@ void Evaluate::start(QStringList &arguments) {
void Evaluate::addVariant(QVariant &object) {
if (object.isValid()) {
std::cout << "Got type:" << object.typeName() << std::endl;
switch(object.type()) {
case QMetaType::QString:
{
@ -48,7 +47,6 @@ void Evaluate::addVariant(QVariant &object) {
m_buffer.append("null");
}
} else {
std::cout << "Got invalid result" << std::endl;
m_buffer.append("null");
}
}