[LEDE-DEV] [PATCH][uclient] uclient-http: send correct "Host:" header if port is not the default

Alexander Couzens lynxis at fe80.eu
Wed Nov 30 08:49:03 PST 2016


When connecting to a website with a special port, uclient-fetch connects to the correct
port, but is sending an incorrect Host: header without the corresponding port.

Signed-off-by: Alexander Couzens <lynxis at fe80.eu>
---
 uclient-http.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/uclient-http.c b/uclient-http.c
index 899f367..da58cda 100644
--- a/uclient-http.c
+++ b/uclient-http.c
@@ -560,6 +560,7 @@ uclient_http_send_headers(struct uclient_http *uh)
 	struct blob_attr *cur;
 	enum request_type req_type = uh->req_type;
 	int rem;
+	char *default_port = "";
 
 	if (uh->state >= HTTP_STATE_HEADERS_SENT)
 		return;
@@ -567,11 +568,25 @@ uclient_http_send_headers(struct uclient_http *uh)
 	if (uh->uc.proxy_url)
 		url = uh->uc.proxy_url;
 
-	ustream_printf(uh->us,
-		"%s %s HTTP/1.1\r\n"
-		"Host: %s\r\n",
-		request_types[req_type],
-		url->location, url->host);
+	if (uh->uc.url->prefix == PREFIX_HTTP)
+		default_port = "80";
+	else if (uh->uc.url->prefix == PREFIX_HTTPS)
+		default_port = "443";
+
+	/* append only the port if given and different from default port */
+	if (uh->uc.url->port && strcmp(default_port, uh->uc.url->port)) {
+		ustream_printf(uh->us,
+			       "%s %s HTTP/1.1\r\n"
+			       "Host: %s:%s\r\n",
+			       request_types[req_type],
+			       url->location, url->host, url->port);
+	} else {
+		ustream_printf(uh->us,
+			       "%s %s HTTP/1.1\r\n"
+			       "Host: %s\r\n",
+			       request_types[req_type],
+			       url->location, url->host);
+	}
 
 	blobmsg_for_each_attr(cur, uh->headers.head, rem)
 		ustream_printf(uh->us, "%s: %s\r\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
-- 
2.10.2




More information about the Lede-dev mailing list