-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathremotezipsource.cpp
More file actions
185 lines (176 loc) · 6.42 KB
/
Copy pathremotezipsource.cpp
File metadata and controls
185 lines (176 loc) · 6.42 KB
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
#include "remotezipsource.h"
#include <playapi/util/http.h>
#include <QDebug>
#include <algorithm>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <stdexcept>
#include <sstream>
#include <unistd.h>
RemoteZipSource::RemoteZipSource(size_t size, std::string url, std::string userAgent, std::string cookie)
: m_size(size), m_url(std::move(url)), m_userAgent(std::move(userAgent)), m_cookie(std::move(cookie)) {
}
RemoteZipSource::~RemoteZipSource() {
stopWorker();
}
void RemoteZipSource::stopWorker() {
if (m_readFd >= 0) {
close(m_readFd);
m_readFd = -1;
}
if (m_worker.joinable())
m_worker.join();
}
void RemoteZipSource::startWorker() {
int pair[] = {0, 0};
if (pipe(pair) != 0)
throw std::runtime_error("pipe failed");
m_readFd = pair[0];
int writeFd = pair[1];
const auto startOffset = m_offset;
#ifdef F_SETNOSIGPIPE
fcntl(writeFd, F_SETNOSIGPIPE, 1);
#endif
qDebug() << "RemoteZipSource: start worker"
<< "offset=" << startOffset
<< "size=" << m_size;
m_worker = std::thread([this, writeFd](uint64_t start) {
playapi::http_request req(m_url);
const auto rangeHeader = (std::stringstream() << "bytes=" << start << "-").str();
req.set_method(playapi::http_method::GET);
req.add_header("Accept-Encoding", "identity");
req.add_header("Cookie", m_cookie);
req.add_header("User-Agent", m_userAgent);
req.add_header("Range", rangeHeader);
req.set_follow_location(true);
req.set_timeout(100L);
req.set_custom_output_func([writeFd](char* data, size_t size) {
return write(writeFd, data, size);
});
try {
qDebug() << "RemoteZipSource: HTTP GET" << QString::fromStdString(rangeHeader);
auto response = req.perform();
qDebug() << "RemoteZipSource: HTTP completed: "
<< "start=" << start << "status=" << response.get_status_code();
} catch (...) {
qDebug() << "RemoteZipSource: HTTP request threw"
<< "start=" << start;
}
qDebug() << "RemoteZipSource: closing write pipe"
<< "start=" << start;
close(writeFd);
}, startOffset);
}
zip_source_t* RemoteZipSource::create() {
return zip_source_function_create(&RemoteZipSource::callback, this, nullptr);
}
zip_int64_t RemoteZipSource::callback(void *context, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
auto* source = static_cast<RemoteZipSource*>(context);
switch (cmd) {
case ZIP_SOURCE_OPEN:
return 0;
case ZIP_SOURCE_CLOSE:
source->stopWorker();
return 0;
case ZIP_SOURCE_STAT: {
auto* st = static_cast<zip_stat_t*>(data);
st->size = source->m_size;
st->comp_size = 0;
st->mtime = 0;
st->crc = 0;
st->comp_method = 0;
st->encryption_method = 0;
st->flags = 0;
st->valid = ZIP_STAT_SIZE;
return sizeof(zip_stat_t);
}
case ZIP_SOURCE_READ: {
bool worker_started = false;
while (source->m_worker.joinable() &&
source->m_offset < source->m_pendingSeek &&
source->m_pendingSeek < source->m_offset + 0x100000) {
size_t remaining = source->m_pendingSeek - source->m_offset;
char buf[0x1000];
int r = read(source->m_readFd, buf, std::min(sizeof(buf), remaining));
if (r <= 0) {
qDebug() << "RemoteZipSource: seek-drain read failed"
<< "r=" << r
<< "errno=" << errno
<< strerror(errno)
<< "offset=" << source->m_offset
<< "pending=" << source->m_pendingSeek
<< "remaining=" << remaining;
return r;
}
source->m_offset += (size_t) r;
}
if (!source->m_worker.joinable() || source->m_pendingSeek != source->m_offset) {
qDebug() << "RemoteZipSource: restart worker"
<< "joinable=" << source->m_worker.joinable()
<< "offset=" << source->m_offset
<< "pending=" << source->m_pendingSeek;
source->m_offset = source->m_pendingSeek;
if (source->m_worker.joinable()) {
close(source->m_readFd);
source->m_readFd = -1;
source->m_worker.join();
}
source->startWorker();
worker_started = true;
}
ssize_t r = read(source->m_readFd, data, len);
if(r == 0) {
if(!worker_started) {
qDebug() << "RemoteZipSource: main read returned 0, but worker not started, maybe corrupted?"
<< "offset=" << source->m_offset
<< "pending=" << source->m_pendingSeek
<< "len=" << len;
}
if (source->m_worker.joinable()) {
close(source->m_readFd);
source->m_readFd = -1;
source->m_worker.join();
}
return callback(context, data, len, ZIP_SOURCE_READ);
}
if (r <= 0) {
qDebug() << "RemoteZipSource: main read returned"
<< "r=" << r
<< "errno=" << errno
<< strerror(errno)
<< "offset=" << source->m_offset
<< "pending=" << source->m_pendingSeek
<< "len=" << len;
}
if (r > 0) {
source->m_offset += static_cast<size_t>(r);
source->m_pendingSeek += static_cast<size_t>(r);
}
return r;
}
case ZIP_SOURCE_SEEK: {
zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, nullptr);
switch (args->whence) {
case SEEK_SET:
source->m_pendingSeek = args->offset;
break;
case SEEK_CUR:
source->m_pendingSeek += args->offset;
break;
case SEEK_END:
source->m_pendingSeek = source->m_size + args->offset;
break;
default:
return -1;
}
return source->m_pendingSeek;
}
case ZIP_SOURCE_TELL:
return source->m_pendingSeek;
case ZIP_SOURCE_SUPPORTS:
return ZIP_SOURCE_SUPPORTS_SEEKABLE;
default:
return -1;
}
}