]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - usr.sbin/bsdconfig/share/media/http.subr
MFS9 r267683 (dteske):
[FreeBSD/releng/9.3.git] / usr.sbin / bsdconfig / share / media / http.subr
1 if [ ! "$_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:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
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$
28 #
29 ############################################################ INCLUDES
30
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." media/http.subr
34 f_include $BSDCFG_SHARE/device.subr
35 f_include $BSDCFG_SHARE/dialog.subr
36 f_include $BSDCFG_SHARE/media/common.subr
37 f_include $BSDCFG_SHARE/media/tcpip.subr
38 f_include $BSDCFG_SHARE/strings.subr
39 f_include $BSDCFG_SHARE/struct.subr
40 f_include $BSDCFG_SHARE/variable.subr
41
42 BSDCFG_LIBE="/usr/libexec/bsdconfig"
43 f_include_lang $BSDCFG_LIBE/include/messages.subr
44
45 ############################################################ GLOBALS
46
47 HTTP_SKIP_RESOLV=
48
49 URL_MAX=261261
50         # NOTE: This is according to actual fetch(1) test-results. We actually
51         # use nc(1) to retrieve files, but it's still a good idea to keep the
52         # URLs short enough that fetch(1) won't complain.
53
54 HTTP_DIRS="
55         .
56         releases/$UNAME_P
57         snapshots/$UNAME_P
58         pub/FreeBSD
59         pub/FreeBSD/releases/$UNAME_P
60         pub/FreeBSD/snapshots/$UNAME_P
61         pub/FreeBSD-Archive/old-releases/$UNAME_P
62 " # END-QUOTE
63
64 ############################################################ FUNCTIONS
65
66 # f_dialog_menu_media_http
67 #
68 # Prompt the user to select from a range of ``built-in'' HTTP servers or
69 # specify their own. If the user makes a choice and doesn't cancel or press
70 # Esc, stores the user's choice in VAR_FTP_PATH (see variable.subr) and returns
71 # success.
72 #
73 f_dialog_menu_media_http()
74 {
75         f_dialog_title "$msg_please_select_a_freebsd_http_distribution_site"
76         local title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE"
77         f_dialog_title_restore
78         local prompt="$msg_please_select_the_site_closest_to_you_or_other"
79         local menu_list="
80                 'dist $msg_main_site' 'ftp.freebsd.org'
81                 'pkg $msg_main_site'  'pkg.freebsd.org'
82                 'URL'                 '$msg_specify_some_other_http_site'
83         " # END-QUOTE
84         local hline="$msg_select_a_site_thats_close"
85
86         local height width rows
87         eval f_dialog_menu_size height width rows \
88                                 \"\$title\"  \
89                                 \"\$btitle\" \
90                                 \"\$prompt\" \
91                                 \"\$hline\"  \
92                                 $menu_list
93
94         local mtag
95         mtag=$( eval $DIALOG \
96                 --title \"\$title\"             \
97                 --backtitle \"\$btitle\"        \
98                 --hline \"\$hline\"             \
99                 --ok-label \"\$msg_ok\"         \
100                 --cancel-label \"\$msg_cancel\" \
101                 --menu \"\$prompt\"             \
102                 $height $width $rows            \
103                 $menu_list                      \
104                 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
105         ) || return $DIALOG_CANCEL
106         f_dialog_data_sanitize mtag
107
108         case "$mtag" in
109         URL) setvar $VAR_HTTP_PATH "other" ;;
110         *)
111                 local value
112                 value=$( eval f_dialog_menutag2item \"\$mtag\" $menu_list )
113                 setvar $VAR_HTTP_PATH "http://$value"
114         esac
115         
116         return $DIALOG_OK
117 }
118
119 # f_media_set_http
120 #
121 # Return success if we both found and set the media type to be an HTTP server.
122 #
123 # Variables from variable.subr that can be used to script user input:
124 #
125 #       VAR_HTTP_PATH
126 #               URL containing host and optionally a target path to the release
127 #               repository on the HTTP server. Valid examples include:
128 #                       http://myhost
129 #                       http://somename:80/pub/
130 #                       http://192.168.2.3/pub/
131 #                       http://[::1]:8000/
132 #               The default port if not specified is 80.
133 #       VAR_NAMESERVER [Optional]
134 #               If set, overrides resolv.conf(5) and sets the nameserver that
135 #               is used to convert names into addresses (when a name converts
136 #               into multiple addresses, the first address to successfully
137 #               connect is used).
138 #
139 # Meanwhile, the following variables from variable.subr are set after
140 # successful execution:
141 #
142 #       VAR_HTTP_HOST
143 #               The HTTP host to connect to, parsed from VAR_HTTP_PATH. In the
144 #               example case of IPv6 where VAR_HTTP_PATH is "http://[::1]" this
145 #               variable will be set to "::1" (the outer brackets are removed).
146 #       VAR_HTTP_PORT
147 #               The TCP port to connect to, parsed from VAR_HTTP_PATH. Usually
148 #               80 unless VAR_HTTP_PATH was one of the following forms:
149 #                       http://hostname:OTHER_PORT
150 #                       http://hostname:OTHER_PORT/*
151 #                       http://ip:OTHER_PORT
152 #                       http://ip:OTHER_PORT/*
153 #                       http://[ip6]:OTHER_PORT
154 #                       http://[ip6]:OTHER_PORT/*
155 #       VAR_HTTP_DIR
156 #               If VAR_HTTP_PATH contained a directory element (e.g.,
157 #               "http://localhost/pub") this variable contains only the
158 #               directory element (e.g., "/pub").
159 #
160 f_media_set_http()
161 {
162         f_media_close
163
164         local url
165         f_getvar $VAR_HTTP_PATH url
166
167         # If we've been through here before ...
168         if f_struct device_network && [ "${url#$msg_other}" ]; then
169                 f_dialog_yesno "$msg_reuse_old_http_site_settings" || url=
170         fi
171
172         if [ ! "$url" ]; then
173                 f_dialog_menu_media_http || return $FAILURE
174                 f_getvar $VAR_HTTP_PATH url
175         fi
176         [ "$url" ] || return $FAILURE
177
178         case "$url" in
179         other)
180                 setvar $VAR_HTTP_PATH "http://"
181                 f_variable_get_value $VAR_HTTP_PATH \
182                         "$msg_please_specify_url_of_freebsd_http_distribution"
183                 f_getvar $VAR_HTTP_PATH url
184                 if [ ! "${url#http://}" ]; then
185                         unset $VAR_HTTP_PATH
186                         return $FAILURE
187                 fi
188                 if [ ${#url} -gt ${URL_MAX:-261261} ]; then
189                         f_show_msg "$msg_length_of_specified_url_is_too_long" \
190                                    ${#url} ${URL_MAX:-261261}
191                         unset $VAR_HTTP_PATH
192                         return $FAILURE
193                 fi
194                 case "$url" in
195                 http://*) : valid URL ;;
196                 *)
197                         f_show_msg "$msg_sorry_invalid_url" "$url"
198                         unset $VAR_HTTP_PATH
199                         return $FAILURE
200                 esac
201         esac
202         case "$url" in
203         http://*) : valid URL ;;
204         *)
205                 f_show_msg "$msg_sorry_invalid_url" "$url"
206                 unset $VAR_HTTP_PATH
207                 return $FAILURE
208         esac
209
210         # Set the name of the HTTP device to the URL
211         f_struct_new DEVICE device_http
212         device_http set name "$url"
213
214         if ! f_struct device_network ||
215            ! f_dialog_yesno "$msg_youve_already_done_the_network_configuration"
216         then
217                 f_struct device_network &&
218                         f_device_shutdown device_network
219                 if ! f_device_select_tcp; then
220                         unset $VAR_HTTP_PATH
221                         return $FAILURE
222                 fi
223                 local dev if
224                 f_getvar $VAR_NETWORK_DEVICE if
225                 f_device_find -1 "$if" $DEVICE_TYPE_NETWORK dev
226                 f_struct_copy "$dev" device_network
227         fi
228         if ! f_device_init device_network; then
229                 f_dprintf "f_media_set_http: %s" "$msg_net_device_init_failed"
230                 unset $VAR_HTTP_PATH
231                 return $FAILURE
232         fi
233
234         local hostname="${url#*://}" port=80 dir=/
235         case "$hostname" in
236         #
237         # The order in-which the below individual cases appear is important!
238         #
239         "["*"]":*/*) # IPv6 address with port and directory
240                 f_dprintf "Looks like an IPv6 addr with port/dir: %s" \
241                           "$hostname"
242                 hostname="${hostname#\[}"
243                 port="${hostname#*\]:}"
244                 port="${port%%[!0-9]*}"
245                 dir="/${hostname#*/}"
246                 hostname="${hostname%%\]:*}"
247                 ;;
248         "["*"]":*) # IPv6 address with port
249                 f_dprintf "Looks like an IPv6 addr with port: %s" "$hostname"
250                 hostname="${hostname#\[}"
251                 port="${hostname#*\]:}"
252                 port="${port%%[!0-9]*}"
253                 hostname="${hostname%%\]:*}"
254                 ;;
255         "["*"]"/*) # IPv6 address with directory
256                 f_dprintf "Looks like an IPv6 addr with dir: %s" "$hostname"
257                 hostname="${hostname#\[}"
258                 dir="/${hostname#*/}"
259                 hostname="${hostname%%\]*}"
260                 ;;
261         "["*"]") # IPv6 address
262                 f_dprintf "Looks like an IPv6 addr: %s" "$hostname"
263                 hostname="${hostname#\[}"
264                 hostname="${hostname%\]}"
265                 ;;
266         #
267         # ^^^ IPv6 above / DNS Name or IPv4 below vvv
268         #
269         *:*/*) # DNS name or IPv4 address with port and directory
270                 f_dprintf "Looks like a %s with port/dir: %s" \
271                           "DNS name or IPv4 addr" "$hostname"
272                 port="${hostname#*:}"
273                 port="${port%%[!0-9]*}"
274                 dir="/${hostname#*/}"
275                 hostname="${hostname%%:*}"
276                 ;;
277         *:*) # DNS name or IPv4 address with port
278                 f_dprintf "Looks like a DNS name or IPv4 addr with port: %s" \
279                           "$hostname"
280                 port="${hostname#*:}"
281                 hostname="${hostname%%:*}"
282                 ;;
283         */*) # DNS name or IPv4 address with directory
284                 f_dprintf "Looks like a DNS name or IPv4 addr with dir: %s" \
285                           "$hostname"
286                 dir="/${hostname#*/}"
287                 hostname="${hostname%%/*}"
288                 ;;
289         *) # DNS name or IPv4 address
290                 f_dprintf "Looks like a DNS name or IPv4 addr: %s" "$hostname"
291                 : leave hostname as-is
292         esac
293
294         f_dprintf "hostname = \`%s'" "$hostname"
295         f_dprintf "dir = \`%s'" "$dir"
296         f_dprintf "port \# = \`%d'" "$port"
297
298         local ns
299         f_getvar $VAR_NAMESERVER ns
300         [ "$ns" ] || f_resolv_conf_nameservers ns
301         if [ "$ns" -a ! "$HTTP_SKIP_RESOLV" ] && ! {
302                 f_validate_ipaddr "$hostname" ||
303                 f_validate_ipaddr6 "$hostname"
304         }; then
305                 f_show_info "$msg_looking_up_host" "$hostname"
306                 f_dprintf "%s: Looking up hostname, %s, using host(1)" \
307                           "f_media_set_http" "$hostname"
308                 if ! f_quietly f_host_lookup "$hostname"; then
309                         f_show_msg "$msg_cannot_resolve_hostname" "$hostname"
310                         f_struct device_network &&
311                                 f_device_shutdown device_network
312                         f_struct_free device_network
313                         unset $VAR_HTTP_PATH
314                         return $FAILURE
315                 fi
316                 f_dprintf "Found DNS entry for %s successfully." "$hostname"
317         fi
318
319         setvar $VAR_HTTP_HOST "$hostname"
320         setvar $VAR_HTTP_PORT "$port"
321         setvar $VAR_HTTP_DIR  "$dir"
322
323         device_http set type     $DEVICE_TYPE_HTTP
324         device_http set init     f_media_init_http
325         device_http set get      f_media_get_http
326         device_http set shutdown f_media_shutdown_http
327         device_http set private  device_network
328         f_struct_copy device_http device_media
329         f_struct_free device_http
330
331         return $SUCCESS
332 }
333
334 # f_http_check_access [$connect_only]
335 #
336 # Return success if able list a remote HTTP directory. If $connect_only is
337 # present and non-null, then returns success if a connection can be made.
338 # Variables from variable.subr that can be used to script user input:
339 #
340 #       VAR_HTTP_HOST
341 #               The HTTP server host name, IPv4 address or IPv6 address.
342 #               Valid examples include:
343 #                       myhost
344 #                       192.168.2.3
345 #                       ::1
346 #       VAR_HTTP_PORT
347 #               The TCP port to connect to when communicating with the server.
348 #       VAR_HTTP_PATH
349 #               The HTTP path sent to the server. Unused if $connect_only is
350 #               present and non-NULL.
351 #
352 f_http_check_access()
353 {
354         local connect_only="$1" hosts=
355
356         local http_host http_port
357         f_getvar $VAR_HTTP_HOST http_host
358         f_getvar $VAR_HTTP_PORT http_port
359
360         if ! {
361                 f_validate_ipaddr "$http_host" ||
362                 f_validate_ipaddr6 "$http_host" ||
363                 {
364                   f_dprintf "%s: Looking up hostname, %s, using host(1)" \
365                             "f_http_check_access" "$http_host"
366                   f_host_lookup "$http_host" hosts
367                 }
368         }; then
369                 # All the above validations failed
370                 [ "$hosts" ] && f_dialog_msgbox "$hosts"
371                 unset $VAR_HTTP_HOST
372                 return $FAILURE
373         elif [ ! "$hosts" ]; then
374                 # One of the first two validations passed
375                 hosts="$http_host"
376         fi
377
378         local host connected=
379         for host in $hosts; do
380                 f_quietly nc -nz "$host" "$http_port" || continue
381                 connected=1; break
382         done
383         if [ ! "$connected" ]; then
384                 f_show_msg "$msg_couldnt_connect_to_server http://%s:%s/" \
385                            "$http_host" "$http_port"
386                 unset $VAR_HTTP_HOST
387                 return $FAILURE
388         fi
389         [ "$connect_only" ] && return $SUCCESS
390
391         local http_path
392         f_getvar $VAR_HTTP_PATH http_path
393         f_show_info "$msg_checking_access_to" "$http_path"
394
395         local rx
396         case "$http_path" in
397         http://*|/*) : valid request ;;
398         *) http_path="/$http_path" # full URI requests only
399         esac
400         if ! rx=$(
401                 printf "GET %s/ HTTP/1.0\r\n\r\n" "${http_path%/}" |
402                         nc -n "$host" "$http_port"
403         ); then
404                 f_show_msg "$msg_couldnt_connect_to_server http://%s:%s/" \
405                            "$http_host" "$http_port"
406                 unset $VAR_HTTP_HOST
407                 return $FAILURE
408         fi
409
410         local hdr
411         hdr=$( echo "$rx" | awk '/^\r$/{exit}{print}' )
412
413         local http_found=$FAILURE
414         if echo "$hdr" | awk '
415                 BEGIN { found = 0 }
416                 /^HTTP.... 200 / {
417                         found = 1
418                         exit
419                 }
420                 END { exit ! found }
421         '; then
422                 http_found=$SUCCESS
423         fi
424
425         return $http_found
426 }
427
428 # f_media_init_http $device
429 #
430 # Initializes the HTTP media device. Returns success if able to confirm the
431 # existence of at least one known HTTP server release path directly via HTTP
432 # using f_http_check_access(), above.
433 #
434 # Variables from variable.subr that can be used to script user input:
435 #
436 #       VAR_HTTP_HOST
437 #               The HTTP server to connect to. Must be set. Also see
438 #               f_http_check_access() for additional variables.
439 #       VAR_RELNAME
440 #               Usually set to `uname -r' but can be overridden.
441 #       VAR_HTTP_PATH
442 #               The HTTP path sent to the server. Usually set by calling
443 #               f_media_set_http().
444 #
445 # Meanwhile, after successful execution, the following variables (also from
446 # variable.subr) are set:
447 #
448 #       VAR_HTTP_PATH
449 #               The [possibly] adjusted VAR_HTTP_PATH that was found to contain
450 #               a valid FreeBSD repository.
451 #
452 f_media_init_http()
453 {
454         local dev="$1"
455         f_dprintf "Init routine called for HTTP device. dev=[%s]" "$dev"
456
457         if [ "$HTTP_INITIALIZED" ]; then
458                 f_dprintf "HTTP device already initialized."
459                 return $SUCCESS
460         fi
461
462         #
463         # First verify access
464         #
465         local connect_only=1
466         f_http_check_access $connect_only
467
468         local http_host
469         f_getvar $VAR_HTTP_HOST http_host
470         while [ ! "$http_host" ]; do
471                 f_media_set_http || return $FAILURE
472                 f_http_check_access $connect_only
473                 f_getvar $VAR_HTTP_HOST http_host
474         done
475
476         local http_path http_found=$FAILURE
477         while :; do
478                 #
479                 # Now that we've verified that the path we're given is ok,
480                 # let's try to be a bit intelligent in locating the release we
481                 # are looking for.  First off, if the release is specified as
482                 # "__RELEASE" or "any", then just assume that the current
483                 # directory is the one we want and give up.
484                 #
485                 local rel
486                 f_getvar $VAR_RELNAME rel
487                 f_dprintf "f_media_init_http: rel=[%s]" "$rel"
488
489                 case "$rel" in
490                 __RELEASE|any)
491                         f_getvar $VAR_HTTP_DIR $VAR_HTTP_PATH
492                         f_http_check_access
493                         http_found=$?
494                         ;;
495                 *)
496                         #
497                         # Ok, since we have a release variable, let's walk
498                         # through the list of directories looking for a release
499                         # directory. First successful path wins.
500                         #
501                         local fdir hp
502                         f_getvar $VAR_HTTP_PATH%/ hp
503                         setvar $VAR_HTTP_PATH "$hp/$PKG_ABI/latest"
504                         if [ "$PKG_ABI" ] && f_http_check_access; then
505                                 http_found=$SUCCESS
506                                 setvar $VAR_HTTP_PATH "$hp"
507                         else
508                                 for fdir in $HTTP_DIRS; do
509                                         setvar $VAR_HTTP_PATH "$hp/$fdir/$rel"
510                                         if f_http_check_access; then
511                                                 http_found=$SUCCESS
512                                                 break
513                                         fi
514                                 done
515                         fi
516                 esac
517
518                 if [ $http_found -eq $SUCCESS ]; then
519                         HTTP_INITIALIZED=YES
520                         break
521                 fi
522
523                 f_getvar $VAR_HTTP_PATH http_path
524                 f_show_msg "$msg_please_check_the_url_and_try_again" \
525                            "$http_path"
526
527                 unset HTTP_INITIALIZED $VAR_HTTP_PATH
528                 f_media_set_http || break
529         done
530
531         return $http_found
532 }
533
534 # f_media_get_http $device $file [$probe_type]
535 #
536 # Returns data from $file on an HTTP server using nc(1). Please note that
537 # $device is unused but must be present (even if null). Information is instead
538 # gathered from the environment. If $probe_type is both present and non-NULL,
539 # this function exits after receiving the HTTP header response from the server
540 # (if the HTTP response code is 200, success is returned; otherwise failure).
541 # If $probe_type is equal to $PROBE_SIZE, prints the content-length in bytes
542 # from the response (or -1 if not found) to standard-out.
543 #
544 # The variables used to configure the connection are as follows (all of which
545 # are configured by f_media_set_http above):
546 #
547 #       VAR_HTTP_HOST
548 #               HTTP server which to connect. Can be an IPv4 address, IPv6
549 #               address, or DNS hostname of your choice.
550 #       VAR_HTTP_PORT
551 #               TCP port to connect on; see f_media_set_http above.
552 #       VAR_HTTP_PATH
553 #               Directory prefix to use when requesting $file. Default is `/'
554 #               unless f_media_init_http was able to use f_http_check_access
555 #               to validate one of the defaults in $HTTP_DIRS (see GLOBALS at
556 #               the top of this file); assuming VAR_RELNAME was not set to
557 #               either `__RELEASE' or `any' (indicating that the global set of
558 #               $HTTP_DIRS should be ignored).
559 #
560 # See variable.subr for additional information.
561 #
562 # Example usage:
563 #       f_media_set_http
564 #       f_media_get_http media $file
565 #
566 f_media_get_http()
567 {
568         local dev="$1" file="$2" probe_type="$3" hosts=
569         local name
570
571         $dev get name name
572         f_dprintf "f_media_get_http: dev=[%s] file=[%s] probe_type=%s" \
573                   "$name" "$file" "$probe_type"
574
575         local http_host http_port
576         f_getvar $VAR_HTTP_HOST http_host
577         f_getvar $VAR_HTTP_PORT http_port
578
579         if [ ! "$HTTP_INITIALIZED" ]; then
580                 f_dprintf "No HTTP connection open, can't get file %s" "$file"
581                 return $FAILURE
582         fi
583
584         if ! {
585                 f_validate_ipaddr "$http_host" ||
586                 f_validate_ipaddr6 "$http_host" ||
587                 {
588                   f_dprintf "%s: Looking up hostname, %s, using host(1)" \
589                             "f_media_get_http" "$http_host"
590                   f_host_lookup "$http_host" hosts
591                 }
592         }; then
593                 # All the above validations failed
594                 [ "$hosts" ] && f_dialog_msgbox "$hosts"
595                 return $FAILURE
596         elif [ ! "$hosts" ]; then
597                 # One of the first two validations passed
598                 hosts="$http_host"
599         fi
600
601         local host connected=
602         for host in $hosts; do
603                 f_quietly nc -nz "$host" "$http_port" || continue
604                 connected=1; break
605         done
606         if [ ! "$connected" ]; then
607                 f_show_msg "$msg_couldnt_connect_to_server http://%s:%s/" \
608                            "$http_host" "$http_port"
609                 return $FAILURE
610         fi
611
612         local http_path
613         f_getvar $VAR_HTTP_PATH%/ http_path
614         case "$http_path" in
615         http://*|/*) : valid request ;;
616         *) http_path="/$http_path" # full URI requests only
617         esac
618
619         local url="$http_path/$file" rx
620         f_dprintf "sending http request for: %s" "$url"
621         f_dprintf "using nc to connect to: %s:%s" "$host" "$http_port"
622         printf "GET %s HTTP/1.0\r\n\r\n" "$url" | nc -n "$host" "$http_port" |
623         (
624                 #
625                 # scan the headers of the response
626                 # this is extremely quick'n dirty
627                 #
628
629                 rv=0 length=-1
630                 while read LINE; do
631                         case "$LINE" in
632                         HTTP*)
633                                 f_dprintf "received response: %s" "$LINE"
634                                 set -- $LINE; rv=$2
635                                 f_isinteger "$rv" || rv=0
636                                 ;;
637                         "Content-Length: "*)
638                                 length="${LINE%\r}"
639                                 length="${length#Content-Length: }"
640                                 f_dprintf "received content-length: %s" \
641                                           "$length"
642                                 ;;
643                         *)
644                                 [ "${LINE%\r}" ] || break # End of headers
645                         esac
646                 done
647
648                 [ $rv -ge 500 ] && exit 5
649                 [ $rv -eq 404 ] && exit 44
650                 [ $rv -ge 400 ] && exit 4
651                 [ $rv -ge 300 ] && exit 3
652                 [ $rv -eq 200 ] || exit $FAILURE
653
654                 if [ ! "$probe_type" ]; then
655                         cat # output the rest ``as-is''
656                 elif [ "$probe_type" = "$PROBE_SIZE" ]; then
657                         f_isinteger "$length" || length=-1
658                         echo "$length"
659                 fi
660                 exit 200
661         )
662         local retval=$?
663         [ $retval -eq 200 ] && return $SUCCESS
664         [ "$probe_type" ] && return $FAILURE
665
666         case "$retval" in
667           5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;;
668          44) f_show_msg "$msg_url_was_not_found" "$url" ;;
669           4) f_show_msg "$msg_client_error" ;;
670           *) f_show_msg "$msg_error_when_requesting_url" "$url" ;;
671         esac
672         return $FAILURE
673 }
674
675 # f_media_shutdown_http $device
676 #
677 # Shuts down the HTTP device. Return status should be ignored. Note that since
678 # we don't maintain an open connection to the HTTP server, nothing to do.
679 #
680 f_media_shutdown_http()
681 {
682         [ "$HTTP_INITIALIZED" ] || return $SUCCESS
683
684         unset HTTP_INITIALIZED
685 }
686
687 ############################################################ MAIN
688
689 f_dprintf "%s: Successfully loaded." media/http.subr
690
691 fi # ! $_MEDIA_HTTP_SUBR