summaryrefslogtreecommitdiff
path: root/networking/wget.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-04-05 21:45:54 +0000
committerEric Andersen <andersen@codepoet.org>2001-04-05 21:45:54 +0000
commit79757c9c37beab0032bc065b10bf84d4afe2b500 (patch)
treeec753d3a25083e41816993be04ca9377160ff0e3 /networking/wget.c
parentea9065072c91b6397cb3cb597f248d23415169f0 (diff)
downloadbusybox-w32-79757c9c37beab0032bc065b10bf84d4afe2b500.tar.gz
busybox-w32-79757c9c37beab0032bc065b10bf84d4afe2b500.tar.bz2
busybox-w32-79757c9c37beab0032bc065b10bf84d4afe2b500.zip
A patch from Dmitry Zakharov <dmit@crp.bank.gov.ua> which adds
- support for ftp downloads - HTTP basic authentication support (as an optional feature) - handling of http redirections - protocol version changed to 1.0 (to stop servers from requesting chunked encoding) - bugfix: in the case when content-length not given, wget didn't download anything - when attempting to continue an aborted download but server doesn't support restarts, reopen output file in write mode - changed assumption that existing file should restart an aborted download. Now the user must explicitly specify this with -c
Diffstat (limited to 'networking/wget.c')
-rw-r--r--networking/wget.c379
1 files changed, 282 insertions, 97 deletions
diff --git a/networking/wget.c b/networking/wget.c
index f62d835e5..5bec0ddd5 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -1,6 +1,6 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * wget - retrieve a file using HTTP 3 * wget - retrieve a file using HTTP or FTP
4 * 4 *
5 * Chip Rosenthal Covad Communications <chip@laserlink.net> 5 * Chip Rosenthal Covad Communications <chip@laserlink.net>
6 * 6 *
@@ -47,9 +47,18 @@
47 } while (0) 47 } while (0)
48#endif 48#endif
49 49
50static void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path); 50struct host_info {
51 char *host;
52 int port;
53 char *path;
54 int is_ftp;
55 char *user;
56};
57
58static void parse_url(char *url, struct host_info *h);
51static FILE *open_socket(char *host, int port); 59static FILE *open_socket(char *host, int port);
52static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc); 60static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
61static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
53static void progressmeter(int flag); 62static void progressmeter(int flag);
54 63
55/* Globals (can be accessed from signal handlers */ 64/* Globals (can be accessed from signal handlers */
@@ -70,16 +79,52 @@ static void close_and_delete_outfile(FILE* output, char *fname_out, int do_conti
70 } 79 }
71} 80}
72 81
82#define close_delete_and_die(s...) { \
83 close_and_delete_outfile(output, fname_out, do_continue); \
84 error_msg_and_die(s); }
85
86
87#ifdef BB_FEATURE_WGET_AUTHENTICATION
88/*
89 * Base64-encode character string
90 * oops... isn't something similar in uuencode.c?
91 * It would be better to use already existing code
92 */
93char *base64enc(char *p, char *buf, int len) {
94
95 char al[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
96 "0123456789+/";
97 char *s = buf;
98
99 while(*p) {
100 if (s >= buf+len-4)
101 error_msg_and_die("buffer overflow");
102 *(s++) = al[(*p >> 2) & 0x3F];
103 *(s++) = al[((*p << 4) & 0x30) | ((*(p+1) >> 4) & 0x0F)];
104 *s = *(s+1) = '=';
105 *(s+2) = 0;
106 if (! *(++p)) break;
107 *(s++) = al[((*p << 2) & 0x3C) | ((*(p+1) >> 6) & 0x03)];
108 if (! *(++p)) break;
109 *(s++) = al[*(p++) & 0x3F];
110 }
111
112 return buf;
113}
114#endif
115
73int wget_main(int argc, char **argv) 116int wget_main(int argc, char **argv)
74{ 117{
75 int n; 118 int n, try=5, status;
76 char *proxy, *proxy_host; 119 int port;
77 int uri_port, proxy_port; 120 char *proxy;
78 char *s, buf[512]; 121 char *s, buf[512];
79 struct stat sbuf; 122 struct stat sbuf;
80 123
81 FILE *sfp; /* socket to web server */ 124 struct host_info server, target;
82 char *uri_host, *uri_path; /* parsed from command line url */ 125
126 FILE *sfp = NULL; /* socket to web/ftp server */
127 FILE *dfp = NULL; /* socket to ftp server (data) */
83 char *fname_out = NULL; /* where to direct output (-O) */ 128 char *fname_out = NULL; /* where to direct output (-O) */
84 int do_continue = 0; /* continue a prev transfer (-c) */ 129 int do_continue = 0; /* continue a prev transfer (-c) */
85 long beg_range = 0L; /* range at which continue begins */ 130 long beg_range = 0L; /* range at which continue begins */
@@ -113,21 +158,16 @@ int wget_main(int argc, char **argv)
113 if (argc - optind != 1) 158 if (argc - optind != 1)
114 show_usage(); 159 show_usage();
115 160
161 parse_url(argv[optind], &target);
162 server.host = target.host;
163 server.port = target.port;
164
116 /* 165 /*
117 * Use the proxy if necessary. 166 * Use the proxy if necessary.
118 */ 167 */
119 if ((proxy = getenv("http_proxy")) != NULL) { 168 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
120 proxy = xstrdup(proxy); 169 if (proxy)
121 parse_url(proxy, &proxy_host, &proxy_port, &uri_path); 170 parse_url(xstrdup(proxy), &server);
122 parse_url(argv[optind], &uri_host, &uri_port, &uri_path);
123 } else {
124 /*
125 * Parse url into components.
126 */
127 parse_url(argv[optind], &uri_host, &uri_port, &uri_path);
128 proxy_host=uri_host;
129 proxy_port=uri_port;
130 }
131 171
132 /* Guess an output filename */ 172 /* Guess an output filename */
133 if (!fname_out) { 173 if (!fname_out) {
@@ -135,7 +175,7 @@ int wget_main(int argc, char **argv)
135#ifdef BB_FEATURE_WGET_STATUSBAR 175#ifdef BB_FEATURE_WGET_STATUSBAR
136 curfile = 176 curfile =
137#endif 177#endif
138 get_last_path_component(uri_path); 178 get_last_path_component(target.path);
139 if (fname_out==NULL || strlen(fname_out)<1) { 179 if (fname_out==NULL || strlen(fname_out)<1) {
140 fname_out = 180 fname_out =
141#ifdef BB_FEATURE_WGET_STATUSBAR 181#ifdef BB_FEATURE_WGET_STATUSBAR
@@ -145,7 +185,7 @@ int wget_main(int argc, char **argv)
145 } 185 }
146#ifdef BB_FEATURE_WGET_STATUSBAR 186#ifdef BB_FEATURE_WGET_STATUSBAR
147 } else { 187 } else {
148 curfile=argv[optind]; 188 curfile = get_last_path_component(fname_out);
149#endif 189#endif
150 } 190 }
151 if (do_continue && !fname_out) 191 if (do_continue && !fname_out)
@@ -153,18 +193,6 @@ int wget_main(int argc, char **argv)
153 193
154 194
155 /* 195 /*
156 * Open socket to server.
157 */
158 sfp = open_socket(proxy_host, proxy_port);
159
160 /* Make the assumption that if the file already exists
161 * on disk that the intention is to continue downloading
162 * a previously aborted download -Erik */
163 if (stat(fname_out, &sbuf) == 0) {
164 ++do_continue;
165 }
166
167 /*
168 * Open the output file stream. 196 * Open the output file stream.
169 */ 197 */
170 if (fname_out != (char *)1) { 198 if (fname_out != (char *)1) {
@@ -185,67 +213,185 @@ int wget_main(int argc, char **argv)
185 do_continue = 0; 213 do_continue = 0;
186 } 214 }
187 215
188 /* 216 if (proxy || !target.is_ftp) {
189 * Send HTTP request. 217 /*
190 */ 218 * HTTP session
191 fprintf(sfp, "GET http://%s:%d/%s HTTP/1.1\r\n", 219 */
192 uri_host, uri_port, uri_path); 220 do {
193 fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", uri_host); 221 if (! --try)
222 close_delete_and_die("too many redirections");
194 223
195 if (do_continue) 224 /*
196 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range); 225 * Open socket to http server
197 fprintf(sfp,"Connection: close\r\n\r\n"); 226 */
227 if (sfp) fclose(sfp);
228 sfp = open_socket(server.host, server.port);
229
230 /*
231 * Send HTTP request.
232 */
233 if (proxy) {
234 fprintf(sfp, "GET %stp://%s:%d/%s HTTP/1.0\r\n",
235 target.is_ftp ? "f" : "ht", target.host,
236 target.port, target.path);
237 } else {
238 fprintf(sfp, "GET /%s HTTP/1.0\r\n", target.path);
239 }
240
241 fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", target.host);
242
243#ifdef BB_FEATURE_WGET_AUTHENTICATION
244 if (target.user) {
245 fprintf(sfp, "Authorization: Basic %s\r\n",
246 base64enc(target.user, buf, sizeof(buf)));
247 }
248 if (proxy && server.user) {
249 fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
250 base64enc(server.user, buf, sizeof(buf)));
251 }
252#endif
198 253
199 /*
200 * Retrieve HTTP response line and check for "200" status code.
201 */
202 if (fgets(buf, sizeof(buf), sfp) == NULL) {
203 close_and_delete_outfile(output, fname_out, do_continue);
204 error_msg_and_die("no response from server");
205 }
206 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
207 ;
208 for ( ; isspace(*s) ; ++s)
209 ;
210 switch (atoi(s)) {
211 case 0:
212 case 200:
213 break;
214 case 206:
215 if (do_continue) 254 if (do_continue)
216 break; 255 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
217 /*FALLTHRU*/ 256 fprintf(sfp,"Connection: close\r\n\r\n");
218 default: 257
219 close_and_delete_outfile(output, fname_out, do_continue); 258 /*
220 chomp(buf); 259 * Retrieve HTTP response line and check for "200" status code.
221 error_msg_and_die("server returned error %d: %s", atoi(s), buf); 260 */
261 if (fgets(buf, sizeof(buf), sfp) == NULL)
262 close_delete_and_die("no response from server");
263
264 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
265 ;
266 for ( ; isspace(*s) ; ++s)
267 ;
268 switch (status = atoi(s)) {
269 case 0:
270 case 200:
271 if (do_continue && output != stdout)
272 output = freopen(fname_out, "w", output);
273 do_continue = 0;
274 break;
275 case 300: /* redirection */
276 case 301:
277 case 302:
278 case 303:
279 break;
280 case 206:
281 if (do_continue)
282 break;
283 /*FALLTHRU*/
284 default:
285 chomp(buf);
286 close_delete_and_die("server returned error %d: %s", atoi(s), buf);
287 }
288
289 /*
290 * Retrieve HTTP headers.
291 */
292 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
293 if (strcasecmp(buf, "content-length") == 0) {
294 filesize = atol(s);
295 got_clen = 1;
296 continue;
297 }
298 if (strcasecmp(buf, "transfer-encoding") == 0)
299 close_delete_and_die("server wants to do %s transfer encoding", s);
300
301 if (strcasecmp(buf, "location") == 0) {
302 if (s[0] == '/')
303 target.path = xstrdup(s+1);
304 else {
305 parse_url(xstrdup(s), &target);
306 if (!proxy) {
307 server.host = target.host;
308 server.port = target.port;
309 }
310 }
311 }
312 }
313 } while(status >= 300);
314
315 dfp = sfp;
222 } 316 }
317 else
318 {
319 /*
320 * FTP session
321 */
322 if (! target.user)
323 target.user = xstrdup("anonymous:busybox@");
223 324
224 /* 325 sfp = open_socket(server.host, server.port);
225 * Retrieve HTTP headers. 326 if (ftpcmd(NULL, NULL, sfp, buf) != 220)
226 */ 327 close_delete_and_die("%s", buf+4);
227 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) { 328
228 if (strcasecmp(buf, "content-length") == 0) { 329 /*
229 filesize = atol(s); 330 * Splitting username:password pair,
331 * trying to log in
332 */
333 s = strchr(target.user, ':');
334 if (s)
335 *(s++) = '\0';
336 switch(ftpcmd("USER ", target.user, sfp, buf)) {
337 case 230:
338 break;
339 case 331:
340 if (ftpcmd("PASS ", s, sfp, buf) == 230)
341 break;
342 /* FALLTHRU (failed login) */
343 default:
344 close_delete_and_die("ftp login: %s", buf+4);
345 }
346
347 ftpcmd("CDUP", NULL, sfp, buf);
348 ftpcmd("TYPE I", NULL, sfp, buf);
349
350 /*
351 * Querying file size
352 */
353 if (ftpcmd("SIZE /", target.path, sfp, buf) == 213) {
354 filesize = atol(buf+4);
230 got_clen = 1; 355 got_clen = 1;
231 continue;
232 } 356 }
233 if (strcasecmp(buf, "transfer-encoding") == 0) { 357
234 close_and_delete_outfile(output, fname_out, do_continue); 358 /*
235 error_msg_and_die("server wants to do %s transfer encoding", s); 359 * Entering passive mode
236 continue; 360 */
361 if (ftpcmd("PASV", NULL, sfp, buf) != 227)
362 close_delete_and_die("PASV: %s", buf+4);
363 s = strrchr(buf, ',');
364 *s = 0;
365 port = atoi(s+1);
366 s = strrchr(buf, ',');
367 port += atoi(s+1) * 256;
368 dfp = open_socket(server.host, port);
369
370 if (do_continue) {
371 sprintf(buf, "REST %ld", beg_range);
372 if (ftpcmd(buf, NULL, sfp, buf) != 350) {
373 if (output != stdout)
374 output = freopen(fname_out, "w", output);
375 do_continue = 0;
376 } else
377 filesize -= beg_range;
237 } 378 }
379
380 if (ftpcmd("RETR /", target.path, sfp, buf) > 150)
381 close_delete_and_die("RETR: %s", buf+4);
382
238 } 383 }
239 384
385
240 /* 386 /*
241 * Retrieve HTTP body. 387 * Retrieve file
242 */ 388 */
243#ifdef BB_FEATURE_WGET_STATUSBAR 389#ifdef BB_FEATURE_WGET_STATUSBAR
244 statbytes=0; 390 statbytes=0;
245 if (quiet_flag==FALSE) 391 if (quiet_flag==FALSE)
246 progressmeter(-1); 392 progressmeter(-1);
247#endif 393#endif
248 while (filesize > 0 && (n = fread(buf, 1, sizeof(buf), sfp)) > 0) { 394 while ((filesize > 0 || !got_clen) && (n = fread(buf, 1, sizeof(buf), dfp)) > 0) {
249 fwrite(buf, 1, n, output); 395 fwrite(buf, 1, n, output);
250#ifdef BB_FEATURE_WGET_STATUSBAR 396#ifdef BB_FEATURE_WGET_STATUSBAR
251 statbytes+=n; 397 statbytes+=n;
@@ -255,37 +401,57 @@ int wget_main(int argc, char **argv)
255 if (got_clen) 401 if (got_clen)
256 filesize -= n; 402 filesize -= n;
257 } 403 }
258 if (n == 0 && ferror(sfp)) 404
405 if (n == 0 && ferror(dfp))
259 perror_msg_and_die("network read error"); 406 perror_msg_and_die("network read error");
260 407
261 exit(0); 408 if (!proxy && target.is_ftp) {
409 fclose(dfp);
410 if (ftpcmd(NULL, NULL, sfp, buf) != 226)
411 error_msg_and_die("ftp error: %s", buf+4);
412 ftpcmd("QUIT", NULL, sfp, buf);
413 }
414
415 exit(EXIT_SUCCESS);
262} 416}
263 417
264 418
265void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path) 419void parse_url(char *url, struct host_info *h)
266{ 420{
267 char *cp, *sp; 421 char *cp, *sp, *up;
268 422
269 *uri_port = 80; 423 if (strncmp(url, "http://", 7) == 0) {
270 424 h->port = 80;
271 if (strncmp(url, "http://", 7) != 0) 425 h->host = url + 7;
272 error_msg_and_die("not an http url: %s", url); 426 h->is_ftp = 0;
427 } else if (strncmp(url, "ftp://", 6) == 0) {
428 h->port = 21;
429 h->host = url + 6;
430 h->is_ftp = 1;
431 } else
432 error_msg_and_die("not an http or ftp url: %s", url);
273 433
274 *uri_host = url + 7; 434 sp = strchr(h->host, '/');
435 if (sp != NULL) {
436 *sp++ = '\0';
437 h->path = sp;
438 } else
439 h->path = "";
275 440
276 cp = strchr(*uri_host, ':'); 441 up = strrchr(h->host, '@');
277 sp = strchr(*uri_host, '/'); 442 if (up != NULL) {
443 h->user = h->host;
444 *up++ = '\0';
445 h->host = up;
446 } else
447 h->user = NULL;
278 448
279 if (cp != NULL && (sp == NULL || cp < sp)) { 449 cp = strchr(h->host, ':');
450 if (cp != NULL) {
280 *cp++ = '\0'; 451 *cp++ = '\0';
281 *uri_port = atoi(cp); 452 h->port = atoi(cp);
282 } 453 }
283 454
284 if (sp != NULL) {
285 *sp++ = '\0';
286 *uri_path = sp;
287 } else
288 *uri_path = "";
289} 455}
290 456
291 457
@@ -364,6 +530,25 @@ char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
364 return hdrval; 530 return hdrval;
365} 531}
366 532
533static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
534{
535 char *p;
536
537 if (s1) {
538 if (!s2) s2="";
539 fprintf(fp, "%s%s\n", s1, s2);
540 fflush(fp);
541 }
542
543 do {
544 p = fgets(buf, 510, fp);
545 if (!p)
546 perror_msg_and_die("fgets()");
547 } while (! isdigit(buf[0]) || buf[3] != ' ');
548
549 return atoi(buf);
550}
551
367#ifdef BB_FEATURE_WGET_STATUSBAR 552#ifdef BB_FEATURE_WGET_STATUSBAR
368/* Stuff below is from BSD rcp util.c, as added to openshh. 553/* Stuff below is from BSD rcp util.c, as added to openshh.
369 * Original copyright notice is retained at the end of this file. 554 * Original copyright notice is retained at the end of this file.
@@ -534,7 +719,7 @@ progressmeter(int flag)
534 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 719 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
535 * SUCH DAMAGE. 720 * SUCH DAMAGE.
536 * 721 *
537 * $Id: wget.c,v 1.30 2001/03/21 07:34:26 andersen Exp $ 722 * $Id: wget.c,v 1.31 2001/04/05 21:45:53 andersen Exp $
538 */ 723 */
539 724
540 725