From 0b3e9e62cbc4824a804fff403ccbcdb744770772 Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Fri, 10 Feb 2017 11:00:38 +0800
Subject: [PATCH] client redirect: make sure there is a leading / on path

---
 lib/client-handshake.c | 15 ++++++++++-----
 lib/client.c           |  2 --
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/lib/client-handshake.c b/lib/client-handshake.c
index 306c3928..9d4b573d 100644
--- a/lib/client-handshake.c
+++ b/lib/client-handshake.c
@@ -372,9 +372,11 @@ failed1:
  * host:	host header to send to the new server
  */
 LWS_VISIBLE struct lws *
-lws_client_reset(struct lws *wsi, int ssl, const char *address, int port, const char *path, const char *host)
+lws_client_reset(struct lws *wsi, int ssl, const char *address, int port,
+		 const char *path, const char *host)
 {
 	char origin[300] = "", protocol[300] = "", method[32] = "", *p;
+
 	if (wsi->u.hdr.redirects == 3) {
 		lwsl_err("%s: Too many redirects\n", __func__);
 		return NULL;
@@ -402,7 +404,8 @@ lws_client_reset(struct lws *wsi, int ssl, const char *address, int port, const
 	if (p)
 		strncpy(method, p, sizeof(method) - 1);
 
-	lwsl_notice("redirect ads='%s', port=%d, path='%s', ssl = %d\n", address, port, path, ssl);
+	lwsl_debug("redirect ads='%s', port=%d, path='%s', ssl = %d\n",
+		   address, port, path, ssl);
 
 	/* close the connection by hand */
 
@@ -420,9 +423,6 @@ lws_client_reset(struct lws *wsi, int ssl, const char *address, int port, const
 	if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS, address))
 		return NULL;
 
-	if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, path))
-		return NULL;
-
 	if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_HOST, host))
 		return NULL;
 
@@ -439,6 +439,11 @@ lws_client_reset(struct lws *wsi, int ssl, const char *address, int port, const
 					  method))
 			return NULL;
 
+	origin[0] = '/';
+	strncpy(&origin[1], path, sizeof(origin) - 2);
+	if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_URI, origin))
+		return NULL;
+
 	return lws_client_connect_2(wsi);
 }
 
diff --git a/lib/client.c b/lib/client.c
index 4ffb1fac..4cbe2d5c 100755
--- a/lib/client.c
+++ b/lib/client.c
@@ -483,8 +483,6 @@ lws_client_interpret_server_handshake(struct lws *wsi)
 		if (!strcmp(prot, "wss") || !strcmp(prot, "https"))
 			ssl = 1;
 
-		lwsl_notice("ssl %d %s\n", ssl, prot);
-
 		if (!lws_client_reset(wsi, ssl, ads, port, path, ads)) {
 			lwsl_err("Redirect failed\n");
 			cce = "HS: Redirect failed";
-- 
GitLab