[PATCHv7 15/15] net/lwip/wget add port selection

Maxim Uvarov maxim.uvarov at linaro.org
Tue Aug 22 11:36:14 CEST 2023


Allow to specify HTTP port instead of just using default for wget command.

Signed-off-by: Maxim Uvarov <maxim.uvarov at linaro.org>
---
 include/net/lwip.h             |  2 +-
 net/lwip/apps/http/lwip-wget.c | 45 +++++++++++++++++++++++++---------
 2 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/include/net/lwip.h b/include/net/lwip.h
index aa82d71715..c66551a14c 100644
--- a/include/net/lwip.h
+++ b/include/net/lwip.h
@@ -54,7 +54,7 @@ int ulwip_tftp(ulong addr, const char *filename);
  *
  *
  * @addr:  start address to download result
- * @url:   url in format http://host/url
+ * @url:   url in format http://host[:port]/url
  * Returns: 0 for success, !0 if error
 */
 int ulwip_wget(ulong addr, char *url);
diff --git a/net/lwip/apps/http/lwip-wget.c b/net/lwip/apps/http/lwip-wget.c
index a9bb29e156..01aa146b13 100644
--- a/net/lwip/apps/http/lwip-wget.c
+++ b/net/lwip/apps/http/lwip-wget.c
@@ -65,19 +65,42 @@ static int parse_url(char *url, char *host, u16 *port)
 	p += strlen("http://");
 
 	/* parse hostname */
-	pp = strchr(p, '/');
-	if (!pp) {
-		printf("wrong url\n");
-		return -2;
+	pp = strchr(p, ':');
+	if (pp) {
+#define PORT_STR_SIZE 5
+		char portstr[PORT_STR_SIZE];
+
+		if (pp - p >= SERVER_NAME_SIZE)
+			return -2;
+		memcpy(host, p, pp - p);
+		host[pp - p + 1] = '\0';
+
+		p = pp + 1;
+		pp = strchr(p, '/');
+		if (!pp) {
+			printf("wrong url\n");
+			return -3;
+		}
+
+		if (pp - p >= PORT_STR_SIZE)
+                        return -4;
+		memcpy(portstr, p, pp - p);
+		portstr[pp - p] = '\0';
+		*port = (u16)dectoul(portstr, NULL);
+	} else {
+		pp = strchr(p, '/');
+		if (!pp) {
+			printf("wrong url\n");
+			return -5;
+		}
+
+		if (pp - p >= SERVER_NAME_SIZE)
+			return -6;
+		memcpy(host, p, pp - p);
+		host[pp - p + 1] = '\0';
+		*port = HTTP_PORT_DEFAULT;
 	}
 
-	if (pp - p >= SERVER_NAME_SIZE)
-		return -3;
-
-	memcpy(host, p, pp - p);
-	host[pp - p + 1] = '\0';
-	*port = HTTP_PORT_DEFAULT;
-
 	return 0;
 }
 
-- 
2.30.2



More information about the U-Boot mailing list