]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
websockets: fix handling of partial frames
authorStefan Eissing <stefan@eissing.org>
Mon, 7 Nov 2022 09:11:17 +0000 (10:11 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 7 Nov 2022 11:29:43 +0000 (12:29 +0100)
buffer used and send length calculations are fixed when a partial
websocket frame has been received.

Closes #9861

lib/ws.c

index 7ae6a0bc009d8f5c29d5618c3018ac82bd7badc6..0f5f0ef6dd66fff8f3be5ea8cda40378389582d4 100644 (file)
--- a/lib/ws.c
+++ b/lib/ws.c
@@ -297,29 +297,28 @@ static CURLcode ws_decode(struct Curl_easy *data,
       p[9];
   }
 
-  total = dataindex + payloadsize;
-  if(total > plen) {
-    /* deliver a partial frame */
-    *oleft = total - dataindex;
-    payloadsize = total - dataindex;
-  }
-  else {
-    *oleft = 0;
-    if(plen > total)
-      /* there is another fragment after */
-      *more = TRUE;
-  }
-
   /* point to the payload */
-  *out = &p[dataindex];
-
-  /* return the payload length */
-  *olen = payloadsize;
-
-  /* number of bytes "used" from the buffer */
-  wsp->usedbuf = dataindex + payloadsize;
-  infof(data, "WS: received %zu bytes payload (%zu left)",
-        payloadsize, *oleft);
+   *out = &p[dataindex];
+   total = dataindex + payloadsize;
+   if(total > plen) {
+     /* buffer contains partial frame */
+     wsp->usedbuf = plen;      /* when written, whole buffer is used */
+     *olen = plen - dataindex; /* bytes to write out */
+     *oleft = total - plen;    /* bytes yet to come (for this frame) */
+     payloadsize = total - dataindex;
+   }
+   else {
+     /* we have the complete frame (`total` bytes) in buffer */
+     wsp->usedbuf = total;   /* when written, total frame has been used */
+     *olen = payloadsize;    /* bytes to write out */
+     *oleft = 0;             /* bytes yet to come (for this frame) */
+     if(plen > total)
+       /* there is another fragment after */
+       *more = TRUE;
+   }
+
+   infof(data, "WS: received %zu bytes payload (%zu left, buflen was %zu)",
+         payloadsize, *oleft, plen);
   return CURLE_OK;
 }