diff options
author | Valentin Bartenev <vbart@nginx.com> | 2017-04-12 22:47:57 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2017-04-12 22:47:57 +0300 |
commit | 97cb30370f6858f33ad37653ffcbccc43026bfff (patch) | |
tree | 1c4a78885aa8fbed3cf894555338107bd60fea30 /src/core/ngx_string.c | |
parent | cac361718c741fd2bfe13d43aa2fabf23d0a2ca4 (diff) |
Core: improved JSON escaping.
Two-character representations are now used for \b, \f, \n, \r, and \t.
Diffstat (limited to 'src/core/ngx_string.c')
-rw-r--r-- | src/core/ngx_string.c | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/src/core/ngx_string.c b/src/core/ngx_string.c index 7a73ef527..7526f60d2 100644 --- a/src/core/ngx_string.c +++ b/src/core/ngx_string.c @@ -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++ = '\\'; + + switch (ch) { + case '\n': + *dst++ = 'n'; + break; + + case '\r': + *dst++ = 'r'; + break; - ch &= 0xf; + case '\t': + *dst++ = 't'; + break; + + case '\b': + *dst++ = 'b'; + break; + + case '\f': + *dst++ = 'f'; + break; - *dst++ = (ch < 10) ? ('0' + ch) : ('A' + ch - 10); + default: + *dst++ = 'u'; *dst++ = '0'; *dst++ = '0'; + *dst++ = '0' + (ch >> 4); + + ch &= 0xf; + + *dst++ = (ch < 10) ? ('0' + ch) : ('A' + ch - 10); + } } size--; |