capybara-webkit/src/Connection.cpp

152 lines
3.6 KiB
C++
Raw Normal View History

2011-02-19 03:53:06 +00:00
#include "Connection.h"
#include "WebPage.h"
#include "UnsupportedContentHandler.h"
2011-02-19 03:53:06 +00:00
#include "Visit.h"
#include "Find.h"
#include "Command.h"
#include "Reset.h"
#include "Node.h"
2011-02-25 22:53:36 +00:00
#include "Url.h"
2011-02-25 23:04:23 +00:00
#include "Source.h"
2011-02-26 18:02:43 +00:00
#include "Evaluate.h"
2011-02-26 19:03:30 +00:00
#include "Execute.h"
#include "FrameFocus.h"
2011-06-08 09:36:45 +00:00
#include "Header.h"
#include "Render.h"
#include "Body.h"
2011-08-12 20:50:43 +00:00
#include "Status.h"
2011-08-19 15:57:39 +00:00
#include "Headers.h"
#include "SetCookie.h"
#include "ClearCookies.h"
#include "GetCookies.h"
2011-02-19 03:53:06 +00:00
#include <QTcpSocket>
2011-02-26 18:02:43 +00:00
#include <iostream>
2011-02-19 03:53:06 +00:00
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) {
m_socket = socket;
m_page = page;
m_command = NULL;
m_expectingDataSize = -1;
m_pageSuccess = true;
m_commandWaiting = false;
2011-02-19 03:53:06 +00:00
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
2011-02-19 03:53:06 +00:00
}
void Connection::checkNext() {
if (m_expectingDataSize == -1) {
if (m_socket->canReadLine()) {
readLine();
checkNext();
}
} else {
if (m_socket->bytesAvailable() >= m_expectingDataSize) {
readDataBlock();
checkNext();
}
2011-02-19 03:53:06 +00:00
}
}
void Connection::readLine() {
char buffer[128];
qint64 lineLength = m_socket->readLine(buffer, 128);
2011-02-19 03:53:06 +00:00
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);
2011-02-19 03:53:06 +00:00
}
}
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));
}
if (m_arguments.length() == m_argumentsExpected) {
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());
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";
writeResponse(new Response(false, failure));
}
m_commandName = QString();
} else {
m_pageSuccess = true;
QString message = m_page->failureString();
writeResponse(new Response(false, message));
}
}
Command *Connection::createCommand(const char *name) {
#include "find_command.h"
return NULL;
}
void Connection::pendingLoadFinished(bool success) {
m_pageSuccess = success;
if (m_commandWaiting)
startCommand();
}
void Connection::finishCommand(Response *response) {
2011-02-19 03:53:06 +00:00
m_command->deleteLater();
m_command = NULL;
writeResponse(response);
2011-02-26 19:03:30 +00:00
}
void Connection::writeResponse(Response *response) {
if (response->isSuccess())
2011-02-19 03:53:06 +00:00
m_socket->write("ok\n");
2011-02-26 19:03:30 +00:00
else
2011-02-19 03:53:06 +00:00
m_socket->write("failure\n");
2011-02-26 19:03:30 +00:00
QByteArray messageUtf8 = response->message().toUtf8();
QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
delete response;
m_arguments.clear();
m_commandName = QString();
m_argumentsExpected = -1;
2011-02-19 03:53:06 +00:00
}