aboutsummaryrefslogtreecommitdiff
path: root/src/core/url.c
blob: 8cb5a2e775961563dbcee1d9ab9bf5b6a14a3c7a (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "core/nng_impl.h"

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include "url.h"

static char
url_hexval(char c)
{
	if ((c >= '0') && (c <= '9')) {
		return (c - '0');
	}
	if ((c >= 'A') && (c <= 'F')) {
		return (c - 'A');
	}
	if ((c >= 'a') && (c <= 'f')) {
		return (c - 'a');
	}
	return (0);
}

static int
url_decode_buf(const char *in, char *out, int len)
{
	int            dlen;
	const uint8_t *src;
	uint8_t *      dst;
	int            c;

	src = (const uint8_t *) in;
	dst = (uint8_t *) out;

	dlen = 0;
	while ((c = *src) != 0) {
		switch (c) {
		case '%':
			if ((!isxdigit(src[1])) || (!isxdigit(src[2]))) {
				return (-1);
			}
			c = (url_hexval(src[1]) * 16) + url_hexval(src[2]);
			// We don't support encoded control characters.
			if ((c < ' ') || (c == 0x7F)) {
				return (-1);
			}
			src += 3;
			break;
		case '+':
			src++;
			c = ' ';
			break;
		default:
			// Reject control characters and non-ASCII
			if ((c >= 0x7F) || (c <= ' ')) {
				return (-1);
			}
			// Technically this will accept some "unsafe"
			// characters as is.
			src++;
			break;
		}

		if (dlen < len) {
			*dst++ = c;
		}
		dlen++;
	}
	if (dlen < len) {
		*dst = '\0';
	}
	dlen++; // for null terminator
	return (dlen);
}

int
nni_url_decode(char **out, const char *in)
{
	int   len = 0;
	char *dst;

	if ((len = url_decode_buf(in, NULL, 0)) < 1) {
		return (NNG_EINVAL);
	}
	if ((dst = nni_alloc(len)) != 0) {
		return (NNG_ENOMEM);
	}
	url_decode_buf(in, dst, len);
	*out = dst;
	return (0);
}

static const char *url_hexdigits = "0123456789ABCDEF";
static const char *url_safe      = "-_.~";

static int
url_encode_buf(const char *in, char *out, int len, const char *specials)
{
	uint8_t *      dst;
	const uint8_t *src;
	int            dlen;
	int            c;

	dlen = 0;
	src  = (const uint8_t *) in;
	dst  = (uint8_t *) out;

	while ((c = *src) != 0) {
		if ((c < ' ') || (c == 0x7F)) {
			// No encoding of control characters
			return (-1);
		}
		if ((c < 0x80) &&
		    ((isalnum(c) || (strchr(specials, c) != NULL) ||
		        (strchr(url_safe, c) != NULL)))) {
			if (dlen < len) {
				*dst++ = c;
			}
			dlen++;
			src++;
			continue;
		}

		if (dlen < len) {
			*dst++ = '%';
		}
		dlen++;
		if (dlen < len) {
			*dst++ = url_hexdigits[((c & 0xf0) >> 4)];
		}
		dlen++;
		if (dlen < len) {
			*dst++ = url_hexdigits[(c & 0xf)];
		}
		dlen++;
		src++;
	}
	if (dlen < len) {
		*dst = '\0';
	}
	dlen++;
	return (dlen);
}

int
nni_url_encode_ext(char **out, const char *in, const char *specials)
{
	int   len;
	char *dst;

	if ((len = url_encode_buf(in, NULL, 0, specials)) < 0) {
		return (NNG_EINVAL);
	}
	if ((dst = nni_alloc(len)) == NULL) {
		return (NNG_ENOMEM);
	}
	url_encode_buf(in, dst, len, specials);
	*out = dst;
	return (0);
}

int
nni_url_encode(char **out, const char *in)
{
	return (nni_url_encode_ext(out, in, ""));
}

// URLs usually follow the following format:
//
// scheme:[//[userinfo@]host][/]path[?query][#fragment]
//
// There are other URL formats, for example mailto: but these are
// generally not used with nanomsg transports.  Golang calls these
//
// scheme:opaque[?query][#fragment]
//
// Nanomsg URLs are always of the first form, we always require a
// scheme with a leading //, such as http:// or tcp://. So our parser
// is a bit more restricted, but sufficient for our needs.
int
nni_url_parse(nni_url **urlp, const char *raw)
{
	nni_url *   url;
	size_t      len;
	const char *s;
	char        c;
	int         rv;

	if ((url = NNI_ALLOC_STRUCT(url)) == NULL) {
		return (NNG_ENOMEM);
	}

	if ((url->u_rawurl = nni_strdup(raw)) == NULL) {
		rv = NNG_ENOMEM;
		goto error;
	}

	// Grab the scheme.
	s = raw;
	for (len = 0; (c = s[len]) != ':'; len++) {
		if (c == 0) {
			break;
		}
	}
	if (strncmp(s + len, "://", 3) != 0) {
		rv = NNG_EINVAL;
		goto error;
	}

	if ((url->u_scheme = nni_alloc(len + 1)) == NULL) {
		rv = NNG_ENOMEM;
		goto error;
	}
	memcpy(url->u_scheme, s, len);
	url->u_scheme[len] = '\0';

	// Look for host part (including colon).  Will be terminated by
	// a path, or NUL.  May also include an "@", separating a user
	// field.
	s += len + 3; // strlen("://")
	for (len = 0; (c = s[len]) != '/'; len++) {
		if ((c == '\0') || (c == '#') || (c == '?')) {
			break;
		}
		if (c == '@') {
			// This is a username.
			if (url->u_userinfo != NULL) { // we already have one
				rv = NNG_EINVAL;
				goto error;
			}
			if ((url->u_userinfo = nni_alloc(len + 1)) == NULL) {
				rv = NNG_ENOMEM;
				goto error;
			}
			memcpy(url->u_userinfo, s, len);
			url->u_userinfo[len] = '\0';
			s += len + 1; // skip past user@ ...
			len = 0;
		}
	}

	if ((url->u_host = nni_alloc(len + 1)) == NULL) {
		rv = NNG_ENOMEM;
		goto error;
	}
	memcpy(url->u_host, s, len);
	url->u_host[len] = '\0';
	s += len;

	if ((url->u_rawpath = nni_strdup(s)) == NULL) {
		rv = NNG_ENOMEM;
		goto error;
	}
	for (len = 0; (c = s[len]) != '\0'; len++) {
		if ((c == '?') || (c == '#')) {
			break;
		}
	}

	if ((url->u_path = nni_alloc(len + 1)) == NULL) {
		rv = NNG_ENOMEM;
		goto error;
	}

	memcpy(url->u_path, s, len);
	url->u_path[len] = '\0';

	s += len;
	len = 0;

	// Look for query info portion.
	if (s[0] == '?') {
		s++;
		for (len = 0; (c = s[len]) != '\0'; len++) {
			if (c == '#') {
				break;
			}
		}
		if ((url->u_query = nni_alloc(len + 1)) == NULL) {
			rv = NNG_ENOMEM;
			goto error;
		}
		memcpy(url->u_query, s, len);
		url->u_query[len] = '\0';
		s += len;
	}

	// Look for fragment.  Will always be last, so we just use
	// strdup.
	if (s[0] == '#') {
		if ((url->u_fragment = nni_strdup(s + 1)) == NULL) {
			rv = NNG_ENOMEM;
			goto error;
		}
	}

	// Now go back to the host portion, and look for a separate
	// port We also yank off the "[" part for IPv6 addresses.
	s = url->u_host;
	if (s[0] == '[') {
		s++;
		for (len = 0; s[len] != ']'; len++) {
			if (s[len] == '\0') {
				rv = NNG_EINVAL;
				goto error;
			}
		}
		if ((s[len + 1] != ':') && (s[len + 1] != '\0')) {
			rv = NNG_EINVAL;
			goto error;
		}
	} else {
		for (len = 0; s[len] != ':'; len++) {
			if (s[len] == '\0') {
				break;
			}
		}
	}
	if ((url->u_hostname = nni_alloc(len + 1)) == NULL) {
		rv = NNG_ENOMEM;
		goto error;
	}
	memcpy(url->u_hostname, s, len);
	url->u_hostname[len] = '\0';
	s += len;

	if (s[0] == ']') {
		s++; // skip over ']', only used with IPv6 addresses
	}
	if (s[0] == ':') {
		if ((url->u_port = nni_strdup(s + 1)) == NULL) {
			rv = NNG_ENOMEM;
			goto error;
		}
	} else if ((url->u_port = nni_strdup("")) == NULL) {
		rv = NNG_ENOMEM;
		goto error;
	}

	*urlp = url;
	return (0);

error:
	nni_url_free(url);
	return (rv);
}

void
nni_url_free(nni_url *url)
{
	nni_strfree(url->u_rawurl);
	nni_strfree(url->u_scheme);
	nni_strfree(url->u_userinfo);
	nni_strfree(url->u_host);
	nni_strfree(url->u_hostname);
	nni_strfree(url->u_port);
	nni_strfree(url->u_path);
	nni_strfree(url->u_query);
	nni_strfree(url->u_fragment);
	nni_strfree(url->u_rawpath);
	NNI_FREE_STRUCT(url);
}