Fixed clean up of invalid sequences in WebSocket server.
This fixes the issue that when an invalid sequence is sent to the
client, the client would break the connection with the server.
@@ -137,20 +137,35 @@ sanitize_utf8 (const char *str, int len)
{
char *buf = NULL;
uint32_t state = UTF8_VALID, prev = UTF8_VALID, cp = 0;
- int i = 0, j = 0;
+ int i = 0, j = 0, k = 0, l = 0;
buf = xcalloc (len + 1, sizeof (char));
for (; i < len; prev = state, ++i) {
switch (utf8_decode (&state, &cp, (unsigned char) str[i])) {
case UTF8_INVAL:
+ /* replace the whole sequence */
+ if (k) {
+ for (l = i - k; l < i; ++l)
buf[j++] = '?';
+ } else {
+ buf[j++] = '?';
+ }
state = UTF8_VALID;
if (prev != UTF8_VALID)
- --i;
+ i--;
+ k = 0;
+ break;
+ case UTF8_VALID:
+ /* fill i - k valid continuation bytes */
+ if (k)
+ for (l = i - k; l < i; ++l)
+ buf[j++] = str[l];
+ buf[j++] = str[i];
+ k = 0;
break;
default:
/* UTF8_VALID + continuation bytes */
- buf[j++] = str[i];
+ k++;
break;
}
}
