169 d_handle = curl_easy_init();
170 if (!d_handle)
throw BESInternalError(
"Could not allocate CURL handle", __FILE__, __LINE__);
172 curl::set_error_buffer(d_handle, d_errbuf.data());
174 CURLcode res = curl_easy_setopt(d_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
175 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_SSLVERSION", d_errbuf.data(), __FILE__, __LINE__);
178 res = curl_easy_setopt(d_handle, CURLOPT_DEBUGFUNCTION, curl_trace);
179 curl::check_setopt_result(res, prolog,
"CURLOPT_DEBUGFUNCTION", d_errbuf, __FILE__, __LINE__);
182 res = curl_easy_setopt(d_handle, CURLOPT_VERBOSE, 1L);
183 curl::check_setopt_result(res, prolog,
"CURLOPT_VERBOSE", d_errbuf, __FILE__, __LINE__);
186 res = curl_easy_setopt(d_handle, CURLOPT_HEADERFUNCTION, chunk_header_callback);
187 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_HEADERFUNCTION", d_errbuf.data(), __FILE__, __LINE__);
190 res = curl_easy_setopt(d_handle, CURLOPT_WRITEFUNCTION, chunk_write_data);
191 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_WRITEFUNCTION", d_errbuf.data(), __FILE__, __LINE__);
193#ifdef CURLOPT_TCP_KEEPALIVE
195 res = curl_easy_setopt(d_handle, CURLOPT_TCP_KEEPALIVE, 1L);
196 curl::check_setopt_result(res, prolog,
"CURLOPT_TCP_KEEPALIVE", d_errbuf, __FILE__, __LINE__);
199#ifdef CURLOPT_TCP_KEEPIDLE
201 res = curl_easy_setopt(d_handle, CURLOPT_TCP_KEEPIDLE, 120L);
202 curl::check_setopt_result(res, prolog,
"CURLOPT_TCP_KEEPIDLE", d_errbuf, __FILE__, __LINE__);
205#ifdef CURLOPT_TCP_KEEPINTVL
207 res = curl_easy_setopt(d_handle, CURLOPT_TCP_KEEPINTVL, 120L)
208 curl::check_setopt_result(res, prolog,
"CURLOPT_TCP_KEEPINTVL", d_errbuf, __FILE__, __LINE__);
308 string reason =
"The requested resource does not match any of the AllowedHost rules.";
309 if (!http::AllowedHosts::theHosts()->is_allowed(chunk->
get_data_url(), reason)) {
311 ss <<
"ERROR! The chunk url " << chunk->
get_data_url()->str() <<
" was rejected because: " << reason;
315 auto handle = make_unique<dmrpp_easy_handle>();
319 handle->d_in_use =
true;
322 handle->d_chunk = chunk;
324 CURLcode res = curl_easy_setopt(handle->d_handle, CURLOPT_URL, chunk->
get_data_url()->str().c_str());
325 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_URL", handle->d_errbuf.data(), __FILE__, __LINE__);
327 res = curl_easy_setopt(handle->d_handle, CURLOPT_SHARE, d_share);
328 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_SHARE", handle->d_errbuf.data(), __FILE__, __LINE__);
332 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_RANGE", handle->d_errbuf.data(), __FILE__, __LINE__);
335 res = curl_easy_setopt(handle->d_handle, CURLOPT_HEADERDATA,
reinterpret_cast<void *
>(chunk));
336 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_HEADERDATA", handle->d_errbuf.data(), __FILE__, __LINE__);
339 res = curl_easy_setopt(handle->d_handle, CURLOPT_WRITEDATA,
reinterpret_cast<void *
>(chunk));
340 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_WRITEDATA", handle->d_errbuf.data(), __FILE__, __LINE__);
343 res = curl_easy_setopt(handle->d_handle, CURLOPT_PRIVATE,
reinterpret_cast<void *
>(handle.get()));
344 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_PRIVATE", handle->d_errbuf.data(), __FILE__, __LINE__);
347 res = curl_easy_setopt(handle->d_handle, CURLOPT_COOKIEFILE, d_cookies_filename.c_str());
348 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_COOKIEFILE", handle->d_errbuf.data(), __FILE__, __LINE__);
350 res = curl_easy_setopt(handle->d_handle, CURLOPT_COOKIEJAR, d_cookies_filename.c_str());
351 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_COOKIEJAR", handle->d_errbuf.data(), __FILE__, __LINE__);
354 res = curl_easy_setopt(handle->d_handle, CURLOPT_FOLLOWLOCATION, 1);
355 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_FOLLOWLOCATION", handle->d_errbuf.data(), __FILE__, __LINE__);
357 res = curl_easy_setopt(handle->d_handle, CURLOPT_MAXREDIRS, d_max_redirects);
358 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_MAXREDIRS", handle->d_errbuf.data(), __FILE__, __LINE__);
361 res = curl_easy_setopt(handle->d_handle, CURLOPT_USERAGENT, d_hyrax_user_agent.c_str());
362 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_USERAGENT", handle->d_errbuf.data(), __FILE__, __LINE__);
367 res = curl_easy_setopt(handle->d_handle, CURLOPT_HTTPAUTH, (
long) CURLAUTH_ANY);
368 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_HTTPAUTH", handle->d_errbuf.data(), __FILE__, __LINE__);
371 res = curl_easy_setopt(handle->d_handle, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
372 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_NETRC", handle->d_errbuf.data(), __FILE__, __LINE__);
375 if (!d_netrc_file.empty()) {
376 res = curl_easy_setopt(handle->d_handle, CURLOPT_NETRC_FILE, d_netrc_file.c_str());
377 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_NETRC_FILE", handle->d_errbuf.data(), __FILE__, __LINE__);
382 if (!curl::is_url_signed_for_s3(handle->d_url->str())) {
384 if (credentials && credentials->
is_s3_cred()) {
385 handle->d_request_headers = curl::sign_s3_url(handle->d_url, credentials, handle->d_request_headers);
386 res = curl_easy_setopt(handle->d_handle, CURLOPT_HTTPHEADER, handle->d_request_headers);
387 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_HTTPHEADER", handle->d_errbuf.data(), __FILE__, __LINE__);
392 INFO_LOG(prolog <<
"Looked for credentials for: " << handle->d_url->str() <<
'\n');
395 if (credentials && credentials->
is_s3_cred()) {
396 BESDEBUG(DMRPP_CURL, prolog <<
"Got AccessCredentials instance:\n" << credentials->to_json() <<
'\n');
398 const std::time_t request_time = std::time(0);
400 const std::string auth_header =
401 AWSV4::compute_awsv4_signature(
404 credentials->
get(AccessCredentials::ID_KEY),
405 credentials->
get(AccessCredentials::KEY_KEY),
406 credentials->
get(AccessCredentials::REGION_KEY),
410 handle->d_request_headers = curl::append_http_header((curl_slist *)
nullptr,
"Authorization", auth_header);
411 handle->d_request_headers = curl::append_http_header(handle->d_request_headers,
"x-amz-content-sha256",
412 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
413 handle->d_request_headers = curl::append_http_header(handle->d_request_headers,
"x-amz-date",
414 AWSV4::ISO8601_date(request_time));
416 res = curl_easy_setopt(handle->d_handle, CURLOPT_HTTPHEADER, handle->d_request_headers);
417 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_HTTPHEADER", handle->d_errbuf.data(), __FILE__, __LINE__);
420#if POC_DMRpp_OWNERSHIP
423 else if (credentials) {
424 INFO_LOG(prolog <<
"Looking for EDL Token for URL: " << handle->d_url->str() <<
'\n');
425 string edl_token = credentials->
get(
"edl_token");
426 if (!edl_token.empty()) {
427 INFO_LOG(prolog <<
"Using EDL Token for URL: " << handle->d_url->str() <<
'\n');
428 handle->d_request_headers = curl::append_http_header(handle->d_request_headers,
"Authorization", edl_token);
429 res = curl_easy_setopt(handle->d_handle, CURLOPT_HTTPHEADER, handle->d_request_headers);
430 curl::eval_curl_easy_setopt_result(res, prolog,
"CURLOPT_HTTPHEADER", handle->d_errbuf.data(), __FILE__, __LINE__);
436 return handle.release();