From 73dda1f765b446989aeb9e71f9fe5f0fb3f72b88 Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Tue, 28 Feb 2017 04:03:26 +0800
Subject: [PATCH] client: take care of rx close during pending partial same as
 server does

https://github.com/warmcat/libwebsockets/issues/816
---
 lib/client-parser.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/lib/client-parser.c b/lib/client-parser.c
index 2151342c..ba1921bc 100644
--- a/lib/client-parser.c
+++ b/lib/client-parser.c
@@ -381,13 +381,22 @@ spill:
 					wsi->user_space, pp,
 					wsi->u.ws.rx_ubuf_head))
 				return -1;
-			/*
-			 * parrot the close packet payload back
-			 * we do not care about how it went, we are closing
-			 * immediately afterwards
-			 */
-			lws_write(wsi, (unsigned char *)&wsi->u.ws.rx_ubuf[LWS_PRE],
-				  wsi->u.ws.rx_ubuf_head, LWS_WRITE_CLOSE);
+			if (lws_partial_buffered(wsi))
+				/*
+				 * if we're in the middle of something,
+				 * we can't do a normal close response and
+				 * have to just close our end.
+				 */
+				wsi->socket_is_permanently_unusable = 1;
+			else
+				/*
+				 * parrot the close packet payload back
+				 * we do not care about how it went, we are closing
+				 * immediately afterwards
+				 */
+				lws_write(wsi, (unsigned char *)&wsi->u.ws.rx_ubuf[LWS_PRE],
+					  wsi->u.ws.rx_ubuf_head,
+					  LWS_WRITE_CLOSE);
 			wsi->state = LWSS_RETURNED_CLOSE_ALREADY;
 			/* close the connection */
 			return -1;