summaryrefslogtreecommitdiffstats
path: root/src/url.c
blob: 010b07dddc69a8a88253d6cc7bf16d28ca665332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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
// code from: http://www.geekhideout.com/urlcode.shtml

/* Converts a hex character to its integer value */
char from_hex(char ch) {
	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];
}

/* 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.");

	while (*str) {
		if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
			*pbuf++ = *str;

		else if (*str == ' ')
			*pbuf++ = '+';

		else
			*pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15);

		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;
}

/* 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;

	if(!buf)
		fatal("Cannot allocate memory.");

	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++ = ' ';

		else
			*pbuf++ = *pstr;

		pstr++;
	}

	*pbuf = '\0';

	return buf;
}