26#include <zypp/base/Logger.h>
28#include <zypp-curl/parser/MetaLinkParser>
29#include <zypp/ManagedFile.h>
31#include <zypp-curl/auth/CurlAuthData>
36#undef CURLVERSION_AT_LEAST
37#define CURLVERSION_AT_LEAST(M,N,O) LIBCURL_VERSION_NUM >= ((((M)<<8)+(N))<<8)+(O)
46class multifetchrequest;
64 void adddnsfd(fd_set &rset,
int &maxfd);
90 static size_t _writefunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream);
93 static size_t _headerfunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream);
106#define WORKER_STARTING 0
107#define WORKER_LOOKUP 1
108#define WORKER_FETCH 2
109#define WORKER_DISCARD 3
111#define WORKER_SLEEP 5
112#define WORKER_BROKEN 6
121 void run(std::vector<Url> &urllist);
179 if (gettimeofday(&tv, NULL))
181 return tv.tv_sec + tv.tv_usec / 1000000.;
205 (void)curl_easy_getinfo(
_curl, CURLINFO_EFFECTIVE_URL, &effurl);
206 if (effurl && !strncasecmp(effurl,
"http", 4))
209 (void)curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &statuscode);
210 if (statuscode != 206)
257 if (l > 9 && !strncasecmp(p,
"Location:", 9))
259 std::string line(p + 9, l - 9);
260 if (line[l - 10] ==
'\r')
261 line.erase(l - 10, 1);
262 XXX <<
"#" <<
_workerno <<
": redirecting to" << line << endl;
265 if (l <= 14 || l >= 128 || strncasecmp(p,
"Content-Range:", 14) != 0)
269 while (l && (*p ==
' ' || *p ==
'\t'))
271 if (l < 6 || strncasecmp(p,
"bytes", 5))
278 unsigned long long start, off, filesize;
279 if (sscanf(buf,
"%llu-%llu/%llu", &start, &off, &filesize) != 3)
283 WAR <<
"#" <<
_workerno <<
": setting request filesize to " << filesize << endl;
290 XXX <<
"#" <<
_workerno <<
": filesize mismatch" << endl;
292 strncpy(
_curlError,
"filesize mismatch", CURL_ERROR_SIZE);
329 XXX <<
"reused worker from pool" << endl;
333 strncpy(
_curlError,
"curl_easy_init failed", CURL_ERROR_SIZE);
342 curl_easy_cleanup(
_curl);
345 strncpy(
_curlError,
"curl_easy_setopt failed", CURL_ERROR_SIZE);
348 curl_easy_setopt(
_curl, CURLOPT_PRIVATE,
this);
351 curl_easy_setopt(
_curl, CURLOPT_WRITEDATA,
this);
355 curl_easy_setopt(
_curl, CURLOPT_HEADERDATA,
this);
369 if (use_auth.empty())
370 use_auth =
"digest,basic";
372 if( auth != CURLAUTH_NONE)
374 XXX <<
"#" <<
_workerno <<
": Enabling HTTP authentication methods: " << use_auth
375 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
376 curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, auth);
391#if CURLVERSION_AT_LEAST(7,15,5)
392 curl_easy_setopt(
_curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)0);
394 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
395 curl_easy_setopt(
_curl, CURLOPT_WRITEFUNCTION, (
void *)0);
396 curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (
void *)0);
397 curl_easy_setopt(
_curl, CURLOPT_HEADERFUNCTION, (
void *)0);
398 curl_easy_setopt(
_curl, CURLOPT_HEADERDATA, (
void *)0);
402 curl_easy_cleanup(
_curl);
409 while (waitpid(
_pid, &status, 0) == -1)
426 const char *s = getenv(name.c_str());
427 return s && *s ? true :
false;
443 if (inet_pton(AF_INET, host.c_str(), addrbuf) == 1)
445 if (inet_pton(AF_INET6, host.c_str(), addrbuf) == 1)
456 if (schemeproxy !=
"http_proxy")
458 std::transform(schemeproxy.begin(), schemeproxy.end(), schemeproxy.begin(), ::toupper);
463 XXX <<
"checking DNS lookup of " << host << endl;
468 strncpy(
_curlError,
"DNS pipe creation failed", CURL_ERROR_SIZE);
472 if (
_pid == pid_t(-1))
478 strncpy(
_curlError,
"DNS checker fork failed", CURL_ERROR_SIZE);
485 struct addrinfo *ai, aihints;
486 memset(&aihints, 0,
sizeof(aihints));
487 aihints.ai_family = PF_UNSPEC;
488 int tstsock = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
490 aihints.ai_family = PF_INET;
493 aihints.ai_socktype = SOCK_STREAM;
494 aihints.ai_flags = AI_CANONNAME;
497 alarm(connecttimeout);
498 signal(SIGALRM, SIG_DFL);
499 if (getaddrinfo(host.c_str(), NULL, &aihints, &ai))
525 while (waitpid(
_pid, &status, 0) == -1)
536 if (!WIFEXITED(status))
539 strncpy(
_curlError,
"DNS lookup failed", CURL_ERROR_SIZE);
543 int exitcode = WEXITSTATUS(status);
544 XXX <<
"#" <<
_workerno <<
": DNS lookup returned " << exitcode << endl;
548 strncpy(
_curlError,
"DNS lookup failed", CURL_ERROR_SIZE);
578 size_t cnt = l >
sizeof(buf) ?
sizeof(buf) : l;
593 XXX <<
"start stealing!" << endl;
597 std::list<multifetchworker *>::iterator workeriter =
_request->
_workers.begin();
604 if (worker->
_pass == -1)
663 XXX <<
"#" <<
_workerno <<
": going to sleep for " << sl * 1000 <<
" ms" << endl;
684 std::list<multifetchworker *>::iterator workeriter =
_request->
_workers.begin();
760 sprintf(rangebuf,
"%llu-", (
unsigned long long)
_blkstart);
768 strncpy(
_curlError,
"curl_easy_setopt range failed", CURL_ERROR_SIZE);
775 strncpy(
_curlError,
"curl_multi_add_handle failed", CURL_ERROR_SIZE);
826 for (
size_t blkno = 0; blkno < blklist->
numBlocks(); blkno++)
832 else if (filesize != off_t(-1))
838 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
851 std::vector<Url>::iterator urliter = urllist.begin();
854 fd_set rset, wset, xset;
859 XXX <<
"finished!" << endl;
883 WAR <<
"No more active workers!" << endl;
885 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
898 curl_multi_fdset(
_multi, &rset, &wset, &xset, &maxfd);
901 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
902 (*workeriter)->adddnsfd(rset, maxfd);
913 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
929 tv.tv_usec = sl * 1000000;
931 int r = select(maxfd + 1, &rset, &wset, &xset, &tv);
932 if (r == -1 && errno != EINTR)
935 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
951 mcode = curl_multi_perform(
_multi, &tasks);
952 if (mcode == CURLM_CALL_MULTI_PERFORM)
954 if (mcode != CURLM_OK)
975 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
984 XXX <<
"#" << worker->
_workerno <<
": sleep done, wake up" << endl;
993 while ((msg = curl_multi_info_read(
_multi, &nqueue)) != 0)
995 if (msg->msg != CURLMSG_DONE)
997 CURL *easy = msg->easy_handle;
998 CURLcode cc = msg->data.result;
1000 if (curl_easy_getinfo(easy, CURLINFO_PRIVATE, &worker) != CURLE_OK)
1010 curl_multi_remove_handle(
_multi, easy);
1011 if (cc == CURLE_HTTP_RETURNED_ERROR)
1013 long statuscode = 0;
1014 (void)curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &statuscode);
1015 XXX <<
"HTTP status " << statuscode << endl;
1016 if (statuscode == 416 && !
_blklist)
1022 XXX <<
"#" << worker->
_workerno <<
": retrying with no end range" << endl;
1042 WAR <<
"#" << worker->
_workerno <<
": checksum error, disable worker" << endl;
1044 strncpy(worker->
_curlError,
"checksum error", CURL_ERROR_SIZE);
1058 XXX <<
"#" << worker->
_workerno <<
": recheck checksum error, refetch block" << endl;
1071 int maxworkerno = 0;
1073 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
1088 double ratio = worker->
_avgspeed / maxavg;
1091 ratio = ratio * ratio;
1094 XXX <<
"#" << worker->
_workerno <<
": too slow ("<< ratio <<
", " << worker->
_avgspeed <<
", #" << maxworkerno <<
": " << maxavg <<
"), going to sleep for " << ratio * 1000 <<
" ms" << endl;
1115#if CURLVERSION_AT_LEAST(7,15,5)
1116 curl_easy_setopt(worker->
_curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)(avg));
1158 WAR <<
"overall result" << endl;
1159 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
1169 if ( filesize == 0 )
return 2 * 1024 * 1024;
1170 else if ( filesize < 2*256*1024 )
return filesize;
1171 else if ( filesize < 8*1024*1024 )
return 256*1024;
1172 else if ( filesize < 256*1024*1024 )
return 1024*1024;
1182 MIL <<
"MediaMultiCurl::MediaMultiCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
1196 curl_multi_cleanup(
_multi);
1199 std::map<std::string, CURL *>::iterator it;
1202 CURL *easy = it->second;
1205 curl_easy_cleanup(easy);
1221 for (; sl; sl = sl->next)
1230 while ((l = pread(fd, buf,
sizeof(buf) - 1, (off_t)0)) == -1 && errno == EINTR)
1236 while (*p ==
' ' || *p ==
'\t' || *p ==
'\r' || *p ==
'\n')
1238 if (!strncasecmp(p,
"<?xml", 5))
1240 while (*p && *p !=
'>')
1244 while (*p ==
' ' || *p ==
'\t' || *p ==
'\r' || *p ==
'\n')
1247 bool ret = !strncasecmp(p,
"<metalink", 9) ? true :
false;
1254 if ((fd = open(file.
asString().c_str(), O_RDONLY|O_CLOEXEC)) == -1)
1258 DBG <<
"looks_like_metalink(" << file <<
"): " << ret << endl;
1274 if ( curl_easy_getinfo(
_curl, CURLINFO_PRIVATE, &fp ) != CURLE_OK || !fp )
1276 if ( ftell( fp ) == 0 )
1281 long httpReturnCode = 0;
1282 if (curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0)
1286 bool ismetalink =
false;
1287 if (curl_easy_getinfo(
_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
1289 std::string ct = std::string(ptr);
1290 if (ct.find(
"application/metalink+xml") == 0 || ct.find(
"application/metalink4+xml") == 0)
1293 if (!ismetalink && dlnow < 256)
1302 DBG <<
"looks_like_metalink_fd: " << ismetalink << endl;
1319 if( assert_dir( dest.
dirname() ) )
1321 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1331 ERR <<
"out of memory for temp file name" << endl;
1335 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1338 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1343 file = ::fdopen( tmp_fd,
"we" );
1346 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1349 tmp_fd.resetDispose();
1352 DBG <<
"dest: " << dest << endl;
1353 DBG <<
"temp: " << destNew << endl;
1358 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1359 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1363 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1364 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1370 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (*file) );
1377 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1378 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1380 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
1383 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1384 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1386 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
1387 long httpReturnCode = 0;
1388 CURLcode infoRet = curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode);
1389 if (infoRet == CURLE_OK)
1392 if ( httpReturnCode == 304
1395 DBG <<
"not modified: " <<
PathInfo(dest) << endl;
1401 WAR <<
"Could not get the response code." << endl;
1404 bool ismetalink =
false;
1407 if (curl_easy_getinfo(
_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
1409 std::string ct = std::string(ptr);
1410 if (ct.find(
"application/metalink+xml") == 0 || ct.find(
"application/metalink4+xml") == 0)
1425 bool userabort =
false;
1439 XXX <<
"No filesize in metalink file and no expected filesize, aborting multicurl." << std::endl;
1443 std::vector<Url> urls = mlp.
getUrls();
1453 file = fopen((*destNew).c_str(),
"w+e");
1458 XXX <<
"reusing blocks from file " << target << endl;
1464 XXX <<
"reusing blocks from file " << failedFile << endl;
1472 XXX <<
"reusing blocks from file " << df << endl;
1482 userabort = ex.
errstr() ==
"User abort";
1493 if (
PathInfo(destNew).size() >= 63336)
1495 ::unlink(failedFile.
asString().c_str());
1502 file = fopen((*destNew).c_str(),
"w+e");
1514 ERR <<
"Failed to chmod file " << destNew << endl;
1521 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1525 if ( rename( destNew, dest ) != 0 )
1527 ERR <<
"Rename failed" << endl;
1530 destNew.resetDispose();
1538 if (blklist && filesize == off_t(-1) && blklist->
haveFilesize())
1540 if (blklist && !blklist->
haveBlocks() && filesize != 0)
1542 if (blklist && (filesize == 0 || !blklist->
numBlocks()))
1551 _multi = curl_multi_init();
1565 std::vector<Url> myurllist;
1566 for (std::vector<Url>::iterator urliter = urllist->begin(); urliter != urllist->end(); ++urliter)
1570 std::string scheme = urliter->getScheme();
1571 if (scheme ==
"http" || scheme ==
"https" || scheme ==
"ftp" || scheme ==
"tftp")
1581 if (!myurllist.size())
1582 myurllist.push_back(baseurl);
1591 if (fseeko(fp, off_t(0), SEEK_SET))
1597 while ((l = fread(buf, 1,
sizeof(buf), fp)) > 0)
1605 return _dnsok.find(host) ==
_dnsok.end() ? false :
true;
1627 curl_easy_cleanup(oldeasy);
std::optional< KeyManagerCtx > _context
void resetDispose()
Set no dispose function.
Store and operate with byte count.
static const Unit MB
1000^2 Byte
std::string asString(unsigned field_width_r=0, unsigned unit_width_r=1) const
Auto selected Unit and precision.
Compute Message Digests (MD5, SHA1 etc)
bool update(const char *bytes, size_t len)
feed data into digest computation algorithm
Base class for Exception.
std::string getScheme() const
Returns the scheme name of the URL.
std::string asString() const
Returns a default string representation of the Url object.
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
Pathname repoCachePath() const
Path where the caches are kept (/var/cache/zypp)
static ZConfig & instance()
Singleton ctor.
Wrapper class for stat/lstat.
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Pathname dirname() const
Return all but the last component od this path.
const std::string & asString() const
String representation.
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
zypp::Url propagateQueryParams(zypp::Url url_r, const zypp::Url &template_r)
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
int unlink(const Pathname &path)
Like 'unlink'.
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
std::string numstring(char n, int w=0)
Easy-to use interface to the ZYPP dependency resolver.
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
AutoDispose<int> calling ::close
AutoDispose<FILE*> calling ::fclose
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.