summaryrefslogtreecommitdiffstats
path: root/src/url.c
diff options
context:
space:
mode:
authorLennart Weller <lhw@ring0.de>2016-05-25 10:36:27 +0000
committerLennart Weller <lhw@ring0.de>2016-05-25 10:36:27 +0000
commit39ecaf8d995e3fefea54f260b8b42bbb9cc806d6 (patch)
tree02ecee3bf4d7dad9dfad2e1799616716f5ffbf03 /src/url.c
parentbroken logrotate (diff)
parentImported Upstream version 1.2.0 (diff)
downloadnetdata-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.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/url.c b/src/url.c
index edf52be7..010b07dd 100644
--- a/src/url.c
+++ b/src/url.c
@@ -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;
}