zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Guido van Steen <gvsteen@yahoo.com>
Cc: zsh-users@zsh.org
Subject: Re: pure zsh implementation of wget
Date: Thu, 22 Sep 2011 23:50:27 +0200	[thread overview]
Message-ID: <CAHYJk3RSzc0kReHoWDqA1qyLMkotRYu2bm3QkQvdDMJCRWOduw@mail.gmail.com> (raw)
In-Reply-To: <1316725600.8003.YahooMailClassic@web65605.mail.ac4.yahoo.com>

On 22 September 2011 23:06, Guido van Steen <gvsteen@yahoo.com> 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


  reply	other threads:[~2011-09-22 22:16 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-22 21:06 Guido van Steen
2011-09-22 21:50 ` Mikael Magnusson [this message]
2011-09-23  5:23   ` Guido van Steen
2011-09-23  6:57     ` Mikael Magnusson
2011-09-23  7:13       ` Guido van Steen
2011-09-23 12:06         ` Guillaume Brunerie
2011-09-23 12:12           ` İsmail Dönmez
2011-09-23  7:01     ` Bart Schaefer
2011-09-23  7:16       ` Guido van Steen
2011-09-27  9:02     ` Oliver Kiddle
2011-09-24 16:49   ` Guido van Steen
2011-09-24 17:58     ` Bart Schaefer
2011-09-24 20:23       ` Guido van Steen
2011-09-24 21:11         ` Guido van Steen
2011-09-24 21:42         ` Bart Schaefer
2011-09-22 21:53 ` Stephane Bortzmeyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHYJk3RSzc0kReHoWDqA1qyLMkotRYu2bm3QkQvdDMJCRWOduw@mail.gmail.com \
    --to=mikachu@gmail.com \
    --cc=gvsteen@yahoo.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).