xref: /freebsd/usr.sbin/bsdconfig/share/media/httpproxy.subr (revision 5e7aa7574445f1fc90140bc93ec75de83e850fbe)
17323adacSDevin Teskeif [ ! "$_MEDIA_HTTPPROXY_SUBR" ]; then _MEDIA_HTTPPROXY_SUBR=1
27323adacSDevin Teske#
37323adacSDevin Teske# Copyright (c) 2012-2013 Devin Teske
4f8ea072aSDevin Teske# All rights reserved.
57323adacSDevin Teske#
67323adacSDevin Teske# Redistribution and use in source and binary forms, with or without
77323adacSDevin Teske# modification, are permitted provided that the following conditions
87323adacSDevin Teske# are met:
97323adacSDevin Teske# 1. Redistributions of source code must retain the above copyright
107323adacSDevin Teske#    notice, this list of conditions and the following disclaimer.
117323adacSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
127323adacSDevin Teske#    notice, this list of conditions and the following disclaimer in the
137323adacSDevin Teske#    documentation and/or other materials provided with the distribution.
147323adacSDevin Teske#
157323adacSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
177323adacSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
187323adacSDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
197323adacSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
217323adacSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
227323adacSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
237323adacSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247323adacSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257323adacSDevin Teske# SUCH DAMAGE.
267323adacSDevin Teske#
277323adacSDevin Teske#
287323adacSDevin Teske############################################################ INCLUDES
297323adacSDevin Teske
307323adacSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
317323adacSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
327323adacSDevin Teskef_dprintf "%s: loading includes..." media/httpproxy.subr
337323adacSDevin Teskef_include $BSDCFG_SHARE/dialog.subr
347323adacSDevin Teskef_include $BSDCFG_SHARE/media/tcpip.subr
351de60ff0SDevin Teskef_include $BSDCFG_SHARE/variable.subr
367323adacSDevin Teske
377323adacSDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig"
387323adacSDevin Teskef_include_lang $BSDCFG_LIBE/include/messages.subr
397323adacSDevin Teske
407323adacSDevin Teske############################################################ FUNCTIONS
417323adacSDevin Teske
427323adacSDevin Teske# f_media_set_http_proxy
437323adacSDevin Teske#
447323adacSDevin Teske# Return success if we both found and set the media type to be an ftp server,
457323adacSDevin Teske# accessed via http proxy.
467323adacSDevin Teske#
477323adacSDevin Teske# Variables from variable.subr that can be used to script user input:
487323adacSDevin Teske#
497323adacSDevin Teske# 	VAR_HTTP_PROXY
507323adacSDevin Teske# 		HTTP Proxy server to use. Valid examples include:
517323adacSDevin Teske# 			myhost
527323adacSDevin Teske# 			somename:3128
537323adacSDevin Teske# 			192.168.2.3
547323adacSDevin Teske# 			[::1]:8080
557323adacSDevin Teske# 		The default port if not specified is 3128.
567323adacSDevin Teske#
577323adacSDevin Teske# Variables from variable.subr that are set after successful execution include
587323adacSDevin Teske# the following:
597323adacSDevin Teske#
607323adacSDevin Teske# 	VAR_HTTP_PROXY_HOST	The host portion of VAR_HTTP_PROXY.
617323adacSDevin Teske# 	VAR_HTTP_PROXY_PORT	The TCP port parsed from VAR_HTTP_PROXY.
627323adacSDevin Teske#
637323adacSDevin Teske# See also f_media_set_ftp() for additional variables.
647323adacSDevin Teske#
657323adacSDevin Teskef_media_set_http_proxy()
667323adacSDevin Teske{
677323adacSDevin Teske	FTP_SKIP_RESOLV=1 f_media_set_ftp || return $FAILURE
687323adacSDevin Teske
697323adacSDevin Teske	f_variable_get_value $VAR_HTTP_PROXY \
707323adacSDevin Teske		"$msg_please_enter_the_address_of_the_http_proxy"
717323adacSDevin Teske
727323adacSDevin Teske	local proxy
737323adacSDevin Teske	f_getvar $VAR_HTTP_PROXY proxy
747323adacSDevin Teske	[ "$proxy" ] || return $FAILURE
757323adacSDevin Teske
767323adacSDevin Teske	local hostname="$proxy" port=3128
777323adacSDevin Teske	case "$hostname" in
786a18711aSDevin Teske	#
796a18711aSDevin Teske	# The order in-which the below individual cases appear is important!
806a18711aSDevin Teske	#
816a18711aSDevin Teske	"["*"]":*) # IPv6 address with port
826a18711aSDevin Teske		f_dprintf "Looks like an IPv6 addr with port: %s" "$hostname"
837323adacSDevin Teske		hostname="${hostname#\[}"
847323adacSDevin Teske		port="${hostname#*\]:}"
857323adacSDevin Teske		port="${port%%[!0-9]*}"
867323adacSDevin Teske		hostname="${hostname%%\]:*}"
877323adacSDevin Teske		;;
886a18711aSDevin Teske	"["*"]") # IPv6 address
896a18711aSDevin Teske		f_dprintf "Looks like an IPv6 addr: %s" "$hostname"
906a18711aSDevin Teske		hostname="${hostname#\[}"
916a18711aSDevin Teske		hostname="${hostname%\]}"
926a18711aSDevin Teske		;;
936a18711aSDevin Teske	#
946a18711aSDevin Teske	# ^^^ IPv6 above / DNS Name or IPv4 below vvv
956a18711aSDevin Teske	#
966a18711aSDevin Teske	*:*) # DNS name or IPv4 address with port
976a18711aSDevin Teske		f_dprintf "Looks like a DNS name or IPv4 addr with port: %s" \
986a18711aSDevin Teske		          "$hostname"
997323adacSDevin Teske		port="${hostname#*:}"
1007323adacSDevin Teske		hostname="${hostname%%:*}"
1016a18711aSDevin Teske		;;
1026a18711aSDevin Teske	*) # DNS name or IPv4 address
1036a18711aSDevin Teske		f_dprintf "Looks like a DNS name or IPv4 addr: %s" "$hostname"
1046a18711aSDevin Teske		: leave hostname as-is
1057323adacSDevin Teske	esac
1067323adacSDevin Teske
1077323adacSDevin Teske	setvar $VAR_HTTP_PROXY_HOST "$hostname"
1087323adacSDevin Teske	setvar $VAR_HTTP_PROXY_PORT "$port"
1097323adacSDevin Teske
1107323adacSDevin Teske	if f_debugging; then
1117323adacSDevin Teske		f_dprintf "VAR_FTP_PATH : %s" "$( f_getvar $VAR_FTP_PATH )"
1127323adacSDevin Teske		f_dprintf "VAR_HTTP_PROXY_HOST, _PORT: %s:%s" \
1137323adacSDevin Teske		          "$( f_getvar $VAR_HTTP_PROXY_HOST )" \
1147323adacSDevin Teske		          "$( f_getvar $VAR_HTTP_PROXY_PORT )"
1157323adacSDevin Teske	fi
1167323adacSDevin Teske
1177323adacSDevin Teske	# media device has been set by f_media_set_ftp(), overwrite partly:
1187323adacSDevin Teske	device_media set   type     $DEVICE_TYPE_HTTP_PROXY
1197323adacSDevin Teske	device_media set   init     f_media_init_http_proxy
1207323adacSDevin Teske	device_media set   get      f_media_get_http_proxy
1217323adacSDevin Teske	device_media unset shutdown
1227323adacSDevin Teske
1237323adacSDevin Teske	return $SUCCESS
1247323adacSDevin Teske}
1257323adacSDevin Teske
1267323adacSDevin Teske# f_http_proxy_check_access [$connect_only]
1277323adacSDevin Teske#
1287323adacSDevin Teske# Return success if able list a remote FTP directory via HTTP proxy. If
1297323adacSDevin Teske# $connect_only is present and non-null, then returns success if a connection
1307323adacSDevin Teske# can be made. Variables from variable.subr that can be used to script user
1317323adacSDevin Teske# input:
1327323adacSDevin Teske#
1337323adacSDevin Teske# 	VAR_HTTP_PROXY_HOST
1347323adacSDevin Teske# 		The HTTP proxy server host name, IPv4 address or IPv6 address.
1357323adacSDevin Teske# 		Valid examples include:
1367323adacSDevin Teske# 			myhost
1377323adacSDevin Teske# 			192.168.2.3
1387323adacSDevin Teske# 			::1
1397323adacSDevin Teske# 	VAR_HTTP_PROXY_PORT
1407323adacSDevin Teske# 		The TCP port to connect to when communicating with the HTTP
1417323adacSDevin Teske# 		proxy server.
1427323adacSDevin Teske# 	VAR_HTTP_PROXY_PATH
1437323adacSDevin Teske# 		The FTP URL sent to the HTTP proxy server. Unused if
1447323adacSDevin Teske# 		$connect_only is present and non-NULL.
1457323adacSDevin Teske#
1467323adacSDevin Teskef_http_proxy_check_access()
1477323adacSDevin Teske{
14866badff9SDevin Teske	local connect_only="$1" hosts=
1497323adacSDevin Teske
1507323adacSDevin Teske	local proxy_host proxy_port
1517323adacSDevin Teske	f_getvar $VAR_HTTP_PROXY_HOST proxy_host
1527323adacSDevin Teske	f_getvar $VAR_HTTP_PROXY_PORT proxy_port
1537323adacSDevin Teske
1547323adacSDevin Teske	if ! {
1557323adacSDevin Teske		f_validate_ipaddr "$proxy_host" ||
1567323adacSDevin Teske		f_validate_ipaddr6 "$proxy_host" ||
1577323adacSDevin Teske		{
1587323adacSDevin Teske		  f_dprintf "%s: Looking up hostname, %s, using host(1)" \
1597323adacSDevin Teske		            "f_http_proxy_check_access" "$proxy_host"
1607323adacSDevin Teske		  f_host_lookup "$proxy_host" hosts
1617323adacSDevin Teske		}
1627323adacSDevin Teske	}; then
1637323adacSDevin Teske		# All the above validations failed
1647323adacSDevin Teske		[ "$hosts" ] && f_dialog_msgbox "$hosts"
1657323adacSDevin Teske		unset $VAR_HTTP_PROXY_HOST
1667323adacSDevin Teske		return $FAILURE
1677323adacSDevin Teske	elif [ ! "$hosts" ]; then
1687323adacSDevin Teske		# One of the first two validations passed
1697323adacSDevin Teske		hosts="$proxy_host"
1707323adacSDevin Teske	fi
1717323adacSDevin Teske
1727323adacSDevin Teske	local host connected=
1737323adacSDevin Teske	for host in $hosts; do
1747323adacSDevin Teske		f_quietly nc -nz "$host" "$proxy_port" || continue
1757323adacSDevin Teske		connected=1; break
1767323adacSDevin Teske	done
1777323adacSDevin Teske	if [ ! "$connected" ]; then
1787323adacSDevin Teske		f_show_msg "$msg_couldnt_connect_to_proxy %s:%s" \
1797323adacSDevin Teske		           "$proxy_host" "$proxy_port"
1807323adacSDevin Teske		unset $VAR_HTTP_PROXY_HOST
1817323adacSDevin Teske		return $FAILURE
1827323adacSDevin Teske	fi
1837323adacSDevin Teske	[ "$connect_only" ] && return $SUCCESS
1847323adacSDevin Teske
1857323adacSDevin Teske	#
1867323adacSDevin Teske	# Some proxies fetch files with certain extensions in "ascii mode"
1877323adacSDevin Teske	# instead of "binary mode" for FTP. The FTP server then translates all
1887323adacSDevin Teske	# LF to CRLF.
1897323adacSDevin Teske	#
1907323adacSDevin Teske	# You can force Squid to use binary mode by appending ";type=i" to the
1917323adacSDevin Teske	# URL, which is what sysinstall(8) has traditionally done.
1927323adacSDevin Teske	#
1937323adacSDevin Teske
1947323adacSDevin Teske	local proxy_path
1957323adacSDevin Teske	f_getvar $VAR_HTTP_PROXY_PATH proxy_path
1967323adacSDevin Teske	f_show_info "$msg_checking_access_to" "$proxy_path"
1977323adacSDevin Teske
1987323adacSDevin Teske	local rx
1997323adacSDevin Teske	if ! rx=$(
2007323adacSDevin Teske		printf "GET %s/ HTTP/1.0\r\n\r\n" "${proxy_path%/}" |
2017323adacSDevin Teske			nc -n "$host" "$proxy_port"
2027323adacSDevin Teske	); then
2037323adacSDevin Teske		f_show_msg "$msg_couldnt_connect_to_proxy %s:%s" \
2047323adacSDevin Teske		           "$proxy_host" "$proxy_port"
2057323adacSDevin Teske		unset $VAR_HTTP_PROXY_HOST
2067323adacSDevin Teske		return $FAILURE
2077323adacSDevin Teske	fi
2087323adacSDevin Teske
2097323adacSDevin Teske	local hdr
2107323adacSDevin Teske	hdr=$( echo "$rx" | awk '/^\r$/{exit}{print}' )
2117323adacSDevin Teske
2127323adacSDevin Teske	local http_found=$FAILURE
2137323adacSDevin Teske	if echo "$hdr" | awk '
2147323adacSDevin Teske		BEGIN { found = 0 }
2157323adacSDevin Teske		/^HTTP.... 200 / {
2167323adacSDevin Teske			found = 1
2177323adacSDevin Teske			exit
2187323adacSDevin Teske		}
2197323adacSDevin Teske		END { exit ! found }
2207323adacSDevin Teske	'; then
2217323adacSDevin Teske		http_found=$SUCCESS
2227323adacSDevin Teske	fi
2237323adacSDevin Teske
2247323adacSDevin Teske	#
2257323adacSDevin Teske	# Scan the headers of the response
2267323adacSDevin Teske	# this is extremely quick'n dity
2277323adacSDevin Teske	#
2287323adacSDevin Teske
2297323adacSDevin Teske	unset $VAR_HTTP_FTP_MODE
2307323adacSDevin Teske	if echo "$hdr" | awk '
2317323adacSDevin Teske		BEGIN { found = 0 }
2327323adacSDevin Teske		{
2337323adacSDevin Teske			if (!match($0, /^Server: /)) next
2347323adacSDevin Teske			found = ( substr($0, 9, 5) ~ /[Ss]quid/ )
2357323adacSDevin Teske		}
2367323adacSDevin Teske		END { exit ! found }
2377323adacSDevin Teske	'; then
2387323adacSDevin Teske		setvar $VAR_HTTP_FTP_MODE ";type=i"
2397323adacSDevin Teske	else
2407323adacSDevin Teske		setvar $VAR_HTTP_FTP_MODE ""
2417323adacSDevin Teske	fi
2427323adacSDevin Teske
2437323adacSDevin Teske	return $http_found
2447323adacSDevin Teske}
2457323adacSDevin Teske
2467323adacSDevin Teske# f_media_init_http_proxy $device
2477323adacSDevin Teske#
2487323adacSDevin Teske# Initializes the HTTP Proxy media device. Returns success if able to confirm
2497323adacSDevin Teske# the existence of at least one known FTP server release path via HTTP proxy
2507323adacSDevin Teske# using f_http_proxy_check_access(), above.
2517323adacSDevin Teske#
2527323adacSDevin Teske# Variables from variable.subr that can be used to script user input:
2537323adacSDevin Teske#
2547323adacSDevin Teske# 	VAR_HTTP_PROXY_HOST
2557323adacSDevin Teske#		The HTTP proxy server to connect to. Usually set by having
2567323adacSDevin Teske# 		f_media_set_http_proxy() parse VAR_HTTP_PROXY. Must be set.
2577323adacSDevin Teske# 		Also see f_http_proxy_check_access() for additional variables.
2587323adacSDevin Teske# 	VAR_RELNAME
2597323adacSDevin Teske# 		Usually set to `uname -r' but can be overridden.
2607323adacSDevin Teske# 	VAR_FTP_PATH
2617323adacSDevin Teske# 		The FTP URL to send to the HTTP proxy server. Usually set by
2627323adacSDevin Teske# 		calling f_media_set_ftp().
2637323adacSDevin Teske#
2647323adacSDevin Teske# Meanwhile, after successful execution, the following variables (also from
2657323adacSDevin Teske# variable.subr) are set:
2667323adacSDevin Teske#
2677323adacSDevin Teske# 	VAR_HTTP_PROXY_PATH
2687323adacSDevin Teske# 		The [possibly] adjusted VAR_FTP_PATH that was found to contain
2697323adacSDevin Teske# 		a valid FreeBSD repository.
2707323adacSDevin Teske#
2717323adacSDevin Teskef_media_init_http_proxy()
2727323adacSDevin Teske{
2737323adacSDevin Teske	local dev="$1"
2747323adacSDevin Teske	f_dprintf "Init routine called for HTTP Proxy device. dev=[%s]" "$dev"
2757323adacSDevin Teske
2767323adacSDevin Teske	#
2777323adacSDevin Teske	# First verify access
2787323adacSDevin Teske	#
2797323adacSDevin Teske	local connect_only=1
2807323adacSDevin Teske	f_http_proxy_check_access $connect_only
2817323adacSDevin Teske
2827323adacSDevin Teske	local proxy_host
2837323adacSDevin Teske	f_getvar $VAR_HTTP_PROXY_HOST proxy_host
2847323adacSDevin Teske	while [ ! "$proxy_host" ]; do
2857323adacSDevin Teske		f_media_set_http_proxy || return $FAILURE
2867323adacSDevin Teske		f_http_proxy_check_access $connect_only
2877323adacSDevin Teske		f_getvar $VAR_HTTP_PROXY_HOST proxy_host
2887323adacSDevin Teske	done
2897323adacSDevin Teske
2907323adacSDevin Teske	local rel proxy_path http_found=$FAILURE
2917323adacSDevin Teske	while :; do
2927323adacSDevin Teske		#
2937323adacSDevin Teske		# If the release is specified as "__RELEASE" or "any", then
2947323adacSDevin Teske		# just assume that the path the user gave is ok.
2957323adacSDevin Teske		#
2967323adacSDevin Teske		f_getvar $VAR_RELNAME rel
2977323adacSDevin Teske		f_dprintf "f_media_init_http_proxy: rel=[%s]" "$rel"
2987323adacSDevin Teske
2997323adacSDevin Teske		case "$rel" in
3007323adacSDevin Teske		__RELEASE|any)
3017323adacSDevin Teske			f_getvar $VAR_FTP_PATH $VAR_HTTP_PROXY_PATH
3027323adacSDevin Teske			f_http_proxy_check_access
3037323adacSDevin Teske			http_found=$?
3047323adacSDevin Teske			;;
3057323adacSDevin Teske		*)
3067323adacSDevin Teske			local fdir fp
3072c2a50fdSDevin Teske			f_getvar $VAR_FTP_PATH%/ fp
3087323adacSDevin Teske			for fdir in $FTP_DIRS; do
3097323adacSDevin Teske				setvar $VAR_HTTP_PROXY_PATH "$fp/$fdir/$rel"
3107323adacSDevin Teske				if f_http_proxy_check_access; then
3117323adacSDevin Teske					http_found=$SUCCESS
3127323adacSDevin Teske					break
3137323adacSDevin Teske				fi
3147323adacSDevin Teske			done
3157323adacSDevin Teske		esac
3167323adacSDevin Teske
3177323adacSDevin Teske		[ $http_found -eq $SUCCESS ] && break
3187323adacSDevin Teske
3197323adacSDevin Teske		f_getvar $VAR_HTTP_PROXY_PATH proxy_path
3207323adacSDevin Teske		f_show_msg "$msg_please_check_the_url_and_try_again" \
3217323adacSDevin Teske		           "$proxy_path"
3227323adacSDevin Teske
3237323adacSDevin Teske		unset $VAR_HTTP_PROXY_PATH
3247323adacSDevin Teske		f_media_set_http_proxy || break
3257323adacSDevin Teske	done
3267323adacSDevin Teske
3277323adacSDevin Teske	return $http_found
3287323adacSDevin Teske}
3297323adacSDevin Teske
330dde7be41SDevin Teske# f_media_get_http_proxy $device $file [$probe_type]
3317323adacSDevin Teske#
3327323adacSDevin Teske# Returns data from $file on an FTP server via HTTP proxy using nc(1). Please
3337323adacSDevin Teske# note that $device is unused but must be present (even if null). Information
334dde7be41SDevin Teske# is instead gathered from the environment. If $probe_type is both present and
3355ffdcd4cSDevin Teske# non-NULL, this function exits after receiving the HTTP header response from
3365ffdcd4cSDevin Teske# the proxy server (if the HTTP response code is 200, success is returned;
337dde7be41SDevin Teske# otherwise failure). If $probe_type is equal to $PROBE_SIZE, prints the
338dde7be41SDevin Teske# content-length in bytes from the response (or -1 if not found) to standard-
339dde7be41SDevin Teske# out.
3407323adacSDevin Teske#
3417323adacSDevin Teske# The variables used to configure the connection are as follows (all of which
3427323adacSDevin Teske# are configured by f_media_set_http_proxy above):
3437323adacSDevin Teske#
3447323adacSDevin Teske# 	VAR_HTTP_PROXY_HOST
3457323adacSDevin Teske# 		HTTP proxy host to connect. Can be an IPv4 address, IPv6
3467323adacSDevin Teske# 		address, or DNS hostname of your choice.
3477323adacSDevin Teske# 	VAR_HTTP_PROXY_PORT
3487323adacSDevin Teske# 		TCP port to connect on; see f_media_set_http_proxy above.
3497323adacSDevin Teske# 	VAR_HTTP_PROXY_PATH
3507323adacSDevin Teske# 		URL (including "ftp://" protocol-prefix) of FTP directory to
3517323adacSDevin Teske# 		use as a prefix when requesting $file via HTTP proxy.
3527323adacSDevin Teske#
3537323adacSDevin Teske# See variable.subr for additional information.
3547323adacSDevin Teske#
3557323adacSDevin Teske# Example usage:
3567323adacSDevin Teske# 	f_media_set_http_proxy
3577323adacSDevin Teske# 	f_media_get_http_proxy media $file
3587323adacSDevin Teske#
3597323adacSDevin Teskef_media_get_http_proxy()
3607323adacSDevin Teske{
361dde7be41SDevin Teske	local dev="$1" file="$2" probe_type="$3" hosts=
3627323adacSDevin Teske
363dde7be41SDevin Teske	f_dprintf "f_media_get_http_proxy: dev=[%s] file=[%s] probe_type=%s" \
364dde7be41SDevin Teske	          "$dev" "$file" "$probe_type"
3657323adacSDevin Teske
3667323adacSDevin Teske	local proxy_host proxy_port
3677323adacSDevin Teske	f_getvar $VAR_HTTP_PROXY_HOST proxy_host
3687323adacSDevin Teske	f_getvar $VAR_HTTP_PROXY_PORT proxy_port
3697323adacSDevin Teske
3707323adacSDevin Teske	if ! {
3717323adacSDevin Teske		f_validate_ipaddr "$proxy_host" ||
3727323adacSDevin Teske		f_validate_ipaddr6 "$proxy_host" ||
3737323adacSDevin Teske		{
3747323adacSDevin Teske		  f_dprintf "%s: Looking up hostname, %s, using host(1)" \
3757323adacSDevin Teske		            "f_media_get_http_proxy" "$proxy_host"
3767323adacSDevin Teske		  f_host_lookup "$proxy_host" hosts
3777323adacSDevin Teske		}
3787323adacSDevin Teske	}; then
3797323adacSDevin Teske		# All the above validations failed
3807323adacSDevin Teske		[ "$hosts" ] && f_dialog_msgbox "$hosts"
3817323adacSDevin Teske		return $FAILURE
3827323adacSDevin Teske	elif [ ! "$hosts" ]; then
3837323adacSDevin Teske		# One of the first two validations passed
3847323adacSDevin Teske		hosts="$proxy_host"
3857323adacSDevin Teske	fi
3867323adacSDevin Teske
3877323adacSDevin Teske	local host connected=
3887323adacSDevin Teske	for host in $hosts; do
3897323adacSDevin Teske		f_quietly nc -nz "$host" "$proxy_port" || continue
3907323adacSDevin Teske		connected=1; break
3917323adacSDevin Teske	done
3927323adacSDevin Teske	if [ ! "$connected" ]; then
3937323adacSDevin Teske		f_show_msg "$msg_couldnt_connect_to_proxy %s:%s" \
3947323adacSDevin Teske		           "$proxy_host" "$proxy_port"
3957323adacSDevin Teske		return $FAILURE
3967323adacSDevin Teske	fi
3977323adacSDevin Teske
3987323adacSDevin Teske	local proxy_path mode
3992c2a50fdSDevin Teske	f_getvar $VAR_HTTP_PROXY_PATH%/ proxy_path
4007323adacSDevin Teske	f_getvar $VAR_HTTP_FTP_MODE mode
4012c2a50fdSDevin Teske	local url="$proxy_path/$file$mode" rx
4027323adacSDevin Teske
4037323adacSDevin Teske	f_dprintf "sending http request for: %s" "$url"
4047323adacSDevin Teske	printf "GET %s HTTP/1.0\r\n\r\n" "$url" | nc -n "$host" "$proxy_port" |
4057323adacSDevin Teske	(
4067323adacSDevin Teske		#
4077323adacSDevin Teske		# scan the headers of the response
4087323adacSDevin Teske		# this is extremely quick'n dirty
4097323adacSDevin Teske		#
4107323adacSDevin Teske
411dde7be41SDevin Teske		rv=0 length=-1
4127323adacSDevin Teske		while read LINE; do
4137323adacSDevin Teske			case "$LINE" in
4147323adacSDevin Teske			HTTP*)
4157323adacSDevin Teske				f_dprintf "received response: %s" "$LINE"
4167323adacSDevin Teske				set -- $LINE; rv=$2
4177323adacSDevin Teske				f_isinteger "$rv" || rv=0
4187323adacSDevin Teske				;;
419dde7be41SDevin Teske			"Content-Length: "*)
420dde7be41SDevin Teske				length="${LINE%
421dde7be41SDevin Teske}"
422dde7be41SDevin Teske				length="${length#Content-Length: }"
423dde7be41SDevin Teske				f_dprintf "received content-length: %s" \
424dde7be41SDevin Teske				          "$length"
4257323adacSDevin Teske				;;
4267323adacSDevin Teske			*)
4277323adacSDevin Teske				[ "${LINE%
4287323adacSDevin Teske}" ] || break # End of headers
4297323adacSDevin Teske			esac
4307323adacSDevin Teske		done
4317323adacSDevin Teske
4327323adacSDevin Teske		[ $rv -ge 500 ] && exit 5
4337323adacSDevin Teske		[ $rv -eq 404 ] && exit 44
4347323adacSDevin Teske		[ $rv -ge 400 ] && exit 4
4357323adacSDevin Teske		[ $rv -ge 300 ] && exit 3
436dde7be41SDevin Teske		[ $rv -eq 200 ] || exit $FAILURE
4377323adacSDevin Teske
438dde7be41SDevin Teske		if [ ! "$probe_type" ]; then
439dde7be41SDevin Teske			cat # output the rest ``as-is''
440dde7be41SDevin Teske		elif [ "$probe_type" = "$PROBE_SIZE" ]; then
4415ffdcd4cSDevin Teske			f_isinteger "$length" || length=-1
4427323adacSDevin Teske			echo "$length"
4437323adacSDevin Teske		fi
4447323adacSDevin Teske		exit 200
4457323adacSDevin Teske	)
446dde7be41SDevin Teske	local retval=$?
4477323adacSDevin Teske	[ $retval -eq 200 ] && return $SUCCESS
4487323adacSDevin Teske	[ "$probe_type" ] && return $FAILURE
4497323adacSDevin Teske
4507323adacSDevin Teske	case "$retval" in
4517323adacSDevin Teske	  5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;;
4527323adacSDevin Teske	 44) f_show_msg "$msg_url_was_not_found" "$url" ;;
453*95d45410SDevin Teske	  4) f_show_msg "$msg_client_error" ;;
4547323adacSDevin Teske	  *) f_show_msg "$msg_error_when_requesting_url" "$url" ;;
4557323adacSDevin Teske	esac 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
4567323adacSDevin Teske	return $FAILURE
4577323adacSDevin Teske}
4587323adacSDevin Teske
4597323adacSDevin Teske############################################################ MAIN
4607323adacSDevin Teske
4617323adacSDevin Teskef_dprintf "%s: Successfully loaded." media/httpproxy.subr
462
463fi # ! $_MEDIA_HTTPPROXY_SUBR
464