Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Read status and headers from response after load

  • Loading branch information...
commit e9a4b77ecba68e9b490ceb9c6d0ee7ae8a90b0b8 1 parent e531c1a
@mhoran mhoran authored
View
3  .gitignore
@@ -16,4 +16,5 @@ src/webkit_server.exe
.DS_Store
tmp
.rvmrc
-src/debug
+src/debug
+webkit_server.pro.user
View
7 src/Headers.cpp
@@ -6,6 +6,11 @@ Headers::Headers(WebPageManager *manager, QStringList &arguments, QObject *paren
}
void Headers::start() {
- emit finished(new Response(true, page()->pageHeaders()));
+ QStringList headers;
+
+ foreach(QNetworkReply::RawHeaderPair header, page()->pageHeaders())
+ headers << header.first+": "+header.second;
+
+ emit finished(new Response(true, headers.join("\n")));
}
View
24 src/NetworkAccessManager.cpp
@@ -3,9 +3,9 @@
#include <iostream>
#include <fstream>
-
NetworkAccessManager::NetworkAccessManager(QObject *parent):QNetworkAccessManager(parent) {
connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
+ connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(finished(QNetworkReply *)));
}
QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operation operation, const QNetworkRequest &request, QIODevice * outgoingData = 0) {
@@ -22,15 +22,22 @@ QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operat
QNetworkReply *reply = QNetworkAccessManager::createRequest(operation, new_request, outgoingData);
emit requestCreated(url, reply);
return reply;
-};
+}
+
+void NetworkAccessManager::finished(QNetworkReply *reply) {
+ NetworkResponse response;
+ response.statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
+ response.headers = reply->rawHeaderPairs();
+ m_responses[reply->url()] = response;
+}
void NetworkAccessManager::addHeader(QString key, QString value) {
m_headers.insert(key, value);
-};
+}
void NetworkAccessManager::resetHeaders() {
m_headers.clear();
-};
+}
void NetworkAccessManager::setUserName(const QString &userName) {
m_userName = userName;
@@ -40,9 +47,16 @@ void NetworkAccessManager::setPassword(const QString &password) {
m_password = password;
}
-
void NetworkAccessManager::provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator) {
Q_UNUSED(reply);
authenticator->setUser(m_userName);
authenticator->setPassword(m_password);
}
+
+int NetworkAccessManager::statusFor(QUrl url) {
+ return m_responses[url].statusCode;
+}
+
+const QList<QNetworkReply::RawHeaderPair> &NetworkAccessManager::headersFor(QUrl url) {
+ return m_responses[url].headers;
+}
View
10 src/NetworkAccessManager.h
@@ -6,12 +6,20 @@ class NetworkAccessManager : public QNetworkAccessManager {
Q_OBJECT
+ struct NetworkResponse {
+ int statusCode;
+ QList<QNetworkReply::RawHeaderPair> headers;
+ NetworkResponse() : statusCode(0) { }
+ };
+
public:
NetworkAccessManager(QObject *parent = 0);
void addHeader(QString key, QString value);
void resetHeaders();
void setUserName(const QString &userName);
void setPassword(const QString &password);
+ int statusFor(QUrl url);
+ const QList<QNetworkReply::RawHeaderPair> &headersFor(QUrl url);
protected:
QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice * outgoingData);
@@ -20,9 +28,11 @@ class NetworkAccessManager : public QNetworkAccessManager {
private:
QHash<QString, QString> m_headers;
+ QHash<QUrl, NetworkResponse> m_responses;
private slots:
void provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator);
+ void finished(QNetworkReply *);
signals:
void requestCreated(QByteArray &url, QNetworkReply *reply);
View
19 src/WebPage.cpp
@@ -14,7 +14,6 @@ WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) {
m_failed = false;
m_manager = manager;
m_uuid = QUuid::createUuid().toString();
- m_lastStatus = 0;
setForwardUnsupportedContent(true);
loadJavascript();
@@ -56,18 +55,6 @@ void WebPage::networkAccessManagerCreatedRequest(QByteArray &url, QNetworkReply
}
void WebPage::networkAccessManagerFinishedReply(QNetworkReply *reply) {
- if (reply->url() == this->currentFrame()->requestedUrl()) {
- QStringList headers;
- m_lastStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
- QList<QByteArray> list = reply->rawHeaderList();
-
- int length = list.size();
- for(int i = 0; i < length; i++) {
- headers << list.at(i)+": "+reply->rawHeader(list.at(i));
- }
-
- m_pageHeaders = headers.join("\n");
- }
emit replyFinished(reply);
}
@@ -265,11 +252,11 @@ void WebPage::setSkipImageLoading(bool skip) {
}
int WebPage::getLastStatus() {
- return m_lastStatus;
+ return qobject_cast<NetworkAccessManager *>(networkAccessManager())->statusFor(currentFrame()->url());
}
-QString WebPage::pageHeaders() {
- return m_pageHeaders;
+const QList<QNetworkReply::RawHeaderPair> &WebPage::pageHeaders() {
+ return qobject_cast<NetworkAccessManager *>(networkAccessManager())->headersFor(currentFrame()->url());
}
void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
View
4 src/WebPage.h
@@ -39,7 +39,7 @@ class WebPage : public QWebPage {
void loadStarted();
void loadFinished(bool);
bool isLoading() const;
- QString pageHeaders();
+ const QList<QNetworkReply::RawHeaderPair> &pageHeaders();
void frameCreated(QWebFrame *);
void handleSslErrorsForReply(QNetworkReply *reply, const QList<QSslError> &);
void handleUnsupportedContent(QNetworkReply *reply);
@@ -67,8 +67,6 @@ class WebPage : public QWebPage {
QString getLastAttachedFileName();
void loadJavascript();
void setUserStylesheet();
- int m_lastStatus;
- QString m_pageHeaders;
bool m_confirm;
bool m_prompt;
QStringList m_consoleMessages;
Please sign in to comment.
Something went wrong with that request. Please try again.