From 7638ce1addb79e4f353f33c1c3ac1be0a11cc7f1 Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 18 Aug 2011 10:38:13 +0100 Subject: Get socket stuff working fully, fix bug in cookie sending --- getgbook.c | 1 + util.c | 14 +++++--------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/getgbook.c b/getgbook.c index 5f0a0ae..cd12c86 100644 --- a/getgbook.c +++ b/getgbook.c @@ -52,6 +52,7 @@ Page *getpagedetail(char *bookid, char *pg, char *cookie) *p = *d; } strncpy(p, "&q=subject:a", 12); + *(p+12) = '\0'; } else d=c; diff --git a/util.c b/util.c index 5b323ff..11d89fe 100644 --- a/util.c +++ b/util.c @@ -49,10 +49,9 @@ int get(char *host, char *path, char *sendcookie, char *savecookie, char **buf) char t[BUFSIZ]; char *t2; - /* TODO: should socket be closed after use? */ if((fd = dial(host, "80")) == -1) return 0; - if(sendcookie) + if(sendcookie && sendcookie[0]) snprintf(c, COOKIEMAX, "\r\nCookie: %s", sendcookie); snprintf(h, HDRMAX, "GET %s HTTP/1.0\r\nUser-Agent: getxbook-"VERSION \ " (not mozilla)\r\nHost: %s%s\r\n\r\n", path, host, c); @@ -60,27 +59,24 @@ int get(char *host, char *path, char *sendcookie, char *savecookie, char **buf) *buf = NULL; l = 0; - while(recv(fd, t, 1024, 0) > 0) { + while((res = recv(fd, t, 1024, 0)) > 0) { if(sscanf(t, "HTTP/%d.%d %d", &i, &i, &p) == 3 && p != 200) return 0; if(savecookie != NULL && sscanf(t, "Set-Cookie: %s;", c)) strncat(savecookie, c, COOKIEMAX); if((t2 = strstr(t, "\r\n\r\n") + 4)) { - l = strlen(t2); + *(t2-1) = '\0'; + l = res - strlen(t) - 1; *buf = malloc(sizeof(char *) * l); - strncpy(*buf, t2, l); + memcpy(*buf, t2, l); break; } } - printf("to start, got %d (%d) bytes:\n%s\n", l, strlen(*buf), *buf); - *buf = realloc(*buf, sizeof(char *) * (l+BUFSIZ)); for(; (res = recv(fd, *buf+l, BUFSIZ, 0)) > 0; l+=res) *buf = realloc(*buf, sizeof(char *) * (l+BUFSIZ)); - printf("got %d bytes (%d):\n%s\n", l, strlen(*buf), *buf); - return l; } -- cgit v1.2.3