svn commit: trunk/busybox/networking

vda at busybox.net vda at busybox.net
Sat Oct 7 14:28:56 UTC 2006


Author: vda
Date: 2006-10-07 07:28:55 -0700 (Sat, 07 Oct 2006)
New Revision: 16337

Log:
wget: don't be careless with xstrdup'ing


Modified:
   trunk/busybox/networking/wget.c


Changeset:
Modified: trunk/busybox/networking/wget.c
===================================================================
--- trunk/busybox/networking/wget.c	2006-10-07 14:28:28 UTC (rev 16336)
+++ trunk/busybox/networking/wget.c	2006-10-07 14:28:55 UTC (rev 16337)
@@ -30,6 +30,8 @@
 #endif
 
 struct host_info {
+	// May be used if we ever will want to free() all xstrdup()s...
+	/* char *allocated; */
 	char *host;
 	int port;
 	char *path;
@@ -136,14 +138,16 @@
 	struct sockaddr_in s_in;
 	llist_t *headers_llist = NULL;
 
+	/* server.allocated = target.allocated = NULL; */
+
 	FILE *sfp = NULL;               /* socket to web/ftp server         */
 	FILE *dfp = NULL;               /* socket to ftp server (data)      */
 	char *fname_out = NULL;         /* where to direct output (-O)      */
 	int got_clen = 0;               /* got content-length: from server  */
 	int output_fd = -1;
 	int use_proxy = 1;              /* Use proxies if env vars are set  */
-	char *proxy_flag = "on";        /* Use proxies if env vars are set  */
-	char *user_agent = "Wget";      /* Content of the "User-Agent" header field */
+	const char *proxy_flag = "on";  /* Use proxies if env vars are set  */
+	const char *user_agent = "Wget";/* Content of the "User-Agent" header field */
 
 	/*
 	 * Crack command line.
@@ -185,7 +189,7 @@
 	if (use_proxy) {
 		proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
 		if (proxy && *proxy) {
-			parse_url(xstrdup(proxy), &server);
+			parse_url(proxy, &server);
 		} else {
 			use_proxy = 0;
 		}
@@ -306,14 +310,14 @@
 			if (fgets(buf, sizeof(buf), sfp) == NULL)
 				bb_error_msg_and_die("no response from server");
 
-			for (s = buf ; *s != '\0' && !isspace(*s) ; ++s)
-				;
-			for ( ; isspace(*s) ; ++s)
-				;
+			s = buf;
+			while (*s != '\0' && !isspace(*s)) ++s;
+			while (isspace(*s)) ++s;
 			switch (status = atoi(s)) {
 				case 0:
 				case 100:
-					while (gethdr(buf, sizeof(buf), sfp, &n) != NULL);
+					while (gethdr(buf, sizeof(buf), sfp, &n) != NULL)
+						/* eat all remaining headers */;
 					goto read_response;
 				case 200:
 					break;
@@ -328,7 +332,7 @@
 					/*FALLTHRU*/
 				default:
 					chomp(buf);
-					bb_error_msg_and_die("server returned error %d: %s", atoi(s), buf);
+					bb_error_msg_and_die("server returned error %s: %s", s, buf);
 			}
 
 			/*
@@ -351,10 +355,10 @@
 				}
 				if (strcasecmp(buf, "location") == 0) {
 					if (s[0] == '/')
-						// FIXME: this is dirty
-						target.path = xstrdup(s+1);
+						/* free(target.allocated); */
+						target.path = /* target.allocated = */ xstrdup(s+1);
 					else {
-						parse_url(xstrdup(s), &target);
+						parse_url(s, &target);
 						if (use_proxy == 0) {
 							server.host = target.host;
 							server.port = target.port;
@@ -368,9 +372,9 @@
 		} while(status >= 300);
 
 		dfp = sfp;
-	}
-	else
-	{
+
+	} else {
+
 		/*
 		 *  FTP session
 		 */
@@ -499,10 +503,12 @@
 }
 
 
-static void parse_url(char *url, struct host_info *h)
+static void parse_url(char *src_url, struct host_info *h)
 {
-	char *p, *cp, *sp, *up, *pp;
+	char *url, *p, *cp, *sp, *up, *pp;
 
+	/* h->allocated = */ url = xstrdup(src_url);
+
 	if (strncmp(url, "http://", 7) == 0) {
 		h->port = bb_lookup_port("http", "tcp", 80);
 		h->host = url + 7;
@@ -517,7 +523,7 @@
 	// FYI:
 	// "Real" wget 'http://busybox.net?var=a/b' sends this request:
 	//   'GET /?var=a/b HTTP 1.0'
-	//   and saves 'index.html?var=a%2Fb'
+	//   and saves 'index.html?var=a%2Fb' (we save 'b')
 	// wget 'http://busybox.net?login=john@doe':
 	//   request: 'GET /?login=john at doe HTTP/1.0'
 	//   saves: 'index.html?login=john at doe' (we save ?login=john at doe)




More information about the busybox-cvs mailing list