~ubuntu-branches/ubuntu/oneiric/psi/oneiric

« back to all changes in this revision

Viewing changes to src/serverlistquerier.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
#include <QHttp>
 
22
#include <QUrl>
22
23
#include <QDomDocument>
23
24
#include <QDomElement>
24
25
#include <QDomNodeList>
25
26
 
26
27
#include "serverlistquerier.h"
27
28
 
28
 
#define SERVERLIST_SERVER "www.jabber.org"
29
 
#define SERVERLIST_PORT 80
30
 
#define SERVERLIST_PATH "/servers.xml"
 
29
#define SERVERLIST_SERVER        "xmpp.org"
 
30
#define SERVERLIST_PORT          80
 
31
#define SERVERLIST_PATH          "/services/services.xml"
 
32
#define SERVERLIST_MAX_REDIRECT  5
31
33
 
32
34
ServerListQuerier::ServerListQuerier(QObject* parent) : QObject(parent)
33
35
{
37
39
 
38
40
void ServerListQuerier::getList()
39
41
{
 
42
        redirectCount_ = 0;
40
43
        http_->get(SERVERLIST_PATH);
41
44
}
42
45
 
43
46
void ServerListQuerier::get_finished(int, bool err)
44
47
{
 
48
        const QHttpResponseHeader response = http_->lastResponse();
 
49
 
45
50
        if (err) {
46
51
                emit error(http_->errorString());
47
52
        }
48
53
        else {
49
 
                // Parse the XML file
50
 
                QDomDocument doc;
51
 
                if (!doc.setContent(http_->readAll())) {
52
 
                        emit error(tr("Unable to parse server list"));
53
 
                        return;
54
 
                }
55
 
                
56
 
                // Fill the list
57
 
                QStringList servers;
58
 
                QDomNodeList items = doc.elementsByTagName("item");
59
 
                for (int i = 0; i < items.count(); i++) {
60
 
                        QString jid = items.item(i).toElement().attribute("jid");
61
 
                        if (!jid.isEmpty()) {
62
 
                                servers.push_back(jid);
63
 
                        }
64
 
                }
65
 
                emit listReceived(servers);
 
54
                if(response.statusCode() == 200) {
 
55
                        // Parse the XML file
 
56
                        QDomDocument doc;
 
57
                        if (!doc.setContent(http_->readAll())) {
 
58
                                emit error(tr("Unable to parse server list"));
 
59
                                return;
 
60
                        }
 
61
 
 
62
                        // Fill the list
 
63
                        QStringList servers;
 
64
                        QDomNodeList items = doc.elementsByTagName("item");
 
65
                        for (int i = 0; i < items.count(); i++) {
 
66
                                QString jid = items.item(i).toElement().attribute("jid");
 
67
                                if (!jid.isEmpty()) {
 
68
                                        servers.push_back(jid);
 
69
                                }
 
70
                        }
 
71
                        emit listReceived(servers);
 
72
                }
 
73
                else if(response.statusCode() == 301 || response.statusCode() == 302) {
 
74
                        // handle redirections:
 
75
                        // 301 = moved permanently
 
76
                        // 302 = found
 
77
                        if (redirectCount_ >= SERVERLIST_MAX_REDIRECT) {
 
78
                                emit error(tr("Maximum redirect count reached"));
 
79
                                return;
 
80
                        }
 
81
 
 
82
                        QString newUrl = response.value("Location");
 
83
 
 
84
                        QUrl url = newUrl;
 
85
                        if (!url.protocol().isEmpty()) {
 
86
                                if (url.protocol() != "http") {
 
87
                                        emit error(tr("Redirect to protocol '%1' not supported").arg(url.protocol()));
 
88
                                        return;
 
89
                                }
 
90
 
 
91
                                if (url.host().isEmpty()) {
 
92
                                        emit error(tr("Cannot redirect to empty host"));
 
93
                                        return;
 
94
                                }
 
95
 
 
96
                                http_->disconnect(this);
 
97
                                http_->deleteLater();
 
98
                                http_ = new QHttp(url.host(), url.port(80), this);
 
99
                                connect(http_,SIGNAL(requestFinished(int,bool)),SLOT(get_finished(int,bool)));
 
100
                        }
 
101
 
 
102
                        ++redirectCount_;
 
103
                        http_->get(newUrl);
 
104
                }
 
105
                else {
 
106
                        emit error(tr("Unexpected HTTP status code: %1").arg(response.statusCode()));
 
107
                }
66
108
        }
67
109
}