From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21139 invoked by alias); 22 Sep 2011 22:16:08 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 16398 Received: (qmail 13297 invoked from network); 22 Sep 2011 22:16:06 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.google.com designates 209.85.212.43 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=7VY33/fMNaUZAyPHjDfLyu6KyDbJMsO3Uv9kvpQgRCs=; b=OEEWbzekj1wxDOox2W8chLOL88QlCn3fsg0wYUIVEpOGDTHNbQEo6lxAzCFcXWE51V yYkJHFNW1IJ4koAn69FSpRM9rAO3m8eoSKTDStOkcc5z3sqB5IusG2p/5iqM+PJe17Oo S+0goh5VYjoZyBSH20iVakFr5EvGmm5RwytX4= MIME-Version: 1.0 In-Reply-To: <1316725600.8003.YahooMailClassic@web65605.mail.ac4.yahoo.com> References: <1316725600.8003.YahooMailClassic@web65605.mail.ac4.yahoo.com> Date: Thu, 22 Sep 2011 23:50:27 +0200 Message-ID: Subject: Re: pure zsh implementation of wget From: Mikael Magnusson To: Guido van Steen Cc: zsh-users@zsh.org Content-Type: text/plain; charset=UTF-8 On 22 September 2011 23:06, Guido van Steen 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