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