-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCookieJar.cpp
69 lines (54 loc) · 1.57 KB
/
CookieJar.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
#include "CookieJar.hpp"
#include <QNetworkCookie>
#include <fcntl.h>
#include <stdio.h>
#include <string>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#define MAX_LINE_LENGTH 4096
bool CookieJar::serialize(const QString& path) {
FILE* f = fopen(path.toUtf8(), "w");
if(f == NULL) {
perror("fopen");
return false;
}
bool success = true;
QList<QNetworkCookie> cookies = allCookies();
for(QList<QNetworkCookie>::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
int ret = fprintf(f, "%s\n", itr->toRawForm().constData());
if(ret < 0) {
perror("fprintf");
success = false;
}
}
fclose(f);
return success;
}
bool CookieJar::deserialize(const QString& path) {
FILE* f = fopen(path.toUtf8(), "r");
if(f == NULL) {
return true; // cookie jar file didn't exist - do nothing
}
QList<QNetworkCookie> cookies;
char line[MAX_LINE_LENGTH + 1];
while(fgets(line, MAX_LINE_LENGTH, f) != NULL) {
QList<QNetworkCookie> tmp = QNetworkCookie::parseCookies(line);
if(tmp.size() == 0) {
continue;
}
if(tmp.size() > 1) {
fprintf(stderr, "warning: cookie jar contains multiple cookies on a single line. We are only taking the first.\n");
fprintf(stderr, "%s\n", line);
}
cookies.push_back(tmp.first());
}
if(errno != 0) {
perror("fgets");
}
setAllCookies(cookies);
fclose(f);
return true;
}