-
Notifications
You must be signed in to change notification settings - Fork 1
/
carddavclient.cpp
277 lines (221 loc) · 8.9 KB
/
carddavclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "carddavclient.h"
#include <QNetworkRequest>
#include <QDebug>
#include <QAuthenticator>
#include <QBuffer>
#include <QXmlQuery>
#include <QXmlResultItems>
#include <QDir>
class CardDavClient::Impl{
CardDavClient *expl;
void query(QString verb, QMap<QString,QString> headers = QMap<QString,QString>(), QString payloadString = QString()) {
query(verb, QString(), headers, payloadString);
}
void query(QString verb, QString path, QMap<QString,QString> headers = QMap<QString,QString>(), QString payloadString = QString()) {
QByteArray payloadBytes(payloadString.toUtf8());
QBuffer* payload = new QBuffer(expl);
payload->write(payloadBytes);
payload->open(QBuffer::ReadWrite);
payload->seek(0);
qDebug() << "Querying " << verb << ":" << expl->baseURL << " and " << path << "!\n for:\n"
<< payloadString;
QUrl url = expl->baseURL;
if (!path.isEmpty()) {
url.setPath(url.path() + "/" + path);
}
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/xml");
request.setRawHeader("Accept", "*/*");
request.setRawHeader("User-Agent", "curl/7.32.0");
request.setRawHeader("Host", "google.com");
if (!payloadString.isEmpty()) {
request.setHeader(QNetworkRequest::ContentLengthHeader, payloadBytes.size());
}
for(QMap<QString,QString>::const_iterator i = headers.constBegin();
i != headers.constEnd(); ++i) {
request.setRawHeader(i.key().toUtf8(), i.value().toUtf8());
}
if (expl->token().isEmpty()){
request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(expl->username()).arg(expl->password()).toAscii().toBase64()));
} else {
request.setRawHeader("Authorization", "Bearer " + expl->token().toAscii());
}
QString header;
foreach(header, request.rawHeaderList()) qDebug() << header << "!" << request.rawHeader(header.toLocal8Bit());
QNetworkReply *reply = expl->networkAccessManager.sendCustomRequest(request, verb.toAscii(), payload);
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), expl, SLOT(replyError(QNetworkReply::NetworkError)));
}
public:
explicit Impl(CardDavClient* parent = 0): expl(parent) {}
void getRedirect() {
QMap<QString, QString> headers;
headers["Accept"] = "*/*";
query("GET", QString(), headers);
}
void optionsStart() {
query("OPTIONS");
}
void optionsFinished(QNetworkReply* reply) {
QList<QByteArray> headerList = reply->rawHeaderList();
QByteArray header;
bool hasPropfind = false;
foreach (header, headerList) {
if (QString(reply->rawHeader(header)).contains(QString("PROPFIND"))) {
hasPropfind = true;
}
}
if (hasPropfind) {
propfindStart();
} else {
emit expl->error("The server doesn't do PROPFIND.");
}
}
void propfindStart(QString payload = QString()) {
QMap<QString, QString> headers;
headers["Depth"] = "1";
query("PROPFIND", headers, payload);
}
void propfindFinished(QNetworkReply* reply) {
QXmlQuery queryCards;
QXmlQuery queryHref;
QStringList output;
QString wholeReply(reply->readAll());
qDebug() << wholeReply;
if (!expl->isHaveURL()) {
queryHref.setFocus(wholeReply);
queryHref.setQuery("declare default element namespace \"DAV:\"; /multistatus/response/href/string()");
if (!queryHref.evaluateTo(&output) || output.length() < 1) {
qDebug()<<output;
emit expl->error("Error Evaluating href Query");
} else {
expl->overrideRelativePath(output[0]);
// loop back
propfindStart();
}
} else {
queryCards.setFocus(wholeReply);
queryCards.setQuery("declare default element namespace \"DAV:\"; /multistatus/response/propstat/prop/concat(displayname/string(),'`',getetag/string())");
if (!queryCards.evaluateTo(&output) || !output.length()) {
expl->error("Error evaluating cards query");
} else {
emit expl->cardNames(output);
}
}
}
void getStart(QString path) {
QUrl url = expl->baseURL;
if (!path.isEmpty()) {
url.setPath(url.path() + path);
}
QNetworkRequest request(url);
QNetworkReply *reply = expl->networkAccessManager.get(request);
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), expl, SLOT(replyError(QNetworkReply::NetworkError)));
}
void getFinished(QNetworkReply* reply) {
QDir path(expl->baseURL.path());
QString file = path.relativeFilePath(reply->request().url().path());
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
emit expl->card(file,reply->readAll());
} else {
qDebug()<<"GET finished with code "<< reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()
<<" for request " << reply->request().url().path();
}
}
void reportStart(){
QString payload("<D:propfind xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n"
"<D:prop>\n"
"<D:getetag />\n"
"<D:name />\n"
"</D:prop>\n"
"</D:propfind>");
query("REPORT", QMap<QString,QString>(), payload);
}
void reportFinished(QNetworkReply* reply) {
qDebug() << "REPORT body ";
qDebug() << reply->readAll();
}
};
CardDavClient::CardDavClient(QUrl url, QObject *parent) :
QObject(parent), baseURL(url), networkAccessManager(this)
{
this->impl = new Impl(this);
connect(&networkAccessManager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
connect(&networkAccessManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
}
void CardDavClient::getCardNamesAsync()
{
//It seems GET is not redirected anymore, nor OPTIONS, only propfind
//impl->getRedirect();
impl->propfindStart();
}
void CardDavClient::getCardAsync(QString cardName) {
impl->getStart(cardName);
}
void CardDavClient::setUsername(QString username) {
this->_username = username;
}
void CardDavClient::setPassword(QString password) {
this->_password = password;
}
void CardDavClient::setToken(QString token) {
this->_token = token;
}
QString CardDavClient::password() {
return this->_password;
}
QString CardDavClient::username() {
return this->_username;
}
QString CardDavClient::token() {
return this->_token;
}
void CardDavClient::authenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator) {
qDebug() << "authentication required ";
authenticator->setUser(_username);
authenticator->setPassword(_password);
}
void CardDavClient::replyError(QNetworkReply::NetworkError networkError) {
emit error(QString("Reply error %1").arg(networkError));
}
void CardDavClient::overrideRelativePath(QString path) {
_haveURL = true;
baseURL.setPath(path);
}
bool CardDavClient::isHaveURL() {
return _haveURL;
}
void CardDavClient::replyFinished(QNetworkReply* reply) {
QString requestVerb(reply->request().attribute(QNetworkRequest::CustomVerbAttribute).toString());
QUrl redirectUrl(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl());
if (!redirectUrl.isEmpty()) {
qDebug()<<"Redirected to:"<<redirectUrl.path();
// replay (yes, with an a) everything with new URL
QString what = redirectUrl.path();
what.chop(1);
overrideRelativePath(what);
if (requestVerb == "PROPFIND") {
impl->propfindStart(QString() + "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> " +
"<D:propfind xmlns:D=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:carddav\"> <D:prop> <D:getetag /> <D:name /> </D:prop> </D:propfind>");
} else if (requestVerb == "OPTIONS") {
impl->optionsStart();
} else if (requestVerb == "REPORT") {
impl->reportStart();
}
} else if (reply->error()) {
qDebug () << "Hopefully this already got logged:" << reply->errorString();
} else {
if (requestVerb == "OPTIONS") {
impl->optionsFinished(reply);
} else if (requestVerb == "PROPFIND") {
impl->propfindFinished(reply);
#ifdef CARD_REPORT
} else if (requestVerb == "REPORT") {
impl->reportFinished(reply);
#endif
} else {
impl->getFinished(reply);
}
}
}