Changeset 5ebff60 for protocols/yahoo/yahoo_httplib.c
- Timestamp:
- 2015-02-20T22:50:54Z (9 years ago)
- Branches:
- master
- Children:
- 0b9daac, 3d45471, 7733b8c
- Parents:
- af359b4
- git-author:
- Indent <please@…> (19-02-15 05:47:20)
- git-committer:
- dequis <dx@…> (20-02-15 22:50:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/yahoo/yahoo_httplib.c
raf359b4 r5ebff60 32 32 char *strchr(), *strrchr(); 33 33 # if !HAVE_MEMCPY 34 # define memcpy(d, s, n) bcopy 35 # define memmove(d, s, n) bcopy 34 # define memcpy(d, s, n) bcopy((s), (d), (n)) 35 # define memmove(d, s, n) bcopy((s), (d), (n)) 36 36 # endif 37 37 #endif … … 53 53 #ifdef USE_STRUCT_CALLBACKS 54 54 extern struct yahoo_callbacks *yc; 55 #define YAHOO_CALLBACK(x) 55 #define YAHOO_CALLBACK(x) yc->x 56 56 #else 57 #define YAHOO_CALLBACK(x) 57 #define YAHOO_CALLBACK(x) x 58 58 #endif 59 59 … … 69 69 70 70 do { 71 rc = YAHOO_CALLBACK (ext_yahoo_read) (fd, &c, 1);72 } while (rc == -1 && (errno == EINTR || errno == EAGAIN)); 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 */ 73 73 74 74 if (rc == 1) { 75 if (c == '\r') 75 if (c == '\r') { /* get rid of \r */ 76 76 continue; 77 } 77 78 *ptr = c; 78 if (c == '\n') 79 if (c == '\n') { 79 80 break; 81 } 80 82 ptr++; 81 83 } else if (rc == 0) { 82 if (n == 1) 83 return (0); /* EOF, no data */ 84 else 85 break; /* EOF, w/ data */ 84 if (n == 1) { 85 return (0); /* EOF, no data */ 86 } else { 87 break; /* EOF, w/ data */ 88 } 86 89 } else { 87 90 return -1; … … 95 98 96 99 static int url_to_host_port_path(const char *url, 97 100 char *host, int *port, char *path, int *ssl) 98 101 { 99 102 char *urlcopy = NULL; … … 127 130 128 131 if (!colon || (slash && slash < colon)) { 129 if (*ssl) 132 if (*ssl) { 130 133 *port = 443; 131 else134 } else { 132 135 *port = 80; 136 } 133 137 } else { 134 138 *colon = 0; … … 161 165 int len = strlen(instr); 162 166 163 if (!(str = y_new(char, 3 *len + 1))) 164 return ""; 167 if (!(str = y_new(char, 3 * len + 1))) { 168 return ""; 169 } 165 170 166 171 while (instr[ipos]) { 167 while (isurlchar(instr[ipos])) 172 while (isurlchar(instr[ipos])) { 168 173 str[bpos++] = instr[ipos++]; 169 if (!instr[ipos]) 174 } 175 if (!instr[ipos]) { 170 176 break; 177 } 171 178 172 179 snprintf(&str[bpos], 4, "%%%02x", instr[ipos] & 0xff); … … 192 199 int len = strlen(instr); 193 200 194 if (!(str = y_new(char, len + 1))) 195 return ""; 201 if (!(str = y_new(char, len + 1))) { 202 return ""; 203 } 196 204 197 205 while (instr[ipos]) { 198 while (instr[ipos] && instr[ipos] != '%') 206 while (instr[ipos] && instr[ipos] != '%') { 199 207 if (instr[ipos] == '+') { 200 208 str[bpos++] = ' '; 201 209 ipos++; 202 } else 210 } else { 203 211 str[bpos++] = instr[ipos++]; 204 if (!instr[ipos]) 212 } 213 } 214 if (!instr[ipos]) { 205 215 break; 216 } 206 217 207 218 if (instr[ipos + 1] && instr[ipos + 2]) { … … 210 221 entity[1] = instr[ipos++]; 211 222 sscanf(entity, "%2x", &dec); 212 str[bpos++] = (char) dec;223 str[bpos++] = (char) dec; 213 224 } else { 214 225 str[bpos++] = instr[ipos++]; … … 230 241 char entity[4] = { 0, 0, 0, 0 }; 231 242 char *entitymap[5][2] = { 232 { "amp;", "&"},233 { "quot;", "\""},234 { "lt;", "<"},235 { "gt;", "<"},236 { "nbsp;", " "}243 { "amp;", "&" }, 244 { "quot;", "\"" }, 245 { "lt;", "<" }, 246 { "gt;", "<" }, 247 { "nbsp;", " " } 237 248 }; 238 249 unsigned dec; 239 250 int len = strlen(instr); 240 251 241 if (!(str = y_new(char, len + 1))) 242 return ""; 252 if (!(str = y_new(char, len + 1))) { 253 return ""; 254 } 243 255 244 256 while (instr[ipos]) { 245 while (instr[ipos] && instr[ipos] != '&') 257 while (instr[ipos] && instr[ipos] != '&') { 246 258 if (instr[ipos] == '+') { 247 259 str[bpos++] = ' '; 248 260 ipos++; 249 } else 261 } else { 250 262 str[bpos++] = instr[ipos++]; 251 if (!instr[ipos] || !instr[ipos + 1]) 263 } 264 } 265 if (!instr[ipos] || !instr[ipos + 1]) { 252 266 break; 267 } 253 268 ipos++; 254 269 … … 256 271 ipos++; 257 272 epos = 0; 258 while (instr[ipos] != ';') 273 while (instr[ipos] != ';') { 259 274 entity[epos++] = instr[ipos++]; 275 } 260 276 sscanf(entity, "%u", &dec); 261 str[bpos++] = (char) dec;277 str[bpos++] = (char) dec; 262 278 ipos++; 263 279 } else { 264 280 int i; 265 for (i = 0; i < 5; i++) 281 for (i = 0; i < 5; i++) { 266 282 if (!strncmp(instr + ipos, entitymap[i][0], 267 283 strlen(entitymap[i][0]))) { 268 284 str[bpos++] = entitymap[i][1][0]; 269 285 ipos += strlen(entitymap[i][0]); 270 286 break; 271 287 } 288 } 272 289 } 273 290 } … … 294 311 { 295 312 struct callback_data *ccd = data; 296 if (error == 0) 297 YAHOO_CALLBACK(ext_yahoo_write) (fd, ccd->request, 298 strlen(ccd->request)); 313 314 if (error == 0) { 315 YAHOO_CALLBACK (ext_yahoo_write) (fd, ccd->request, 316 strlen(ccd->request)); 317 } 299 318 free(ccd->request); 300 319 ccd->callback(ccd->id, fd, error, ccd->user_data); … … 303 322 304 323 static void yahoo_send_http_request(int id, char *host, int port, char *request, 305 324 yahoo_get_fd_callback callback, void *data, int use_ssl) 306 325 { 307 326 struct callback_data *ccd = y_new0(struct callback_data, 1); 327 308 328 ccd->callback = callback; 309 329 ccd->id = id; … … 311 331 ccd->user_data = data; 312 332 313 YAHOO_CALLBACK (ext_yahoo_connect_async) (id, host, port,314 333 YAHOO_CALLBACK (ext_yahoo_connect_async) (id, host, port, 334 connect_complete, ccd, use_ssl); 315 335 } 316 336 317 337 void yahoo_http_post(int id, const char *url, const char *cookies, 318 338 long content_length, yahoo_get_fd_callback callback, void *data) 319 339 { 320 340 char host[255]; … … 324 344 int ssl = 0; 325 345 326 if (!url_to_host_port_path(url, host, &port, path, &ssl)) 346 if (!url_to_host_port_path(url, host, &port, path, &ssl)) { 327 347 return; 348 } 328 349 329 350 /* thanks to kopete dumpcap */ 330 351 snprintf(buff, sizeof(buff), 331 332 333 334 335 336 337 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); 338 359 339 360 yahoo_send_http_request(id, host, port, buff, callback, data, ssl); … … 341 362 342 363 void yahoo_http_get(int id, const char *url, const char *cookies, int http11, 343 364 int keepalive, yahoo_get_fd_callback callback, void *data) 344 365 { 345 366 char host[255]; … … 350 371 int ssl = 0; 351 372 352 if (!url_to_host_port_path(url, host, &port, path, &ssl)) 373 if (!url_to_host_port_path(url, host, &port, path, &ssl)) { 353 374 return; 375 } 354 376 355 377 /* Allow cases when we don't need to send a cookie */ 356 if (cookies) 378 if (cookies) { 357 379 snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n", 358 359 else380 cookies); 381 } else { 360 382 cookiebuff[0] = '\0'; 383 } 361 384 362 385 snprintf(buff, sizeof(buff), 363 364 365 366 367 "%s" "\r\n", path, http11?"1":"0", cookiebuff, host,368 keepalive? "Connection: Keep-Alive\r\n":"Connection: close\r\n");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"); 369 392 370 393 yahoo_send_http_request(id, host, port, buff, callback, data, ssl); … … 372 395 373 396 void yahoo_http_head(int id, const char *url, const char *cookies, int len, 374 397 char *payload, yahoo_get_fd_callback callback, void *data) 375 398 { 376 399 char host[255]; … … 381 404 int ssl = 0; 382 405 383 if (!url_to_host_port_path(url, host, &port, path, &ssl)) 406 if (!url_to_host_port_path(url, host, &port, path, &ssl)) { 384 407 return; 408 } 385 409 386 410 /* Allow cases when we don't need to send a cookie */ 387 if (cookies) 411 if (cookies) { 388 412 snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n", 389 390 else413 cookies); 414 } else { 391 415 cookiebuff[0] = '\0'; 416 } 392 417 393 418 snprintf(buff, sizeof(buff), 394 395 396 397 398 399 400 401 402 payload?payload:"");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 : ""); 403 428 404 429 yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
Note: See TracChangeset
for help on using the changeset viewer.