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

View File

@@ -90,9 +90,14 @@ check_updates()
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"
disp E "Cannot download version file; unable to continue."
(( quiet != 1 )) && disp E "Cannot download version file; unable to continue."
return 5
}