diff options
author | Eric Andersen <andersen@codepoet.org> | 2000-12-09 16:55:35 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2000-12-09 16:55:35 +0000 |
commit | 29edd005f9e16d66f8fe85ac81da39d7d4311bc7 (patch) | |
tree | 82f333450c721f91dc09daf2f791f19c5c51f84a /networking/wget.c | |
parent | afdde3e356bb621f230e4ef556609368647399e6 (diff) | |
download | busybox-w32-29edd005f9e16d66f8fe85ac81da39d7d4311bc7.tar.gz busybox-w32-29edd005f9e16d66f8fe85ac81da39d7d4311bc7.tar.bz2 busybox-w32-29edd005f9e16d66f8fe85ac81da39d7d4311bc7.zip |
More wget cleanups I've been working on...
Diffstat (limited to 'networking/wget.c')
-rw-r--r-- | networking/wget.c | 83 |
1 files changed, 53 insertions, 30 deletions
diff --git a/networking/wget.c b/networking/wget.c index 9e150769c..50b9aeec7 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
@@ -50,19 +50,21 @@ volatile unsigned long statbytes; /* Number of bytes transferred so far. */ | |||
50 | 50 | ||
51 | int wget_main(int argc, char **argv) | 51 | int wget_main(int argc, char **argv) |
52 | { | 52 | { |
53 | FILE *sfp; /* socket to web server */ | ||
54 | char *uri_host, *uri_path; /* parsed from command line url */ | ||
55 | char *proxy; | ||
56 | int uri_port; | ||
57 | char *s, buf[512]; | ||
58 | int n; | 53 | int n; |
54 | char *proxy, *proxy_host; | ||
55 | int uri_port, proxy_port; | ||
56 | char *s, buf[512]; | ||
57 | struct stat sbuf; | ||
59 | 58 | ||
59 | FILE *sfp; /* socket to web server */ | ||
60 | char *uri_host, *uri_path; /* parsed from command line url */ | ||
60 | char *fname_out = NULL; /* where to direct output (-O) */ | 61 | char *fname_out = NULL; /* where to direct output (-O) */ |
61 | int do_continue = 0; /* continue a prev transfer (-c) */ | 62 | int do_continue = 0; /* continue a prev transfer (-c) */ |
62 | long beg_range = 0L; /* range at which continue begins */ | 63 | long beg_range = 0L; /* range at which continue begins */ |
63 | int got_clen = 0; /* got content-length: from server */ | 64 | int got_clen = 0; /* got content-length: from server */ |
64 | FILE *output; /* socket to web server */ | 65 | FILE *output; /* socket to web server */ |
65 | int quiet_flag = FALSE; | 66 | int quiet_flag = FALSE; /* Be verry, verry quiet... */ |
67 | |||
66 | /* | 68 | /* |
67 | * Crack command line. | 69 | * Crack command line. |
68 | */ | 70 | */ |
@@ -89,20 +91,6 @@ int wget_main(int argc, char **argv) | |||
89 | if (argc - optind != 1) | 91 | if (argc - optind != 1) |
90 | usage(wget_usage); | 92 | usage(wget_usage); |
91 | 93 | ||
92 | /* Guess an output filename */ | ||
93 | if (!fname_out) { | ||
94 | fname_out = | ||
95 | #ifdef BB_FEATURE_STATUSBAR | ||
96 | curfile = | ||
97 | #endif | ||
98 | get_last_path_component(argv[optind]); | ||
99 | #ifdef BB_FEATURE_STATUSBAR | ||
100 | } else { | ||
101 | curfile=argv[optind]; | ||
102 | #endif | ||
103 | } | ||
104 | |||
105 | |||
106 | if (do_continue && !fname_out) | 94 | if (do_continue && !fname_out) |
107 | error_msg_and_die("cannot specify continue (-c) without a filename (-O)\n"); | 95 | error_msg_and_die("cannot specify continue (-c) without a filename (-O)\n"); |
108 | 96 | ||
@@ -111,22 +99,52 @@ int wget_main(int argc, char **argv) | |||
111 | */ | 99 | */ |
112 | if ((proxy = getenv("http_proxy")) != NULL) { | 100 | if ((proxy = getenv("http_proxy")) != NULL) { |
113 | proxy = xstrdup(proxy); | 101 | proxy = xstrdup(proxy); |
114 | parse_url(proxy, &uri_host, &uri_port, &uri_path); | 102 | parse_url(proxy, &proxy_host, &proxy_port, &uri_path); |
115 | uri_path = argv[optind]; | 103 | parse_url(argv[optind], &uri_host, &uri_port, &uri_path); |
116 | } else { | 104 | } else { |
117 | /* | 105 | /* |
118 | * Parse url into components. | 106 | * Parse url into components. |
119 | */ | 107 | */ |
120 | parse_url(argv[optind], &uri_host, &uri_port, &uri_path); | 108 | parse_url(argv[optind], &uri_host, &uri_port, &uri_path); |
109 | proxy_host=uri_host; | ||
110 | proxy_port=uri_port; | ||
111 | } | ||
112 | |||
113 | /* Guess an output filename */ | ||
114 | if (!fname_out) { | ||
115 | fname_out = | ||
116 | #ifdef BB_FEATURE_STATUSBAR | ||
117 | curfile = | ||
118 | #endif | ||
119 | get_last_path_component(uri_path); | ||
120 | if (fname_out==NULL || strlen(fname_out)<1) { | ||
121 | fname_out = | ||
122 | #ifdef BB_FEATURE_STATUSBAR | ||
123 | curfile = | ||
124 | #endif | ||
125 | "index.html"; | ||
126 | } | ||
127 | #ifdef BB_FEATURE_STATUSBAR | ||
128 | } else { | ||
129 | curfile=argv[optind]; | ||
130 | #endif | ||
121 | } | 131 | } |
122 | 132 | ||
133 | |||
123 | /* | 134 | /* |
124 | * Open socket to server. | 135 | * Open socket to server. |
125 | */ | 136 | */ |
126 | sfp = open_socket(uri_host, uri_port); | 137 | sfp = open_socket(proxy_host, proxy_port); |
138 | |||
139 | /* Make the assumption that if the file already exists | ||
140 | * on disk that the intention is to continue downloading | ||
141 | * a previously aborted download -Erik */ | ||
142 | if (stat(fname_out, &sbuf) == 0) { | ||
143 | ++do_continue; | ||
144 | } | ||
127 | 145 | ||
128 | /* | 146 | /* |
129 | * Open the output stream. | 147 | * Open the output file stream. |
130 | */ | 148 | */ |
131 | if (fname_out != (char *)1) { | 149 | if (fname_out != (char *)1) { |
132 | if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) | 150 | if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) |
@@ -140,7 +158,6 @@ int wget_main(int argc, char **argv) | |||
140 | * Determine where to start transfer. | 158 | * Determine where to start transfer. |
141 | */ | 159 | */ |
142 | if (do_continue) { | 160 | if (do_continue) { |
143 | struct stat sbuf; | ||
144 | if (fstat(fileno(output), &sbuf) < 0) | 161 | if (fstat(fileno(output), &sbuf) < 0) |
145 | error_msg_and_die("fstat()"); | 162 | error_msg_and_die("fstat()"); |
146 | if (sbuf.st_size > 0) | 163 | if (sbuf.st_size > 0) |
@@ -152,16 +169,19 @@ int wget_main(int argc, char **argv) | |||
152 | /* | 169 | /* |
153 | * Send HTTP request. | 170 | * Send HTTP request. |
154 | */ | 171 | */ |
155 | fprintf(sfp, "GET %s HTTP/1.1\r\nHost: %s\r\n", uri_path, uri_host); | 172 | fprintf(sfp, "GET http://%s:%d/%s HTTP/1.1\r\n", |
173 | uri_host, uri_port, uri_path); | ||
174 | fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", uri_host); | ||
156 | if (do_continue) | 175 | if (do_continue) |
157 | fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range); | 176 | fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range); |
158 | fputs("Connection: close\r\n\r\n", sfp); | 177 | fprintf(sfp,"Connection: close\r\n\r\n"); |
159 | 178 | ||
160 | /* | 179 | /* |
161 | * Retrieve HTTP response line and check for "200" status code. | 180 | * Retrieve HTTP response line and check for "200" status code. |
162 | */ | 181 | */ |
163 | if (fgets(buf, sizeof(buf), sfp) == NULL) | 182 | if (fgets(buf, sizeof(buf), sfp) == NULL) { |
164 | error_msg_and_die("no response from server\n"); | 183 | error_msg_and_die("no response from server\n"); |
184 | } | ||
165 | for (s = buf ; *s != '\0' && !isspace(*s) ; ++s) | 185 | for (s = buf ; *s != '\0' && !isspace(*s) ; ++s) |
166 | ; | 186 | ; |
167 | for ( ; isspace(*s) ; ++s) | 187 | for ( ; isspace(*s) ; ++s) |
@@ -243,6 +263,9 @@ void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path) | |||
243 | 263 | ||
244 | *uri_host = url; | 264 | *uri_host = url; |
245 | *uri_path = h; | 265 | *uri_path = h; |
266 | |||
267 | if (!strcmp( *uri_host, *uri_path)) | ||
268 | *uri_path = defaultpath; | ||
246 | } | 269 | } |
247 | 270 | ||
248 | 271 | ||
@@ -491,7 +514,7 @@ progressmeter(int flag) | |||
491 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 514 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
492 | * SUCH DAMAGE. | 515 | * SUCH DAMAGE. |
493 | * | 516 | * |
494 | * $Id: wget.c,v 1.12 2000/12/09 08:12:06 bug1 Exp $ | 517 | * $Id: wget.c,v 1.13 2000/12/09 16:55:35 andersen Exp $ |
495 | */ | 518 | */ |
496 | 519 | ||
497 | 520 | ||