diff options
author | Lennart Weller <lhw@ring0.de> | 2016-05-25 10:36:27 +0000 |
---|---|---|
committer | Lennart Weller <lhw@ring0.de> | 2016-05-25 10:36:27 +0000 |
commit | 39ecaf8d995e3fefea54f260b8b42bbb9cc806d6 (patch) | |
tree | 02ecee3bf4d7dad9dfad2e1799616716f5ffbf03 /src/url.c | |
parent | broken logrotate (diff) | |
parent | Imported Upstream version 1.2.0 (diff) | |
download | netdata-39ecaf8d995e3fefea54f260b8b42bbb9cc806d6.tar.xz netdata-39ecaf8d995e3fefea54f260b8b42bbb9cc806d6.zip |
Merge tag 'upstream/1.2.0'
Upstream version 1.2.0
Diffstat (limited to 'src/url.c')
-rw-r--r-- | src/url.c | 28 |
1 files changed, 18 insertions, 10 deletions
@@ -27,28 +27,36 @@ char to_hex(char code) { /* Returns a url-encoded version of str */ /* IMPORTANT: be sure to free() the returned string after use */ char *url_encode(char *str) { - char *pstr = str, - *buf = malloc(strlen(str) * 3 + 1), - *pbuf = buf; + char *buf, *pbuf; + + pbuf = buf = malloc(strlen(str) * 3 + 1); if(!buf) fatal("Cannot allocate memory."); - while (*pstr) { - if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') - *pbuf++ = *pstr; + while (*str) { + if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~') + *pbuf++ = *str; - else if (*pstr == ' ') + else if (*str == ' ') *pbuf++ = '+'; else - *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); + *pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15); - pstr++; + str++; } - *pbuf = '\0'; + // FIX: I think this is prudent. URLs can be as long as 2 KiB or more. + // We allocated 3 times more space to accomodate %NN encoding of + // non ASCII chars. If URL has none of these kind of chars we will + // end up with a big unused buffer. + // + // Try to shrink the buffer... + if (!!(pbuf = (char *)realloc(buf, strlen(buf)+1))) + buf = pbuf; + return buf; } |