plan9fox/rc/bin/hget
cinap_lenrek e611879eab hget: work arround apache Content-Encoding: gzip for Content-Type: application/x-gzip bug
apache sends Content-Encoding: gzip header for Content-Type: application/x-gzip
causing hget to decompress tgz files.

from the w3c:

The Content-Encoding entity-header field is used as a modifier to the media-type.
When presented, its value indicates what additional content codings have been applied
to the entity-body, and thus what decoding mechanisms must be applied in order to
obtail the media-type referenced by the Conent-Type header field. Content-Encoding
is primarily used to allow a document to be compressed without losing the
identity of its underlying media type.

this is clearly silly, as the file is already compressed, and decompressing it
will not yield the indicated Content-type: application/x-gzip, but a tarball.

examples:

http://zlib.net/zlib-1.2.8.tar.gz
https://www.mirbsd.org/MirOS/dist/mir/mksh/mksh-R50f.tgz
2015-05-14 14:12:28 +02:00

95 lines
1.5 KiB
Bash
Executable file

#!/bin/rc
rfork e
argv0=$0
fn usage {
echo usage: $argv0 '[ -l | -o file] [ -p body | -P ] [ -r header ] [ -m method ] [ -b baseurl ] url' >[1=2]
exit usage
}
s=0
o=()
p=()
P=()
r=()
m=()
b=()
l=()
while(~ $1 -*){
switch($1){
case -o
o=$2
shift
case -p
p=$2
shift
case -P
P=1
case -r
r=($r $2)
shift
case -l
l=1
case -m
m=$2
shift
case -b
b=$2
shift
case *
usage
}
shift
}
if(! ~ $#* 1)
usage
if(! ~ $#o 0){
if(! ~ $#o 1)
usage
if(test -s $o)
s=`{ls -l $o | awk '{print $6}'}
}
if(! ~ $s 0)
r=($r 'Range: bytes='^$s^'-')
<[3=0] <>/mnt/web/clone {
d=/mnt/web/^`{sed 1q}
if(~ $#b 1)
echo -n baseurl $b >[1=0]
echo -n url $1 >[1=0]
for(i in $r)
echo -n headers $i >[1=0]
if(~ $#m 1)
echo -n request $m >[1=0]
if(! ~ $#p 0 || ! ~ $#P 0){
>$d/postbody {
if(! ~ $#p 0)
echo -n $"p
if(! ~ $#P 0)
cat <[0=3]
}
}
<$d/body {
if(~ $#l 1){
echo `{cat $d/parsed/url}
exit
}
if(~ $#o 1){
l=`{cat $d/contentlength >[2]/dev/null}
x=`{awk 'BEGIN{FS=" |-"}/^bytes ([0-9]+)\-/{print $2}' \
$d/contentrange >[2]/dev/null}
if(~ $s $l && ~ $#x 0)
exit
if(~ $s $x)
exec cat >>$o
exec cat >$o
}
c=`{cat $d/contentencoding >[2]/dev/null}
switch($c){
case *gzip*
~ `{cat $d/contenttype >[2]/dev/null} *gzip* || exec gunzip
case *bzip2*
~ `{cat $d/contenttype >[2]/dev/null} *bzip2* || exec bunzip2
case *compress*
~ `{cat $d/contenttype >[2]/dev/null} *compress* || exec uncompress
}
exec cat
}
}