]> arthur.barton.de Git - netdata.git/blobdiff - src/url.c
dns_query_time plugin: replace "." with "_" in dimensions
[netdata.git] / src / url.c
index 010b07dddc69a8a88253d6cc7bf16d28ca665332..6be4d96489351d84374642169f38e94d7f132996 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -1,13 +1,4 @@
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-
 #include "common.h"
-#include "log.h"
-#include "url.h"
 
 // ----------------------------------------------------------------------------
 // URL encode / decode
 
 /* Converts a hex character to its integer value */
 char from_hex(char ch) {
-       return (char)(isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10);
+    return (char)(isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10);
 }
 
 /* Converts an integer value to its hex character*/
 char to_hex(char code) {
-       static char hex[] = "0123456789abcdef";
-       return hex[code & 15];
+    static char hex[] = "0123456789abcdef";
+    return hex[code & 15];
 }
 
 /* Returns a url-encoded version of str */
 /* IMPORTANT: be sure to free() the returned string after use */
 char *url_encode(char *str) {
-       char *buf, *pbuf;
-
-       pbuf = buf = malloc(strlen(str) * 3 + 1);
-
-       if(!buf)
-               fatal("Cannot allocate memory.");
+    char *buf, *pbuf;
 
-       while (*str) {
-               if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
-                       *pbuf++ = *str;
+    pbuf = buf = mallocz(strlen(str) * 3 + 1);
 
-               else if (*str == ' ')
-                       *pbuf++ = '+';
+    while (*str) {
+        if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
+            *pbuf++ = *str;
 
-               else
-                       *pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15);
+        else if (*str == ' ')
+            *pbuf++ = '+';
 
-               str++;
-       }
-       *pbuf = '\0';
+        else
+            *pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15);
 
-       // 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;
+        str++;
+    }
+    *pbuf = '\0';
 
-       return buf;
+    pbuf = strdupz(buf);
+    freez(buf);
+    return pbuf;
 }
 
 /* Returns a url-decoded version of str */
 /* IMPORTANT: be sure to free() the returned string after use */
 char *url_decode(char *str) {
-       char *pstr = str,
-               *buf = malloc(strlen(str) + 1),
-               *pbuf = buf;
+    size_t size = strlen(str) + 1;
 
-       if(!buf)
-               fatal("Cannot allocate memory.");
+    char *buf = mallocz(size);
+    return url_decode_r(buf, str, size);
+}
 
-       while (*pstr) {
-               if (*pstr == '%') {
-                       if (pstr[1] && pstr[2]) {
-                               *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
-                               pstr += 2;
-                       }
-               }
-               else if (*pstr == '+')
-                       *pbuf++ = ' ';
+char *url_decode_r(char *to, char *url, size_t size) {
+    char *s = url,           // source
+         *d = to,            // destination
+         *e = &to[size - 1]; // destination end
 
-               else
-                       *pbuf++ = *pstr;
+    while(*s && d < e) {
+        if(unlikely(*s == '%')) {
+            if(likely(s[1] && s[2])) {
+                *d++ = from_hex(s[1]) << 4 | from_hex(s[2]);
+                s += 2;
+            }
+        }
+        else if(unlikely(*s == '+'))
+            *d++ = ' ';
 
-               pstr++;
-       }
+        else
+            *d++ = *s;
 
-       *pbuf = '\0';
+        s++;
+    }
 
-       return buf;
-}
+    *d = '\0';
 
+    return to;
+}