From dacf8ff0900bfd0356dfab733661ddec8349f60e Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Sat, 11 Nov 2017 21:17:55 +0000 Subject: [PATCH] htget: Fix various bugs in data processing With this I can now htget a file and actually get what appear to be all the right bits the other end. --- Applications/netd/htget.c | 40 +++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/Applications/netd/htget.c b/Applications/netd/htget.c index 14aa19a5..103eefb6 100644 --- a/Applications/netd/htget.c +++ b/Applications/netd/htget.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -41,7 +42,17 @@ void xwrites(const char *p) int xread(void) { - int len = read(sock, buf, 512); + int len; + + /* The first call we return the stray bytes from the line parser */ + if (bufend != buf && bufend > readp) { + len = bufend - readp; + memcpy(buf, readp, bufend - readp); + bufend = buf; + readp = buf; + return len; + } + len = read(sock, buf, 512); if (len < 0) { perror("read"); exit(1); @@ -89,13 +100,13 @@ int main(int argc, char *argv[]) uint16_t port = 80; char *pp; char *fp; - int of = 1; + int of; int code; int len; uint8_t looped = 0; - if (argc != 2) { - writes(2, "htget url\n"); + if (argc != 3) { + writes(2, "htget url file\n"); exit(1); } if (strncmp(argv[1], "http://", 7)) { @@ -103,6 +114,11 @@ int main(int argc, char *argv[]) exit(2); } argv[1] += 7; + + fp = strchr(argv[1], '/'); + if (fp) + *fp++ = 0; + pp = strrchr(argv[1], ':'); if (pp) { *pp++ = 0; @@ -113,9 +129,6 @@ int main(int argc, char *argv[]) } } - fp = strchr(argv[1], '/'); - if (fp) - *fp = 0; sin.sin_family = AF_INET; sin.sin_port = htons(port); @@ -182,6 +195,12 @@ int main(int argc, char *argv[]) if (code == 100) xreadline(); } while (code == 100 && !looped++); + + of = open(argv[2], O_WRONLY|O_CREAT, 0666); + if (of == -1) { + perror(argv[2]); + exit(1); + } /* FIXME: if we saw a Transfer-Encoding: chunked" we need to do this bit differently */ if (code == 200) { @@ -190,10 +209,11 @@ int main(int argc, char *argv[]) perror("write"); exit(1); } + write(1,".",1); } } + write(1,"\n",1); + close(of); + close(sock); return 0; } - - - -- 2.34.1