wget.c 21 KB

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