aboutsummaryrefslogtreecommitdiff
path: root/src/core/url.c
blob: 48c79037d4cf9de392d5b36594c9f8eccd64bdb5 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//
// Copyright 2024 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/defs.h"
#include "core/nng_impl.h"

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

#include "core/platform.h"
#include "nng/nng.h"
#include "url.h"

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

// This returns either 0, or NNG_EINVAL, if the supplied input string
// is malformed UTF-8.  We consider UTF-8 malformed when the sequence
// is an invalid code point, not the shortest possible code point, or
// incomplete.
static int
url_utf8_validate(void *arg)
{
	uint8_t *s = arg;
	uint32_t v, minv;
	int      nb;

	while (*s) {
		if ((s[0] & 0x80u) == 0) {
			s++;
			continue;
		}
		if ((s[0] & 0xe0u) == 0xc0) {
			// 0x80 thru 0x7ff
			v    = (s[0] & 0x1fu);
			minv = 0x80;
			nb   = 1;
		} else if ((s[0] & 0xf0u) == 0xe0) {
			v    = (s[0] & 0xfu);
			minv = 0x800;
			nb   = 2;
		} else if ((s[0] & 0xf8u) == 0xf0) {
			v    = (s[0] & 0x7u);
			minv = 0x10000;
			nb   = 3;
		} else {
			// invalid byte, either continuation, or too many
			// leading 1 bits.
			return (NNG_EINVAL);
		}
		s++;
		for (int i = 0; i < nb; i++) {
			if ((s[0] & 0xc0u) != 0x80) {
				return (NNG_EINVAL); // not continuation
			}
			s++;
			v <<= 6u;
			v += s[0] & 0x3fu;
		}
		if (v < minv) {
			return (NNG_EINVAL);
		}
		if ((v >= 0xd800) && (v <= 0xdfff)) {
			return (NNG_EINVAL);
		}
		if (v > 0x10ffff) {
			return (NNG_EINVAL);
		}
	}
	return (0);
}

size_t
nni_url_decode(uint8_t *out, const char *in, size_t max_len)
{
	size_t  len;
	uint8_t c;

	len = 0;
	while ((c = (uint8_t) *in) != '\0') {
		if (len >= max_len) {
			return ((size_t) -1);
		}
		if (c == '%') {
			in++;
			if ((!isxdigit(in[0])) || (!isxdigit(in[1]))) {
				return ((size_t) -1);
			}
			out[len] = url_hex_val(*in++);
			out[len] <<= 4u;
			out[len] += url_hex_val(*in++);
			len++;
		} else {
			out[len++] = c;
			in++;
		}
	}
	return (len);
}

static int
url_canonify_uri(char *out)
{
	size_t  src, dst;
	uint8_t c;
	int     rv;
	bool    skip;

	// First pass, convert '%xx' for safe characters to unescaped forms.
	src = dst = 0;
	while ((c = out[src]) != 0) {
		if (c == '%') {
			if ((!isxdigit(out[src + 1])) ||
			    (!isxdigit(out[src + 2]))) {
				return (NNG_EINVAL);
			}
			c = url_hex_val(out[src + 1]);
			c *= 16;
			c += url_hex_val(out[src + 2]);
			// If it's a safe character, decode, otherwise leave
			// it alone.  We also decode valid high-bytes for
			// UTF-8, which will let us validate them and use
			// those characters in file names later.
			if (((c >= 'A') && (c <= 'Z')) ||
			    ((c >= 'a') && (c <= 'z')) ||
			    ((c >= '0') && (c <= '9')) || (c == '.') ||
			    (c == '~') || (c == '_') || (c == '-') ||
			    (c >= 0x80)) {
				out[dst++] = (char) c;
			} else {
				out[dst++] = '%';
				out[dst++] = toupper((uint8_t) out[src + 1]);
				out[dst++] = toupper((uint8_t) out[src + 2]);
			}
			src += 3;
			continue;
		} else {
			out[dst++] = out[src++];
		}
	}
	out[dst] = 0;

	// Second pass, eliminate redundant //.
	src = dst = 0;
	skip      = false;
	while ((c = out[src]) != 0) {
		if ((c == '/') && (!skip)) {
			out[dst++] = '/';
			while (out[src] == '/') {
				src++;
			}
			continue;
		}
		if ((c == '?') || (c == '#')) {
			skip = true;
		}
		out[dst++] = (char) c;
		src++;
	}
	out[dst] = 0;

	// Second pass, reduce /. and /.. elements, but only in the path.
	src = dst = 0;
	skip      = false;
	while ((c = out[src]) != 0) {
		if ((c == '/') && (!skip)) {
			if ((strncmp(out + src, "/..", 3) == 0) &&
			    (out[src + 3] == 0 || out[src + 3] == '#' ||
			        out[src + 3] == '?' || out[src + 3] == '/')) {

				if (dst > 0) {
					do {
						dst--;
					} while ((dst) && (out[dst] != '/'));
				}
				src += 3;
				continue;
			}
			if ((strncmp(out + src, "/.", 2) == 0) &&
			    (out[src + 2] == 0 || out[src + 2] == '#' ||
			        out[src + 2] == '?' || out[src + 2] == '/')) {
				src += 2; // just skip over it
				continue;
			}
			out[dst++] = '/';
			src++;
		} else {
			if ((c == '?') || (c == '#')) {
				skip = true;
			}
			out[dst++] = (char) c;
			src++;
		}
	}
	out[dst] = 0;

	// Finally lets make sure that the results are valid UTF-8.
	// This guards against using UTF-8 redundancy to break security.
	if ((rv = url_utf8_validate(out)) != 0) {
		return (rv);
	}

	return (0);
}

static struct {
	const char *scheme;
	uint16_t    port;
} nni_url_default_ports[] = {
	// This list is not exhaustive, but likely covers the main ones we
	// care about.  Feel free to add additional ones as use cases arise.
	// Note also that we don't use "default" ports for SP protocols
	// that have no "default" port, like tcp:// or tls+tcp://.
	// clang-format off
	{ "git", 9418 },
	{ "gopher", 70 },
	{ "http", 80 },
	{ "https", 443 },
	{ "ssh", 22 },
	{ "telnet", 23 },
	{ "ws", 80 },
	{ "ws4", 80 },
	{ "ws6", 80 },
	{ "wss", 443 },
	{ "wss4", 443 },
	{ "wss6", 443 },
	{ NULL, 0 },
	// clang-format on
};

// List of schemes that we recognize.  We don't support them all.
static const char *nni_schemes[] = {
	"http",
	"https",
	"tcp",
	"tcp4",
	"tcp6",
	"tls+tcp",
	"tls+tcp4",
	"tls+tcp6",
	"socket",
	"inproc",
	"ipc",
	"unix",
	"abstract",
	"ws",
	"ws4",
	"ws6",
	"wss",
	"wss4",
	"wss6",
	"udp",
	"udp4",
	"udp6",
	// we don't support these
	"file",
	"mailto",
	"gopher",
	"ftp",
	"ssh",
	"git",
	"telnet",
	"irc",
	"imap",
	"imaps",
	NULL,
};

uint16_t
nni_url_default_port(const char *scheme)
{
	const char *s;

	for (int i = 0; (s = nni_url_default_ports[i].scheme) != NULL; i++) {
		size_t l = strlen(s);
		if (strncmp(s, scheme, strlen(s)) != 0) {
			continue;
		}
		// It can have a suffix of either "4" or "6" to restrict
		// the address family.  This is an NNG extension.
		switch (scheme[l]) {
		case '\0':
			return (nni_url_default_ports[i].port);
		case '4':
		case '6':
			if (scheme[l + 1] == '\0') {
				return (nni_url_default_ports[i].port);
			}
			break;
		}
	}
	return (0);
}

// 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.
static int
nni_url_parse_inline_inner(nng_url *url, const char *raw)
{
	size_t      len;
	const char *s;
	char       *p;
	char        c;
	int         rv;

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

	for (int i = 0; nni_schemes[i] != NULL; i++) {
		if (strncmp(s, nni_schemes[i], len) == 0) {
			url->u_scheme = nni_schemes[i];
			break;
		}
	}
	if (url->u_scheme == NULL) {
		return (NNG_ENOTSUP);
	}
	s += len;

	// A little tricky.  We copy the "://" here, even though we don't need
	// it. This affords us some space for zero bytes between URL components
	// if needed

	if (strlen(s) >= sizeof(url->u_static)) {
		url->u_buffer = nni_strdup(s);
		url->u_bufsz  = strlen(s) + 1;
	} else {
		snprintf(url->u_static, sizeof(url->u_static), "%s", s);
		url->u_buffer = url->u_static;
		url->u_bufsz  = 0;
	}

	p = url->u_buffer + strlen("://");
	s = p;

	// For compatibility reasons, we treat ipc:// and inproc:// paths
	// specially. These names URLs have a path name (ipc) or arbitrary
	// string (inproc) and don't include anything like a host.  Note that
	// in the case of path names, it is incumbent upon the application to
	// ensure that valid and safe path names are used.  Note also that
	// path names are not canonicalized, which means that the address and
	// URL properties for relative paths won't be portable to other
	// processes unless they are in the same directory.  When in doubt,
	// we recommend using absolute paths, such as ipc:///var/run/socket.

	if ((strcmp(url->u_scheme, "ipc") == 0) ||
	    (strcmp(url->u_scheme, "unix") == 0) ||
	    (strcmp(url->u_scheme, "abstract") == 0) ||
	    (strcmp(url->u_scheme, "inproc") == 0) ||
	    (strcmp(url->u_scheme, "socket") == 0)) {
		url->u_path     = p;
		url->u_hostname = NULL;
		url->u_query    = NULL;
		url->u_fragment = NULL;
		url->u_userinfo = NULL;
		return (0);
	}

	// Look for host part (including colon).  Will be terminated by
	// a path, or NUL.  May also include an "@", separating a user
	// field.
	for (;;) {
		c = *p;
		if ((c == '\0') || (c == '/') || (c == '#') || (c == '?')) {
			*p = '\0';
			memmove(url->u_buffer, s, strlen(s) + 1);
			*p = c;
			break;
		}
		p++;
	}

	s           = p;
	url->u_path = p;

	// shift the host back to the start of the buffer, which gives us
	// padding so we don't have to clobber the leading "/" in the path.
	url->u_hostname = url->u_buffer;

	char *at;
	if ((at = strchr(url->u_hostname, '@')) != NULL) {
		url->u_userinfo = url->u_hostname;
		*at++           = 0;
		url->u_hostname = at;

		// make sure only one '@' appears in the host (only one user
		// info is allowed)
		if (strchr(url->u_hostname, '@') != NULL) {
			return (NNG_EINVAL);
		}
	}

	// Copy the host portion, but make it lower case (hostnames are
	// case insensitive).
	for (int i = 0; url->u_hostname[i]; i++) {
		url->u_hostname[i] = (char) tolower(url->u_hostname[i]);
	}

	if ((rv = url_canonify_uri(p)) != 0) {
		return (rv);
	}

	while ((c = *p) != '\0') {
		if ((c == '?') || (c == '#')) {
			break;
		}
		p++;
	}

	// Look for query info portion.
	if (*p == '?') {
		*p++         = '\0';
		url->u_query = p;
		while ((c = *p) != '\0') {
			if (c == '#') {
				*p++            = '\0';
				url->u_fragment = p;
				break;
			}
			p++;
		}
	} else if (c == '#') {
		*p++            = '\0';
		url->u_fragment = p;
	}

	// Now go back to the host portion, and look for a separate
	// port We also yank off the "[" part for IPv6 addresses.
	p = url->u_hostname;
	if (*p == '[') {
		url->u_hostname++;
		p++;
		while (*p != ']') {
			if (*p++ == '\0') {
				return (NNG_EINVAL);
			}
		}
		*p++ = '\0';
		if ((*p != ':') && (*p != '\0')) {
			return (NNG_EINVAL);
		}
	} else {
		while (*p != ':' && *p != '\0') {
			p++;
		}
	}
	if ((c = *p) == ':') {
		*p++ = '\0';
	}
	// hostname length check
	if (strlen(url->u_hostname) >= 256) {
		return (NNG_EINVAL);
	}

	if (c == ':') {
		// If a colon was present, but no port value present, then
		// that is an error.
		if (*p == '\0') {
			return (NNG_EINVAL);
		}
		if (nni_get_port_by_name(p, &url->u_port) != 0) {
			return (NNG_EINVAL);
		}
	} else {
		url->u_port = nni_url_default_port(url->u_scheme);
	}

	return (0);
}

int
nni_url_parse_inline(nng_url *url, const char *raw)
{
	int rv = nni_url_parse_inline_inner(url, raw);
	if (rv != 0) {
		nni_url_fini(url);
	}
	return (rv);
}

int
nng_url_parse(nng_url **urlp, const char *raw)
{
	nng_url *url;
	int      rv;

	if ((url = NNI_ALLOC_STRUCT(url)) == NULL) {
		return (NNG_ENOMEM);
	}
	if ((rv = nni_url_parse_inline(url, raw)) != 0) {
		NNI_FREE_STRUCT(url);
		return (rv);
	}
	*urlp = url;
	return (0);
}

void
nni_url_fini(nng_url *url)
{
	if (url->u_bufsz != 0) {
		nni_free(url->u_buffer, url->u_bufsz);
		url->u_buffer = NULL;
		url->u_bufsz  = 0;
	}
}

void
nng_url_free(nng_url *url)
{
	if (url != NULL) {
		nni_url_fini(url);
		NNI_FREE_STRUCT(url);
	}
}

int
nng_url_sprintf(char *str, size_t size, const nng_url *url)
{
	const char *scheme  = url->u_scheme;
	const char *host    = url->u_hostname;
	const char *hostob  = "";
	const char *hostcb  = "";
	bool        do_port = true;

	if ((strcmp(scheme, "ipc") == 0) || (strcmp(scheme, "inproc") == 0) ||
	    (strcmp(scheme, "unix") == 0) ||
	    (strcmp(scheme, "abstract") == 0) ||
	    (strcmp(scheme, "socket") == 0)) {
		return (snprintf(str, size, "%s://%s", scheme, url->u_path));
	}

	if (url->u_port == nni_url_default_port(scheme)) {
		do_port = false;
	}
	if (strchr(host, ':') != 0) {
		hostob = "[";
		hostcb = "]";
	}
	char portstr[8];
	if (do_port) {
		snprintf(portstr, sizeof(portstr), ":%u", url->u_port);
	} else {
		portstr[0] = 0;
	}
	return (snprintf(str, size, "%s://%s%s%s%s%s%s%s%s%s", scheme, hostob,
	    host, hostcb, portstr, url->u_path,
	    url->u_query != NULL ? "?" : "",
	    url->u_query != NULL ? url->u_query : "",
	    url->u_fragment != NULL ? "#" : "",
	    url->u_fragment != NULL ? url->u_fragment : ""));
}

int
nni_url_asprintf(char **str, const nng_url *url)
{
	char  *result;
	size_t sz;

	sz = nng_url_sprintf(NULL, 0, url) + 1;
	if ((result = nni_alloc(sz)) == NULL) {
		return (NNG_ENOMEM);
	}
	nng_url_sprintf(result, sz, url);
	*str = result;
	return (0);
}

// nni_url_asprintf_port is like nni_url_asprintf, but includes a port
// override.  If non-zero, this port number replaces the port number
// in the port string.
int
nni_url_asprintf_port(char **str, const nng_url *url, int port)
{
	nng_url myurl = *url;

	if (port > 0) {
		myurl.u_port = (uint16_t) port;
	}
	return (nni_url_asprintf(str, &myurl));
}

#define URL_COPYSTR(d, s) ((s != NULL) && ((d = nni_strdup(s)) == NULL))

int
nni_url_clone_inline(nng_url *dst, const nng_url *src)
{
	if (src->u_bufsz != 0) {
		if ((dst->u_buffer = nni_alloc(dst->u_bufsz)) == NULL) {
			return (NNG_ENOMEM);
		}
		dst->u_bufsz = src->u_bufsz;
		memcpy(dst->u_buffer, src->u_buffer, src->u_bufsz);
	} else {
		memcpy(dst->u_static, src->u_static, sizeof(src->u_static));
		dst->u_buffer =
		    dst->u_static + (src->u_buffer - src->u_static);
	}

	dst->u_hostname = dst->u_buffer + (src->u_hostname - src->u_buffer);
	dst->u_path     = dst->u_buffer + (src->u_path - src->u_buffer);

	if (src->u_userinfo != NULL) {
		dst->u_userinfo =
		    dst->u_buffer + (src->u_userinfo - src->u_buffer);
	}
	if (src->u_query != NULL) {
		dst->u_query = dst->u_buffer + (src->u_query - src->u_buffer);
	}
	if (src->u_fragment != NULL) {
		dst->u_fragment =
		    dst->u_buffer + (src->u_fragment - src->u_buffer);
	}
	dst->u_scheme = src->u_scheme;
	dst->u_port   = src->u_port;
	return (0);
}

#undef URL_COPYSTR

int
nng_url_clone(nng_url **dstp, const nng_url *src)
{
	nng_url *dst;
	int      rv;
	if ((dst = NNI_ALLOC_STRUCT(dst)) == NULL) {
		return (NNG_ENOMEM);
	}
	if ((rv = nni_url_clone_inline(dst, src) != 0)) {
		NNI_FREE_STRUCT(dst);
		return (rv);
	}
	*dstp = dst;
	return (0);
}

// nni_url_to_address resolves a URL into a sockaddr, assuming the URL is for
// an IP address.
int
nni_url_to_address(nng_sockaddr *sa, const nng_url *url)
{
	int             af;
	nni_aio         aio;
	const char     *h;
	int             rv;
	nni_resolv_item ri;

	// This assumes the scheme is one that uses TCP/IP addresses.

	if (strchr(url->u_scheme, '4') != NULL) {
		af = NNG_AF_INET;
	} else if (strchr(url->u_scheme, '6') != NULL) {
		af = NNG_AF_INET6;
	} else {
		af = NNG_AF_UNSPEC;
	}

	nni_aio_init(&aio, NULL, NULL);

	h = url->u_hostname;
	if ((h != NULL) && (strcmp(h, "") == 0)) {
		h = NULL;
	}

	memset(&ri, 0, sizeof(ri));
	ri.ri_family  = af;
	ri.ri_passive = true;
	ri.ri_host    = h;
	ri.ri_port    = url->u_port;
	ri.ri_sa      = sa;
	nni_resolv(&ri, &aio);
	nni_aio_wait(&aio);
	rv = nni_aio_result(&aio);
	nni_aio_fini(&aio);
	return (rv);
}

const char *
nng_url_scheme(const nng_url *url)
{
	return (url->u_scheme);
}

uint32_t
nng_url_port(const nng_url *url)
{
	return (url->u_port);
}

void
nng_url_resolve_port(nng_url *url, uint32_t port)
{
	if (url->u_port == 0) {
		url->u_port = port;
	}
}

const char *
nng_url_hostname(const nng_url *url)
{
	return (url->u_hostname);
}

const char *
nng_url_path(const nng_url *url)
{
	return (url->u_path);
}

const char *
nng_url_query(const nng_url *url)
{
	return (url->u_query);
}

const char *
nng_url_userinfo(const nng_url *url)
{
	return (url->u_userinfo);
}

const char *
nng_url_fragment(const nng_url *url)
{
	return (url->u_fragment);
}