Handle unsupported content types gracefully
This commit is contained in:
parent
f493b22f6b
commit
353fe860b2
|
@ -134,6 +134,20 @@ describe Capybara::Driver::Webkit do
|
||||||
end
|
end
|
||||||
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
|
context "hello app" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
@app = lambda do |env|
|
@app = lambda do |env|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "Connection.h"
|
#include "Connection.h"
|
||||||
#include "WebPage.h"
|
#include "WebPage.h"
|
||||||
|
#include "UnsupportedContentHandler.h"
|
||||||
#include "Visit.h"
|
#include "Visit.h"
|
||||||
#include "Find.h"
|
#include "Find.h"
|
||||||
#include "Command.h"
|
#include "Command.h"
|
||||||
|
@ -28,7 +29,7 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
|
||||||
m_pageSuccess = true;
|
m_pageSuccess = true;
|
||||||
m_commandWaiting = false;
|
m_commandWaiting = false;
|
||||||
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
|
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() {
|
void Connection::checkNext() {
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
};
|
|
@ -3,7 +3,7 @@
|
||||||
#include "WebPage.h"
|
#include "WebPage.h"
|
||||||
|
|
||||||
Visit::Visit(WebPage *page, QObject *parent) : Command(page, parent) {
|
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) {
|
void Visit::start(QStringList &arguments) {
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
#include "WebPage.h"
|
#include "WebPage.h"
|
||||||
#include "JavascriptInvocation.h"
|
#include "JavascriptInvocation.h"
|
||||||
#include "NetworkAccessManager.h"
|
#include "NetworkAccessManager.h"
|
||||||
|
#include "UnsupportedContentHandler.h"
|
||||||
#include <QResource>
|
#include <QResource>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
WebPage::WebPage(QObject *parent) : QWebPage(parent) {
|
WebPage::WebPage(QObject *parent) : QWebPage(parent) {
|
||||||
|
setForwardUnsupportedContent(true);
|
||||||
loadJavascript();
|
loadJavascript();
|
||||||
setUserStylesheet();
|
setUserStylesheet();
|
||||||
|
|
||||||
|
@ -15,6 +17,8 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) {
|
||||||
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
|
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
|
||||||
connect(this, SIGNAL(frameCreated(QWebFrame *)),
|
connect(this, SIGNAL(frameCreated(QWebFrame *)),
|
||||||
this, SLOT(frameCreated(QWebFrame *)));
|
this, SLOT(frameCreated(QWebFrame *)));
|
||||||
|
connect(this, SIGNAL(unsupportedContent(QNetworkReply*)),
|
||||||
|
this, SLOT(handleUnsupportedContent(QNetworkReply*)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebPage::setCustomNetworkAccessManager() {
|
void WebPage::setCustomNetworkAccessManager() {
|
||||||
|
@ -111,8 +115,8 @@ void WebPage::loadStarted() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebPage::loadFinished(bool success) {
|
void WebPage::loadFinished(bool success) {
|
||||||
Q_UNUSED(success);
|
|
||||||
m_loading = false;
|
m_loading = false;
|
||||||
|
emit pageFinished(success);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebPage::isLoading() const {
|
bool WebPage::isLoading() const {
|
||||||
|
@ -198,3 +202,8 @@ void WebPage::resetResponseHeaders() {
|
||||||
QString WebPage::pageHeaders() {
|
QString WebPage::pageHeaders() {
|
||||||
return m_pageHeaders;
|
return m_pageHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
|
||||||
|
UnsupportedContentHandler *handler = new UnsupportedContentHandler(this, reply);
|
||||||
|
Q_UNUSED(handler);
|
||||||
|
}
|
||||||
|
|
|
@ -25,6 +25,10 @@ class WebPage : public QWebPage {
|
||||||
QString pageHeaders();
|
QString pageHeaders();
|
||||||
void frameCreated(QWebFrame *);
|
void frameCreated(QWebFrame *);
|
||||||
void replyFinished(QNetworkReply *reply);
|
void replyFinished(QNetworkReply *reply);
|
||||||
|
void handleUnsupportedContent(QNetworkReply *reply);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void pageFinished(bool);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID);
|
virtual void javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
TEMPLATE = app
|
TEMPLATE = app
|
||||||
TARGET = webkit_server
|
TARGET = webkit_server
|
||||||
DESTDIR = .
|
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
|
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
|
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
|
RESOURCES = webkit_server.qrc
|
||||||
QT += network webkit
|
QT += network webkit
|
||||||
CONFIG += console
|
CONFIG += console
|
||||||
|
|
Loading…
Reference in New Issue