From: <si...@us...> - 2010-08-09 10:03:26
|
Revision: 1238 http://qterm.svn.sourceforge.net/qterm/?rev=1238&view=rev Author: sidos Date: 2010-08-09 10:03:20 +0000 (Mon, 09 Aug 2010) Log Message: ----------- a test case for http redirection Added Paths: ----------- trunk/qterm-qt4/src/test/http/ trunk/qterm-qt4/src/test/http/http.pro trunk/qterm-qt4/src/test/http/main.cpp trunk/qterm-qt4/src/test/http/qnamredirect.cpp trunk/qterm-qt4/src/test/http/qnamredirect.h Added: trunk/qterm-qt4/src/test/http/http.pro =================================================================== --- trunk/qterm-qt4/src/test/http/http.pro (rev 0) +++ trunk/qterm-qt4/src/test/http/http.pro 2010-08-09 10:03:20 UTC (rev 1238) @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Mon Jun 21 08:53:54 2010 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . +QT += network +# Input +HEADERS += qnamredirect.h +SOURCES += main.cpp qnamredirect.cpp Added: trunk/qterm-qt4/src/test/http/main.cpp =================================================================== --- trunk/qterm-qt4/src/test/http/main.cpp (rev 0) +++ trunk/qterm-qt4/src/test/http/main.cpp 2010-08-09 10:03:20 UTC (rev 1238) @@ -0,0 +1,12 @@ +#include "qnamredirect.h" + +#include <QtGui> +#include <QApplication> + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + QNAMRedirect w; + w.show(); + return a.exec(); +} Added: trunk/qterm-qt4/src/test/http/qnamredirect.cpp =================================================================== --- trunk/qterm-qt4/src/test/http/qnamredirect.cpp (rev 0) +++ trunk/qterm-qt4/src/test/http/qnamredirect.cpp 2010-08-09 10:03:20 UTC (rev 1238) @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2009 Nokia Corporation + */ + +#include "qnamredirect.h" + +QNAMRedirect::QNAMRedirect(QWidget *parent) : QMainWindow(parent) { + _qnam = createQNAM(); + _originalUrl = "http://mitbbs.com/article2/PhotoForum/31264659_224.jpg"; + + QVBoxLayout* layout = new QVBoxLayout; + +#ifdef Q_OS_SYMBIAN + setStyleSheet("QLabel, QPushButton{ font: 5pt; }"); +#endif + + _textContainer = new QLabel("Click the button to test URL redirect!"); + _textContainer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + _textContainer->setAlignment(Qt::AlignCenter); + _textContainer->setWordWrap(true); + layout->addWidget(_textContainer); + + QPushButton* testButton = new QPushButton("Click here!"); + /* If the button is clicked, we'll do a request. */ + connect(testButton, SIGNAL(clicked()), + this, SLOT(doRequest())); + layout->addWidget(testButton); + + QFrame* frame = new QFrame; + frame->setLayout(layout); + setCentralWidget(frame); +} + +QNAMRedirect::~QNAMRedirect() { + if(_qnam) { + _qnam->deleteLater(); + } + if(_textContainer) { + _textContainer->deleteLater(); + } +} + +QNetworkAccessManager* QNAMRedirect::createQNAM() { + QNetworkAccessManager* qnam = new QNetworkAccessManager(this); + /* We'll handle the finished reply in replyFinished */ + connect(qnam, SIGNAL(finished(QNetworkReply*)), + this, SLOT(replyFinished(QNetworkReply*))); + return qnam; +} + +void QNAMRedirect::doRequest() { + QString text = "QNAMRedirect::doRequest doing request to "; + text.append(this->_originalUrl.toString()); + this->_textContainer->setText(text); + /* Let's just create network request for this predifned URL... */ + QNetworkRequest request(this->_originalUrl); + /* ...and ask the manager to do the request. */ + QNetWorkReply reply = this->_qnam->get(request); + connect(reply, SIGNAL(finished()), this, SLOT(httpFinished())); + connect(reply, SIGNAL(readyRead()),this, SLOT(httpReadyRead())); + connect(reply, SIGNAL(downloadProgress(qint64,qint64)), + this, SLOT(updateDataReadProgress(qint64,qint64))); +} + +void QNAMRedirect::replyFinished(QNetworkReply* reply) { + /* + * Reply is finished! + * We'll ask for the reply about the Redirection attribute + * http://doc.trolltech.com/qnetworkrequest.html#Attribute-enum + */ + QVariant possibleRedirectUrl = + reply->attribute(QNetworkRequest::RedirectionTargetAttribute); + + /* We'll deduct if the redirection is valid in the redirectUrl function */ + _urlRedirectedTo = this->redirectUrl(possibleRedirectUrl.toUrl(), + _urlRedirectedTo); + + /* If the URL is not empty, we're being redirected. */ + if(!_urlRedirectedTo.isEmpty()) { + QString text = QString("QNAMRedirect::replyFinished: Redirected to ") + .append(_urlRedirectedTo.toString()); + this->_textContainer->setText(text); + + /* We'll do another request to the redirection url. */ + this->_qnam->get(QNetworkRequest(_urlRedirectedTo)); + } + else { + /* + * We weren't redirected anymore + * so we arrived to the final destination... + */ + QString text = QString("QNAMRedirect::replyFinished: Arrived to ") + .append(reply->url().toString()); + this->_textContainer->setText(text); + /* ...so this can be cleared. */ + _urlRedirectedTo.clear(); + } + /* Clean up. */ + reply->deleteLater(); +} + +QUrl QNAMRedirect::redirectUrl(const QUrl& possibleRedirectUrl, + const QUrl& oldRedirectUrl) const { + QUrl redirectUrl; + /* + * Check if the URL is empty and + * that we aren't being fooled into a infinite redirect loop. + * We could also keep track of how many redirects we have been to + * and set a limit to it, but we'll leave that to you. + */ + if(!possibleRedirectUrl.isEmpty() && + possibleRedirectUrl != oldRedirectUrl) { + redirectUrl = possibleRedirectUrl; + } + return redirectUrl; +} Added: trunk/qterm-qt4/src/test/http/qnamredirect.h =================================================================== --- trunk/qterm-qt4/src/test/http/qnamredirect.h (rev 0) +++ trunk/qterm-qt4/src/test/http/qnamredirect.h 2010-08-09 10:03:20 UTC (rev 1238) @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2009 Nokia Corporation + */ + +#ifndef QNAMREDIRECT_H +#define QNAMREDIRECT_H + +#include <QtGui/QMainWindow> +#include <QtGui/QFrame> +#include <QtGui/QVBoxLayout> +#include <QtGui/QLabel> +#include <QtGui/QPushButton> + +#include <QtNetwork/QNetworkAccessManager> +#include <QtNetwork/QNetworkRequest> +#include <QtNetwork/QNetworkReply> + +#include <QtCore/QPointer> +#include <QtCore/QUrl> + +class QNAMRedirect : public QMainWindow +{ + Q_OBJECT + +public: + QNAMRedirect(QWidget *parent = 0); + ~QNAMRedirect(); + +private slots: + void doRequest(); + void replyFinished(QNetworkReply* reply); + +private: + QPointer<QNetworkAccessManager> _qnam; + QUrl _originalUrl; + QUrl _urlRedirectedTo; + + QPointer<QLabel> _textContainer; + + QNetworkAccessManager* createQNAM(); + QUrl redirectUrl(const QUrl& possibleRedirectUrl, + const QUrl& oldRedirectUrl) const; +}; + +#endif // QNAMREDIRECT_H This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |