From 77f8c2afc7f6a9e3f28585810104be7f13cc3503 Mon Sep 17 00:00:00 2001 From: Nick White Date: Sat, 16 Jul 2011 15:00:00 +0100 Subject: Simplify header processing --- util.c | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index d9aaa54..ab84663 100644 --- a/util.c +++ b/util.c @@ -37,38 +37,31 @@ static int dial(char *host, char *port) { int get(char *host, char *path, char **gotbuf) { size_t l, res; int fd, i; - char *buf, *c, *p; + char *buf; + char h[1024] = "\0"; FILE *srv; fd = dial(host, "80"); srv = fdopen(fd, "r+"); - fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getgbook-"VERSION" (not mozilla)\r\nHost: %s\r\n\r\n", path, host); + fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getgbook-"VERSION \ + " (not mozilla)\r\nHost: %s\r\n\r\n", path, host); fflush(srv); - buf = malloc(sizeof(char *) * 4096); - for(i=0, l=0; (res = read(fd, buf+l, 4096)) > 0; l+=res, i++) - buf = realloc(buf, sizeof(char *) * (l+4096)); - - /* check that it's a 200 */ - if(strncmp(buf+9, "200 ", 4)) { - free(buf); - return 0; + /* process headers */ + while(h[0] != '\r') { + fgets(h, 1024, srv); + if(sscanf(h, "HTTP/%d.%d %d", &i, &i, &l) == 3 && l != 200) + return 1; } - /* exclude header */ - for(p = buf; *p && *(p+1) && *(p+2) && *(p+3); p++) - if(!strncmp(p, "\r\n\r\n", 4)) break; - p+=4; - - i = l - (p - buf); - c = malloc(i+1); - memcpy(c, p, i); - free(buf); + buf = malloc(sizeof(char *) * 4096); + for(i=0, l=0; (res = fread(buf+l, 1, 4096, srv)) > 0; l+=res, i++) + buf = realloc(buf, sizeof(char *) * (l+4096)); - *gotbuf = c; + *gotbuf = buf; - return i; + return l; } int gettofile(char *url, char *savepath) { @@ -80,7 +73,6 @@ int gettofile(char *url, char *savepath) { fprintf(stderr, "Could not download %s\n", url); return 1; } - if((f = fopen(savepath, "w")) == NULL) { fprintf(stderr, "Could not create file %s\n", savepath); return 1; -- cgit v1.2.3