1 | /* |
---|
2 | * libyahoo2: yahoo_httplib.c |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | * |
---|
20 | */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | |
---|
25 | #if STDC_HEADERS |
---|
26 | # include <string.h> |
---|
27 | #else |
---|
28 | # if !HAVE_STRCHR |
---|
29 | # define strchr index |
---|
30 | # define strrchr rindex |
---|
31 | # endif |
---|
32 | char *strchr(), *strrchr(); |
---|
33 | # if !HAVE_MEMCPY |
---|
34 | # define memcpy(d, s, n) bcopy ((s), (d), (n)) |
---|
35 | # define memmove(d, s, n) bcopy ((s), (d), (n)) |
---|
36 | # endif |
---|
37 | #endif |
---|
38 | |
---|
39 | #include <errno.h> |
---|
40 | #ifndef _WIN32 |
---|
41 | #include <unistd.h> |
---|
42 | #endif |
---|
43 | #include <ctype.h> |
---|
44 | #include "yahoo2.h" |
---|
45 | #include "yahoo2_callbacks.h" |
---|
46 | #include "yahoo_httplib.h" |
---|
47 | #include "yahoo_util.h" |
---|
48 | |
---|
49 | #include "yahoo_debug.h" |
---|
50 | #ifdef __MINGW32__ |
---|
51 | # include <winsock2.h> |
---|
52 | # define snprintf _snprintf |
---|
53 | #endif |
---|
54 | |
---|
55 | #ifdef USE_STRUCT_CALLBACKS |
---|
56 | extern struct yahoo_callbacks *yc; |
---|
57 | #define YAHOO_CALLBACK(x) yc->x |
---|
58 | #else |
---|
59 | #define YAHOO_CALLBACK(x) x |
---|
60 | #endif |
---|
61 | |
---|
62 | extern enum yahoo_log_level log_level; |
---|
63 | |
---|
64 | int yahoo_tcp_readline(char *ptr, int maxlen, void *fd) |
---|
65 | { |
---|
66 | int n, rc; |
---|
67 | char c; |
---|
68 | |
---|
69 | for (n = 1; n < maxlen; n++) { |
---|
70 | |
---|
71 | do { |
---|
72 | rc = YAHOO_CALLBACK(ext_yahoo_read) (fd, &c, 1); |
---|
73 | } while (rc == -1 && (errno == EINTR || errno == EAGAIN)); /* this is bad - it should be done asynchronously */ |
---|
74 | |
---|
75 | if (rc == 1) { |
---|
76 | if (c == '\r') /* get rid of \r */ |
---|
77 | continue; |
---|
78 | *ptr = c; |
---|
79 | if (c == '\n') |
---|
80 | break; |
---|
81 | ptr++; |
---|
82 | } else if (rc == 0) { |
---|
83 | if (n == 1) |
---|
84 | return (0); /* EOF, no data */ |
---|
85 | else |
---|
86 | break; /* EOF, w/ data */ |
---|
87 | } else { |
---|
88 | return -1; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | *ptr = 0; |
---|
93 | return (n); |
---|
94 | } |
---|
95 | |
---|
96 | static int url_to_host_port_path(const char *url, |
---|
97 | char *host, int *port, char *path, int *ssl) |
---|
98 | { |
---|
99 | char *urlcopy = NULL; |
---|
100 | char *slash = NULL; |
---|
101 | char *colon = NULL; |
---|
102 | |
---|
103 | /* |
---|
104 | * http://hostname |
---|
105 | * http://hostname/ |
---|
106 | * http://hostname/path |
---|
107 | * http://hostname/path:foo |
---|
108 | * http://hostname:port |
---|
109 | * http://hostname:port/ |
---|
110 | * http://hostname:port/path |
---|
111 | * http://hostname:port/path:foo |
---|
112 | * and https:// variants of the above |
---|
113 | */ |
---|
114 | |
---|
115 | if (strstr(url, "http://") == url) { |
---|
116 | urlcopy = strdup(url + 7); |
---|
117 | } else if (strstr(url, "https://") == url) { |
---|
118 | urlcopy = strdup(url + 8); |
---|
119 | *ssl = 1; |
---|
120 | } else { |
---|
121 | WARNING(("Weird url - unknown protocol: %s", url)); |
---|
122 | return 0; |
---|
123 | } |
---|
124 | |
---|
125 | slash = strchr(urlcopy, '/'); |
---|
126 | colon = strchr(urlcopy, ':'); |
---|
127 | |
---|
128 | if (!colon || (slash && slash < colon)) { |
---|
129 | if (*ssl) |
---|
130 | *port = 443; |
---|
131 | else |
---|
132 | *port = 80; |
---|
133 | } else { |
---|
134 | *colon = 0; |
---|
135 | *port = atoi(colon + 1); |
---|
136 | } |
---|
137 | |
---|
138 | if (!slash) { |
---|
139 | strcpy(path, "/"); |
---|
140 | } else { |
---|
141 | strcpy(path, slash); |
---|
142 | *slash = 0; |
---|
143 | } |
---|
144 | |
---|
145 | strcpy(host, urlcopy); |
---|
146 | |
---|
147 | FREE(urlcopy); |
---|
148 | |
---|
149 | return 1; |
---|
150 | } |
---|
151 | |
---|
152 | static int isurlchar(unsigned char c) |
---|
153 | { |
---|
154 | return (isalnum(c)); |
---|
155 | } |
---|
156 | |
---|
157 | char *yahoo_urlencode(const char *instr) |
---|
158 | { |
---|
159 | int ipos = 0, bpos = 0; |
---|
160 | char *str = NULL; |
---|
161 | int len = strlen(instr); |
---|
162 | |
---|
163 | if (!(str = y_new(char, 3 *len + 1))) |
---|
164 | return ""; |
---|
165 | |
---|
166 | while (instr[ipos]) { |
---|
167 | while (isurlchar(instr[ipos])) |
---|
168 | str[bpos++] = instr[ipos++]; |
---|
169 | if (!instr[ipos]) |
---|
170 | break; |
---|
171 | |
---|
172 | snprintf(&str[bpos], 4, "%%%02x", instr[ipos] & 0xff); |
---|
173 | bpos += 3; |
---|
174 | ipos++; |
---|
175 | } |
---|
176 | str[bpos] = '\0'; |
---|
177 | |
---|
178 | /* free extra alloc'ed mem. */ |
---|
179 | len = strlen(str); |
---|
180 | str = y_renew(char, str, len + 1); |
---|
181 | |
---|
182 | return (str); |
---|
183 | } |
---|
184 | |
---|
185 | char *yahoo_urldecode(const char *instr) |
---|
186 | { |
---|
187 | int ipos = 0, bpos = 0; |
---|
188 | char *str = NULL; |
---|
189 | char entity[3] = { 0, 0, 0 }; |
---|
190 | unsigned dec; |
---|
191 | int len = strlen(instr); |
---|
192 | |
---|
193 | if (!(str = y_new(char, len + 1))) |
---|
194 | return ""; |
---|
195 | |
---|
196 | while (instr[ipos]) { |
---|
197 | while (instr[ipos] && instr[ipos] != '%') |
---|
198 | if (instr[ipos] == '+') { |
---|
199 | str[bpos++] = ' '; |
---|
200 | ipos++; |
---|
201 | } else |
---|
202 | str[bpos++] = instr[ipos++]; |
---|
203 | if (!instr[ipos]) |
---|
204 | break; |
---|
205 | |
---|
206 | if (instr[ipos + 1] && instr[ipos + 2]) { |
---|
207 | ipos++; |
---|
208 | entity[0] = instr[ipos++]; |
---|
209 | entity[1] = instr[ipos++]; |
---|
210 | sscanf(entity, "%2x", &dec); |
---|
211 | str[bpos++] = (char)dec; |
---|
212 | } else { |
---|
213 | str[bpos++] = instr[ipos++]; |
---|
214 | } |
---|
215 | } |
---|
216 | str[bpos] = '\0'; |
---|
217 | |
---|
218 | /* free extra alloc'ed mem. */ |
---|
219 | len = strlen(str); |
---|
220 | str = y_renew(char, str, len + 1); |
---|
221 | |
---|
222 | return (str); |
---|
223 | } |
---|
224 | |
---|
225 | char *yahoo_xmldecode(const char *instr) |
---|
226 | { |
---|
227 | int ipos = 0, bpos = 0, epos = 0; |
---|
228 | char *str = NULL; |
---|
229 | char entity[4] = { 0, 0, 0, 0 }; |
---|
230 | char *entitymap[5][2] = { |
---|
231 | {"amp;", "&"}, |
---|
232 | {"quot;", "\""}, |
---|
233 | {"lt;", "<"}, |
---|
234 | {"gt;", "<"}, |
---|
235 | {"nbsp;", " "} |
---|
236 | }; |
---|
237 | unsigned dec; |
---|
238 | int len = strlen(instr); |
---|
239 | |
---|
240 | if (!(str = y_new(char, len + 1))) |
---|
241 | return ""; |
---|
242 | |
---|
243 | while (instr[ipos]) { |
---|
244 | while (instr[ipos] && instr[ipos] != '&') |
---|
245 | if (instr[ipos] == '+') { |
---|
246 | str[bpos++] = ' '; |
---|
247 | ipos++; |
---|
248 | } else |
---|
249 | str[bpos++] = instr[ipos++]; |
---|
250 | if (!instr[ipos] || !instr[ipos + 1]) |
---|
251 | break; |
---|
252 | ipos++; |
---|
253 | |
---|
254 | if (instr[ipos] == '#') { |
---|
255 | ipos++; |
---|
256 | epos = 0; |
---|
257 | while (instr[ipos] != ';') |
---|
258 | entity[epos++] = instr[ipos++]; |
---|
259 | sscanf(entity, "%u", &dec); |
---|
260 | str[bpos++] = (char)dec; |
---|
261 | ipos++; |
---|
262 | } else { |
---|
263 | int i; |
---|
264 | for (i = 0; i < 5; i++) |
---|
265 | if (!strncmp(instr + ipos, entitymap[i][0], |
---|
266 | strlen(entitymap[i][0]))) { |
---|
267 | str[bpos++] = entitymap[i][1][0]; |
---|
268 | ipos += strlen(entitymap[i][0]); |
---|
269 | break; |
---|
270 | } |
---|
271 | } |
---|
272 | } |
---|
273 | str[bpos] = '\0'; |
---|
274 | |
---|
275 | /* free extra alloc'ed mem. */ |
---|
276 | len = strlen(str); |
---|
277 | str = y_renew(char, str, len + 1); |
---|
278 | |
---|
279 | return (str); |
---|
280 | } |
---|
281 | |
---|
282 | typedef void (*http_connected) (int id, void *fd, int error); |
---|
283 | |
---|
284 | struct callback_data { |
---|
285 | int id; |
---|
286 | yahoo_get_fd_callback callback; |
---|
287 | char *request; |
---|
288 | void *user_data; |
---|
289 | }; |
---|
290 | |
---|
291 | static void connect_complete(void *fd, int error, void *data) |
---|
292 | { |
---|
293 | struct callback_data *ccd = data; |
---|
294 | if (error == 0) |
---|
295 | YAHOO_CALLBACK(ext_yahoo_write) (fd, ccd->request, |
---|
296 | strlen(ccd->request)); |
---|
297 | free(ccd->request); |
---|
298 | ccd->callback(ccd->id, fd, error, ccd->user_data); |
---|
299 | FREE(ccd); |
---|
300 | } |
---|
301 | |
---|
302 | static void yahoo_send_http_request(int id, char *host, int port, char *request, |
---|
303 | yahoo_get_fd_callback callback, void *data, int use_ssl) |
---|
304 | { |
---|
305 | struct callback_data *ccd = y_new0(struct callback_data, 1); |
---|
306 | ccd->callback = callback; |
---|
307 | ccd->id = id; |
---|
308 | ccd->request = strdup(request); |
---|
309 | ccd->user_data = data; |
---|
310 | |
---|
311 | YAHOO_CALLBACK(ext_yahoo_connect_async) (id, host, port, |
---|
312 | connect_complete, ccd, use_ssl); |
---|
313 | } |
---|
314 | |
---|
315 | void yahoo_http_post(int id, const char *url, const char *cookies, |
---|
316 | long content_length, yahoo_get_fd_callback callback, void *data) |
---|
317 | { |
---|
318 | char host[255]; |
---|
319 | int port = 80; |
---|
320 | char path[255]; |
---|
321 | char buff[1024]; |
---|
322 | int ssl = 0; |
---|
323 | |
---|
324 | if (!url_to_host_port_path(url, host, &port, path, &ssl)) |
---|
325 | return; |
---|
326 | |
---|
327 | /* thanks to kopete dumpcap */ |
---|
328 | snprintf(buff, sizeof(buff), |
---|
329 | "POST %s HTTP/1.1\r\n" |
---|
330 | "Cookie: %s\r\n" |
---|
331 | "User-Agent: Mozilla/5.0\r\n" |
---|
332 | "Host: %s\r\n" |
---|
333 | "Content-Length: %ld\r\n" |
---|
334 | "Cache-Control: no-cache\r\n" |
---|
335 | "\r\n", path, cookies, host, content_length); |
---|
336 | |
---|
337 | yahoo_send_http_request(id, host, port, buff, callback, data, ssl); |
---|
338 | } |
---|
339 | |
---|
340 | void yahoo_http_get(int id, const char *url, const char *cookies, int http11, |
---|
341 | int keepalive, yahoo_get_fd_callback callback, void *data) |
---|
342 | { |
---|
343 | char host[255]; |
---|
344 | int port = 80; |
---|
345 | char path[255]; |
---|
346 | char buff[2048]; |
---|
347 | char cookiebuff[1024]; |
---|
348 | int ssl = 0; |
---|
349 | |
---|
350 | if (!url_to_host_port_path(url, host, &port, path, &ssl)) |
---|
351 | return; |
---|
352 | |
---|
353 | /* Allow cases when we don't need to send a cookie */ |
---|
354 | if (cookies) |
---|
355 | snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n", |
---|
356 | cookies); |
---|
357 | else |
---|
358 | cookiebuff[0] = '\0'; |
---|
359 | |
---|
360 | snprintf(buff, sizeof(buff), |
---|
361 | "GET %s HTTP/1.%s\r\n" |
---|
362 | "%sHost: %s\r\n" |
---|
363 | "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n" |
---|
364 | "Accept: */*\r\n" |
---|
365 | "%s" "\r\n", path, http11?"1":"0", cookiebuff, host, |
---|
366 | keepalive? "Connection: Keep-Alive\r\n":"Connection: close\r\n"); |
---|
367 | |
---|
368 | yahoo_send_http_request(id, host, port, buff, callback, data, ssl); |
---|
369 | } |
---|
370 | |
---|
371 | void yahoo_http_head(int id, const char *url, const char *cookies, int len, |
---|
372 | char *payload, yahoo_get_fd_callback callback, void *data) |
---|
373 | { |
---|
374 | char host[255]; |
---|
375 | int port = 80; |
---|
376 | char path[255]; |
---|
377 | char buff[2048]; |
---|
378 | char cookiebuff[1024]; |
---|
379 | int ssl = 0; |
---|
380 | |
---|
381 | if (!url_to_host_port_path(url, host, &port, path, &ssl)) |
---|
382 | return; |
---|
383 | |
---|
384 | /* Allow cases when we don't need to send a cookie */ |
---|
385 | if (cookies) |
---|
386 | snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n", |
---|
387 | cookies); |
---|
388 | else |
---|
389 | cookiebuff[0] = '\0'; |
---|
390 | |
---|
391 | snprintf(buff, sizeof(buff), |
---|
392 | "HEAD %s HTTP/1.0\r\n" |
---|
393 | "Accept: */*\r\n" |
---|
394 | "Host: %s:%d\r\n" |
---|
395 | "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n" |
---|
396 | "%s" |
---|
397 | "Content-Length: %d\r\n" |
---|
398 | "Cache-Control: no-cache\r\n" |
---|
399 | "\r\n%s", path, host, port, cookiebuff, len, |
---|
400 | payload?payload:""); |
---|
401 | |
---|
402 | yahoo_send_http_request(id, host, port, buff, callback, data, ssl); |
---|
403 | } |
---|
404 | |
---|