1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#define usage "getgbook bookid"
#define hostname "books.google.com"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.c"
char * get(FILE *srv, char *host, char *path) {
size_t l, res;
int fd, i;
char *buf, *c, *p;
fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getgbook-prealpha (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++)
buf = realloc(buf, sizeof(char *) * (l+4096));
/* check that it's a 200 */
if(strncmp(buf+9, "200 ", 4)) {
free(buf);
return NULL;
}
/* 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);
memcpy(c, p, i);
free(buf);
return c;
}
int main(int argc, char *argv[])
{
int i, s;
char *bookid, url[80];
char *curpage;
FILE *srv;
if(argc != 2)
die("usage: " usage "\n");
bookid = argv[1];
i = dial(hostname, "80");
srv = fdopen(i, "r+");
snprintf(url, 80, "/books?id=%s&pg=%s&jscmd=click3", bookid, "PA1");
if((curpage = get(srv, hostname, url)) == NULL)
fprintf(stderr,"Error downloading page\n");
else {
fputs(curpage,stdout);
free(curpage);
}
return 0;
}
|