Skip to content

Commit

Permalink
Handle unsupported content types gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
halogenandtoast committed Sep 23, 2011
1 parent f493b22 commit 353fe86
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 5 deletions.
14 changes: 14 additions & 0 deletions spec/driver_spec.rb
Expand Up @@ -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|
Expand Down
3 changes: 2 additions & 1 deletion 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"
Expand Down Expand Up @@ -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() {
Expand Down
23 changes: 23 additions & 0 deletions 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);
}
}
16 changes: 16 additions & 0 deletions 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;
};
2 changes: 1 addition & 1 deletion src/Visit.cpp
Expand Up @@ -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) {
Expand Down
11 changes: 10 additions & 1 deletion 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();

Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions src/WebPage.h
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions 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
Expand Down

0 comments on commit 353fe86

Please sign in to comment.