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

Re: pure zsh implementation of wget



On Sep 24,  1:23pm, Guido van Steen wrote:
}
} Now hopefully my last question: is there an easy way to get rid of the
} first part of the file

That's just the standard header in the HTTP format, like the header on
an email message.  You just have to discard everything up to the first
blank line.

One other thing you may want to do is switch from 'HTTP/1.1' to 1.0 to
prevent the server from breaking the content into chunks.  You'll be
limited to 10MB downloads but won't have to interpret any content after
the first blank line.

A blank line here means one matching $'\015\012', but "read" will strip
the $'\012' so:

zwget() {
    emulate -LR zsh
    local scheme empty server resource fd headerline
    IFS=/ read scheme empty server resource <<<$1
    case $scheme in
    (https:) print -u2 SSL unsupported, falling back on HTTP ;&
    (http:)
        zmodload zsh/net/tcp
        ztcp $server 80 && fd=$REPLY || return 1;;
    (*) print -u2 $scheme unsupported; return 1;;
    esac
    print -l -u$fd -- \
        "GET /$resource HTTP/1.0"$'\015' \
        "Host: $server"$'\015' \
        'Connection: close'$'\015' $'\015'
    while IFS= read -u $fd -r headerline
    do
	[[ $headerline == $'\015' ]] && break
    done
    while IFS= read -u $fd -r -e; do :; done
    ztcp -c $fd
}



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