fix too long long on non functionnal networt (and improve dwl)

This commit is contained in:
fatalerrors
2026-05-07 11:52:53 +02:00
parent ddd7d4193a
commit e64a857a43
2 changed files with 60 additions and 24 deletions

View File

@@ -36,27 +36,52 @@
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Download a resource using curl, wget, or fetch. # Download a resource using curl, wget, or fetch.
# Usage: dwl <url> [output_file] # Usage: dwl [-t <seconds>] <url> [output_file]
dwl() dwl()
{ {
local timeout=""
# Parse leading options before the URL.
while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
--help|-h) --help|-h)
echo "Usage: dwl <url> [output_file]" echo "Usage: dwl [-t <seconds>|--timeout <seconds>] <url> [output_file]"
echo "Downloads a resource using curl, wget, or fetch." echo "Downloads a resource using curl, wget, or fetch."
echo "" echo ""
echo "Arguments:" echo "Arguments:"
echo " -t, --timeout Maximum time in seconds to wait for the transfer."
echo " url The full URL to download (http/https/ftp)." echo " url The full URL to download (http/https/ftp)."
echo " output_file (Optional) Path to save the file. If omitted, prints to stdout." echo " output_file (Optional) Path to save the file. If omitted, prints to stdout."
return 0 return 0
;; ;;
"") -t|--timeout)
echo "Error: URL argument is missing." >&2 [[ -z "${2:-}" || ! "${2:-}" =~ ^[0-9]+$ ]] && {
echo "Try 'get_resource --help' for usage." >&2 echo "Error: --timeout requires a positive integer argument." >&2
return 1
}
timeout="$2"
shift 2
;;
--)
shift
break
;;
-*)
echo "Error: Unknown option '$1'. Try 'dwl --help'." >&2
return 1 return 1
;; ;;
*)
break
;;
esac esac
done
case "$1" in case "${1:-}" in
"")
echo "Error: URL argument is missing." >&2
echo "Try 'dwl --help' for usage." >&2
return 1
;;
http://*|https://*|ftp://*) ;; http://*|https://*|ftp://*) ;;
*) *)
echo "Error: '$1' does not look like a valid URL. Must start with http://, https://, or ftp://" >&2 echo "Error: '$1' does not look like a valid URL. Must start with http://, https://, or ftp://" >&2
@@ -65,35 +90,41 @@ dwl()
esac esac
local url="$1" local url="$1"
local output="$2" local output="${2:-}"
# Honour preferred tool from configuration; fall back to auto-detection. # Honour preferred tool from configuration; fall back to auto-detection.
local preferred="${DWL_PREFERRED_TOOL:-}" local preferred="${DWL_PREFERRED_TOOL:-}"
_try_curl() _try_curl()
{ {
local args=(-sL)
[[ -n "$timeout" ]] && args+=(--max-time "$timeout" --connect-timeout "$timeout")
if [[ -z "$output" ]]; then if [[ -z "$output" ]]; then
curl -sL "$url" curl "${args[@]}" "$url"
else else
curl -sL -o "$output" "$url" curl "${args[@]}" -o "$output" "$url"
fi fi
} }
_try_wget() _try_wget()
{ {
local args=(-q)
[[ -n "$timeout" ]] && args+=(--timeout="$timeout")
if [[ -z "$output" ]]; then if [[ -z "$output" ]]; then
wget -qO- "$url" wget "${args[@]}" -O- "$url"
else else
wget -q -O "$output" "$url" wget "${args[@]}" -O "$output" "$url"
fi fi
} }
_try_fetch() _try_fetch()
{ {
local args=()
[[ -n "$timeout" ]] && args+=(-T "$timeout")
if [[ -z "$output" ]]; then if [[ -z "$output" ]]; then
fetch -o - "$url" fetch "${args[@]}" -o - "$url"
else else
fetch -o "$output" "$url" fetch "${args[@]}" -o "$output" "$url"
fi fi
} }

View File

@@ -90,9 +90,14 @@ check_updates()
return 4 return 4
} }
dwl "$UPDT_URL/version" "$vfile" >/dev/null 2>&1 || { # In quiet mode (startup), use a short timeout so a missing or slow network
# never blocks the interactive prompt.
local dwl_opts=()
(( quiet == 1 )) && dwl_opts+=(-t 3)
dwl "${dwl_opts[@]}" "$UPDT_URL/version" "$vfile" >/dev/null 2>&1 || {
rm -f "$vfile" rm -f "$vfile"
disp E "Cannot download version file; unable to continue." (( quiet != 1 )) && disp E "Cannot download version file; unable to continue."
return 5 return 5
} }