svn commit: trunk/busybox: libbb networking

aldot at busybox.net aldot at busybox.net
Tue Apr 10 02:37:31 PDT 2007


Author: aldot
Date: 2007-04-10 02:37:29 -0700 (Tue, 10 Apr 2007)
New Revision: 18377

Log:
- add libbb function str_tolower to convert a string to lowercase.
- shrink wget a bit


Added:
   trunk/busybox/libbb/str_tolower.c

Modified:
   trunk/busybox/libbb/Kbuild
   trunk/busybox/networking/wget.c


Changeset:
Modified: trunk/busybox/libbb/Kbuild
===================================================================
--- trunk/busybox/libbb/Kbuild	2007-04-09 21:35:07 UTC (rev 18376)
+++ trunk/busybox/libbb/Kbuild	2007-04-10 09:37:29 UTC (rev 18377)
@@ -77,6 +77,7 @@
 lib-y += simplify_path.o
 lib-y += skip_whitespace.o
 lib-y += speed_table.o
+lib-y += str_tolower.o
 lib-y += trim.o
 lib-y += u_signal_names.o
 lib-y += udp_io.o

Added: trunk/busybox/libbb/str_tolower.c
===================================================================
--- trunk/busybox/libbb/str_tolower.c	                        (rev 0)
+++ trunk/busybox/libbb/str_tolower.c	2007-04-10 09:37:29 UTC (rev 18377)
@@ -0,0 +1,13 @@
+/* vi set: sw=4 ts=4: */
+/* Convert string str to lowercase, return str.
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+#include "libbb.h"
+char* str_tolower(char *str)
+{
+	char *c;
+	for (c = str; *c; ++c)
+		*c = tolower(*c);
+	return str;
+}

Modified: trunk/busybox/networking/wget.c
===================================================================
--- trunk/busybox/networking/wget.c	2007-04-09 21:35:07 UTC (rev 18376)
+++ trunk/busybox/networking/wget.c	2007-04-10 09:37:29 UTC (rev 18377)
@@ -34,7 +34,7 @@
 #if ENABLE_FEATURE_WGET_STATUSBAR
 static off_t transferred;        /* Number of bytes transferred so far */
 #endif
-static int chunked;                     /* chunked transfer encoding */
+static bool chunked;                     /* chunked transfer encoding */
 #if ENABLE_FEATURE_WGET_STATUSBAR
 static void progressmeter(int flag);
 static const char *curfile;             /* Name of current file being transferred */
@@ -76,9 +76,7 @@
 }
 
 #if ENABLE_FEATURE_WGET_AUTHENTICATION
-/*
- *  Base64-encode character string and return the string.
- */
+/* Base64-encode character string and return the string.  */
 static char *base64enc(unsigned char *p, char *buf, int len)
 {
 	bb_uuencode(p, buf, len, bb_uuenc_tbl_base64);
@@ -96,7 +94,7 @@
 	int port;
 	int try = 5;
 	unsigned opt;
-	char *s;
+	char *str;
 	char *proxy = 0;
 	char *dir_prefix = NULL;
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
@@ -104,21 +102,21 @@
 	llist_t *headers_llist = NULL;
 #endif
 
-	/* 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  */
+	bool got_clen = 0;               /* got content-length: from server  */
 	int output_fd = -1;
-	int use_proxy = 1;              /* Use proxies if env vars are set  */
+	bool use_proxy = 1;              /* Use proxies if env vars are set  */
 	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.
-	 */
+	const char *user_agent = "Wget";/* "User-Agent" header field        */
+	static const char * const keywords[] = {
+		"content-length", "transfer-encoding", "chunked", "location", NULL
+	};
 	enum {
+		KEY_content_length = 1, KEY_transfer_encoding, KEY_chunked, KEY_location
+	};
+	enum {
 		WGET_OPT_CONTINUE   = 0x1,
 		WGET_OPT_SPIDER	    = 0x2,
 		WGET_OPT_QUIET      = 0x4,
@@ -131,7 +129,7 @@
 	};
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
 	static const struct option wget_long_options[] = {
-		// name, has_arg, flag, val
+		/* name, has_arg, flag, val */
 		{ "continue",         no_argument, NULL, 'c' },
 		{ "spider",           no_argument, NULL, 's' },
 		{ "quiet",            no_argument, NULL, 'q' },
@@ -145,6 +143,7 @@
 	};
 	applet_long_options = wget_long_options;
 #endif
+	/* server.allocated = target.allocated = NULL; */
 	opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
 	opt = getopt32(argc, argv, "csqO:P:Y:U:",
 				&fname_out, &dir_prefix,
@@ -152,7 +151,7 @@
 				USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
 				);
 	if (strcmp(proxy_flag, "off") == 0) {
-		/* Use the proxy if necessary. */
+		/* Use the proxy if necessary */
 		use_proxy = 0;
 	}
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
@@ -176,9 +175,7 @@
 	server.host = target.host;
 	server.port = target.port;
 
-	/*
-	 * Use the proxy if necessary.
-	 */
+	/* Use the proxy if necessary */
 	if (use_proxy) {
 		proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
 		if (proxy && *proxy) {
@@ -217,9 +214,7 @@
 	if ((opt & WGET_OPT_CONTINUE) && !fname_out)
 		bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
 
-	/*
-	 * Determine where to start transfer.
-	 */
+	/* Determine where to start transfer */
 	if (LONE_DASH(fname_out)) {
 		output_fd = 1;
 		opt &= ~WGET_OPT_CONTINUE;
@@ -253,15 +248,11 @@
 			if (!--try)
 				bb_error_msg_and_die("too many redirections");
 
-			/*
-			 * Open socket to http server
-			 */
+			/* Open socket to http server */
 			if (sfp) fclose(sfp);
 			sfp = open_socket(lsa);
 
-			/*
-			 * Send HTTP request.
-			 */
+			/* Send HTTP request.  */
 			if (use_proxy) {
 				fprintf(sfp, "GET %stp://%s/%s HTTP/1.1\r\n",
 					target.is_ftp ? "f" : "ht", target.host,
@@ -299,12 +290,12 @@
 			if (fgets(buf, sizeof(buf), sfp) == NULL)
 				bb_error_msg_and_die("no response from server");
 
-			s = buf;
-			while (*s != '\0' && !isspace(*s)) ++s;
-			s = skip_whitespace(s);
+			str = buf;
+			str = skip_non_whitespace(str);
+			str = skip_whitespace(str);
 			// FIXME: no error check
 			// xatou wouldn't work: "200 OK"
-			status = atoi(s);
+			status = atoi(str);
 			switch (status) {
 			case 0:
 			case 100:
@@ -331,26 +322,28 @@
 			/*
 			 * Retrieve HTTP headers.
 			 */
-			while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
-				if (strcasecmp(buf, "content-length") == 0) {
-					content_len = BB_STRTOOFF(s, NULL, 10);
+			while ((str = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
+				/* gethdr did already convert the "FOO:" string to lowercase */
+				smalluint key = index_in_str_array(keywords, *&buf) + 1;
+				if (key == KEY_content_length) {
+					content_len = BB_STRTOOFF(str, NULL, 10);
 					if (errno || content_len < 0) {
-						bb_error_msg_and_die("content-length %s is garbage", s);
+						bb_error_msg_and_die("content-length %s is garbage", str);
 					}
 					got_clen = 1;
 					continue;
 				}
-				if (strcasecmp(buf, "transfer-encoding") == 0) {
-					if (strcasecmp(s, "chunked") != 0)
-						bb_error_msg_and_die("server wants to do %s transfer encoding", s);
+				if (key == KEY_transfer_encoding) {
+					if (index_in_str_array(keywords, str_tolower(str)) + 1 != KEY_chunked)
+						bb_error_msg_and_die("server wants to do %s transfer encoding", str);
 					chunked = got_clen = 1;
 				}
-				if (strcasecmp(buf, "location") == 0) {
-					if (s[0] == '/')
+				if (key == KEY_location) {
+					if (str[0] == '/')
 						/* free(target.allocated); */
-						target.path = /* target.allocated = */ xstrdup(s+1);
+						target.path = /* target.allocated = */ xstrdup(str+1);
 					else {
-						parse_url(s, &target);
+						parse_url(str, &target);
 						if (use_proxy == 0) {
 							server.host = target.host;
 							server.port = target.port;
@@ -381,14 +374,14 @@
 		 * Splitting username:password pair,
 		 * trying to log in
 		 */
-		s = strchr(target.user, ':');
-		if (s)
-			*(s++) = '\0';
+		str = strchr(target.user, ':');
+		if (str)
+			*(str++) = '\0';
 		switch (ftpcmd("USER ", target.user, sfp, buf)) {
 		case 230:
 			break;
 		case 331:
-			if (ftpcmd("PASS ", s, sfp, buf) == 230)
+			if (ftpcmd("PASS ", str, sfp, buf) == 230)
 				break;
 			/* FALLTHRU (failed login) */
 		default:
@@ -418,15 +411,15 @@
 		// Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage]
 		// Server's IP is N1.N2.N3.N4 (we ignore it)
 		// Server's port for data connection is P1*256+P2
-		s = strrchr(buf, ')');
-		if (s) s[0] = '\0';
-		s = strrchr(buf, ',');
-		if (!s) goto pasv_error;
-		port = xatou_range(s+1, 0, 255);
-		*s = '\0';
-		s = strrchr(buf, ',');
-		if (!s) goto pasv_error;
-		port += xatou_range(s+1, 0, 255) * 256;
+		str = strrchr(buf, ')');
+		if (str) str[0] = '\0';
+		str = strrchr(buf, ',');
+		if (!str) goto pasv_error;
+		port = xatou_range(str+1, 0, 255);
+		*str = '\0';
+		str = strrchr(buf, ',');
+		if (!str) goto pasv_error;
+		port += xatou_range(str+1, 0, 255) * 256;
 		set_nport(lsa, htons(port));
 		dfp = open_socket(lsa);
 



More information about the busybox-cvs mailing list