]> arthur.barton.de Git - netdata.git/blob - src/url.c
prevent a malloc/free per web request
[netdata.git] / src / url.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7
8 #include "common.h"
9 #include "log.h"
10 #include "url.h"
11
12 // ----------------------------------------------------------------------------
13 // URL encode / decode
14 // code from: http://www.geekhideout.com/urlcode.shtml
15
16 /* Converts a hex character to its integer value */
17 char from_hex(char ch) {
18         return (char)(isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10);
19 }
20
21 /* Converts an integer value to its hex character*/
22 char to_hex(char code) {
23         static char hex[] = "0123456789abcdef";
24         return hex[code & 15];
25 }
26
27 /* Returns a url-encoded version of str */
28 /* IMPORTANT: be sure to free() the returned string after use */
29 char *url_encode(char *str) {
30         char *buf, *pbuf;
31
32         pbuf = buf = malloc(strlen(str) * 3 + 1);
33
34         if(!buf)
35                 fatal("Cannot allocate memory.");
36
37         while (*str) {
38                 if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
39                         *pbuf++ = *str;
40
41                 else if (*str == ' ')
42                         *pbuf++ = '+';
43
44                 else
45                         *pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15);
46
47                 str++;
48         }
49         *pbuf = '\0';
50
51         // FIX: I think this is prudent. URLs can be as long as 2 KiB or more.
52         //      We allocated 3 times more space to accomodate %NN encoding of
53         //      non ASCII chars. If URL has none of these kind of chars we will
54         //      end up with a big unused buffer.
55         //
56         //      Try to shrink the buffer...
57         if (!!(pbuf = (char *)realloc(buf, strlen(buf)+1)))
58                 buf = pbuf;
59
60         return buf;
61 }
62
63 /* Returns a url-decoded version of str */
64 /* IMPORTANT: be sure to free() the returned string after use */
65 char *url_decode(char *str) {
66         size_t size = strlen(str) + 1;
67
68         char *buf = malloc(size);
69         if(!buf)
70                 fatal("Cannot allocate %zu bytes of memory.", size);
71
72         return url_decode_r(buf, str, size);
73 }
74
75 char *url_decode_r(char *to, char *url, size_t size) {
76         char *s = url,           // source
77                  *d = to,            // destination
78                  *e = &to[size - 1]; // destination end
79
80         while(*s && d < e) {
81                 if(unlikely(*s == '%')) {
82                         if(likely(s[1] && s[2])) {
83                                 *d++ = from_hex(s[1]) << 4 | from_hex(s[2]);
84                                 s += 2;
85                         }
86                 }
87                 else if(unlikely(*s == '+'))
88                         *d++ = ' ';
89
90                 else
91                         *d++ = *s;
92
93                 s++;
94         }
95
96         *d = '\0';
97
98         return to;
99 }