1if [ ! "$_MEDIA_HTTP_SUBR" ]; then _MEDIA_HTTP_SUBR=1
2#
3# Copyright (c) 2012-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
--- 10 unchanged lines hidden (view full) ---
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/share/media/http.subr 253333 2013-07-14 03:08:52Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/http.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/dialog.subr
--- 469 unchanged lines hidden (view full) ---
505
506 unset $VAR_HTTP_PATH
507 f_media_set_http || break
508 done
509
510 return $http_found
511}
512
513# f_media_get_http $device $file [$probe_type]
514#
515# Returns data from $file on an HTTP server using nc(1). Please note that
516# $device is unused but must be present (even if null). Information is instead
517# gathered from the environment. If $probe_type is both present and non-NULL,
518# this function exits after receiving the HTTP header response from the server
519# (if the HTTP response code is 200, success is returned; otherwise failure).
520# If $probe_type is equal to $PROBE_SIZE, prints the content-length in bytes
521# from the response (or -1 if not found) to standard-out.
522#
523# The variables used to configure the connection are as follows (all of which
524# are configured by f_media_set_http above):
525#
526# VAR_HTTP_HOST
527# HTTP server which to connect. Can be an IPv4 address, IPv6
528# address, or DNS hostname of your choice.
529# VAR_HTTP_PORT
--- 9 unchanged lines hidden (view full) ---
539# See variable.subr for additional information.
540#
541# Example usage:
542# f_media_set_http
543# f_media_get_http media $file
544#
545f_media_get_http()
546{
547 local dev="$1" file="$2" probe_type="$3" hosts=
548
549 f_dprintf "f_media_get_http: dev=[%s] file=[%s] probe_type=%s" \
550 "$dev" "$file" "$probe_type"
551
552 local http_host http_port
553 f_getvar $VAR_HTTP_HOST http_host
554 f_getvar $VAR_HTTP_PORT http_port
555
556 if ! {
557 f_validate_ipaddr "$http_host" ||
558 f_validate_ipaddr6 "$http_host" ||
--- 29 unchanged lines hidden (view full) ---
588 f_dprintf "sending http request for: %s" "$url"
589 printf "GET %s HTTP/1.0\r\n\r\n" "$url" | nc -n "$host" "$http_port" |
590 (
591 #
592 # scan the headers of the response
593 # this is extremely quick'n dirty
594 #
595
596 rv=0 length=-1
597 while read LINE; do
598 case "$LINE" in
599 HTTP*)
600 f_dprintf "received response: %s" "$LINE"
601 set -- $LINE; rv=$2
602 f_isinteger "$rv" || rv=0
603 ;;
604 "Content-Length: "*)
605 length="${LINE%
606}"
607 length="${length#Content-Length: }"
608 f_dprintf "received content-length: %s" \
609 "$length"
610 ;;
611 *)
612 [ "${LINE%
613}" ] || break # End of headers
614 esac
615 done
616
617 [ $rv -ge 500 ] && exit 5
618 [ $rv -eq 404 ] && exit 44
619 [ $rv -ge 400 ] && exit 4
620 [ $rv -ge 300 ] && exit 3
621 [ $rv -eq 200 ] || exit $FAILURE
622
623 if [ ! "$probe_type" ]; then
624 cat # output the rest ``as-is''
625 elif [ "$probe_type" = "$PROBE_SIZE" ]; then
626 f_isinteger "$length" || length=-1
627 echo "$length"
628 fi
629 exit 200
630 )
631 local retval=$?
632 [ $retval -eq 200 ] && return $SUCCESS
633 [ "$probe_type" ] && return $FAILURE
634
635 case "$retval" in
636 5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;;
637 44) f_show_msg "$msg_url_was_not_found" "$url" ;;
638 4) f_show_msg "$msg_client_error" ;;
639 *) f_show_msg "$msg_error_when_requesting_url" "$url" ;;
640 esac
641 return $FAILURE
642}
643
644############################################################ MAIN
645
646f_dprintf "%s: Successfully loaded." media/http.subr
647
648fi # ! $_MEDIA_HTTP_SUBR
2#
3# Copyright (c) 2012-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
--- 10 unchanged lines hidden (view full) ---
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/share/media/http.subr 253333 2013-07-14 03:08:52Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/http.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/dialog.subr
--- 469 unchanged lines hidden (view full) ---
505
506 unset $VAR_HTTP_PATH
507 f_media_set_http || break
508 done
509
510 return $http_found
511}
512
513# f_media_get_http $device $file [$probe_type]
514#
515# Returns data from $file on an HTTP server using nc(1). Please note that
516# $device is unused but must be present (even if null). Information is instead
517# gathered from the environment. If $probe_type is both present and non-NULL,
518# this function exits after receiving the HTTP header response from the server
519# (if the HTTP response code is 200, success is returned; otherwise failure).
520# If $probe_type is equal to $PROBE_SIZE, prints the content-length in bytes
521# from the response (or -1 if not found) to standard-out.
522#
523# The variables used to configure the connection are as follows (all of which
524# are configured by f_media_set_http above):
525#
526# VAR_HTTP_HOST
527# HTTP server which to connect. Can be an IPv4 address, IPv6
528# address, or DNS hostname of your choice.
529# VAR_HTTP_PORT
--- 9 unchanged lines hidden (view full) ---
539# See variable.subr for additional information.
540#
541# Example usage:
542# f_media_set_http
543# f_media_get_http media $file
544#
545f_media_get_http()
546{
547 local dev="$1" file="$2" probe_type="$3" hosts=
548
549 f_dprintf "f_media_get_http: dev=[%s] file=[%s] probe_type=%s" \
550 "$dev" "$file" "$probe_type"
551
552 local http_host http_port
553 f_getvar $VAR_HTTP_HOST http_host
554 f_getvar $VAR_HTTP_PORT http_port
555
556 if ! {
557 f_validate_ipaddr "$http_host" ||
558 f_validate_ipaddr6 "$http_host" ||
--- 29 unchanged lines hidden (view full) ---
588 f_dprintf "sending http request for: %s" "$url"
589 printf "GET %s HTTP/1.0\r\n\r\n" "$url" | nc -n "$host" "$http_port" |
590 (
591 #
592 # scan the headers of the response
593 # this is extremely quick'n dirty
594 #
595
596 rv=0 length=-1
597 while read LINE; do
598 case "$LINE" in
599 HTTP*)
600 f_dprintf "received response: %s" "$LINE"
601 set -- $LINE; rv=$2
602 f_isinteger "$rv" || rv=0
603 ;;
604 "Content-Length: "*)
605 length="${LINE%
606}"
607 length="${length#Content-Length: }"
608 f_dprintf "received content-length: %s" \
609 "$length"
610 ;;
611 *)
612 [ "${LINE%
613}" ] || break # End of headers
614 esac
615 done
616
617 [ $rv -ge 500 ] && exit 5
618 [ $rv -eq 404 ] && exit 44
619 [ $rv -ge 400 ] && exit 4
620 [ $rv -ge 300 ] && exit 3
621 [ $rv -eq 200 ] || exit $FAILURE
622
623 if [ ! "$probe_type" ]; then
624 cat # output the rest ``as-is''
625 elif [ "$probe_type" = "$PROBE_SIZE" ]; then
626 f_isinteger "$length" || length=-1
627 echo "$length"
628 fi
629 exit 200
630 )
631 local retval=$?
632 [ $retval -eq 200 ] && return $SUCCESS
633 [ "$probe_type" ] && return $FAILURE
634
635 case "$retval" in
636 5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;;
637 44) f_show_msg "$msg_url_was_not_found" "$url" ;;
638 4) f_show_msg "$msg_client_error" ;;
639 *) f_show_msg "$msg_error_when_requesting_url" "$url" ;;
640 esac
641 return $FAILURE
642}
643
644############################################################ MAIN
645
646f_dprintf "%s: Successfully loaded." media/http.subr
647
648fi # ! $_MEDIA_HTTP_SUBR