1######################################################################## 2# # 3# This software is part of the ast package # 4# Copyright (c) 1994-2011 AT&T Intellectual Property # 5# and is licensed under the # 6# Eclipse Public License, Version 1.0 # 7# by AT&T Intellectual Property # 8# # 9# A copy of the License is available at # 10# http://www.eclipse.org/org/documents/epl-v10.html # 11# (with md5 checksum b35adb5213ca9657e911e9befb180842) # 12# # 13# Information and Software Systems Research # 14# AT&T Research # 15# Florham Park NJ # 16# # 17# Glenn Fowler <gsf@research.att.com> # 18# # 19######################################################################## 20: copy http url data 21 22command=hurl 23agent="$command/2009-01-20 (AT&T Research)" 24authorize= 25verbose=0 26 27case `(getopts '[-][123:xyz]' opt --xyz; echo 0$opt) 2>/dev/null` in 280123) ARGV0="-a $command" 29 USAGE=$' 30[-? 31@(#)$Id: hurl (AT&T Research) 2009-01-20 $ 32] 33'$USAGE_LICENSE$' 34[+NAME?hurl - copy http url data] 35[+DESCRIPTION?\bhurl\b copies the data for the \bhttp\b \aurl\a operand 36 to the standard output. The \aurl\a must be of the form 37 \b[http://]]\b\ahost\a[\b:\b\aport\a]]\b/\b\apath\a. The default 38 \aport\a is \b80\b.] 39[+?\bhurl\b is a shell script that attempts to access the \aurl\a by 40 these methods:]{ 41 [+/dev/tcp/\ahost\a\b/80\b?Supported by \bksh\b(1) and recent 42 \bbash\b(1).] 43 [+wget -nv -O - \aurl\a?] 44 [+lynx -source \aurl\a?] 45 [+curl -s -L -o - \aurl\a?] 46} 47[a:authorize?The url authorization user name and password, separated 48 by \b:\b (one colon character.)]:[user::password] 49[s:size?Terminate the data transmission after \abytes\a have been 50 transferred.]:[bytes] 51[v:verbose?Verbose trace.] 52 53url 54 55[+SEE ALSO?\bcurl\b(1), \blynx\b(1), \bwget\b(1)] 56' 57 ;; 58*) ARGV0="" 59 USAGE="a:v" 60 ;; 61esac 62 63usage() 64{ 65 OPTIND=0 66 getopts $ARGV0 "$USAGE" OPT '-?' 67 exit 2 68} 69 70integer limit=0 total=0 block=8*1024 71 72while getopts $ARGV0 "$USAGE" OPT 73do case $OPT in 74 a) authorize=$OPTARG ;; 75 s) limit=$OPTARG ;; 76 v) verbose=1 ;; 77 esac 78done 79shift `expr $OPTIND - 1` 80 81url=$1 82AUTHORIZE= 83 84exec 9<&0 85 86while : 87do test 0 != $verbose && echo "$command: url=$url" >&2 88 case $url in 89 *://*/*)prot=${url%%:*} 90 url=${url#*://} 91 ;; 92 *) prot=http 93 ;; 94 esac 95 host=$url 96 path=/${host#*/} 97 host=${host%%/*} 98 case $host in 99 *:+([0-9])) 100 port=${host##*:} 101 host=${host%:*} 102 ;; 103 *) port=80 104 ;; 105 esac 106 test 0 != $verbose && echo "$command: prot=$prot host=$host port=$port path=$path" >&2 107 case $prot in 108 http) if (eval "exec >" || exit 0) 2>/dev/null && 109 eval "exec 8<> /dev/tcp/\$host/$port" 2>/dev/null 110 then test 0 != $verbose && echo "$command: using /dev/tcp/$host/$port" >&2 111 if ! echo "GET $path HTTP/1.0 112Host: $host 113User-Agent: $agent 114${AUTHORIZE} 115 116" >&8 117 then echo "$command: $host: write error" 118 exit 1 119 fi 120 { 121 if ! read prot code text 122 then echo "$command: $host: read error" >&2 123 exit 1 124 fi 125 code=${code%:*} 126 type=Basic 127 realm=access 128 test 0 != $verbose && echo "$command: prot=$prot code=$code $text" >&2 129 while : 130 do if ! read head data 131 then echo "$command: $host: read error" >&2 132 exit 1 133 fi 134 test 0 != $verbose && echo "$command: head=$head $data" >&2 135 case $head in 136 Location:) 137 case $code in 138 30[123])url=$data 139 continue 2 140 ;; 141 esac 142 ;; 143 WWW-Authenticate:) 144 set -- $data 145 type=$1 146 shift 147 eval "$@" 148 realm=${realm%$'\r'} 149 ;; 150 ''|?) break 151 ;; 152 esac 153 done 154 case $code in 155 200) if (( limit )) 156 then (( limit = (limit + block - 1) / block)) 157 dd bs=$block count=$limit silent=1 158 else cat 159 fi 160 exit 161 ;; 162 401) { 163 if [[ $AUTHORIZE || $type != Basic ]] 164 then print authorization failed 165 exit 1 166 fi 167 if [[ ! $authorize ]] 168 then if [[ ! -t 0 ]] 169 then print authorization failed 170 exit 1 171 fi 172 print -n "Enter user name for $realm: " 173 read -u9 user 174 print -n "Password: " 175 trap 'stty echo <&9' 0 1 2 3 15 176 stty -echo 177 read password 178 stty echo 179 print 180 trap - 0 1 2 3 15 181 authorize=$user:$password 182 fi 183 AUTHORIZE=$'\nAuthorization: '$type' '$(print -n -r -- "$authorize" | uuencode -h -x base64)$'\r' 184 } <&9 >&2 185 continue 2 186 ;; 187 *) echo "$0: $url: $code: $text" >&2 188 exit 1 189 ;; 190 esac 191 } <&8 192 elif wget ${authorize:+--http-user="${authorize%:*}"} ${password:+--http-passwd="${password##*:}"} -nv -O - $url 2>/dev/null 193 then test 0 != $verbose && echo "$command: using wget" >&2 194 exit 195 elif lynx ${authorize:+-auth "$authorize"} -source $url 2>/dev/null 196 then test 0 != $verbose && echo "$command: using wget" >&2 197 exit 198 elif curl ${authorize:+-u "$authorize"} -s -L -o - $url 2>/dev/null 199 then test 0 != $verbose && echo "$command: using curl" >&2 200 exit 201 else echo "$command: $url: { /dev/tcp/$host/$port wget curl } failed" >&2 202 exit 1 203 fi 204 ;; 205 *) echo "$command: $prot: protocol not supported" >&2 206 exit 1 207 ;; 208 esac 209done 210