Module Name:    src
Committed By:   christos
Date:           Thu Dec 17 17:26:46 UTC 2015

Modified Files:
        src/usr.bin/ftp: fetch.c

Log Message:
Split the position/size parsing into a separate function.


To generate a diff of this commit:
cvs rdiff -u -r1.217 -r1.218 src/usr.bin/ftp/fetch.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/ftp/fetch.c
diff -u src/usr.bin/ftp/fetch.c:1.217 src/usr.bin/ftp/fetch.c:1.218
--- src/usr.bin/ftp/fetch.c:1.217	Thu Dec 17 12:08:45 2015
+++ src/usr.bin/ftp/fetch.c	Thu Dec 17 12:26:45 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: fetch.c,v 1.217 2015/12/17 17:08:45 christos Exp $	*/
+/*	$NetBSD: fetch.c,v 1.218 2015/12/17 17:26:45 christos Exp $	*/
 
 /*-
  * Copyright (c) 1997-2015 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: fetch.c,v 1.217 2015/12/17 17:08:45 christos Exp $");
+__RCSID("$NetBSD: fetch.c,v 1.218 2015/12/17 17:26:45 christos Exp $");
 #endif /* not lint */
 
 /*
@@ -94,6 +94,12 @@ struct urlinfo {
 	in_port_t portnum;
 };
 
+struct posinfo {
+	off_t rangestart;
+	off_t rangeend;
+	off_t entitylen;
+};
+
 __dead static void	aborthttp(int);
 __dead static void	timeouthttp(int);
 #ifndef NO_AUTH
@@ -161,6 +167,12 @@ match_token(const char **buf, const char
 }
 
 static void
+initposinfo(struct posinfo *pi)
+{
+	pi->rangestart = pi->rangeend = pi->entitylen = -1;
+}
+
+static void
 initauthinfo(struct authinfo *ai, char *auth)
 {
 	ai->auth = auth;
@@ -890,10 +902,56 @@ getresponse(FETCH *fin, char **cp, size_
 }
 
 static int
+parse_posinfo(const char **cp, struct posinfo *pi)
+{
+	char *ep;
+	if (!match_token(cp, "bytes"))
+		return -1;
+
+	if (**cp == '*')
+		(*cp)++;
+	else {
+		pi->rangestart = STRTOLL(*cp, &ep, 10);
+		if (pi->rangestart < 0 || *ep != '-')
+			return -1;
+		*cp = ep + 1;
+		pi->rangeend = STRTOLL(*cp, &ep, 10);
+		if (pi->rangeend < 0 || pi->rangeend < pi->rangestart)
+			return -1;
+		*cp = ep;
+	}
+	if (**cp != '/')
+		return -1;
+	(*cp)++;
+	if (**cp == '*')
+		(*cp)++;
+	else {
+		pi->entitylen = STRTOLL(*cp, &ep, 10);
+		if (pi->entitylen < 0)
+			return -1;
+		*cp = ep;
+	}
+	if (**cp != '\0')
+		return -1;
+
+#ifndef NO_DEBUG
+	if (ftp_debug) {
+		fprintf(ttyout, "parsed range as: ");
+		if (pi->rangestart == -1)
+			fprintf(ttyout, "*");
+		else
+			fprintf(ttyout, LLF "-" LLF, (LLT)pi->rangestart,
+			    (LLT)pi->rangeend);
+		fprintf(ttyout, "/" LLF "\n", (LLT)pi->entitylen);
+	}
+#endif
+	return 0;
+}
+
+static int
 negotiate_connection(FETCH *fin, const char *url, const char *penv,
-    off_t *rangestart, off_t *rangeend, off_t *entitylen,
-    time_t *mtime, struct authinfo *wauth, struct authinfo *pauth,
-    int *rval, int *ischunked, char **auth)
+    struct posinfo *pi, time_t *mtime, struct authinfo *wauth,
+    struct authinfo *pauth, int *rval, int *ischunked, char **auth)
 {
 	int			len, hcode, rv;
 	char			buf[FTPBUFLEN], *ep;
@@ -936,47 +994,8 @@ negotiate_connection(FETCH *fin, const c
 			    __func__, (LLT)filesize);
 
 		} else if (match_token(&cp, "Content-Range:")) {
-			if (! match_token(&cp, "bytes"))
+			if (parse_posinfo(&cp, pi) == -1)
 				goto improper;
-
-			if (*cp == '*')
-				cp++;
-			else {
-				*rangestart = STRTOLL(cp, &ep, 10);
-				if (*rangestart < 0 || *ep != '-')
-					goto improper;
-				cp = ep + 1;
-				*rangeend = STRTOLL(cp, &ep, 10);
-				if (*rangeend < 0 || *rangeend < *rangestart)
-					goto improper;
-				cp = ep;
-			}
-			if (*cp != '/')
-				goto improper;
-			cp++;
-			if (*cp == '*')
-				cp++;
-			else {
-				*entitylen = STRTOLL(cp, &ep, 10);
-				if (*entitylen < 0)
-					goto improper;
-				cp = ep;
-			}
-			if (*cp != '\0')
-				goto improper;
-
-#ifndef NO_DEBUG
-			if (ftp_debug) {
-				fprintf(ttyout, "parsed range as: ");
-				if (*rangestart == -1)
-					fprintf(ttyout, "*");
-				else
-					fprintf(ttyout, LLF "-" LLF,
-					    (LLT)*rangestart,
-					    (LLT)*rangeend);
-				fprintf(ttyout, "/" LLF "\n", (LLT)*entitylen);
-			}
-#endif
 			if (! restart_point) {
 				warnx(
 			    "Received unexpected Content-Range header");
@@ -1248,7 +1267,8 @@ fetch_url(const char *url, const char *p
 	char			*volatile message;
 	char			*volatile decodedpath;
 	struct authinfo 	wauth, pauth;
-	off_t			hashbytes, rangestart, rangeend, entitylen;
+	struct posinfo		pi;
+	off_t			hashbytes;
 	int			(*volatile closefunc)(FILE *);
 	FETCH			*volatile fin;
 	FILE			*volatile fout;
@@ -1325,7 +1345,7 @@ fetch_url(const char *url, const char *p
 
 	restart_point = 0;
 	filesize = -1;
-	rangestart = rangeend = entitylen = -1;
+	initposinfo(&pi);
 	mtime = -1;
 	if (restartautofetch) {
 		if (stat(savefile, &sb) == 0)
@@ -1452,8 +1472,7 @@ fetch_url(const char *url, const char *p
 		}
 		alarmtimer(0);
 
-		switch (negotiate_connection(fin, url, penv,
-		    &rangestart, &rangeend, &entitylen,
+		switch (negotiate_connection(fin, url, penv, &pi,
 		    &mtime, &wauth, &pauth, &rval, &ischunked, &auth)) {
 		case C_OK:
 			break;
@@ -1486,18 +1505,19 @@ fetch_url(const char *url, const char *p
 		}
 	}
 	if (fout == NULL) {
-		if ((rangeend != -1 && rangeend <= restart_point) ||
-		    (rangestart == -1 && filesize != -1 && filesize <= restart_point)) {
+		if ((pi.rangeend != -1 && pi.rangeend <= restart_point) ||
+		    (pi.rangestart == -1 &&
+		    filesize != -1 && filesize <= restart_point)) {
 			/* already done */
 			if (verbose)
 				fprintf(ttyout, "already done\n");
 			rval = 0;
 			goto cleanup_fetch_url;
 		}
-		if (restart_point && rangestart != -1) {
-			if (entitylen != -1)
-				filesize = entitylen;
-			if (rangestart != restart_point) {
+		if (restart_point && pi.rangestart != -1) {
+			if (pi.entitylen != -1)
+				filesize = pi.entitylen;
+			if (pi.rangestart != restart_point) {
 				warnx(
 				    "Size of `%s' differs from save file `%s'",
 				    url, savefile);

Reply via email to