summaryrefslogtreecommitdiff
path: root/tests/bin/wget
blob: ad5f2350f10d2db7df1bfa404cbef27e26437e09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh

prog=${0##*/}
usage() {
	cat <<EOF
usage: wget [-cqS] [--spider] [-O FILE] [-o LOGFILE] [--header STR]
	[--post-data STR | --post-file FILE] [-Y on/off]
	[-P DIR] [-U AGENT] [-T SEC] URL...

Retrieve files via HTTP or FTP

	--spider	Only check URL existence: \$? is 0 if exists
	--header STR	Add STR (of form 'header: value') to headers
	--post-data STR	Send STR using POST method
	--post-file FILE	Send FILE using POST method
	-c		Continue retrieval of aborted transfer
	-q		Quiet
	-P DIR		Save to DIR (default .)
	-S    		Show server response
	-T SEC		Network read timeout is SEC seconds
	-O FILE		Save to FILE ('-' for stdout)
	-o LOGFILE	Log messages to FILE
	-U STR		Use STR for User-Agent header
	-Y on/off	Use proxy
EOF
	exit $1
}

msg() {
	if ! [ -n "$quiet" ]; then
		# busybox wget sends to stderr
		echo "$@" >&2
	fi
}

OPTS=$(getopt -l quiet,help,spider,timeout,tries -o "qhO:T:t:" -n $prog -- "$@") || usage "1" >&2

quiet=
eval set -- "$OPTS"
while true; do
	opt="$1"
	case "$opt" in
		-h|--help)
			usage 0
			;;
		-q|--quiet)
			quiet=1
			;;
		--spider)
			exit ${SPIDER_STATUS:-0}
			;;
		-O)
			shift
			outfile="$1"
			;;
		-T|--timeout) shift;;
		-t|--tries) shift;;
		--)
			shift
			break
			;;
		*) usage "1" >&2
			;;
	esac
	shift
done

if [ $# -eq 0 ]; then
	usage "1" >&2
fi

for url; do
	case "$url" in
		"") msg "bad address"; exit 1;;
		*fail*) msg "bad address"; exit 1;;
		*404*) msg "wget: server returned error: HTTP/1.1 404 Not Found"; exit 1;;
	esac
done

: ${outfile:=index.html}
case "$outfile" in
	-)	msg "writing to stdout"
		echo "$WGETCONTENT"
		;;
	*) 	msg "saving to '$outfile'"
		echo "$WGETCONTENT" > "$outfile"
		;;
esac