-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalext_test.c
219 lines (189 loc) · 5.23 KB
/
alext_test.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// module: alex_test.c
// runs Google search and prints out first URL and Title found
// author: Alex Fok
// modules:
// cJSON.c - JSON parser
// alext_test.c - main program
// compilation: gcc cJSON.c alext_test.c -o alext_test
// usage: alex_test "video%20chat"
// sample search URL: "ajax/services/search/web?v=1.0&q=Earth%20Day"
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include "cJSON.h"
void parseJSON(char *text);
int create_tcp_socket();
char *get_ip(char *host);
char *build_get_query(char *host, char *page);
void usage();
#define HOST "ajax.googleapis.com"
#define SEARCH_URL "ajax/services/search/web?v=1.0&q="
#define PORT 80
#define USERAGENT "ALEXGET 1.0"
#define RES_MAX_SIZE 4096
#define URL_MAX_SIZE 1024
int main(int argc, char **argv)
{
struct sockaddr_in *remote;
int sock;
int tmpres;
char *ip;
char *get;
char buf[BUFSIZ+1];
char *host;
char page[URL_MAX_SIZE];
char json_res[RES_MAX_SIZE];
if(argc != 2){
usage();
exit(2);
}
host = HOST;
if(argc == 2){
snprintf(page, URL_MAX_SIZE,"%s%s", SEARCH_URL,argv[1]);
}
sock = create_tcp_socket();
ip = get_ip(host);
fprintf(stderr, "IP is %s\n", ip);
remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *));
remote->sin_family = AF_INET;
tmpres = inet_pton(AF_INET, ip, (void *)(&(remote->sin_addr.s_addr)));
if( tmpres < 0)
{
perror("Can't set remote->sin_addr.s_addr");
exit(1);
}else if(tmpres == 0)
{
fprintf(stderr, "%s is not a valid IP address\n", ip);
exit(1);
}
remote->sin_port = htons(PORT);
if(connect(sock, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0){
perror("Could not connect");
exit(1);
}
get = build_get_query(host, page);
fprintf(stderr, "Query is:\n<<START>>\n%s<<END>>\n", get);
//Send the query to the server
int sent = 0;
while(sent < strlen(get))
{
tmpres = send(sock, get+sent, strlen(get)-sent, 0);
if(tmpres == -1){
perror("Can't send query");
exit(1);
}
sent += tmpres;
}
//now it is time to receive the page
memset(buf, 0, sizeof(buf));
// Buffer for page chunks aggregation
memset(json_res, 0, sizeof(json_res));
int htmlstart = 0;
char * htmlcontent;
while((tmpres = recv(sock, buf, BUFSIZ, 0)) > 0){
if(htmlstart == 0)
{
/* Under certain conditions this will not work.
* If the \r\n\r\n part is splitted into two messages
* it will fail to detect the beginning of HTML content
*/
htmlcontent = strstr(buf, "\r\n\r\n");
if(htmlcontent != NULL){
htmlstart = 1;
htmlcontent += 4;
}
}else{
htmlcontent = buf;
}
if(htmlstart){
/* fprintf(stdout, "%s", htmlcontent);*/
// fprintf(stdout,"\n\njson2: %s, \n\nhtml2: %s\n\n",json_res, htmlcontent);
strncat(json_res, htmlcontent, RES_MAX_SIZE);
}
memset(buf, 0, tmpres);
}
// fprintf(stdout, "%s", json_res);
/* Parse JSON result */
parseJSON(json_res);
if(tmpres < 0)
{
perror("Error receiving data");
return -1;
}
free(get);
free(remote);
free(ip);
close(sock);
return 0;
}
/* Parse text to JSON, then render back to text, and print! */
void parseJSON(char *text)
{
char *out;cJSON *json;
json=cJSON_Parse(text);
if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}
else
{
cJSON *responseData = cJSON_GetObjectItem(json,"responseData");
cJSON *results = cJSON_GetObjectItem(responseData,"results");
cJSON *firstRes = results->child;
char* str_value = cJSON_GetObjectItem(firstRes,"unescapedUrl")->valuestring;
char* str_value1 = cJSON_GetObjectItem(firstRes,"titleNoFormatting")->valuestring;
out=cJSON_Print(firstRes);
fprintf(stdout,"First URL: %s\n", str_value);
fprintf(stdout,"First Title: %s\n", str_value1);
// out=cJSON_Print(json);
cJSON_Delete(json);
// printf("%s\n",out);
free(out);
}
}
void usage()
{
fprintf(stderr, "USAGE: alex_test search_term\n\
\tsearch_term: the search term. ex: video, default: video\n");
}
int create_tcp_socket()
{
int sock;
if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
perror("Can't create TCP socket");
exit(1);
}
return sock;
}
char *get_ip(char *host)
{
struct hostent *hent;
int iplen = 15; //XXX.XXX.XXX.XXX
char *ip = (char *)malloc(iplen+1);
memset(ip, 0, iplen+1);
if((hent = gethostbyname(host)) == NULL)
{
herror("Can't get IP");
exit(1);
}
if(inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, iplen) == NULL)
{
perror("Can't resolve host");
exit(1);
}
return ip;
}
char *build_get_query(char *host, char *page)
{
char *query;
char *getpage = page;
char *tpl = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n";
if(getpage[0] == '/'){
getpage = getpage + 1;
fprintf(stderr,"Removing leading \"/\", converting %s to %s\n", page, getpage);
}
// -5 is to consider the %s %s %s in tpl and the ending \0
query = (char *)malloc(strlen(host)+strlen(getpage)+strlen(USERAGENT)+strlen(tpl)-5);
sprintf(query, tpl, getpage, host, USERAGENT);
return query;
}