/var/lib/sorcery/modules/dl_handlers/dl_wget

     1	#!/bin/bash
     2	#---------------------------------------------------------------------
     3	##
     4	##=head1 SYNOPSIS
     5	##
     6	## Url handler functions for downloading http, https, and ftp urls
     7	##
     8	##=head1 DESCRIPTION
     9	##
    10	## This file contains functions for downloading and verifying
    11	## http, https, and ftp urls.  This file uses the "wget" program.
    12	##
    13	##=head1 COPYRIGHT
    14	##
    15	## Copyright 2002 by the Source Mage Team
    16	##
    17	##=head1 FUNCTIONS
    18	##
    19	##=over 4
    20	##
    21	#---------------------------------------------------------------------
    22	
    23	function dl_wget_get() {
    24	  debug libdownload "$FUNCNAME -- $@"
    25	  dl_command_check wget || return 254
    26	
    27	  local target=$1
    28	  local url_list=$2
    29	  local hints=$3
    30	  local dl_target=$4
    31	  local dl_type=$5
    32	  local rc=1 url
    33	
    34	  [[ $target ]] &&
    35	  dl_connect || return 255
    36	
    37	  for url in $url_list; do
    38	    local WGET_OPTIONS
    39	    dl_wget_set_options $url $hints
    40	    dl_wget_call_wget  $target $url
    41	    rc=$?
    42	    [[ $rc == 0 ]] && break
    43	  done
    44	
    45	  dl_disconnect
    46	
    47	  eval "$dl_target=\"$target\""
    48	  eval "$dl_type=\"file\""
    49	  return $rc
    50	
    51	}
    52	
    53	#---------------------------------------------------------------------
    54	## dl_wget_call_wget <filename> <extension-less filename> <url>
    55	##
    56	## Private Function. Call wgets to download the url.
    57	##
    58	#---------------------------------------------------------------------
    59	function dl_wget_call_wget()  {
    60	  local FILE=$1
    61	  local URL=$2
    62	
    63	  debug 'dl_wget' "$funcname -- $@"
    64	
    65	  rm -f $FILE
    66	  wget $WGET_OPTIONS -O "$FILE" "$URL" 2>&1 &&
    67	  if ! test -f "$FILE" ; then
    68	    # stupid http site trying to be nice and re-direct us, this is a failure
    69	    # even though wget doesnt notice it...
    70	    rm -f $FILE*
    71	    return 1
    72	  fi
    73	}
    74	
    75	#---------------------------------------------------------------------
    76	## dl_wget_set_wget_options
    77	##
    78	## Private Function. Sets wget options
    79	##
    80	#---------------------------------------------------------------------
    81	function dl_wget_set_options()  {
    82	  local URL=$1
    83	  local hints="$2"
    84	
    85	  if [  -n  "$ACTIVE_FTP"  ] ; then
    86	    unset  PASSIVE
    87	  else
    88	    PASSIVE="--passive-ftp"
    89	  fi
    90	
    91	  # Check for ? in the url, this seems to indicate that there may be
    92	  # some cgi redirection involved which means continued downloading would
    93	  # not work (bug 8993).
    94	  # The sourceforge check pre-dates that but is lacking any documentation,
    95	  # I suspect it is a less general attempt to solve the same problem.
    96	  # (afk 2005-06-25)
    97	  if grep -qE "(\?|sourceforge)" <<< "$URL"; then
    98	    unset  CONTINUE
    99	  else         CONTINUE="-c"
   100	  fi
   101	
   102	  if [  -n  "$DOWNLOAD_RATE"  ] ; then
   103	    RATE="--limit-rate=${DOWNLOAD_RATE}"
   104	  fi
   105	
   106	  if [  -n  "$URL_HTTP_FTP_TIMEOUT"  ] ; then
   107	    URL_HTTP_TIMEOUT="-T $URL_HTTP_FTP_TIMEOUT"
   108	  else
   109	    unset  URL_HTTP_TIMEOUT
   110	  fi
   111	
   112	  if [  -n  "$URL_HTTP_FTP_RETRIES"  ] ; then
   113	    URL_HTTP_RETRIES="-t $URL_HTTP_FTP_RETRIES"
   114	  else
   115	    URL_HTTP_RETRIES="-t 3"
   116	  fi
   117	
   118	  local check_certificate
   119	  if list_find "$hints" no-check-certificate && [[ $(url_get_prefix "$URL") == https ]]; then
   120	    check_certificate=--no-check-certificate
   121	  fi
   122	
   123	  DEREF_SYM="--retr-symlinks"
   124	
   125	  WGET_OPTIONS="$URL_HTTP_TIMEOUT $URL_HTTP_RETRIES $RATE $PASSIVE $CONTINUE $DEREF_SYM $check_certificate"
   126	  debug 'dl_wget' "wget options: $WGET_OPTIONS"
   127	}
   128	#---------------------------------------------------------------------
   129	##=back
   130	##
   131	##=head1 LICENSE
   132	##
   133	## This software is free software; you can redistribute it and/or modify
   134	## it under the terms of the GNU General Public License as published by
   135	## the Free Software Foundation; either version 2 of the License, or
   136	## (at your option) any later version.
   137	##
   138	## This software is distributed in the hope that it will be useful,
   139	## but WITHOUT ANY WARRANTY; without even the implied warranty of
   140	## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   141	## GNU General Public License for more details.
   142	##
   143	## You should have received a copy of the GNU General Public License
   144	## along with this software; if not, write to the Free Software
   145	## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   146	##
   147	#---------------------------------------------------------------------