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