ba9e2a8
From 10520de65d10d2ab34329e24144aa922a430b229 Mon Sep 17 00:00:00 2001
ba9e2a8
From: Dalibor Pospisil <dapospis@redhat.com>
ba9e2a8
Date: Wed, 22 Aug 2018 13:09:47 +0200
ba9e2a8
Subject: [PATCH 4/4] use wget or curl for web download
ba9e2a8
ba9e2a8
Now there's a fallback to curl if wget is not available.
ba9e2a8
Wget has still a preference as it has got better progress printing while
ba9e2a8
the output is redirected to a file.
ba9e2a8
---
ba9e2a8
 src/rpms.sh | 32 +++++++++++++++++++++++++++-----
ba9e2a8
 1 file changed, 27 insertions(+), 5 deletions(-)
ba9e2a8
ba9e2a8
diff --git a/src/rpms.sh b/src/rpms.sh
ba9e2a8
index 878abba..e51dd4f 100644
ba9e2a8
--- a/src/rpms.sh
ba9e2a8
+++ b/src/rpms.sh
ba9e2a8
@@ -630,7 +630,27 @@ __INTERNAL_rpmInitUrl() {
ba9e2a8
 }
ba9e2a8
ba9e2a8
ba9e2a8
-__INTERNAL_WGET="wget -t 3 -T 180 -w 20 --waitretry=30 --no-check-certificate --progress=dot:giga"
ba9e2a8
+__INTERNAL_WGET() {
ba9e2a8
+  local QUIET
ba9e2a8
+  [[ "$1" == "--quiet" ]] && { QUIET=1; shift; }
ba9e2a8
+  local URL="$2"
ba9e2a8
+  local FILE="$1"
ba9e2a8
+  local res=0
ba9e2a8
+  if which wget &> /dev/null; then
ba9e2a8
+    rlLogDebug "$FUNCNAME(): using wget for download"
ba9e2a8
+    QUIET="${QUIET:+--quiet}"
ba9e2a8
+    wget $QUIET -t 3 -T 180 -w 20 --waitretry=30 --no-check-certificate --progress=dot:giga -O $FILE $URL || let res++
ba9e2a8
+  elif which curl &> /dev/null; then
ba9e2a8
+    rlLogDebug "$FUNCNAME(): using curl for download"
ba9e2a8
+    QUIET="${QUIET:+--silent}"
ba9e2a8
+    [[ -t 2 ]] || QUIET="${QUIET:---silent --show-error}"
ba9e2a8
+    curl $QUIET --location --retry-connrefused --retry-delay 3 --retry-max-time 3600 --retry 3 --connect-timeout 180 --max-time 1800 --insecure -o $FILE "$URL" || let res++
ba9e2a8
+  else
ba9e2a8
+    rlLogError "$FUNCNAME(): no tool for downloading web content is available"
ba9e2a8
+    let res++
ba9e2a8
+  fi
ba9e2a8
+  return $res
ba9e2a8
+}
ba9e2a8
ba9e2a8
 # __INTERNAL_rpmGetNextUrl N V R A | --source N V R
ba9e2a8
 __INTERNAL_rpmGetNextUrl() {
ba9e2a8
@@ -673,9 +695,9 @@ __INTERNAL_rpmGetNextUrl() {
ba9e2a8
           rlLogDebug "$FUNCNAME(): get rpm info"
ba9e2a8
           local rpm_info
ba9e2a8
           if [[ -n "$source" ]]; then
ba9e2a8
-            rpm_info=$($__INTERNAL_WGET -O - "$base_url/search?match=exact&type=rpm&terms=$N-$V-$R.src.rpm")
ba9e2a8
+            rpm_info=$(__INTERNAL_WGET - "$base_url/search?match=exact&type=rpm&terms=$N-$V-$R.src.rpm")
ba9e2a8
           else
ba9e2a8
-            rpm_info=$($__INTERNAL_WGET -O - "$base_url/search?match=exact&type=rpm&terms=$N-$V-$R.$A.rpm")
ba9e2a8
+            rpm_info=$(__INTERNAL_WGET - "$base_url/search?match=exact&type=rpm&terms=$N-$V-$R.$A.rpm")
ba9e2a8
           fi
ba9e2a8
           [[ $? -ne 0 || -z "$rpm_info" ]] && {
ba9e2a8
             rlLogError "could not download rpm information"
ba9e2a8
@@ -692,7 +714,7 @@ __INTERNAL_rpmGetNextUrl() {
ba9e2a8
           rlLogDebug "$FUNCNAME(): extracted buildurl='$buildurl'"
ba9e2a8
           [[ "$buildurl" =~ http ]] || buildurl="$base_url/$buildurl"
ba9e2a8
           rlLogDebug "$FUNCNAME(): using buildurl='$buildurl'"
ba9e2a8
-          local buildinfo=$($__INTERNAL_WGET -O - "$buildurl")
ba9e2a8
+          local buildinfo=$(__INTERNAL_WGET - "$buildurl")
ba9e2a8
           [[ $? -ne 0 || -z "$buildinfo" ]] && {
ba9e2a8
             rlLogError "could not download build information"
ba9e2a8
             let res++
ba9e2a8
@@ -752,7 +774,7 @@ __INTERNAL_rpmDirectDownload() {
ba9e2a8
         url="$__INTERNAL_RETURN_VALUE"; unset __INTERNAL_RETURN_VALUE
ba9e2a8
         local pkg=$(basename "$url")
ba9e2a8
         rlLog "trying download from '$url'"
ba9e2a8
-        if $__INTERNAL_WGET $quiet -O $pkg "$url"; then
ba9e2a8
+        if __INTERNAL_WGET $quiet $pkg "$url"; then
ba9e2a8
             rlLogDebug "$FUNCNAME(): package '$pkg' was successfully downloaded"
ba9e2a8
             echo "$pkg"
ba9e2a8
             return 0
ba9e2a8
--
ba9e2a8
2.17.1