forked from thoughtbot/capybara-webkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds cookie management functions to Webkit::Browser
- Loading branch information
1 parent
dc5130c
commit fb119f6
Showing
15 changed files
with
298 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¤t = 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 ¤t = 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters