From 399438e4a78777db9230968ef5902915b4c814ce Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 29 Sep 2011 19:13:27 +0100 Subject: Post to goToPage amazon url; more reliable for some reason --- getabook.c | 6 ++++-- util.c | 38 ++++++++++++++++++++++++++++++++++++++ util.h | 1 + 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/getabook.c b/getabook.c index 03552a7..ac88f6d 100644 --- a/getabook.c +++ b/getabook.c @@ -91,11 +91,13 @@ int getpagelist() int getpageurls(int pagenum) { char url[URLMAX]; + char query[URLMAX]; char *buf = NULL; - snprintf(url, URLMAX, "/gp/search-inside/service-data?method=goToPage&asin=%s&page=%d", bookid, pagenum); + strncpy(url, "/gp/search-inside/service-data", URLMAX); + snprintf(query, URLMAX, "method=goToPage&asin=%s&page=%d", bookid, pagenum); - if(!get("www.amazon.com", url, NULL, NULL, &buf)) + if(!post("www.amazon.com", url, query, &buf)) return 1; fillurls(buf); diff --git a/util.c b/util.c index 6cc0075..e8badae 100644 --- a/util.c +++ b/util.c @@ -120,3 +120,41 @@ int gettofile(char *host, char *url, char *sendcookie, char *savecookie, char *s return 0; } + +int post(char *host, char *path, char *data, char **buf) { + size_t l, res; + int fd, i, p; + char h[HDRMAX] = ""; + char t[BUFSIZ]; + char *t2; + + if((fd = dial(host, "80")) == -1) return 0; + + snprintf(h, HDRMAX, "POST %s HTTP/1.0\r\nUser-Agent: getxbook-"VERSION \ + " (not mozilla)\r\nContent-Length: %d\r\n" \ + "Content-Type: application/x-www-form-urlencoded\r\n" \ + "Host: %s\r\n\r\n%s\r\n", + path, strlen(data), host, data); + if(!send(fd, h, strlen(h), 0)) return 0; + + *buf = NULL; + l = 0; + while((res = recv(fd, t, BUFSIZ, 0)) > 0) { + if(sscanf(t, "HTTP/%d.%d %d", &i, &i, &p) == 3 && p != 200) + return 0; + t2 = t; + if((t2 = strstr(t, "\r\n\r\n")) != NULL && (t2 - t) < (signed)res) { + t2+=4; + l = res - (t2 - t); + *buf = malloc(sizeof(char *) * l); + memcpy(*buf, t2, l); + break; + } + } + + *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)); + + return l; +} diff --git a/util.h b/util.h index 1a82a80..235303b 100644 --- a/util.h +++ b/util.h @@ -4,3 +4,4 @@ int dial(char *host, char *port); int get(char *host, char *path, char *sendcookie, char *savecookie, char **buf); int gettofile(char *host, char *url, char *sendcookie, char *savecookie, char *savepath); +int post(char *host, char *path, char *data, char **buf); -- cgit v1.2.3