#!/bin/rc
. /bin/maths.rc
. /bin/httplib.rc
fn length {
HEAD $args $1 |[2] awk ' BEGIN { ranged=0} /Accept-Ranges: bytes/ { ranged = 1} /^Content-Length:/ { size = $2 } END { if(ranged) { print size } else { print 0 } }'
}
fn got {
ls -l $out | awk '/[0-9]/ { print $6 ; exit} { print "0" } '
}
fn fetch {
size = `{length $1}
if(test $size -gt 0) {
tries = 0
red = `{got}
while(test $red -lt $size ) {
GET $args -r $red `{add $red $block} $1 >> $out >[2]/dev/null
red = `{got}
}
}
}
fn usage {
echo Usage : get_file.rc [-e referer] [-b blocksize] -o outfile uri >[1=2]
exit usage
}
out = ''
args = ()
block = 1024
while(~ $1 -*) {
switch($1) {
case -e :
args = `{echo $args -e $2}
shift 2
case -b :
block = $2
shift 2
case -o :
out = $2
shift 2
}
}
test -z $out && usage
if(test -e $out) {
if(test -w $out)
fetch $1
}
if not {
touch $out
fetch $1
}
|