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

	wget404() {
	# see README.wget404 in the debian-installer-utils udeb source for more info about this
		local RETVAL=$( {
			echo 1
			wget "$@" 2>&1 >&3 && echo %OK%
			echo %EOF%
			} | ( sed -ne '1{h;d};/server returned error 404/{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; then
		export ${proto}_proxy="$RET"
	fi

	RETVAL=0
	for i in 1 2; do
		wget404 -q -O "$file" "$url" || RETVAL=$?
		[ $RETVAL = 1 ] || return $RETVAL

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