diff options
author | dteske <dteske@FreeBSD.org> | 2013-07-14 03:08:52 +0000 |
---|---|---|
committer | dteske <dteske@FreeBSD.org> | 2013-07-14 03:08:52 +0000 |
commit | e9ad9b489dd3d2b2dd4dd9f43ab1c04b9c00d98a (patch) | |
tree | a9511d0f643be26ac006b8084db35a955fc813f9 /usr.sbin/bsdconfig/share/media/http.subr | |
parent | c6eaf899761fc14eab6497d13c5ac87c11300777 (diff) | |
download | FreeBSD-src-e9ad9b489dd3d2b2dd4dd9f43ab1c04b9c00d98a.zip FreeBSD-src-e9ad9b489dd3d2b2dd4dd9f43ab1c04b9c00d98a.tar.gz |
Re-implement $probe_only aspect of f_media_get_TYPE() (where TYPE is cdrom,
nfs, ftp, http, httpproxy, etc.) and f_device_get() (abstract method for
calling aforementioned f_media_get_TYPE()).
Previously, if $probe_only was present and non-NULL, the TYPE functions
would check for $file and exit with an appropriate error status (success if
the file exists and readable, failure otherwise).
While this has been retained, a pair of globals has been introduced:
$PROBE_EXIST and $PROBE_SIZE (see `/usr/share/bsdconfig/media/common.subr')
The $PROBE_EXIST global can be used where you need the functionality of
simply testing for existence (previously the _only_ functionality).
Meanwhile, the new $PROBE_SIZE global can be used to cause the TYPE function
to print the size of the file (in bytes) on standard-out (or -1) if not
found or an error occurs. NOTE: If an error occurs, it is logged with the
dprintf function, which is visible with `-d' flag or debug=1.
In many cases, where you need to get the size of a file _and_ check for its
existence, you can use the return status of a $PROBE_SIZE call.
Diffstat (limited to 'usr.sbin/bsdconfig/share/media/http.subr')
-rw-r--r-- | usr.sbin/bsdconfig/share/media/http.subr | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/usr.sbin/bsdconfig/share/media/http.subr b/usr.sbin/bsdconfig/share/media/http.subr index a904d01..1236c99 100644 --- a/usr.sbin/bsdconfig/share/media/http.subr +++ b/usr.sbin/bsdconfig/share/media/http.subr @@ -510,13 +510,15 @@ f_media_init_http() return $http_found } -# f_media_get_http $device $file [$probe_only] +# f_media_get_http $device $file [$probe_type] # # Returns data from $file on an HTTP server using nc(1). Please note that # $device is unused but must be present (even if null). Information is instead -# gathered from the environment. If $probe_only is both present and non-NULL, +# gathered from the environment. If $probe_type is both present and non-NULL, # this function exits after receiving the HTTP header response from the server # (if the HTTP response code is 200, success is returned; otherwise failure). +# If $probe_type is equal to $PROBE_SIZE, prints the content-length in bytes +# from the response (or -1 if not found) to standard-out. # # The variables used to configure the connection are as follows (all of which # are configured by f_media_set_http above): @@ -542,10 +544,10 @@ f_media_init_http() # f_media_get_http() { - local dev="$1" file="$2" probe_only="$3" hosts= + local dev="$1" file="$2" probe_type="$3" hosts= - f_dprintf "f_media_get_http: dev=[%s] file=[%s] probe_only=%s" \ - "$dev" "$file" "$probe_only" + f_dprintf "f_media_get_http: dev=[%s] file=[%s] probe_type=%s" \ + "$dev" "$file" "$probe_type" local http_host http_port f_getvar $VAR_HTTP_HOST http_host @@ -591,7 +593,7 @@ f_media_get_http() # this is extremely quick'n dirty # - rv=0 + rv=0 length=-1 while read LINE; do case "$LINE" in HTTP*) @@ -599,6 +601,12 @@ f_media_get_http() set -- $LINE; rv=$2 f_isinteger "$rv" || rv=0 ;; + "Content-Length: "*) + length="${LINE%
}" + length="${length#Content-Length: }" + f_dprintf "received content-length: %s" \ + "$length" + ;; *) [ "${LINE%
}" ] || break # End of headers esac @@ -610,14 +618,17 @@ f_media_get_http() [ $rv -ge 300 ] && exit 3 [ $rv -eq 200 ] || exit $FAILURE - if [ ! "$probe_only" ]; then + if [ ! "$probe_type" ]; then cat # output the rest ``as-is'' + elif [ "$probe_type" = "$PROBE_SIZE" ]; then + f_isinteger "$length" || length=-1 + echo "$length" fi exit 200 ) local retval=$? [ $retval -eq 200 ] && return $SUCCESS - [ "$probe_only" ] && return $FAILURE + [ "$probe_type" ] && return $FAILURE case "$retval" in 5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;; |