-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathhttp_file.cc
404 lines (350 loc) · 12.2 KB
/
http_file.cc
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
// Copyright 2020 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include <packager/file/http_file.h>
#include <absl/flags/declare.h>
#include <absl/flags/flag.h>
#include <absl/log/check.h>
#include <absl/log/log.h>
#include <absl/strings/escaping.h>
#include <absl/strings/str_format.h>
#include <curl/curl.h>
#include <packager/file/file_closer.h>
#include <packager/file/thread_pool.h>
#include <packager/macros/compiler.h>
#include <packager/macros/logging.h>
#include <packager/version/version.h>
ABSL_FLAG(std::string,
user_agent,
"",
"Set a custom User-Agent string for HTTP requests.");
ABSL_FLAG(std::string,
ca_file,
"",
"Absolute path to the Certificate Authority file for the "
"server cert. PEM format");
ABSL_FLAG(std::string,
client_cert_file,
"",
"Absolute path to client certificate file.");
ABSL_FLAG(std::string,
client_cert_private_key_file,
"",
"Absolute path to the Private Key file.");
ABSL_FLAG(std::string,
client_cert_private_key_password,
"",
"Password to the private key file.");
ABSL_FLAG(bool,
disable_peer_verification,
false,
"Disable peer verification. This is needed to talk to servers "
"without valid certificates.");
ABSL_FLAG(bool,
ignore_http_output_failures,
false,
"Ignore HTTP output failures. Can help recover from live stream "
"upload errors.");
ABSL_DECLARE_FLAG(uint64_t, io_cache_size);
namespace shaka {
namespace {
constexpr const char* kBinaryContentType = "application/octet-stream";
constexpr const int kMinLogLevelForCurlDebugFunction = 2;
size_t CurlWriteCallback(char* buffer, size_t size, size_t nmemb, void* user) {
IoCache* cache = reinterpret_cast<IoCache*>(user);
size_t length = size * nmemb;
if (cache) {
length = cache->Write(buffer, length);
VLOG(3) << "CurlWriteCallback length=" << length;
} else {
// For the case of HTTP Put, the returned data may not be consumed. Return
// the size of the data to avoid curl errors.
}
return length;
}
size_t CurlReadCallback(char* buffer, size_t size, size_t nitems, void* user) {
IoCache* cache = reinterpret_cast<IoCache*>(user);
size_t length = cache->Read(buffer, size * nitems);
VLOG(3) << "CurlRead length=" << length;
return length;
}
int CurlDebugCallback(CURL* /* handle */,
curl_infotype type,
const char* data,
size_t size,
void* /* userptr */) {
const char* type_text;
int log_level;
bool in_hex;
switch (type) {
case CURLINFO_TEXT:
type_text = "== Info";
log_level = kMinLogLevelForCurlDebugFunction + 1;
in_hex = false;
break;
case CURLINFO_HEADER_IN:
type_text = "<= Recv header";
log_level = kMinLogLevelForCurlDebugFunction;
in_hex = false;
break;
case CURLINFO_HEADER_OUT:
type_text = "=> Send header";
log_level = kMinLogLevelForCurlDebugFunction;
in_hex = false;
break;
case CURLINFO_DATA_IN:
type_text = "<= Recv data";
log_level = kMinLogLevelForCurlDebugFunction + 1;
in_hex = true;
break;
case CURLINFO_DATA_OUT:
type_text = "=> Send data";
log_level = kMinLogLevelForCurlDebugFunction + 1;
in_hex = true;
break;
case CURLINFO_SSL_DATA_IN:
type_text = "<= Recv SSL data";
log_level = kMinLogLevelForCurlDebugFunction + 2;
in_hex = true;
break;
case CURLINFO_SSL_DATA_OUT:
type_text = "=> Send SSL data";
log_level = kMinLogLevelForCurlDebugFunction + 2;
in_hex = true;
break;
default:
// Ignore other debug data.
return 0;
}
const std::string data_string(data, size);
VLOG(log_level) << "\n\n"
<< type_text << " (0x" << std::hex << size << std::dec
<< " bytes)\n"
<< (in_hex ? absl::BytesToHexString(data_string)
: data_string);
return 0;
}
class LibCurlInitializer {
public:
LibCurlInitializer() {
curl_global_init(CURL_GLOBAL_DEFAULT);
}
~LibCurlInitializer() {
curl_global_cleanup();
}
LibCurlInitializer(const LibCurlInitializer&) = delete;
LibCurlInitializer& operator=(const LibCurlInitializer&) = delete;
};
template <typename List>
bool AppendHeader(const std::string& header, List* list) {
auto* temp = curl_slist_append(list->get(), header.c_str());
if (temp) {
list->release(); // Don't free old list since it's part of the new one.
list->reset(temp);
return true;
} else {
return false;
}
}
} // namespace
HttpFile::HttpFile(HttpMethod method, const std::string& url)
: HttpFile(method, url, kBinaryContentType, {}, 0) {}
HttpFile::HttpFile(HttpMethod method,
const std::string& url,
const std::string& upload_content_type,
const std::vector<std::string>& headers,
int32_t timeout_in_seconds)
: File(url.c_str()),
url_(url),
upload_content_type_(upload_content_type),
timeout_in_seconds_(timeout_in_seconds),
method_(method),
isUpload_(method == HttpMethod::kPut || method == HttpMethod::kPost),
download_cache_(absl::GetFlag(FLAGS_io_cache_size)),
upload_cache_(absl::GetFlag(FLAGS_io_cache_size)),
curl_(curl_easy_init()),
status_(Status::OK),
user_agent_(absl::GetFlag(FLAGS_user_agent)),
ca_file_(absl::GetFlag(FLAGS_ca_file)),
client_cert_file_(absl::GetFlag(FLAGS_client_cert_file)),
client_cert_private_key_file_(
absl::GetFlag(FLAGS_client_cert_private_key_file)),
client_cert_private_key_password_(
absl::GetFlag(FLAGS_client_cert_private_key_password)) {
static LibCurlInitializer lib_curl_initializer;
if (user_agent_.empty()) {
user_agent_ += "ShakaPackager/" + GetPackagerVersion();
}
// We will have at least one header, so use a null header to signal error
// to Open.
// Don't wait for 100-Continue.
std::unique_ptr<curl_slist, CurlDelete> temp_headers;
if (!AppendHeader("Expect:", &temp_headers))
return;
if (!upload_content_type.empty() &&
!AppendHeader("Content-Type: " + upload_content_type_, &temp_headers)) {
return;
}
if (isUpload_ && !AppendHeader("Transfer-Encoding: chunked", &temp_headers)) {
return;
}
for (const auto& item : headers) {
if (!AppendHeader(item, &temp_headers)) {
return;
}
}
request_headers_ = std::move(temp_headers);
}
HttpFile::~HttpFile() {}
// static
bool HttpFile::Delete(const std::string& url) {
std::unique_ptr<HttpFile, FileCloser> file(
new HttpFile(HttpMethod::kDelete, url));
if (!file->Open()) {
return false;
}
return file.release()->Close();
}
bool HttpFile::Open() {
VLOG(2) << "Opening " << url_;
if (!curl_ || !request_headers_) {
LOG(ERROR) << "curl_easy_init() failed.";
return false;
}
// TODO: Try to connect initially so we can return connection error here.
// TODO: Implement retrying with exponential backoff, see
// "widevine_key_source.cc"
ThreadPool::instance.PostTask(std::bind(&HttpFile::ThreadMain, this));
return true;
}
Status HttpFile::CloseWithStatus() {
VLOG(2) << "Closing " << url_;
// Close the upload cache first so the thread will finish uploading.
// Otherwise it will wait for more data forever.
// Don't close the download cache, so that the server's response (HTTP status
// code at minimum) can still be written after uploading is complete.
// The task will close the download cache when it is complete.
upload_cache_.Close();
task_exit_event_.WaitForNotification();
const Status result = status_;
LOG_IF(ERROR, !result.ok()) << "HttpFile request failed: " << result;
delete this;
return absl::GetFlag(FLAGS_ignore_http_output_failures) ? Status::OK : result;
}
bool HttpFile::Close() {
return CloseWithStatus().ok();
}
int64_t HttpFile::Read(void* buffer, uint64_t length) {
VLOG(2) << "Reading from " << url_ << ", length=" << length;
return download_cache_.Read(buffer, length);
}
int64_t HttpFile::Write(const void* buffer, uint64_t length) {
DCHECK(!upload_cache_.closed());
VLOG(2) << "Writing to " << url_ << ", length=" << length;
return upload_cache_.Write(buffer, length);
}
void HttpFile::CloseForWriting() {
VLOG(2) << "Closing further writes to " << url_;
upload_cache_.Close();
}
int64_t HttpFile::Size() {
VLOG(1) << "HttpFile does not support Size().";
return -1;
}
bool HttpFile::Flush() {
// Wait for curl to read any data we may have buffered.
upload_cache_.WaitUntilEmptyOrClosed();
return true;
}
bool HttpFile::Seek(uint64_t position) {
UNUSED(position);
LOG(ERROR) << "HttpFile does not support Seek().";
return false;
}
bool HttpFile::Tell(uint64_t* position) {
UNUSED(position);
LOG(ERROR) << "HttpFile does not support Tell().";
return false;
}
void HttpFile::CurlDelete::operator()(CURL* curl) {
curl_easy_cleanup(curl);
}
void HttpFile::CurlDelete::operator()(curl_slist* headers) {
curl_slist_free_all(headers);
}
void HttpFile::SetupRequest() {
auto* curl = curl_.get();
switch (method_) {
case HttpMethod::kGet:
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
break;
case HttpMethod::kPost:
curl_easy_setopt(curl, CURLOPT_POST, 1L);
break;
case HttpMethod::kPut:
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
break;
case HttpMethod::kDelete:
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
}
curl_easy_setopt(curl, CURLOPT_URL, url_.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent_.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout_in_seconds_);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &download_cache_);
if (isUpload_) {
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &CurlReadCallback);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_cache_);
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, request_headers_.get());
if (absl::GetFlag(FLAGS_disable_peer_verification))
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
// Client authentication
if (!client_cert_private_key_file_.empty() && !client_cert_file_.empty()) {
curl_easy_setopt(curl, CURLOPT_SSLKEY,
client_cert_private_key_file_.data());
curl_easy_setopt(curl, CURLOPT_SSLCERT, client_cert_file_.data());
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
if (!client_cert_private_key_password_.empty()) {
curl_easy_setopt(curl, CURLOPT_KEYPASSWD,
client_cert_private_key_password_.data());
}
}
if (!ca_file_.empty()) {
curl_easy_setopt(curl, CURLOPT_CAINFO, ca_file_.data());
}
if (VLOG_IS_ON(kMinLogLevelForCurlDebugFunction)) {
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, CurlDebugCallback);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
}
}
void HttpFile::ThreadMain() {
SetupRequest();
CURLcode res = curl_easy_perform(curl_.get());
if (res != CURLE_OK) {
std::string error_message = curl_easy_strerror(res);
if (res == CURLE_HTTP_RETURNED_ERROR) {
long response_code = 0;
curl_easy_getinfo(curl_.get(), CURLINFO_RESPONSE_CODE, &response_code);
error_message += absl::StrFormat(", response code: %ld.", response_code);
}
status_ = Status(
res == CURLE_OPERATION_TIMEDOUT ? error::TIME_OUT : error::HTTP_FAILURE,
error_message);
}
// In some cases it is possible that the server has already closed the
// connection without reading the request body. This can for example happen
// when the server responds with a non-successful status code. In this case we
// need to make sure to close the upload cache here, otherwise some other
// thread may block forever on Flush().
upload_cache_.Close();
download_cache_.Close();
task_exit_event_.Notify();
}
} // namespace shaka