aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-01-24 20:28:35 +0000
committerEric Andersen <andersen@codepoet.org>2001-01-24 20:28:35 +0000
commit7d69701d39323f31025a138e23757a2538d75983 (patch)
tree8106aa2436503cc07a7723ee4c9972b8d7738f5a
parent8c5cb31e1ba19ca1431d4cbe1d521cf3447dd0c6 (diff)
downloadbusybox-w32-7d69701d39323f31025a138e23757a2538d75983.tar.gz
busybox-w32-7d69701d39323f31025a138e23757a2538d75983.tar.bz2
busybox-w32-7d69701d39323f31025a138e23757a2538d75983.zip
Some behavioral updates to wget so it acts a bit nicer.
-Erik
-rw-r--r--networking/wget.c33
-rw-r--r--wget.c33
2 files changed, 42 insertions, 24 deletions
diff --git a/networking/wget.c b/networking/wget.c
index 77c72cbc3..419435362 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -47,6 +47,14 @@ volatile unsigned long statbytes; /* Number of bytes transferred so far. */
47/* For progressmeter() -- number of seconds before xfer considered "stalled" */ 47/* For progressmeter() -- number of seconds before xfer considered "stalled" */
48static const int STALLTIME = 5; 48static const int STALLTIME = 5;
49#endif 49#endif
50
51void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue)
52{
53 if (output != stdout && do_continue==0) {
54 fclose(output);
55 unlink(fname_out);
56 }
57}
50 58
51int wget_main(int argc, char **argv) 59int wget_main(int argc, char **argv)
52{ 60{
@@ -91,9 +99,6 @@ int wget_main(int argc, char **argv)
91 if (argc - optind != 1) 99 if (argc - optind != 1)
92 usage(wget_usage); 100 usage(wget_usage);
93 101
94 if (do_continue && !fname_out)
95 error_msg_and_die("cannot specify continue (-c) without a filename (-O)\n");
96
97 /* 102 /*
98 * Use the proxy if necessary. 103 * Use the proxy if necessary.
99 */ 104 */
@@ -129,6 +134,8 @@ int wget_main(int argc, char **argv)
129 curfile=argv[optind]; 134 curfile=argv[optind];
130#endif 135#endif
131 } 136 }
137 if (do_continue && !fname_out)
138 error_msg_and_die("cannot specify continue (-c) without a filename (-O)\n");
132 139
133 140
134 /* 141 /*
@@ -147,8 +154,7 @@ int wget_main(int argc, char **argv)
147 * Open the output file stream. 154 * Open the output file stream.
148 */ 155 */
149 if (fname_out != (char *)1) { 156 if (fname_out != (char *)1) {
150 if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) 157 if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) == NULL)
151 == NULL)
152 perror_msg_and_die("fopen(%s)", fname_out); 158 perror_msg_and_die("fopen(%s)", fname_out);
153 } else { 159 } else {
154 output = stdout; 160 output = stdout;
@@ -172,6 +178,7 @@ int wget_main(int argc, char **argv)
172 fprintf(sfp, "GET http://%s:%d/%s HTTP/1.1\r\n", 178 fprintf(sfp, "GET http://%s:%d/%s HTTP/1.1\r\n",
173 uri_host, uri_port, uri_path); 179 uri_host, uri_port, uri_path);
174 fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", uri_host); 180 fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", uri_host);
181
175 if (do_continue) 182 if (do_continue)
176 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range); 183 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
177 fprintf(sfp,"Connection: close\r\n\r\n"); 184 fprintf(sfp,"Connection: close\r\n\r\n");
@@ -180,6 +187,7 @@ int wget_main(int argc, char **argv)
180 * Retrieve HTTP response line and check for "200" status code. 187 * Retrieve HTTP response line and check for "200" status code.
181 */ 188 */
182 if (fgets(buf, sizeof(buf), sfp) == NULL) { 189 if (fgets(buf, sizeof(buf), sfp) == NULL) {
190 close_and_delete_outfile(output, fname_out, do_continue);
183 error_msg_and_die("no response from server\n"); 191 error_msg_and_die("no response from server\n");
184 } 192 }
185 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s) 193 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
@@ -187,28 +195,29 @@ int wget_main(int argc, char **argv)
187 for ( ; isspace(*s) ; ++s) 195 for ( ; isspace(*s) ; ++s)
188 ; 196 ;
189 switch (atoi(s)) { 197 switch (atoi(s)) {
198 case 0:
190 case 200: 199 case 200:
191 if (!do_continue) 200 break;
192 break;
193 error_msg_and_die("server does not support ranges\n");
194 case 206: 201 case 206:
195 if (do_continue) 202 if (do_continue)
196 break; 203 break;
197 /*FALLTHRU*/ 204 /*FALLTHRU*/
198 default: 205 default:
199 error_msg_and_die("server returned error: %s", buf); 206 close_and_delete_outfile(output, fname_out, do_continue);
207 error_msg_and_die("server returned error %d: %s", atoi(s), buf);
200 } 208 }
201 209
202 /* 210 /*
203 * Retrieve HTTP headers. 211 * Retrieve HTTP headers.
204 */ 212 */
205 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) { 213 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
206 if (strcmp(buf, "content-length") == 0) { 214 if (strcasecmp(buf, "content-length") == 0) {
207 filesize = atol(s); 215 filesize = atol(s);
208 got_clen = 1; 216 got_clen = 1;
209 continue; 217 continue;
210 } 218 }
211 if (strcmp(buf, "transfer-encoding") == 0) { 219 if (strcasecmp(buf, "transfer-encoding") == 0) {
220 close_and_delete_outfile(output, fname_out, do_continue);
212 error_msg_and_die("server wants to do %s transfer encoding\n", s); 221 error_msg_and_die("server wants to do %s transfer encoding\n", s);
213 continue; 222 continue;
214 } 223 }
@@ -511,7 +520,7 @@ progressmeter(int flag)
511 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 520 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
512 * SUCH DAMAGE. 521 * SUCH DAMAGE.
513 * 522 *
514 * $Id: wget.c,v 1.20 2001/01/24 18:44:54 andersen Exp $ 523 * $Id: wget.c,v 1.21 2001/01/24 20:28:35 andersen Exp $
515 */ 524 */
516 525
517 526
diff --git a/wget.c b/wget.c
index 77c72cbc3..419435362 100644
--- a/wget.c
+++ b/wget.c
@@ -47,6 +47,14 @@ volatile unsigned long statbytes; /* Number of bytes transferred so far. */
47/* For progressmeter() -- number of seconds before xfer considered "stalled" */ 47/* For progressmeter() -- number of seconds before xfer considered "stalled" */
48static const int STALLTIME = 5; 48static const int STALLTIME = 5;
49#endif 49#endif
50
51void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue)
52{
53 if (output != stdout && do_continue==0) {
54 fclose(output);
55 unlink(fname_out);
56 }
57}
50 58
51int wget_main(int argc, char **argv) 59int wget_main(int argc, char **argv)
52{ 60{
@@ -91,9 +99,6 @@ int wget_main(int argc, char **argv)
91 if (argc - optind != 1) 99 if (argc - optind != 1)
92 usage(wget_usage); 100 usage(wget_usage);
93 101
94 if (do_continue && !fname_out)
95 error_msg_and_die("cannot specify continue (-c) without a filename (-O)\n");
96
97 /* 102 /*
98 * Use the proxy if necessary. 103 * Use the proxy if necessary.
99 */ 104 */
@@ -129,6 +134,8 @@ int wget_main(int argc, char **argv)
129 curfile=argv[optind]; 134 curfile=argv[optind];
130#endif 135#endif
131 } 136 }
137 if (do_continue && !fname_out)
138 error_msg_and_die("cannot specify continue (-c) without a filename (-O)\n");
132 139
133 140
134 /* 141 /*
@@ -147,8 +154,7 @@ int wget_main(int argc, char **argv)
147 * Open the output file stream. 154 * Open the output file stream.
148 */ 155 */
149 if (fname_out != (char *)1) { 156 if (fname_out != (char *)1) {
150 if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) 157 if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) == NULL)
151 == NULL)
152 perror_msg_and_die("fopen(%s)", fname_out); 158 perror_msg_and_die("fopen(%s)", fname_out);
153 } else { 159 } else {
154 output = stdout; 160 output = stdout;
@@ -172,6 +178,7 @@ int wget_main(int argc, char **argv)
172 fprintf(sfp, "GET http://%s:%d/%s HTTP/1.1\r\n", 178 fprintf(sfp, "GET http://%s:%d/%s HTTP/1.1\r\n",
173 uri_host, uri_port, uri_path); 179 uri_host, uri_port, uri_path);
174 fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", uri_host); 180 fprintf(sfp, "Host: %s\r\nUser-Agent: Wget\r\n", uri_host);
181
175 if (do_continue) 182 if (do_continue)
176 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range); 183 fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range);
177 fprintf(sfp,"Connection: close\r\n\r\n"); 184 fprintf(sfp,"Connection: close\r\n\r\n");
@@ -180,6 +187,7 @@ int wget_main(int argc, char **argv)
180 * Retrieve HTTP response line and check for "200" status code. 187 * Retrieve HTTP response line and check for "200" status code.
181 */ 188 */
182 if (fgets(buf, sizeof(buf), sfp) == NULL) { 189 if (fgets(buf, sizeof(buf), sfp) == NULL) {
190 close_and_delete_outfile(output, fname_out, do_continue);
183 error_msg_and_die("no response from server\n"); 191 error_msg_and_die("no response from server\n");
184 } 192 }
185 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s) 193 for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
@@ -187,28 +195,29 @@ int wget_main(int argc, char **argv)
187 for ( ; isspace(*s) ; ++s) 195 for ( ; isspace(*s) ; ++s)
188 ; 196 ;
189 switch (atoi(s)) { 197 switch (atoi(s)) {
198 case 0:
190 case 200: 199 case 200:
191 if (!do_continue) 200 break;
192 break;
193 error_msg_and_die("server does not support ranges\n");
194 case 206: 201 case 206:
195 if (do_continue) 202 if (do_continue)
196 break; 203 break;
197 /*FALLTHRU*/ 204 /*FALLTHRU*/
198 default: 205 default:
199 error_msg_and_die("server returned error: %s", buf); 206 close_and_delete_outfile(output, fname_out, do_continue);
207 error_msg_and_die("server returned error %d: %s", atoi(s), buf);
200 } 208 }
201 209
202 /* 210 /*
203 * Retrieve HTTP headers. 211 * Retrieve HTTP headers.
204 */ 212 */
205 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) { 213 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
206 if (strcmp(buf, "content-length") == 0) { 214 if (strcasecmp(buf, "content-length") == 0) {
207 filesize = atol(s); 215 filesize = atol(s);
208 got_clen = 1; 216 got_clen = 1;
209 continue; 217 continue;
210 } 218 }
211 if (strcmp(buf, "transfer-encoding") == 0) { 219 if (strcasecmp(buf, "transfer-encoding") == 0) {
220 close_and_delete_outfile(output, fname_out, do_continue);
212 error_msg_and_die("server wants to do %s transfer encoding\n", s); 221 error_msg_and_die("server wants to do %s transfer encoding\n", s);
213 continue; 222 continue;
214 } 223 }
@@ -511,7 +520,7 @@ progressmeter(int flag)
511 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 520 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
512 * SUCH DAMAGE. 521 * SUCH DAMAGE.
513 * 522 *
514 * $Id: wget.c,v 1.20 2001/01/24 18:44:54 andersen Exp $ 523 * $Id: wget.c,v 1.21 2001/01/24 20:28:35 andersen Exp $
515 */ 524 */
516 525
517 526