aboutsummaryrefslogtreecommitdiff
path: root/networking/wget.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2009-03-04 14:13:37 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2009-03-04 14:13:37 +0000
commit5a2ad699fca8ce2a1e008dfaacd30f7b3f611721 (patch)
tree84f249fa5728ad9decd23e30b62e637baa0a684c /networking/wget.c
parent9604e1b8fc1e6c64fb977791d5f2819389a37377 (diff)
downloadbusybox-w32-5a2ad699fca8ce2a1e008dfaacd30f7b3f611721.tar.gz
busybox-w32-5a2ad699fca8ce2a1e008dfaacd30f7b3f611721.tar.bz2
busybox-w32-5a2ad699fca8ce2a1e008dfaacd30f7b3f611721.zip
wget: --post-data support by Harald Kuthe (harald-tuxbox AT arcor.de)
function old new delta wget_main 2467 2793 +326 static.wget_longopts 110 122 +12 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/0 up/down: 338/0) Total: 338 bytes
Diffstat (limited to 'networking/wget.c')
-rw-r--r--networking/wget.c66
1 files changed, 61 insertions, 5 deletions
diff --git a/networking/wget.c b/networking/wget.c
index 6527538e0..48759049d 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -387,6 +387,43 @@ static char *gethdr(char *buf, size_t bufsiz, FILE *fp /*, int *istrunc*/)
387 return hdrval; 387 return hdrval;
388} 388}
389 389
390#if ENABLE_FEATURE_WGET_LONG_OPTIONS
391static char *URL_escape(const char *str)
392{
393 /* URL encode, see RFC 2396 */
394 char *dst;
395 char *res = dst = xmalloc(strlen(str) * 3 + 1);
396 unsigned char c;
397
398 while (1) {
399 c = *str++;
400 if (c == '\0'
401 /* || strchr("!&'()*-.=_~", c) - more code */
402 || c == '!'
403 || c == '&'
404 || c == '\''
405 || c == '('
406 || c == ')'
407 || c == '*'
408 || c == '-'
409 || c == '.'
410 || c == '='
411 || c == '_'
412 || c == '~'
413 || (c >= '0' && c <= '9')
414 || ((c|0x20) >= 'a' && (c|0x20) <= 'z')
415 ) {
416 *dst++ = c;
417 if (c == '\0')
418 return res;
419 } else {
420 *dst++ = '%';
421 *dst++ = bb_hexdigits_upcase[c >> 4];
422 *dst++ = bb_hexdigits_upcase[c & 0xf];
423 }
424 }
425}
426#endif
390 427
391int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 428int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
392int wget_main(int argc UNUSED_PARAM, char **argv) 429int wget_main(int argc UNUSED_PARAM, char **argv)
@@ -402,6 +439,7 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
402 char *proxy = 0; 439 char *proxy = 0;
403 char *dir_prefix = NULL; 440 char *dir_prefix = NULL;
404#if ENABLE_FEATURE_WGET_LONG_OPTIONS 441#if ENABLE_FEATURE_WGET_LONG_OPTIONS
442 char *post_data;
405 char *extra_headers = NULL; 443 char *extra_headers = NULL;
406 llist_t *headers_llist = NULL; 444 llist_t *headers_llist = NULL;
407#endif 445#endif
@@ -430,7 +468,8 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
430 WGET_OPT_RETRIES = (1 << 7), 468 WGET_OPT_RETRIES = (1 << 7),
431 WGET_OPT_NETWORK_READ_TIMEOUT = (1 << 8), 469 WGET_OPT_NETWORK_READ_TIMEOUT = (1 << 8),
432 WGET_OPT_PASSIVE = (1 << 9), 470 WGET_OPT_PASSIVE = (1 << 9),
433 WGET_OPT_HEADER = (1 << 10), 471 WGET_OPT_HEADER = (1 << 10) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
472 WGET_OPT_POST_DATA = (1 << 11) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
434 }; 473 };
435#if ENABLE_FEATURE_WGET_LONG_OPTIONS 474#if ENABLE_FEATURE_WGET_LONG_OPTIONS
436 static const char wget_longopts[] ALIGN1 = 475 static const char wget_longopts[] ALIGN1 =
@@ -448,6 +487,7 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
448 /* Ignored (we always use PASV): */ 487 /* Ignored (we always use PASV): */
449 "passive-ftp\0" No_argument "\xff" 488 "passive-ftp\0" No_argument "\xff"
450 "header\0" Required_argument "\xfe" 489 "header\0" Required_argument "\xfe"
490 "post-data\0" Required_argument "\xfd"
451 ; 491 ;
452#endif 492#endif
453 493
@@ -464,6 +504,7 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
464 NULL, /* -t RETRIES */ 504 NULL, /* -t RETRIES */
465 NULL /* -T NETWORK_READ_TIMEOUT */ 505 NULL /* -T NETWORK_READ_TIMEOUT */
466 USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist) 506 USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
507 USE_FEATURE_WGET_LONG_OPTIONS(, &post_data)
467 ); 508 );
468 if (strcmp(proxy_flag, "off") == 0) { 509 if (strcmp(proxy_flag, "off") == 0) {
469 /* Use the proxy if necessary */ 510 /* Use the proxy if necessary */
@@ -564,7 +605,10 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
564 target.is_ftp ? "f" : "ht", target.host, 605 target.is_ftp ? "f" : "ht", target.host,
565 target.path); 606 target.path);
566 } else { 607 } else {
567 fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path); 608 if (opt & WGET_OPT_POST_DATA)
609 fprintf(sfp, "POST /%s HTTP/1.1\r\n", target.path);
610 else
611 fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
568 } 612 }
569 613
570 fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n", 614 fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n",
@@ -586,12 +630,24 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
586#if ENABLE_FEATURE_WGET_LONG_OPTIONS 630#if ENABLE_FEATURE_WGET_LONG_OPTIONS
587 if (extra_headers) 631 if (extra_headers)
588 fputs(extra_headers, sfp); 632 fputs(extra_headers, sfp);
633
634 if (opt & WGET_OPT_POST_DATA) {
635 char *estr = URL_escape(post_data);
636 fprintf(sfp, "Content-Type: application/x-www-form-urlencoded\r\n");
637 fprintf(sfp, "Content-Length: %u\r\n" "\r\n" "%s",
638 (int) strlen(estr), estr);
639 /*fprintf(sfp, "Connection: Keep-Alive\r\n\r\n");*/
640 /*fprintf(sfp, "%s\r\n", estr);*/
641 free(estr);
642 } else
589#endif 643#endif
590 fprintf(sfp, "Connection: close\r\n\r\n"); 644 { /* If "Connection:" is needed, document why */
645 fprintf(sfp, /* "Connection: close\r\n" */ "\r\n");
646 }
591 647
592 /* 648 /*
593 * Retrieve HTTP response line and check for "200" status code. 649 * Retrieve HTTP response line and check for "200" status code.
594 */ 650 */
595 read_response: 651 read_response:
596 if (fgets(buf, sizeof(buf), sfp) == NULL) 652 if (fgets(buf, sizeof(buf), sfp) == NULL)
597 bb_error_msg_and_die("no response from server"); 653 bb_error_msg_and_die("no response from server");