Skip to content

Commit fcc3c15

Browse files
committed
Fix escaping filter_key in prometheus output
* Fixes #142 * it can be escaped the 2 - 4 bytes character
1 parent 5b3812e commit fcc3c15

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/ngx_http_vhost_traffic_status_string.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ ngx_http_vhost_traffic_status_escape_prometheus(ngx_pool_t *pool, ngx_str_t *buf
172172
u_char c, *pa, *pb, *last, *char_end;
173173
size_t size;
174174
u_char HEX_MAP[] = "0123456789ABCDEF";
175+
uint32_t dec;
175176

176177
last = p + n;
177178
pa = p;
@@ -187,7 +188,7 @@ ngx_http_vhost_traffic_status_escape_prometheus(ngx_pool_t *pool, ngx_str_t *buf
187188
}
188189
} else {
189190
char_end = pa;
190-
if (ngx_utf8_decode(&char_end, last - pa) > 0x10ffff) {
191+
if (ngx_utf8_decode(&char_end, last - pa) >= 0x80) {
191192
break;
192193
} else {
193194
pa = char_end;
@@ -237,7 +238,16 @@ ngx_http_vhost_traffic_status_escape_prometheus(ngx_pool_t *pool, ngx_str_t *buf
237238
}
238239
} else {
239240
char_end = pa;
240-
if (ngx_utf8_decode(&char_end, last - pa) > 0x10ffff) {
241+
dec = ngx_utf8_decode(&char_end, last - pa);
242+
if (dec >= 0x80 && dec <= 0x10ffff) {
243+
while (last - pa > 0) {
244+
c = *pa++;
245+
*pb++ = '%';
246+
*pb++ = HEX_MAP[c >> 4];
247+
*pb++ = HEX_MAP[c & 0x0f];
248+
size += 3;
249+
}
250+
} else if (dec > 0x10ffff) {
241251
/* invalid UTF-8 - escape single char to allow resynchronization */
242252
c = *pa++;
243253
/* two slashes are required to be valid encoding for prometheus*/

0 commit comments

Comments
 (0)