Skip to content
This repository
Browse code

Read status and headers from response after load

  • Loading branch information...
commit e9a4b77ecba68e9b490ceb9c6d0ee7ae8a90b0b8 1 parent e531c1a
Matthew Horan authored October 21, 2012
3  .gitignore
@@ -16,4 +16,5 @@ src/webkit_server.exe
16 16
 .DS_Store
17 17
 tmp
18 18
 .rvmrc
19  
-src/debug
  19
+src/debug
  20
+webkit_server.pro.user
7  src/Headers.cpp
@@ -6,6 +6,11 @@ Headers::Headers(WebPageManager *manager, QStringList &arguments, QObject *paren
6 6
 }
7 7
 
8 8
 void Headers::start() {
9  
-  emit finished(new Response(true, page()->pageHeaders()));
  9
+  QStringList headers;
  10
+
  11
+  foreach(QNetworkReply::RawHeaderPair header, page()->pageHeaders())
  12
+    headers << header.first+": "+header.second;
  13
+
  14
+  emit finished(new Response(true, headers.join("\n")));
10 15
 }
11 16
 
24  src/NetworkAccessManager.cpp
@@ -3,9 +3,9 @@
3 3
 #include <iostream>
4 4
 #include <fstream>
5 5
 
6  
-
7 6
 NetworkAccessManager::NetworkAccessManager(QObject *parent):QNetworkAccessManager(parent) {
8 7
   connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
  8
+  connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(finished(QNetworkReply *)));
9 9
 }
10 10
 
11 11
 QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operation operation, const QNetworkRequest &request, QIODevice * outgoingData = 0) {
@@ -22,15 +22,22 @@ QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operat
22 22
   QNetworkReply *reply = QNetworkAccessManager::createRequest(operation, new_request, outgoingData);
23 23
   emit requestCreated(url, reply);
24 24
   return reply;
25  
-};
  25
+}
  26
+
  27
+void NetworkAccessManager::finished(QNetworkReply *reply) {
  28
+  NetworkResponse response;
  29
+  response.statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
  30
+  response.headers = reply->rawHeaderPairs();
  31
+  m_responses[reply->url()] = response;
  32
+}
26 33
 
27 34
 void NetworkAccessManager::addHeader(QString key, QString value) {
28 35
   m_headers.insert(key, value);
29  
-};
  36
+}
30 37
 
31 38
 void NetworkAccessManager::resetHeaders() {
32 39
   m_headers.clear();
33  
-};
  40
+}
34 41
 
35 42
 void NetworkAccessManager::setUserName(const QString &userName) {
36 43
   m_userName = userName;
@@ -40,9 +47,16 @@ void NetworkAccessManager::setPassword(const QString &password) {
40 47
   m_password = password;
41 48
 }
42 49
 
43  
-
44 50
 void NetworkAccessManager::provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator) {
45 51
   Q_UNUSED(reply);
46 52
   authenticator->setUser(m_userName);
47 53
   authenticator->setPassword(m_password);
48 54
 }
  55
+
  56
+int NetworkAccessManager::statusFor(QUrl url) {
  57
+  return m_responses[url].statusCode;
  58
+}
  59
+
  60
+const QList<QNetworkReply::RawHeaderPair> &NetworkAccessManager::headersFor(QUrl url) {
  61
+  return m_responses[url].headers;
  62
+}
10  src/NetworkAccessManager.h
@@ -6,12 +6,20 @@ class NetworkAccessManager : public QNetworkAccessManager {
6 6
 
7 7
   Q_OBJECT
8 8
 
  9
+  struct NetworkResponse {
  10
+    int statusCode;
  11
+    QList<QNetworkReply::RawHeaderPair> headers;
  12
+    NetworkResponse() : statusCode(0) { }
  13
+  };
  14
+
9 15
   public:
10 16
     NetworkAccessManager(QObject *parent = 0);
11 17
     void addHeader(QString key, QString value);
12 18
     void resetHeaders();
13 19
     void setUserName(const QString &userName);
14 20
     void setPassword(const QString &password);
  21
+    int statusFor(QUrl url);
  22
+    const QList<QNetworkReply::RawHeaderPair> &headersFor(QUrl url);
15 23
 
16 24
   protected:
17 25
     QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice * outgoingData);
@@ -20,9 +28,11 @@ class NetworkAccessManager : public QNetworkAccessManager {
20 28
 
21 29
   private:
22 30
     QHash<QString, QString> m_headers;
  31
+    QHash<QUrl, NetworkResponse> m_responses;
23 32
 
24 33
   private slots:
25 34
     void provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator);
  35
+    void finished(QNetworkReply *);
26 36
 
27 37
   signals:
28 38
     void requestCreated(QByteArray &url, QNetworkReply *reply);
19  src/WebPage.cpp
@@ -14,7 +14,6 @@ WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) {
14 14
   m_failed = false;
15 15
   m_manager = manager;
16 16
   m_uuid = QUuid::createUuid().toString();
17  
-  m_lastStatus = 0;
18 17
 
19 18
   setForwardUnsupportedContent(true);
20 19
   loadJavascript();
@@ -56,18 +55,6 @@ void WebPage::networkAccessManagerCreatedRequest(QByteArray &url, QNetworkReply
56 55
 }
57 56
 
58 57
 void WebPage::networkAccessManagerFinishedReply(QNetworkReply *reply) {
59  
-  if (reply->url() == this->currentFrame()->requestedUrl()) {
60  
-    QStringList headers;
61  
-    m_lastStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
62  
-    QList<QByteArray> list = reply->rawHeaderList();
63  
-
64  
-    int length = list.size();
65  
-    for(int i = 0; i < length; i++) {
66  
-      headers << list.at(i)+": "+reply->rawHeader(list.at(i));
67  
-    }
68  
-
69  
-    m_pageHeaders = headers.join("\n");
70  
-  }
71 58
   emit replyFinished(reply);
72 59
 }
73 60
 
@@ -265,11 +252,11 @@ void WebPage::setSkipImageLoading(bool skip) {
265 252
 }
266 253
 
267 254
 int WebPage::getLastStatus() {
268  
-  return m_lastStatus;
  255
+  return qobject_cast<NetworkAccessManager *>(networkAccessManager())->statusFor(currentFrame()->url());
269 256
 }
270 257
 
271  
-QString WebPage::pageHeaders() {
272  
-  return m_pageHeaders;
  258
+const QList<QNetworkReply::RawHeaderPair> &WebPage::pageHeaders() {
  259
+  return qobject_cast<NetworkAccessManager *>(networkAccessManager())->headersFor(currentFrame()->url());
273 260
 }
274 261
 
275 262
 void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
4  src/WebPage.h
@@ -39,7 +39,7 @@ class WebPage : public QWebPage {
39 39
     void loadStarted();
40 40
     void loadFinished(bool);
41 41
     bool isLoading() const;
42  
-    QString pageHeaders();
  42
+    const QList<QNetworkReply::RawHeaderPair> &pageHeaders();
43 43
     void frameCreated(QWebFrame *);
44 44
     void handleSslErrorsForReply(QNetworkReply *reply, const QList<QSslError> &);
45 45
     void handleUnsupportedContent(QNetworkReply *reply);
@@ -67,8 +67,6 @@ class WebPage : public QWebPage {
67 67
     QString getLastAttachedFileName();
68 68
     void loadJavascript();
69 69
     void setUserStylesheet();
70  
-    int m_lastStatus;
71  
-    QString m_pageHeaders;
72 70
     bool m_confirm;
73 71
     bool m_prompt;
74 72
     QStringList m_consoleMessages;

0 notes on commit e9a4b77

Please sign in to comment.
Something went wrong with that request. Please try again.