Permalink
Browse files

Merge in a lot of upstream changes.

  • Loading branch information...
1 parent 3e2fee9 commit fdd178c47dc933c18cf8b444d30641232997fa90 @tristandunn committed Oct 13, 2011
View
@@ -0,0 +1,18 @@
+#include "ClearCookies.h"
+#include "WebPage.h"
+#include "NetworkCookieJar.h"
+#include <QNetworkCookie>
+
+ClearCookies::ClearCookies(WebPage *page, QObject *parent)
+ : Command(page, parent)
+{ }
+
+void ClearCookies::start(QStringList &arguments)
+{
+ Q_UNUSED(arguments);
+ NetworkCookieJar *jar = qobject_cast<NetworkCookieJar*>(page()
+ ->networkAccessManager()
+ ->cookieJar());
+ jar->clearCookies();
+ emit finished(new Response(true));
+}
View
@@ -0,0 +1,11 @@
+#include "Command.h"
+
+class WebPage;
+
+class ClearCookies : public Command {
+ Q_OBJECT;
+
+ public:
+ ClearCookies(WebPage *page, QObject *parent = 0);
+ virtual void start(QStringList &arguments);
+};
View
@@ -1,5 +1,6 @@
#include "Connection.h"
#include "WebPage.h"
+#include "UnsupportedContentHandler.h"
#include "Visit.h"
#include "Find.h"
#include "Command.h"
@@ -15,6 +16,9 @@
#include "Body.h"
#include "Status.h"
#include "Headers.h"
+#include "SetCookie.h"
+#include "ClearCookies.h"
+#include "GetCookies.h"
#include <QTcpSocket>
#include <iostream>
@@ -28,7 +32,7 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
m_pageSuccess = true;
m_commandWaiting = false;
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
- connect(m_page, SIGNAL(loadFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
+ connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
}
void Connection::checkNext() {
View
@@ -0,0 +1,22 @@
+#include "GetCookies.h"
+#include "WebPage.h"
+#include "NetworkCookieJar.h"
+
+GetCookies::GetCookies(WebPage *page, QObject *parent)
+ : Command(page, parent)
+{
+ m_buffer = "";
+}
+
+void GetCookies::start(QStringList &arguments)
+{
+ Q_UNUSED(arguments);
+ NetworkCookieJar *jar = qobject_cast<NetworkCookieJar*>(page()
+ ->networkAccessManager()
+ ->cookieJar());
+ foreach (QNetworkCookie cookie, jar->getAllCookies()) {
+ m_buffer.append(cookie.toRawForm());
+ m_buffer.append("\n");
+ }
+ emit finished(new Response(true, m_buffer));
+}
View
@@ -0,0 +1,14 @@
+#include "Command.h"
+
+class WebPage;
+
+class GetCookies : public Command {
+ Q_OBJECT;
+
+ public:
+ GetCookies(WebPage *page, QObject *parent = 0);
+ virtual void start(QStringList &arguments);
+
+ private:
+ QString m_buffer;
+};
@@ -6,14 +6,17 @@
NetworkAccessManager::NetworkAccessManager(QObject *parent):QNetworkAccessManager(parent) {
}
-QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operation oparation, const QNetworkRequest &request, QIODevice * outgoingData = 0) {
+QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operation operation, const QNetworkRequest &request, QIODevice * outgoingData = 0) {
QNetworkRequest new_request(request);
+ if (operation != QNetworkAccessManager::PostOperation && operation != QNetworkAccessManager::PutOperation) {
+ new_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant());
+ }
QHashIterator<QString, QString> item(m_headers);
while (item.hasNext()) {
item.next();
new_request.setRawHeader(item.key().toAscii(), item.value().toAscii());
}
- return QNetworkAccessManager::createRequest(oparation, new_request, outgoingData);
+ return QNetworkAccessManager::createRequest(operation, new_request, outgoingData);
};
void NetworkAccessManager::addHeader(QString key, QString value) {
View
@@ -0,0 +1,101 @@
+#include "NetworkCookieJar.h"
+#include "QtCore/qdatetime.h"
+
+NetworkCookieJar::NetworkCookieJar(QObject *parent)
+ : QNetworkCookieJar(parent)
+{ }
+
+QList<QNetworkCookie> NetworkCookieJar::getAllCookies() const
+{
+ return allCookies();
+}
+
+void NetworkCookieJar::clearCookies()
+{
+ setAllCookies(QList<QNetworkCookie>());
+}
+
+static inline bool isParentDomain(QString domain, QString reference)
+{
+ if (!reference.startsWith(QLatin1Char('.')))
+ return domain == reference;
+
+ return domain.endsWith(reference) || domain == reference.mid(1);
+}
+
+void NetworkCookieJar::overwriteCookies(const QList<QNetworkCookie>& cookieList)
+{
+ /* this function is basically a copy-and-paste of the original
+ QNetworkCookieJar::setCookiesFromUrl with the domain and
+ path validations removed */
+
+ QString defaultPath(QLatin1Char('/'));
+ QDateTime now = QDateTime::currentDateTime();
+ QList<QNetworkCookie> newCookies = allCookies();
+
+ foreach (QNetworkCookie cookie, cookieList) {
+ bool isDeletion = (!cookie.isSessionCookie() &&
+ cookie.expirationDate() < now);
+
+ // validate the cookie & set the defaults if unset
+ if (cookie.path().isEmpty())
+ cookie.setPath(defaultPath);
+
+ // don't do path checking. See http://bugreports.qt.nokia.com/browse/QTBUG-5815
+ // else if (!isParentPath(pathAndFileName, cookie.path())) {
+ // continue; // not accepted
+ // }
+
+ if (cookie.domain().isEmpty()) {
+ continue;
+ } else {
+ // Ensure the domain starts with a dot if its field was not empty
+ // in the HTTP header. There are some servers that forget the
+ // leading dot and this is actually forbidden according to RFC 2109,
+ // but all browsers accept it anyway so we do that as well.
+ if (!cookie.domain().startsWith(QLatin1Char('.')))
+ cookie.setDomain(QLatin1Char('.') + cookie.domain());
+
+ QString domain = cookie.domain();
+
+ // the check for effective TLDs makes the "embedded dot" rule from RFC 2109 section 4.3.2
+ // redundant; the "leading dot" rule has been relaxed anyway, see above
+ // we remove the leading dot for this check
+ /*
+ if (QNetworkCookieJarPrivate::isEffectiveTLD(domain.remove(0, 1)))
+ continue; // not accepted
+ */
+ }
+
+ for (int i = 0; i < newCookies.size(); ++i) {
+ // does this cookie already exist?
+ const QNetworkCookie &current = newCookies.at(i);
+ if (cookie.name() == current.name() &&
+ cookie.domain() == current.domain() &&
+ cookie.path() == current.path()) {
+ // found a match
+ newCookies.removeAt(i);
+ break;
+ }
+ }
+
+ // did not find a match
+ if (!isDeletion) {
+ int countForDomain = 0;
+ for (int i = newCookies.size() - 1; i >= 0; --i) {
+ // Start from the end and delete the oldest cookies to keep a maximum count of 50.
+ const QNetworkCookie &current = newCookies.at(i);
+ if (isParentDomain(cookie.domain(), current.domain())
+ || isParentDomain(current.domain(), cookie.domain())) {
+ if (countForDomain >= 49)
+ newCookies.removeAt(i);
+ else
+ ++countForDomain;
+ }
+ }
+
+ newCookies += cookie;
+ }
+ }
+ setAllCookies(newCookies);
+}
View
@@ -0,0 +1,15 @@
+#include <QtNetwork/QNetworkCookieJar>
+#include <QtNetwork/QNetworkCookie>
+
+class NetworkCookieJar : public QNetworkCookieJar {
+
+ Q_OBJECT;
+
+ public:
+
+ NetworkCookieJar(QObject *parent = 0);
+
+ QList<QNetworkCookie> getAllCookies() const;
+ void clearCookies();
+ void overwriteCookies(const QList<QNetworkCookie>& cookieList);
+};
View
@@ -1,6 +1,7 @@
#include "Reset.h"
#include "WebPage.h"
#include "NetworkAccessManager.h"
+#include "NetworkCookieJar.h"
Reset::Reset(WebPage *page, QObject *parent) : Command(page, parent) {
}
@@ -10,9 +11,10 @@ void Reset::start(QStringList &arguments) {
page()->triggerAction(QWebPage::Stop);
page()->currentFrame()->setHtml("<html><body></body></html>");
- page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
- page()->setNetworkAccessManager(new NetworkAccessManager());
+ page()->networkAccessManager()->setCookieJar(new NetworkCookieJar());
+ page()->setCustomNetworkAccessManager();
page()->setUserAgent(NULL);
+ page()->resetResponseHeaders();
emit finished(new Response(true));
}
View
@@ -0,0 +1,18 @@
+#include "SetCookie.h"
+#include "WebPage.h"
+#include "NetworkCookieJar.h"
+#include <QNetworkCookie>
+
+SetCookie::SetCookie(WebPage *page, QObject *parent)
+ : Command(page, parent)
+{ }
+
+void SetCookie::start(QStringList &arguments)
+{
+ QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(arguments[0].toAscii());
+ NetworkCookieJar *jar = qobject_cast<NetworkCookieJar*>(page()
+ ->networkAccessManager()
+ ->cookieJar());
+ jar->overwriteCookies(cookies);
+ emit finished(new Response(true));
+}
View
@@ -0,0 +1,11 @@
+#include "Command.h"
+
+class WebPage;
+
+class SetCookie : public Command {
+ Q_OBJECT;
+
+ public:
+ SetCookie(WebPage *page, QObject *parent = 0);
+ virtual void start(QStringList &arguments);
+};
@@ -0,0 +1,31 @@
+#include "UnsupportedContentHandler.h"
+#include "WebPage.h"
+#include <QNetworkReply>
+
+UnsupportedContentHandler::UnsupportedContentHandler(WebPage *page, QNetworkReply *reply, QObject *parent) : QObject(parent) {
+ m_page = page;
+ m_reply = reply;
+ connect(m_reply, SIGNAL(finished()), this, SLOT(handleUnsupportedContent()));
+ disconnect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
+}
+
+void UnsupportedContentHandler::handleUnsupportedContent() {
+ QVariant contentMimeType = m_reply->header(QNetworkRequest::ContentTypeHeader);
+ if(contentMimeType.isNull()) {
+ this->finish(false);
+ } else {
+ this->loadUnsupportedContent();
+ this->finish(true);
+ }
+ this->deleteLater();
+}
+
+void UnsupportedContentHandler::loadUnsupportedContent() {
+ QByteArray text = m_reply->readAll();
+ m_page->mainFrame()->setContent(text, QString("text/plain"), m_reply->url());
+}
+
+void UnsupportedContentHandler::finish(bool success) {
+ connect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
+ m_page->loadFinished(success);
+}
@@ -0,0 +1,18 @@
+#include <QObject>
+class WebPage;
+class QNetworkReply;
+class UnsupportedContentHandler : public QObject {
+ Q_OBJECT
+
+ public:
+ UnsupportedContentHandler(WebPage *page, QNetworkReply *reply, QObject *parent = 0);
+
+ public slots:
+ void handleUnsupportedContent();
+
+ private:
+ WebPage *m_page;
+ QNetworkReply *m_reply;
+ void loadUnsupportedContent();
+ void finish(bool success);
+};
View
@@ -3,22 +3,19 @@
#include "WebPage.h"
Visit::Visit(WebPage *page, QObject *parent) : Command(page, parent) {
- connect(page, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
+ connect(page, SIGNAL(pageFinished(bool)), this, SLOT(loadFinished(bool)));
}
void Visit::start(QStringList &arguments) {
QUrl requestedUrl = QUrl(arguments[0]);
- page()->currentFrame()->setUrl(QUrl(requestedUrl));
- if(requestedUrl.hasFragment()) {
- // workaround for https://bugs.webkit.org/show_bug.cgi?id=32723
- page()->currentFrame()->setUrl(QUrl(requestedUrl));
- }
+ page()->currentFrame()->load(QUrl(requestedUrl));
}
void Visit::loadFinished(bool success) {
QString message;
if (!success)
message = page()->failureString();
+ disconnect(page(), SIGNAL(pageFinished(bool)), this, SLOT(loadFinished(bool)));
emit finished(new Response(success, message));
}
Oops, something went wrong.

0 comments on commit fdd178c

Please sign in to comment.