#include <u.h>
#include <libc.h>
#include <bio.h>
Biobuf stdin, stdout;
char* buf;
int linelen, chunked=0;
char* tec = "Transfer-Encoding: chunked\r\n";
char *
readline(void) {
if(buf = Brdline(&stdin, '\n'))
return buf;
exits("eof stdin");
}
void
read_plain(void) {
while(buf = Brdline(&stdin, '\n'))
Bwrite(&stdout, buf, Blinelen(&stdin));
}
void
read_chunked(void) {
long chunksize;
void* rbuff;
int bread;
readline();
buf[Blinelen(&stdin) -2] = 0;
chunksize = strtol(buf, 0, 16);
rbuff = malloc(1024);
do {
while(chunksize > 0) {
bread = Bread(&stdin, rbuff, 1024L < chunksize ? 1024L : chunksize);
if(bread < 0)
exits("EOF stdin");
Bwrite(&stdout, rbuff, bread);
chunksize -= bread;
}
readline();
readline();
buf[Blinelen(&stdin) -2] = 0;
chunksize = strtol(buf, 0, 16);
} while(chunksize > 0);
free(rbuff);
}
void
main(int argc, char *argv[])
{
Binit(&stdin, 0, OREAD);
Binit(&stdout, 1, OWRITE);
while(readline()) {
linelen = Blinelen(&stdin);
if(linelen == 2) // blank line
break;
if(linelen == strlen(tec) && strncmp(tec, buf, strlen(tec)) == 0)
chunked = 1;
else
Bwrite(&stdout, buf, linelen);
}
if(buf) {
Bwrite(&stdout, buf, linelen);
chunked ? read_chunked() : read_plain();
}
Bterm(&stdin);
Bterm(&stdout);
exits(0);
}
// 8c dechunk.c && 8l dechunk.8 && mv 8.out dechunk
// dechunk < chunked.html
|