wget.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * wget - retrieve a file using HTTP or FTP
  4. *
  5. * Chip Rosenthal Covad Communications <chip@laserlink.net>
  6. *
  7. */
  8. /* We want libc to give us xxx64 functions also */
  9. /* http://www.unix.org/version2/whatsnew/lfs20mar.html */
  10. #define _LARGEFILE64_SOURCE 1
  11. #include "busybox.h"
  12. #include <getopt.h> /* for struct option */
  13. struct host_info {
  14. // May be used if we ever will want to free() all xstrdup()s...
  15. /* char *allocated; */
  16. char *host;
  17. int port;
  18. char *path;
  19. int is_ftp;
  20. char *user;
  21. };
  22. static void parse_url(char *url, struct host_info *h);
  23. static FILE *open_socket(len_and_sockaddr *lsa);
  24. static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
  25. static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
  26. /* Globals (can be accessed from signal handlers */
  27. static off_t content_len; /* Content-length of the file */
  28. static off_t beg_range; /* Range at which continue begins */
  29. #if ENABLE_FEATURE_WGET_STATUSBAR
  30. static off_t transferred; /* Number of bytes transferred so far */
  31. #endif
  32. static int chunked; /* chunked transfer encoding */
  33. #if ENABLE_FEATURE_WGET_STATUSBAR
  34. static void progressmeter(int flag);
  35. static char *curfile; /* Name of current file being transferred */
  36. static struct timeval start; /* Time a transfer started */
  37. enum {
  38. STALLTIME = 5 /* Seconds when xfer considered "stalled" */
  39. };
  40. #else
  41. static void progressmeter(int flag) {}
  42. #endif
  43. /* Read NMEMB elements of SIZE bytes into PTR from STREAM. Returns the
  44. * number of elements read, and a short count if an eof or non-interrupt
  45. * error is encountered. */
  46. static size_t safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
  47. {
  48. size_t ret = 0;
  49. do {
  50. clearerr(stream);
  51. ret += fread((char *)ptr + (ret * size), size, nmemb - ret, stream);
  52. } while (ret < nmemb && ferror(stream) && errno == EINTR);
  53. return ret;
  54. }
  55. /* Read a line or SIZE - 1 bytes into S, whichever is less, from STREAM.
  56. * Returns S, or NULL if an eof or non-interrupt error is encountered. */
  57. static char *safe_fgets(char *s, int size, FILE *stream)
  58. {
  59. char *ret;
  60. do {
  61. clearerr(stream);
  62. ret = fgets(s, size, stream);
  63. } while (ret == NULL && ferror(stream) && errno == EINTR);
  64. return ret;
  65. }
  66. #if ENABLE_FEATURE_WGET_AUTHENTICATION
  67. /*
  68. * Base64-encode character string and return the string.
  69. */
  70. static char *base64enc(unsigned char *p, char *buf, int len)
  71. {
  72. bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
  73. return buf;
  74. }
  75. #endif
  76. int wget_main(int argc, char **argv)
  77. {
  78. char buf[512];
  79. struct host_info server, target;
  80. len_and_sockaddr *lsa;
  81. int n, status;
  82. int port;
  83. int try = 5;
  84. unsigned opt;
  85. char *s;
  86. char *proxy = 0;
  87. char *dir_prefix = NULL;
  88. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  89. char *extra_headers = NULL;
  90. llist_t *headers_llist = NULL;
  91. #endif
  92. /* server.allocated = target.allocated = NULL; */
  93. FILE *sfp = NULL; /* socket to web/ftp server */
  94. FILE *dfp = NULL; /* socket to ftp server (data) */
  95. char *fname_out = NULL; /* where to direct output (-O) */
  96. int got_clen = 0; /* got content-length: from server */
  97. int output_fd = -1;
  98. int use_proxy = 1; /* Use proxies if env vars are set */
  99. const char *proxy_flag = "on"; /* Use proxies if env vars are set */
  100. const char *user_agent = "Wget";/* Content of the "User-Agent" header field */
  101. /*
  102. * Crack command line.
  103. */
  104. enum {
  105. WGET_OPT_CONTINUE = 0x1,
  106. WGET_OPT_QUIET = 0x2,
  107. WGET_OPT_OUTNAME = 0x4,
  108. WGET_OPT_PREFIX = 0x8,
  109. WGET_OPT_PROXY = 0x10,
  110. WGET_OPT_USER_AGENT = 0x20,
  111. WGET_OPT_PASSIVE = 0x40,
  112. WGET_OPT_HEADER = 0x80,
  113. };
  114. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  115. static const struct option wget_long_options[] = {
  116. // name, has_arg, flag, val
  117. { "continue", no_argument, NULL, 'c' },
  118. { "quiet", no_argument, NULL, 'q' },
  119. { "output-document", required_argument, NULL, 'O' },
  120. { "directory-prefix", required_argument, NULL, 'P' },
  121. { "proxy", required_argument, NULL, 'Y' },
  122. { "user-agent", required_argument, NULL, 'U' },
  123. { "passive-ftp", no_argument, NULL, 0xff },
  124. { "header", required_argument, NULL, 0xfe },
  125. { 0, 0, 0, 0 }
  126. };
  127. applet_long_options = wget_long_options;
  128. #endif
  129. opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
  130. opt = getopt32(argc, argv, "cqO:P:Y:U:",
  131. &fname_out, &dir_prefix,
  132. &proxy_flag, &user_agent
  133. USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
  134. );
  135. if (strcmp(proxy_flag, "off") == 0) {
  136. /* Use the proxy if necessary. */
  137. use_proxy = 0;
  138. }
  139. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  140. if (headers_llist) {
  141. int size = 1;
  142. char *cp;
  143. llist_t *ll = headers_llist = rev_llist(headers_llist);
  144. while (ll) {
  145. size += strlen(ll->data) + 2;
  146. ll = ll->link;
  147. }
  148. extra_headers = cp = xmalloc(size);
  149. while (headers_llist) {
  150. cp += sprintf(cp, "%s\r\n", headers_llist->data);
  151. headers_llist = headers_llist->link;
  152. }
  153. }
  154. #endif
  155. parse_url(argv[optind], &target);
  156. server.host = target.host;
  157. server.port = target.port;
  158. /*
  159. * Use the proxy if necessary.
  160. */
  161. if (use_proxy) {
  162. proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
  163. if (proxy && *proxy) {
  164. parse_url(proxy, &server);
  165. } else {
  166. use_proxy = 0;
  167. }
  168. }
  169. /* Guess an output filename */
  170. if (!fname_out) {
  171. // Dirty hack. Needed because bb_get_last_path_component
  172. // will destroy trailing / by storing '\0' in last byte!
  173. if (!last_char_is(target.path, '/')) {
  174. fname_out =
  175. #if ENABLE_FEATURE_WGET_STATUSBAR
  176. curfile =
  177. #endif
  178. bb_get_last_path_component(target.path);
  179. }
  180. if (!fname_out || !fname_out[0]) {
  181. fname_out =
  182. #if ENABLE_FEATURE_WGET_STATUSBAR
  183. curfile =
  184. #endif
  185. "index.html";
  186. }
  187. if (dir_prefix != NULL)
  188. fname_out = concat_path_file(dir_prefix, fname_out);
  189. #if ENABLE_FEATURE_WGET_STATUSBAR
  190. } else {
  191. curfile = bb_get_last_path_component(fname_out);
  192. #endif
  193. }
  194. /* Impossible?
  195. if ((opt & WGET_OPT_CONTINUE) && !fname_out)
  196. bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
  197. /*
  198. * Determine where to start transfer.
  199. */
  200. if (LONE_DASH(fname_out)) {
  201. output_fd = 1;
  202. opt &= ~WGET_OPT_CONTINUE;
  203. }
  204. if (opt & WGET_OPT_CONTINUE) {
  205. output_fd = open(fname_out, O_WRONLY);
  206. if (output_fd >= 0) {
  207. beg_range = xlseek(output_fd, 0, SEEK_END);
  208. }
  209. /* File doesn't exist. We do not create file here yet.
  210. We are not sure it exists on remove side */
  211. }
  212. /* We want to do exactly _one_ DNS lookup, since some
  213. * sites (i.e. ftp.us.debian.org) use round-robin DNS
  214. * and we want to connect to only one IP... */
  215. lsa = host2sockaddr(server.host, server.port);
  216. if (!(opt & WGET_OPT_QUIET)) {
  217. fprintf(stderr, "Connecting to %s [%s]\n", server.host,
  218. xmalloc_sockaddr2dotted(&lsa->sa, lsa->len));
  219. /* We leak xmalloc_sockaddr2dotted result */
  220. }
  221. if (use_proxy || !target.is_ftp) {
  222. /*
  223. * HTTP session
  224. */
  225. do {
  226. got_clen = chunked = 0;
  227. if (!--try)
  228. bb_error_msg_and_die("too many redirections");
  229. /*
  230. * Open socket to http server
  231. */
  232. if (sfp) fclose(sfp);
  233. sfp = open_socket(lsa);
  234. /*
  235. * Send HTTP request.
  236. */
  237. if (use_proxy) {
  238. // const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n";
  239. //#if ENABLE_FEATURE_WGET_IP6_LITERAL
  240. // if (strchr(target.host, ':'))
  241. // format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n";
  242. //#endif
  243. // fprintf(sfp, format,
  244. // target.is_ftp ? "f" : "ht", target.host,
  245. // ntohs(target.port), target.path);
  246. fprintf(sfp, "GET %stp://%s/%s HTTP/1.1\r\n",
  247. target.is_ftp ? "f" : "ht", target.host,
  248. target.path);
  249. } else {
  250. fprintf(sfp, "GET /%s HTTP/1.1\r\n", target.path);
  251. }
  252. fprintf(sfp, "Host: %s\r\nUser-Agent: %s\r\n",
  253. target.host, user_agent);
  254. #if ENABLE_FEATURE_WGET_AUTHENTICATION
  255. if (target.user) {
  256. fprintf(sfp, "Authorization: Basic %s\r\n",
  257. base64enc((unsigned char*)target.user, buf, sizeof(buf)));
  258. }
  259. if (use_proxy && server.user) {
  260. fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
  261. base64enc((unsigned char*)server.user, buf, sizeof(buf)));
  262. }
  263. #endif
  264. if (beg_range)
  265. fprintf(sfp, "Range: bytes=%"OFF_FMT"d-\r\n", beg_range);
  266. #if ENABLE_FEATURE_WGET_LONG_OPTIONS
  267. if (extra_headers)
  268. fputs(extra_headers, sfp);
  269. #endif
  270. fprintf(sfp, "Connection: close\r\n\r\n");
  271. /*
  272. * Retrieve HTTP response line and check for "200" status code.
  273. */
  274. read_response:
  275. if (fgets(buf, sizeof(buf), sfp) == NULL)
  276. bb_error_msg_and_die("no response from server");
  277. s = buf;
  278. while (*s != '\0' && !isspace(*s)) ++s;
  279. s = skip_whitespace(s);
  280. // FIXME: no error check
  281. // xatou wouldn't work: "200 OK"
  282. status = atoi(s);
  283. switch (status) {
  284. case 0:
  285. case 100:
  286. while (gethdr(buf, sizeof(buf), sfp, &n) != NULL)
  287. /* eat all remaining headers */;
  288. goto read_response;
  289. case 200:
  290. break;
  291. case 300: /* redirection */
  292. case 301:
  293. case 302:
  294. case 303:
  295. break;
  296. case 206:
  297. if (beg_range)
  298. break;
  299. /*FALLTHRU*/
  300. default:
  301. /* Show first line only and kill any ESC tricks */
  302. buf[strcspn(buf, "\n\r\x1b")] = '\0';
  303. bb_error_msg_and_die("server returned error: %s", buf);
  304. }
  305. /*
  306. * Retrieve HTTP headers.
  307. */
  308. while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
  309. if (strcasecmp(buf, "content-length") == 0) {
  310. content_len = BB_STRTOOFF(s, NULL, 10);
  311. if (errno || content_len < 0) {
  312. bb_error_msg_and_die("content-length %s is garbage", s);
  313. }
  314. got_clen = 1;
  315. continue;
  316. }
  317. if (strcasecmp(buf, "transfer-encoding") == 0) {
  318. if (strcasecmp(s, "chunked") != 0)
  319. bb_error_msg_and_die("server wants to do %s transfer encoding", s);
  320. chunked = got_clen = 1;
  321. }
  322. if (strcasecmp(buf, "location") == 0) {
  323. if (s[0] == '/')
  324. /* free(target.allocated); */
  325. target.path = /* target.allocated = */ xstrdup(s+1);
  326. else {
  327. parse_url(s, &target);
  328. if (use_proxy == 0) {
  329. server.host = target.host;
  330. server.port = target.port;
  331. }
  332. free(lsa);
  333. lsa = host2sockaddr(server.host, server.port);
  334. break;
  335. }
  336. }
  337. }
  338. } while (status >= 300);
  339. dfp = sfp;
  340. } else {
  341. /*
  342. * FTP session
  343. */
  344. if (!target.user)
  345. target.user = xstrdup("anonymous:busybox@");
  346. sfp = open_socket(lsa);
  347. if (ftpcmd(NULL, NULL, sfp, buf) != 220)
  348. bb_error_msg_and_die("%s", buf+4);
  349. /*
  350. * Splitting username:password pair,
  351. * trying to log in
  352. */
  353. s = strchr(target.user, ':');
  354. if (s)
  355. *(s++) = '\0';
  356. switch (ftpcmd("USER ", target.user, sfp, buf)) {
  357. case 230:
  358. break;
  359. case 331:
  360. if (ftpcmd("PASS ", s, sfp, buf) == 230)
  361. break;
  362. /* FALLTHRU (failed login) */
  363. default:
  364. bb_error_msg_and_die("ftp login: %s", buf+4);
  365. }
  366. ftpcmd("TYPE I", NULL, sfp, buf);
  367. /*
  368. * Querying file size
  369. */
  370. if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
  371. content_len = BB_STRTOOFF(buf+4, NULL, 10);
  372. if (errno || content_len < 0) {
  373. bb_error_msg_and_die("SIZE value is garbage");
  374. }
  375. got_clen = 1;
  376. }
  377. /*
  378. * Entering passive mode
  379. */
  380. if (ftpcmd("PASV", NULL, sfp, buf) != 227) {
  381. pasv_error:
  382. bb_error_msg_and_die("bad response to %s: %s", "PASV", buf);
  383. }
  384. // Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
  385. // Server's IP is N1.N2.N3.N4 (we ignore it)
  386. // Server's port for data connection is P1*256+P2
  387. s = strrchr(buf, ')');
  388. if (s) s[0] = '\0';
  389. s = strrchr(buf, ',');
  390. if (!s) goto pasv_error;
  391. port = xatou_range(s+1, 0, 255);
  392. *s = '\0';
  393. s = strrchr(buf, ',');
  394. if (!s) goto pasv_error;
  395. port += xatou_range(s+1, 0, 255) * 256;
  396. set_nport(lsa, htons(port));
  397. dfp = open_socket(lsa);
  398. if (beg_range) {
  399. sprintf(buf, "REST %"OFF_FMT"d", beg_range);
  400. if (ftpcmd(buf, NULL, sfp, buf) == 350)
  401. content_len -= beg_range;
  402. }
  403. if (ftpcmd("RETR ", target.path, sfp, buf) > 150)
  404. bb_error_msg_and_die("bad response to RETR: %s", buf);
  405. }
  406. /*
  407. * Retrieve file
  408. */
  409. if (chunked) {
  410. fgets(buf, sizeof(buf), dfp);
  411. content_len = STRTOOFF(buf, NULL, 16);
  412. /* FIXME: error check?? */
  413. }
  414. /* Do it before progressmeter (want to have nice error message) */
  415. if (output_fd < 0)
  416. output_fd = xopen(fname_out,
  417. O_WRONLY|O_CREAT|O_EXCL|O_TRUNC);
  418. if (!(opt & WGET_OPT_QUIET))
  419. progressmeter(-1);
  420. do {
  421. while (content_len > 0 || !got_clen) {
  422. unsigned rdsz = sizeof(buf);
  423. if (content_len < sizeof(buf) && (chunked || got_clen))
  424. rdsz = (unsigned)content_len;
  425. n = safe_fread(buf, 1, rdsz, dfp);
  426. if (n <= 0)
  427. break;
  428. if (full_write(output_fd, buf, n) != n) {
  429. bb_perror_msg_and_die(bb_msg_write_error);
  430. }
  431. #if ENABLE_FEATURE_WGET_STATUSBAR
  432. transferred += n;
  433. #endif
  434. if (got_clen) {
  435. content_len -= n;
  436. }
  437. }
  438. if (chunked) {
  439. safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
  440. safe_fgets(buf, sizeof(buf), dfp);
  441. content_len = STRTOOFF(buf, NULL, 16);
  442. /* FIXME: error check? */
  443. if (content_len == 0) {
  444. chunked = 0; /* all done! */
  445. }
  446. }
  447. if (n == 0 && ferror(dfp)) {
  448. bb_perror_msg_and_die(bb_msg_read_error);
  449. }
  450. } while (chunked);
  451. if (!(opt & WGET_OPT_QUIET))
  452. progressmeter(1);
  453. if ((use_proxy == 0) && target.is_ftp) {
  454. fclose(dfp);
  455. if (ftpcmd(NULL, NULL, sfp, buf) != 226)
  456. bb_error_msg_and_die("ftp error: %s", buf+4);
  457. ftpcmd("QUIT", NULL, sfp, buf);
  458. }
  459. exit(EXIT_SUCCESS);
  460. }
  461. static void parse_url(char *src_url, struct host_info *h)
  462. {
  463. char *url, *p, *sp;
  464. /* h->allocated = */ url = xstrdup(src_url);
  465. if (strncmp(url, "http://", 7) == 0) {
  466. h->port = bb_lookup_port("http", "tcp", 80);
  467. h->host = url + 7;
  468. h->is_ftp = 0;
  469. } else if (strncmp(url, "ftp://", 6) == 0) {
  470. h->port = bb_lookup_port("ftp", "tcp", 21);
  471. h->host = url + 6;
  472. h->is_ftp = 1;
  473. } else
  474. bb_error_msg_and_die("not an http or ftp url: %s", url);
  475. // FYI:
  476. // "Real" wget 'http://busybox.net?var=a/b' sends this request:
  477. // 'GET /?var=a/b HTTP 1.0'
  478. // and saves 'index.html?var=a%2Fb' (we save 'b')
  479. // wget 'http://busybox.net?login=john@doe':
  480. // request: 'GET /?login=john@doe HTTP/1.0'
  481. // saves: 'index.html?login=john@doe' (we save '?login=john@doe')
  482. // wget 'http://busybox.net#test/test':
  483. // request: 'GET / HTTP/1.0'
  484. // saves: 'index.html' (we save 'test')
  485. //
  486. // We also don't add unique .N suffix if file exists...
  487. sp = strchr(h->host, '/');
  488. p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p;
  489. p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p;
  490. if (!sp) {
  491. /* must be writable because of bb_get_last_path_component() */
  492. static char nullstr[] = "";
  493. h->path = nullstr;
  494. } else if (*sp == '/') {
  495. *sp = '\0';
  496. h->path = sp + 1;
  497. } else { // '#' or '?'
  498. // http://busybox.net?login=john@doe is a valid URL
  499. // memmove converts to:
  500. // http:/busybox.nett?login=john@doe...
  501. memmove(h->host-1, h->host, sp - h->host);
  502. h->host--;
  503. sp[-1] = '\0';
  504. h->path = sp;
  505. }
  506. sp = strrchr(h->host, '@');
  507. h->user = NULL;
  508. if (sp != NULL) {
  509. h->user = h->host;
  510. *sp = '\0';
  511. h->host = sp + 1;
  512. }
  513. sp = h->host;
  514. //host2sockaddr does this itself
  515. //#if ENABLE_FEATURE_WGET_IP6_LITERAL
  516. // if (sp[0] == '[') {
  517. // char *ep;
  518. //
  519. // ep = sp + 1;
  520. // while (*ep == ':' || isxdigit(*ep))
  521. // ep++;
  522. // if (*ep == ']') {
  523. // h->host++;
  524. // *ep = '\0';
  525. // sp = ep + 1;
  526. // }
  527. // }
  528. //#endif
  529. //
  530. // p = strchr(sp, ':');
  531. // if (p != NULL) {
  532. // *p = '\0';
  533. // h->port = htons(xatou16(p + 1));
  534. // }
  535. }
  536. static FILE *open_socket(len_and_sockaddr *lsa)
  537. {
  538. FILE *fp;
  539. /* glibc 2.4 seems to try seeking on it - ??! */
  540. /* hopefully it understands what ESPIPE means... */
  541. fp = fdopen(xconnect_stream(lsa), "r+");
  542. if (fp == NULL)
  543. bb_perror_msg_and_die("fdopen");
  544. return fp;
  545. }
  546. static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
  547. {
  548. char *s, *hdrval;
  549. int c;
  550. *istrunc = 0;
  551. /* retrieve header line */
  552. if (fgets(buf, bufsiz, fp) == NULL)
  553. return NULL;
  554. /* see if we are at the end of the headers */
  555. for (s = buf; *s == '\r'; ++s)
  556. ;
  557. if (s[0] == '\n')
  558. return NULL;
  559. /* convert the header name to lower case */
  560. for (s = buf; isalnum(*s) || *s == '-'; ++s)
  561. *s = tolower(*s);
  562. /* verify we are at the end of the header name */
  563. if (*s != ':')
  564. bb_error_msg_and_die("bad header line: %s", buf);
  565. /* locate the start of the header value */
  566. for (*s++ = '\0'; *s == ' ' || *s == '\t'; ++s)
  567. ;
  568. hdrval = s;
  569. /* locate the end of header */
  570. while (*s != '\0' && *s != '\r' && *s != '\n')
  571. ++s;
  572. /* end of header found */
  573. if (*s != '\0') {
  574. *s = '\0';
  575. return hdrval;
  576. }
  577. /* Rats! The buffer isn't big enough to hold the entire header value. */
  578. while (c = getc(fp), c != EOF && c != '\n')
  579. ;
  580. *istrunc = 1;
  581. return hdrval;
  582. }
  583. static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
  584. {
  585. int result;
  586. if (s1) {
  587. if (!s2) s2 = "";
  588. fprintf(fp, "%s%s\r\n", s1, s2);
  589. fflush(fp);
  590. }
  591. do {
  592. char *buf_ptr;
  593. if (fgets(buf, 510, fp) == NULL) {
  594. bb_perror_msg_and_die("error getting response");
  595. }
  596. buf_ptr = strstr(buf, "\r\n");
  597. if (buf_ptr) {
  598. *buf_ptr = '\0';
  599. }
  600. } while (!isdigit(buf[0]) || buf[3] != ' ');
  601. buf[3] = '\0';
  602. result = xatoi_u(buf);
  603. buf[3] = ' ';
  604. return result;
  605. }
  606. #if ENABLE_FEATURE_WGET_STATUSBAR
  607. /* Stuff below is from BSD rcp util.c, as added to openshh.
  608. * Original copyright notice is retained at the end of this file.
  609. */
  610. static int
  611. getttywidth(void)
  612. {
  613. int width;
  614. get_terminal_width_height(0, &width, NULL);
  615. return width;
  616. }
  617. static void
  618. updateprogressmeter(int ignore)
  619. {
  620. int save_errno = errno;
  621. progressmeter(0);
  622. errno = save_errno;
  623. }
  624. static void alarmtimer(int iwait)
  625. {
  626. struct itimerval itv;
  627. itv.it_value.tv_sec = iwait;
  628. itv.it_value.tv_usec = 0;
  629. itv.it_interval = itv.it_value;
  630. setitimer(ITIMER_REAL, &itv, NULL);
  631. }
  632. static void
  633. progressmeter(int flag)
  634. {
  635. static struct timeval lastupdate;
  636. static off_t lastsize, totalsize;
  637. struct timeval now, td, tvwait;
  638. off_t abbrevsize;
  639. int elapsed, ratio, barlength, i;
  640. char buf[256];
  641. if (flag == -1) { /* first call to progressmeter */
  642. gettimeofday(&start, (struct timezone *) 0);
  643. lastupdate = start;
  644. lastsize = 0;
  645. totalsize = content_len + beg_range; /* as content_len changes.. */
  646. }
  647. gettimeofday(&now, (struct timezone *) 0);
  648. ratio = 100;
  649. if (totalsize != 0 && !chunked) {
  650. /* long long helps to have working ETA even if !LFS */
  651. ratio = (int) (100 * (unsigned long long)(transferred+beg_range) / totalsize);
  652. ratio = MIN(ratio, 100);
  653. }
  654. fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
  655. barlength = getttywidth() - 51;
  656. if (barlength > 0 && barlength < sizeof(buf)) {
  657. i = barlength * ratio / 100;
  658. memset(buf, '*', i);
  659. memset(buf + i, ' ', barlength - i);
  660. buf[barlength] = '\0';
  661. fprintf(stderr, "|%s|", buf);
  662. }
  663. i = 0;
  664. abbrevsize = transferred + beg_range;
  665. while (abbrevsize >= 100000) {
  666. i++;
  667. abbrevsize >>= 10;
  668. }
  669. /* see http://en.wikipedia.org/wiki/Tera */
  670. fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' ');
  671. timersub(&now, &lastupdate, &tvwait);
  672. if (transferred > lastsize) {
  673. lastupdate = now;
  674. lastsize = transferred;
  675. if (tvwait.tv_sec >= STALLTIME)
  676. timeradd(&start, &tvwait, &start);
  677. tvwait.tv_sec = 0;
  678. }
  679. timersub(&now, &start, &td);
  680. elapsed = td.tv_sec;
  681. if (tvwait.tv_sec >= STALLTIME) {
  682. fprintf(stderr, " - stalled -");
  683. } else {
  684. off_t to_download = totalsize - beg_range;
  685. if (transferred <= 0 || elapsed <= 0 || transferred > to_download || chunked) {
  686. fprintf(stderr, "--:--:-- ETA");
  687. } else {
  688. /* to_download / (transferred/elapsed) - elapsed: */
  689. int eta = (int) ((unsigned long long)to_download*elapsed/transferred - elapsed);
  690. /* (long long helps to have working ETA even if !LFS) */
  691. i = eta % 3600;
  692. fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
  693. }
  694. }
  695. if (flag == -1) { /* first call to progressmeter */
  696. struct sigaction sa;
  697. sa.sa_handler = updateprogressmeter;
  698. sigemptyset(&sa.sa_mask);
  699. sa.sa_flags = SA_RESTART;
  700. sigaction(SIGALRM, &sa, NULL);
  701. alarmtimer(1);
  702. } else if (flag == 1) { /* last call to progressmeter */
  703. alarmtimer(0);
  704. transferred = 0;
  705. putc('\n', stderr);
  706. }
  707. }
  708. #endif
  709. /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
  710. * much of which was blatantly stolen from openssh. */
  711. /*-
  712. * Copyright (c) 1992, 1993
  713. * The Regents of the University of California. All rights reserved.
  714. *
  715. * Redistribution and use in source and binary forms, with or without
  716. * modification, are permitted provided that the following conditions
  717. * are met:
  718. * 1. Redistributions of source code must retain the above copyright
  719. * notice, this list of conditions and the following disclaimer.
  720. * 2. Redistributions in binary form must reproduce the above copyright
  721. * notice, this list of conditions and the following disclaimer in the
  722. * documentation and/or other materials provided with the distribution.
  723. *
  724. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  725. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  726. *
  727. * 4. Neither the name of the University nor the names of its contributors
  728. * may be used to endorse or promote products derived from this software
  729. * without specific prior written permission.
  730. *
  731. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  732. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  733. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  734. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  735. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  736. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  737. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  738. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  739. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  740. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  741. * SUCH DAMAGE.
  742. *
  743. * $Id: wget.c,v 1.75 2004/10/08 08:27:40 andersen Exp $
  744. */