protocol_fetch() {
	local url file RETVAL i j options
	url="$1"
	file="$2"
	options=

	wget404() {
	# see README.wget404 in the debian-installer-utils udeb source for more info about this
		if [ "ftp" = "$proto" ] ; then
			local file_not_found_pattern='bad response to RETR: 550 \|^No such file '
		else
			local file_not_found_pattern='server returned error: HTTP\/[0-9.]\+ 404 \| ERROR 404: '
		fi

		local RETVAL=$( {
			echo 1
			wget "$@" 2>&1 >&3 && echo %OK%
			echo %EOF%
			} | ( sed -ne '1{h;d};/'"$file_not_found_pattern"'/{p;s/.*/4/;h;d};/^%OK%$/{s/.*/0/;h;d};$!p;$x;$w /proc/self/fd/4' >&2 ) 4>&1
		) 3>&1
		return $RETVAL
	}

	# use the proxy for wgets (should speed things up)
	if db_get mirror/$proto/proxy && [ -n "$RET" ]; then
		export ${proto}_proxy="$RET"
	fi

	if wget --version 2>/dev/null | grep -q 'GNU Wget'; then
		options=--no-verbose
		if [ "https" = "$proto" ] && \
		   db_get debian-installer/allow_unauthenticated_ssl && [ "$RET" = true ]; then
			options="$options --no-check-certificate"
		fi
	else
		if [ "https" = "$proto" ]; then
			echo "busybox wget does not support https" >&2
			return 1
		fi
		options=-q
	fi
	options="$options -U debian-installer"
	RETVAL=0
	for i in 1 2; do
		wget404 $options -O "$file" "$url" || RETVAL=$?
		[ $RETVAL = 1 ] || return $RETVAL

		if [ "$TRY_CONTINUE" ] && [ -s "$file" ]; then
			for j in 1 2 3; do
				wget404 -c $options "$url" -O "$file" || RETVAL=$?
				[ $RETVAL = 1 ] || return $RETVAL
			done
		fi
		[ "$TRY_REPEAT" ] || break
	done
	return $RETVAL
}
