From 5f2172cc8ca50cea7d09b4a569f958a3015f33e7 Mon Sep 17 00:00:00 2001 From: Nick White Date: Sat, 16 Jul 2011 01:15:26 +0100 Subject: Simplify http get code --- getgbook.c | 15 +++------------ util.c | 7 +++++-- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/getgbook.c b/getgbook.c index bb809d4..ae47993 100644 --- a/getgbook.c +++ b/getgbook.c @@ -33,13 +33,8 @@ pgtype pgtypes[] = { char *getbookid(char *isbn) { char url[URLMAX]; - int i; - FILE *srv; char *buf, *bookid, *c; - i = dial("books.google.com", "80"); - srv = fdopen(i, "r+"); - /* NOTE: new api returns json, and looks like this: * http://www.googleapis.com/books/v1/volumes?q=isbn:1589235126 * (this needs https, which we don't yet support) */ @@ -48,7 +43,7 @@ char *getbookid(char *isbn) bookid = malloc(sizeof(char *) * BOOKID_LEN); - if((buf = get(srv, "books.google.com", url)) == NULL) + if((buf = get("books.google.com", url)) == NULL) return NULL; if((c = strstr(buf,"")) == NULL) @@ -63,16 +58,12 @@ char *getbookid(char *isbn) char *getpageurl(char *bookid, char *pg) { char url[URLMAX]; - int i, l; - FILE *srv; + int l; char *buf, *c, *d, m[80], *pageurl; - i = dial("books.google.com", "80"); - srv = fdopen(i, "r+"); - snprintf(url, URLMAX, "/books?id=%s&pg=%s&jscmd=click3", bookid, pg); - if((buf = get(srv, "books.google.com", url)) == NULL) + if((buf = get("books.google.com", url)) == NULL) return NULL; snprintf(m, 80, "\"pid\":\"%s\"", pg); diff --git a/util.c b/util.c index 5c3cb23..fc5c5c5 100644 --- a/util.c +++ b/util.c @@ -34,17 +34,20 @@ static int dial(char *host, char *port) { return srv; } -char *get(FILE *srv, char *host, char *path) { +char *get(char *host, char *path) { size_t l, res; int fd, i; char *buf, *c, *p; + FILE *srv; + + fd = dial("books.google.com", "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); fflush(srv); l=0; - fd = fileno(srv); buf = malloc(sizeof(char *) * 4096); for(i=0; (res = read(fd, buf+l, 4096)) > 0; l+=res, i++) -- cgit v1.2.3