alias download {
if ($sock(download)) echo -a There is already a download in progress, please wait until it has finished.
else {
%dl.file = $1
if ($file($nopath(%dl.file))) write -c $nopath(%dl.file)
sockopen download $http.parse($1).host 80
}
}
on *:SOCKOPEN:download:{
sockwrite -nt $sockname GET $http.parse(%dl.file).path HTTP/1.1
sockwrite -nt $sockname Host: $+($http.parse(%dl.file).host,$str($crlf,2))
}
on *:SOCKREAD:download:{
if (%dl.header != 1) {
var %header
sockread %header
while ($sockbr) {
if (Content-length: * iswm %header) {
%dl.length = $gettok(%header,2,32)
echo -a Filesize of $+($nopath(%dl.file),:) $bytes(%dl.length,b) bytes
}
if (!%header) {
%dl.header = 1
%dl.offset = $sock(download).rcvd
break
}
sockread %header
}
}
echo -a Beginning download.
sockread 4096 &data
while ($sockbr) {
bwrite $nopath(%dl.file) -1 -1 &data
sockread 4096 &data
}
}
on *:SOCKCLOSE:download:{
echo -a Download of $nopath(%dl.file) completed.
unset %dl.*
}
alias http.parse {
if ($regex($1-,^http://([^/]+)(/.*))) {
if ($prop == host) return $regml(1)
if ($prop == path) return $iif(($regml(2)),$regml(2),/)
}
}