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

Felix Fietkau nbd at nbd.name
Wed Nov 30 09:02:53 PST 2016


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




More information about the Lede-dev mailing list