-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.cpp
615 lines (494 loc) · 18.6 KB
/
session.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
<one line to give the library's name and an idea of what it does.>
Copyright (C) 2013 <copyright holder> <email>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <libgen.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdexcept>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <neon/ne_alloc.h>
#include <neon/ne_auth.h>
#include <neon/ne_basic.h>
#include <neon/ne_dates.h>
#include <neon/ne_locks.h>
#include <neon/ne_props.h>
#include <neon/ne_request.h>
#include <neon/ne_session.h>
#include <neon/ne_socket.h>
#include <neon/ne_string.h>
#include <neon/ne_uri.h>
#include <neon/ne_utils.h>
#include <neon/ne_xml.h>
#include <QDebug>
#include "tools.h"
#include "session.h"
#define EUNKNOWN 99999
/*
enum {
ETAG = 0,
LENGTH,
CREATION,
MODIFIED,
TYPE,
EXECUTE,
PERMISSIONS,
END
};
static const std::vector<ne_propname> prop_names = [] {
std::vector<ne_propname> v(END + 1);
v[ETAG] = {"DAV:", "getetag"};
v[LENGTH] = {"DAV:", "getcontentlength"};
v[CREATION] = {"DAV:", "creationdate"};
v[MODIFIED] = {"DAV:", "getlastmodified"};
v[TYPE] = {"DAV:", "resourcetype"};
v[EXECUTE] = {"http://apache.org/dav/props/", "executable"};
v[PERMISSIONS] = {"DAVQT:", "permissions"};
v[END] = {NULL, NULL};
return v;
} ();
static const std::vector<ne_propname> anonymous_prop_names = [] {
std::vector<ne_propname> v = prop_names;
for(auto it = v.begin(); it != v.end(); ++ it) it->nspace = NULL;
return v;
} ();
typedef std::function<void (ne_session_status, const ne_session_status_info*)> Notify;
struct auth_data_t {
QString user;
QString passwd;
};
struct neon_context_t {
// QString etag;
stat_t stat;
volatile bool& cancell;
ne_session* session;
};
struct cache_context_t {
QString path;
QList<remote_res_t> resources;
};
struct session_t::Pimpl {
QString scheme;
auth_data_t auth;
volatile bool cancell;
Notify notify;
bool notify_enabled;
std::shared_ptr<ne_session> session;
QDateTime lastprogress;
};
static int auth(void *userdata, const char *realm, int attempt, char *user, char *pwd)
{
if (attempt) {
return attempt;
}
auth_data_t* data = reinterpret_cast<auth_data_t*>(userdata);
strncpy(user, qPrintable(data->user), NE_ABUFSIZ - 1);
strncpy(pwd, qPrintable(data->passwd), NE_ABUFSIZ - 1);
return 0;
}
static int ssl_verify(void *userdata, int failures, const ne_ssl_certificate *cert)
{
return 0;
char *issuer = ne_ssl_readable_dname(ne_ssl_cert_issuer(cert));
char *subject = ne_ssl_readable_dname(ne_ssl_cert_subject(cert));
char *digest = (char*)ne_calloc(NE_SSL_DIGESTLEN);
if (!issuer || !subject || ne_ssl_cert_digest(cert, digest) != 0) {
std::cerr << "error processing server certificate" << std::endl;
if (issuer) free(issuer);
if (subject) free(subject);
if (digest) free(digest);
return -1;
}
int ret = -1;
if (failures & NE_SSL_NOTYETVALID)
std::cerr << "the server certificate is not yet valid" << std::endl;
if (failures & NE_SSL_EXPIRED)
std::cerr << "the server certificate has expired" << std::endl;
if (failures & NE_SSL_IDMISMATCH)
std::cerr << "the server certificate does not match the server name" << std::endl;
if (failures & NE_SSL_UNTRUSTED)
std::cerr << "the server certificate is not trusted" << std::endl;
if (failures & ~NE_SSL_FAILMASK)
std::cerr << "unknown certificate error" << std::endl;
printf((" issuer: %s"), issuer);
printf("\n");
printf((" subject: %s"), subject);
printf("\n");
printf((" identity: %s"), ne_ssl_cert_identity(cert));
printf("\n");// ne_session* session = ne_session_create("https", "webdav.yandex.ru", ne_uri_defaultport("https"));
printf((" fingerprint: %s"), digest);
printf("\n");
printf(("You only should accept this certificate, if you can\n"
"verify the fingerprint! The server might be faked\n"
"or there might be a man-in-the-middle-attack.\n"));
printf(("Accept certificate for this session? [y,N] "));
char *s = NULL;
size_t n = 0;
ssize_t len = 0;
len = getline(&s, &n, stdin);
if (len < 0)
abort();
if (rpmatch(s) > 0)
ret = 0;
free(s);
if (failures & NE_SSL_NOTYETVALID)
std::cerr << "the server certificate is not yet valid" << std::endl;
if (failures & NE_SSL_EXPIRED)
std::cerr << "the server certificate has expired" << std::endl;
if (failures & NE_SSL_IDMISMATCH)
std::cerr << "the server certificate does not match the server name" << std::endl;
if (failures & NE_SSL_UNTRUSTED)
std::cerr << "the server certificate is not trusted" << std::endl;
if (failures & ~NE_SSL_FAILMASK)
std::cerr << "unknown certificate error" << std::endl;
ne_ssl_cert_identity(cert);
if (issuer) free(issuer);
if (subject) free(subject);
if (digest) free(digest);
return ret;
}
inline QString normalize_etag(const char *etag)
{
if (!etag) return QString();
const char * e = etag;
if (*e == 'W') return QString();
if (!*e) return QString();
return QString(etag);
}
void create_request_handler(ne_request *req, void *userdata, const char *method, const char *requri) {
neon_context_t* ctx = reinterpret_cast<neon_context_t*>(userdata);
// if (ctx->etag.isNull()) {
// ne_add_request_header(req, "If-None-Match", "*");
// }
// else if(!ctx->etag.isEmpty()) {
// ne_add_request_header(req, "If-Match", qPrintable(ctx->etag));
// }
// else
// Q_ASSERT(ctx->etag == "");
}
int post_send_handler(ne_request *req, void *userdata, const ne_status *status) {
neon_context_t* ctx = reinterpret_cast<neon_context_t*>(userdata);
const char *value = ne_get_response_header(req, "Last-Modified");
if (!value) value = ne_get_response_header(req, "Date");
if (value) {
ctx->stat.mtime = ne_httpdate_parse(value);
} else {
ctx->stat.mtime = 0;
}
return NE_OK;
}
struct hook_helper_t {
ne_session* session;
neon_context_t* ctx;
hook_helper_t(ne_session* s, neon_context_t* c) : session(s), ctx(c) {
Q_ASSERT(session);
Q_ASSERT(ctx);
ne_hook_create_request(session, create_request_handler, ctx);
ne_hook_post_send(session, post_send_handler, ctx);
}
~hook_helper_t() {
ne_unhook_post_send(session, post_send_handler, ctx);
ne_unhook_create_request(session, create_request_handler, ctx);
}
};
void cache_result(void *userdata, const ne_uri *uri, const ne_prop_result_set *set)
{
cache_context_t* ctx = reinterpret_cast<cache_context_t*>(userdata);
assert(ctx);
assert(uri);
assert(set);
remote_res_t resource;
resource.path = QString::fromUtf8(ne_path_unescape(uri->path));
const char *data;
data = ne_propset_value(set, &prop_names[ETAG]);
if (!data) data = ne_propset_value(set, &anonymous_prop_names[ETAG]);
if (data) resource.etag = data;
data = ne_propset_value(set, &prop_names[TYPE]);
if (!data) data = ne_propset_value(set, &anonymous_prop_names[TYPE]);
resource.dir = (data && strstr(data, "collection"));
data = ne_propset_value(set, &prop_names[LENGTH]);
if (!data) data = ne_propset_value(set, &anonymous_prop_names[LENGTH]);
if (data) resource.size = boost::lexical_cast<off_t>(data);
data = ne_propset_value(set, &prop_names[PERMISSIONS]);
if (!data) data = ne_propset_value(set, &anonymous_prop_names[PERMISSIONS]);
if (data) {
resource.perms = QFile::Permissions(boost::lexical_cast<int>(data));
}
data = ne_propset_value(set, &prop_names[CREATION]);
if (!data) data = ne_propset_value(set, &anonymous_prop_names[CREATION]);
resource.ctime = 0;
if (data) {
resource.ctime = ne_iso8601_parse(data);
if (resource.ctime == static_cast<time_t>(-1))
resource.ctime = ne_httpdate_parse(data);
if (resource.ctime == static_cast<time_t>(-1))
resource.ctime = 0;
}
data = ne_propset_value(set, &prop_names[MODIFIED]);
if (!data) data = ne_propset_value(set, &anonymous_prop_names[MODIFIED]);
resource.mtime = 0;
if (data) {
resource.mtime = ne_httpdate_parse(data);
if (resource.mtime == static_cast<time_t>(-1))
resource.mtime = ne_iso8601_parse(data);
if (resource.mtime == static_cast<time_t>(-1))
resource.mtime = 0;
}
data = ne_propset_value(set, &prop_names[EXECUTE]);
if (!data) data = ne_propset_value(set, &anonymous_prop_names[EXECUTE]);
resource.exec = remote_res_t::none;
if (!data) {
resource.exec = (resource.dir) ? remote_res_t::none : remote_res_t::not_executable;
} else if (*data == 'T') {
resource.exec = remote_res_t::executable;
}
if (resource.path.right(1) == "/") resource.path = resource.path.left(resource.path.length() - 1);
if (QFileInfo(ctx->path).absoluteFilePath() == QFileInfo(resource.path).absoluteFilePath()) {
resource.name = ".";
}
else {
resource.name = QFileInfo(resource.path).fileName();
}
// qDebug() << "resource uri:" << ne_path_unescape(uri->path);
// qDebug() << "resource ctx:" << ctx->path;
// qDebug() << "resource ctx a:" << QFileInfo(ctx->path).absoluteFilePath();
// qDebug() << "resource path:" << resource.path ;
// qDebug() << "resource path a:" << QFileInfo(resource.path).absoluteFilePath() ;
// qDebug() << "resource name:" << resource.name ;
ctx->resources << resource;
}
void notify(void *userdata, ne_session_status status, const ne_session_status_info *info)
{
Notify* fn = reinterpret_cast<Notify*>(userdata);
fn->operator()(status, info);
}
static int http_code(ne_session* session) {
const char *p = ne_get_error(session);
char *q;
int err;
err = strtol(p, &q, 10);
if (p == q) {
err = EUNKNOWN;
}
return err;
}
session_t::session_t(QObject* parent, const QString& schema, const QString& host, quint32 port)
: QObject(parent)
, p_(new Pimpl)
{
p_->scheme = schema;
p_->lastprogress = QDateTime::currentDateTime();
p_->cancell = false;
p_->session.reset(
ne_session_create(schema.toLocal8Bit().constData(), host.toLocal8Bit().constData(), (port == -1) ? ne_uri_defaultport("https") : port),
ne_session_destroy
);
if (!p_->session.get()) throw std::runtime_error("Can't create session");
p_->notify_enabled = false;
p_->notify = [this] (ne_session_status status, const ne_session_status_info *info) {
if (p_->cancell) {
ne_set_notifier(p_->session.get(), NULL, NULL);
ne_close_connection(p_->session.get());
}
if (!p_->notify_enabled) return;
switch (status) {
case ne_status_lookup:
case ne_status_connecting:
break;
case ne_status_connected:
Q_EMIT connected();
break;
case ne_status_sending:
if (info->sr.progress == info->sr.total) {
p_->lastprogress = QDateTime::currentDateTime();
Q_EMIT put_progress(info->sr.progress, info->sr.total);
}
else if (p_->lastprogress.msecsTo(QDateTime::currentDateTime()) > 300) {
p_->lastprogress = QDateTime::currentDateTime();
Q_EMIT put_progress(info->sr.progress, info->sr.total);
}
break;
case ne_status_recving:
if (info->sr.progress == info->sr.total) {
p_->lastprogress = QDateTime::currentDateTime();
Q_EMIT get_progress(info->sr.progress, info->sr.total);
}
else if (p_->lastprogress.msecsTo(QDateTime::currentDateTime()) > 300) {
p_->lastprogress = QDateTime::currentDateTime();
Q_EMIT get_progress(info->sr.progress, info->sr.total);
}
break;
case ne_status_disconnected:
Q_EMIT disconnected();
break;
default:
Q_ASSERT(!"Unhandled status type");
};
};
ne_set_notifier(p_->session.get(), notify, &p_->notify);
}
session_t::~session_t()
{
ne_set_notifier(p_->session.get(), NULL, NULL);
}
void session_t::set_auth(const QString& user, const QString& password)
{
p_->auth.user = user;
p_->auth.passwd = password;
ne_add_server_auth(p_->session.get(), NE_AUTH_ALL, auth, &p_->auth);
}
void session_t::set_ssl()
{
if (p_->scheme != "https") return;
ne_ssl_set_verify(p_->session.get(), ssl_verify, NULL);
ne_ssl_trust_default_ca(p_->session.get());
}
void session_t::open()
{
std::shared_ptr<char> path(ne_path_escape("/"), free);
ne_server_capabilities caps = {0, 0, 0};
int ret = ne_options(p_->session.get(), path.get(), &caps);
if (ret) {
throw ne_exception_t(http_code(p_->session.get()), QString("Can't get server options:") + ne_get_error(p_->session.get()));
}
}
void session_t::cancell()
{
p_->cancell = true;
}
bool session_t::is_closed() const
{
return p_->cancell;
}
QList<remote_res_t> session_t::get_resources(QString unescaped_path) {
unescaped_path.prepend("/").replace("///", "/").replace("//", "/");
qDebug() << Q_FUNC_INFO << "rawpath: " << unescaped_path;
std::shared_ptr<char> path(ne_path_escape(qPrintable(unescaped_path)), free);
qDebug() << Q_FUNC_INFO << "path: " << path.get();
std::shared_ptr<ne_propfind_handler> ph(
ne_propfind_create(p_->session.get(), path.get(), NE_DEPTH_ONE),
ne_propfind_destroy
);
cache_context_t ctx;
ctx.path = unescaped_path;
if (int err = ne_propfind_named(ph.get(), &prop_names[0], cache_result, &ctx)) {
throw ne_exception_t(http_code(p_->session.get()), ne_get_error(p_->session.get()));
}
return ctx.resources;
}
stat_t session_t::head(const QString& unescaped_path)
{
std::shared_ptr<char> path(ne_path_escape(qPrintable(unescaped_path)), free);
std::shared_ptr<ne_request> request(
ne_request_create(p_->session.get(), "HEAD", path.get()),
ne_request_destroy
);
if (ne_request_dispatch(request.get()) != NE_OK) {
throw ne_exception_t(http_code(p_->session.get()), ne_get_error(p_->session.get()));
}
stat_t stat;
const char *value = ne_get_response_header(request.get(), "Last-Modified");
if (!value) value = ne_get_response_header(request.get(), "Date");
if (value) {
stat.mtime = ne_httpdate_parse(value);
}
value = ne_get_response_header(request.get(), "Content-Length");
if (value) {
stat.size = strtol(value, NULL, 10);
}
return stat;
}
stat_t session_t::set_permissions(const QString& unescaped_path, QFile::Permissions perms)
{
std::shared_ptr<char> path(ne_path_escape(qPrintable(unescaped_path)), free);
const std::string value = boost::lexical_cast<std::string>(perms);
const ne_proppatch_operation ops[2] = {
{
&prop_names[PERMISSIONS],
ne_propset,
value.c_str()
},
NULL
};
neon_context_t ctx{
stat_t(),
p_->cancell,
p_->session.get()
};
hook_helper_t hooks(p_->session.get(), &ctx);
if (ne_proppatch(p_->session.get(), path.get(), ops) != NE_OK) {
throw ne_exception_t(http_code(p_->session.get()), ne_get_error(p_->session.get()));
}
return ctx.stat;
}
stat_t session_t::get(const QString& unescaped_path, int fd)
{
std::shared_ptr<char> path(ne_path_escape(qPrintable(unescaped_path)), free);
neon_context_t ctx{
stat_t(),
p_->cancell,
p_->session.get()
};
scoped_value<bool> v(p_->notify_enabled, true, false);
hook_helper_t hooker(p_->session.get(), &ctx);
if (ne_get(p_->session.get(), path.get(), fd) != NE_OK) {
throw ne_exception_t(http_code(p_->session.get()), ne_get_error(p_->session.get()));
}
return ctx.stat;
}
stat_t session_t::put(const QString& unescaped_path, int fd)
{
std::shared_ptr<char> path(ne_path_escape(qPrintable(unescaped_path)), free);
neon_context_t ctx{
stat_t(),
p_->cancell,
p_->session.get()
};
scoped_value<bool> v(p_->notify_enabled, true, false);
hook_helper_t hooker(p_->session.get(), &ctx);
if (ne_put(p_->session.get(), path.get(), fd) != NE_OK) {
throw ne_exception_t(http_code(p_->session.get()), ne_get_error(p_->session.get()));
}
return ctx.stat;
}
void session_t::remove(const QString& unescaped_path)
{
std::shared_ptr<char> path(ne_path_escape(qPrintable(unescaped_path)), free);
neon_context_t ctx{
stat_t(),
p_->cancell,
p_->session.get()
};
hook_helper_t hooker(p_->session.get(), &ctx);
if (ne_delete(p_->session.get(), path.get()) != NE_OK) {
throw ne_exception_t(http_code(p_->session.get()), ne_get_error(p_->session.get()));
}
}
stat_t session_t::mkcol(const QString& raw)
{
QString unescaped_path = raw;
unescaped_path.append("/").replace("//", "/");
std::shared_ptr<char> path(ne_path_escape(qPrintable(unescaped_path)), free);
neon_context_t ctx{
stat_t(),
p_->cancell,
p_->session.get()
};
hook_helper_t hooker(p_->session.get(), &ctx);
if (ne_mkcol(p_->session.get(), path.get()) != NE_OK) {
throw ne_exception_t(http_code(p_->session.get()), ne_get_error(p_->session.get()));
}
return ctx.stat;
}*/