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