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