Core: improved JSON escaping.

Two-character representations are now used for \b, \f, \n, \r, and \t.
This commit is contained in:
Valentin Bartenev 2017-04-12 22:47:57 +03:00
parent 3664dfc498
commit 1bda840e2c

View file

@ -1808,7 +1808,19 @@ ngx_escape_json(u_char *dst, u_char *src, size_t size)
len++;
} else if (ch <= 0x1f) {
len += sizeof("\\u001F") - 2;
switch (ch) {
case '\n':
case '\r':
case '\t':
case '\b':
case '\f':
len++;
break;
default:
len += sizeof("\\u001F") - 2;
}
}
size--;
@ -1829,12 +1841,37 @@ ngx_escape_json(u_char *dst, u_char *src, size_t size)
*dst++ = ch;
} else {
*dst++ = '\\'; *dst++ = 'u'; *dst++ = '0'; *dst++ = '0';
*dst++ = '0' + (ch >> 4);
*dst++ = '\\';
ch &= 0xf;
switch (ch) {
case '\n':
*dst++ = 'n';
break;
*dst++ = (ch < 10) ? ('0' + ch) : ('A' + ch - 10);
case '\r':
*dst++ = 'r';
break;
case '\t':
*dst++ = 't';
break;
case '\b':
*dst++ = 'b';
break;
case '\f':
*dst++ = 'f';
break;
default:
*dst++ = 'u'; *dst++ = '0'; *dst++ = '0';
*dst++ = '0' + (ch >> 4);
ch &= 0xf;
*dst++ = (ch < 10) ? ('0' + ch) : ('A' + ch - 10);
}
}
size--;