Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handle unsupported content types gracefully

  • Loading branch information...
commit 353fe860b2faf7f29a89ccdb2ee3813c2052e6a6 1 parent f493b22
@halogenandtoast halogenandtoast authored
View
14 spec/driver_spec.rb
@@ -134,6 +134,20 @@
end
end
+ context "css app" do
+ before(:all) do
+ body = "css"
+ @app = lambda do |env|
+ [200, {"Content-Type" => "text/css", "Content-Length" => body.length.to_s}, [body]]
+ end
+ end
+
+ it "should render unsupported content types gracefully" do
+ subject.visit("/")
+ subject.body.should =~ /css/
+ end
+ end
+
context "hello app" do
before(:all) do
@app = lambda do |env|
View
3  src/Connection.cpp
@@ -1,5 +1,6 @@
#include "Connection.h"
#include "WebPage.h"
+#include "UnsupportedContentHandler.h"
#include "Visit.h"
#include "Find.h"
#include "Command.h"
@@ -28,7 +29,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
23 src/UnsupportedContentHandler.cpp
@@ -0,0 +1,23 @@
+#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()) {
+ QByteArray text = m_reply->readAll();
+ m_page->mainFrame()->setContent(text, QString("text/plain"), m_reply->url());
+ connect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
+ m_page->loadFinished(true);
+ } else {
+ connect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
+ m_page->loadFinished(false);
+ }
+}
View
16 src/UnsupportedContentHandler.h
@@ -0,0 +1,16 @@
+#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;
+};
View
2  src/Visit.cpp
@@ -3,7 +3,7 @@
#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) {
View
11 src/WebPage.cpp
@@ -1,10 +1,12 @@
#include "WebPage.h"
#include "JavascriptInvocation.h"
#include "NetworkAccessManager.h"
+#include "UnsupportedContentHandler.h"
#include <QResource>
#include <iostream>
WebPage::WebPage(QObject *parent) : QWebPage(parent) {
+ setForwardUnsupportedContent(true);
loadJavascript();
setUserStylesheet();
@@ -15,6 +17,8 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) {
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
connect(this, SIGNAL(frameCreated(QWebFrame *)),
this, SLOT(frameCreated(QWebFrame *)));
+ connect(this, SIGNAL(unsupportedContent(QNetworkReply*)),
+ this, SLOT(handleUnsupportedContent(QNetworkReply*)));
}
void WebPage::setCustomNetworkAccessManager() {
@@ -111,8 +115,8 @@ void WebPage::loadStarted() {
}
void WebPage::loadFinished(bool success) {
- Q_UNUSED(success);
m_loading = false;
+ emit pageFinished(success);
}
bool WebPage::isLoading() const {
@@ -198,3 +202,8 @@ void WebPage::resetResponseHeaders() {
QString WebPage::pageHeaders() {
return m_pageHeaders;
}
+
+void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
+ UnsupportedContentHandler *handler = new UnsupportedContentHandler(this, reply);
+ Q_UNUSED(handler);
+}
View
4 src/WebPage.h
@@ -25,6 +25,10 @@ class WebPage : public QWebPage {
QString pageHeaders();
void frameCreated(QWebFrame *);
void replyFinished(QNetworkReply *reply);
+ void handleUnsupportedContent(QNetworkReply *reply);
+
+ signals:
+ void pageFinished(bool);
protected:
virtual void javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID);
View
4 src/webkit_server.pro
@@ -1,8 +1,8 @@
TEMPLATE = app
TARGET = webkit_server
DESTDIR = .
-HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h FrameFocus.h Response.h NetworkAccessManager.h Header.h Render.h body.h Status.h Headers.h
-SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp FrameFocus.cpp Response.cpp NetworkAccessManager.cpp Header.cpp Render.cpp body.cpp Status.cpp Headers.cpp
+HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h FrameFocus.h Response.h NetworkAccessManager.h Header.h Render.h body.h Status.h Headers.h UnsupportedContentHandler.h
+SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp FrameFocus.cpp Response.cpp NetworkAccessManager.cpp Header.cpp Render.cpp body.cpp Status.cpp Headers.cpp UnsupportedContentHandler.cpp
RESOURCES = webkit_server.qrc
QT += network webkit
CONFIG += console
Please sign in to comment.
Something went wrong with that request. Please try again.