Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: pure zsh implementation of wget



On 22 September 2011 23:06, Guido van Steen <gvsteen@xxxxxxxxx> wrote:
> Hi!
>
> I was wondering if someone could point me to a pure zsh implementation of a function/script that downloads files like wget.
>
> I have been look at this (slightly modified from http://rosettacode.org/wiki/HTTP#Zsh):
>
> $cat wget.zsh
> #!/usr/bin/env zsh
> zmodload zsh/net/tcp
> ztcp google.com 80
> fd=$REPLY
> print -l -u $fd -- 'GET / HTTP/1.1' 'Host: google.com' ''
> while read -u $fd -r -e -t 1; do; :; done
> ztcp -c $fd
>
> However, when I run this script, I do not receive any files. Neither do I observe any other results. I tried it with other sites, e.g. replacing google.com by localhost, and so on.
> Does someone know what am I missing?

It works for me, but there are a couple of problems. It uses a timeout
of one second, so if your connection is slow it won't do anything, and
it doesn't include the dos newlines i believe you should have in http.
A more severe problem is that it uses read without setting IFS empty
which means it will mangle initial whitespace input. All these issues
(but possibly not others) are solved here (possibly):

#!/bin/zsh
zmodload zsh/net/tcp
ztcp google.com 80
fd=$REPLY
print -l -u $fd -- 'GET / HTTP/1.1'$'\015' 'Host:
www.google.com'$'\015' 'Connection: close'$'\015' $'\015'
while IFS= read -u $fd -r -e; do; :; done
ztcp -c $fd

note that gmail will insert a newline in the print line, so join that
back yourself :) Instead of the while loop you can also just write cat
<&$fd

> BTW, would this work with https as well?

Definitely not :).

-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author