diff mbox

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

Message ID 20161130164903.10693-1-lynxis@fe80.eu
State Changes Requested
Headers show

Commit Message

Alexander 'lynxis' Couzens Nov. 30, 2016, 4:49 p.m. UTC
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@fe80.eu>
---
 uclient-http.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

Comments

Felix Fietkau Nov. 30, 2016, 5:02 p.m. UTC | #1
On 2016-11-30 17:49, Alexander Couzens wrote:
> 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@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);
I think the comparison against the default port is unnecessary, just
include the port if it was provided as part of the URL.
I also don't like the duplication of the ustream_printf line.
How about:
"Host: %s%s%s" ...
url->port ? ":" : "",
url->port ? url->port : "",

- Felix
Alexander 'lynxis' Couzens Nov. 30, 2016, 5:04 p.m. UTC | #2
On Wed, 30 Nov 2016 18:02:53 +0100
Felix Fietkau <nbd@nbd.name> wrote:

> On 2016-11-30 17:49, Alexander Couzens wrote:
>  [...]  
> I think the comparison against the default port is unnecessary, just
> include the port if it was provided as part of the URL.
> I also don't like the duplication of the ustream_printf line.
> How about:
> "Host: %s%s%s" ...
> url->port ? ":" : "",
> url->port ? url->port : "",
looks better.

best,
lynxis
diff mbox

Patch

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));