summaryrefslogtreecommitdiff
path: root/utils/url.c
blob: 5eb7c97b9eeb28e60a7fb16dab48bc046dbcac01 (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
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
 */

/** \file
 * URL parsing and joining (implementation).
 */

#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#include "netsurf/utils/log.h"
#include "netsurf/utils/url.h"
#include "netsurf/utils/utils.h"


regex_t url_re, url_up_re, url_nice_re;

/**
 * Initialise URL routines.
 *
 * Compiles regular expressions required by the url_ functions.
 */

void url_init(void)
{
	/* regex from RFC 2396 */
	regcomp_wrapper(&url_re, "^[[:space:]]*(([a-zA-Z][-a-zA-Z0-9+.]*):)?"
			"(//([^/?#[:space:]]*))?([^?#[:space:]]*)"
			"(\\?([^#[:space:]]*))?(#([^[:space:]]*))?"
			"[[:space:]]*$", REG_EXTENDED);
	regcomp_wrapper(&url_up_re,
			"/(|[^/]|[.][^./]|[^./][.]|[^/][^/][^/]+)/[.][.](/|$)",
			REG_EXTENDED);
	regcomp_wrapper(&url_nice_re,
			"^([^.]{0,4}[.])?([^.][^.][.])?([^/?&;.=]*)"
			"(=[^/?&;.]*)?[/?&;.]",
			REG_EXTENDED);
}


/**
 * Normalize a URL.
 *
 * \param  url  an absolute URL
 * \return  cleaned up url, allocated on the heap, or 0 on failure
 *
 * If there is no scheme, http:// is added. The scheme and host are
 * lower-cased. Default ports are removed (http only). An empty path is
 * replaced with "/". Characters are unescaped if safe.
 */

url_func_result url_normalize(const char *url, char **result)
{
	char c;
	int m;
	int i;
	size_t len;
	bool http = false;
	regmatch_t match[10];

	*result = NULL;

	if ((m = regexec(&url_re, url, 10, match, 0))) {
		LOG(("url '%s' failed to match regex", url));
		return URL_FUNC_FAILED;
	}

	len = strlen(url);

	if (match[1].rm_so == -1) {
		/* scheme missing: add http:// and reparse */
		LOG(("scheme missing: using http"));
		if ((*result = malloc(len + 13)) == NULL) {
			LOG(("malloc failed"));
			return URL_FUNC_NOMEM;
		}
		strcpy(*result, "http://");
		strcpy(*result + sizeof("http://")-1, url);
		if ((m = regexec(&url_re, *result, 10, match, 0))) {
			LOG(("url '%s' failed to match regex", (*result)));
			free(*result);
			return URL_FUNC_FAILED;
		}
		len += sizeof("http://")-1;
	} else {
		if ((*result = malloc(len + 6)) == NULL) {
			LOG(("strdup failed"));
			return URL_FUNC_FAILED;
		}
		strcpy(*result, url);
	}

	/*for (unsigned int i = 0; i != 10; i++) {
		if (match[i].rm_so == -1)
			continue;
		fprintf(stderr, "%i: '%.*s'\n", i,
				match[i].rm_eo - match[i].rm_so,
				res + match[i].rm_so);
	}*/

	/* see RFC 2616 section 3.2.3 */
	/* make scheme lower-case */
	if (match[2].rm_so != -1) {
		for (i = match[2].rm_so; i != match[2].rm_eo; i++)
			(*result)[i] = tolower((*result)[i]);
		if (match[2].rm_eo == 4
				&& (*result)[0] == 'h'
				&& (*result)[1] == 't'
				&& (*result)[2] == 't'
				&& (*result)[3] == 'p')
			http = true;
	}

	/* make empty path into "/" */
	if (match[5].rm_so != -1 && match[5].rm_so == match[5].rm_eo) {
		memmove((*result) + match[5].rm_so + 1,
				(*result) + match[5].rm_so,
				len - match[5].rm_so + 1);
		(*result)[match[5].rm_so] = '/';
		len++;
	}

	/* make host lower-case */
	if (match[4].rm_so != -1) {
		for (i = match[4].rm_so; i != match[4].rm_eo; i++) {
			if ((*result)[i] == ':') {
				if (http && (*result)[i + 1] == '8' &&
						(*result)[i + 2] == '0' &&
						i + 3 == match[4].rm_eo) {
					memmove((*result) + i,
							(*result) + i + 3,
							len - match[4].rm_eo);
					len -= 3;
					(*result)[len] = '\0';
				} else if (i + 1 == match[4].rm_eo) {
					memmove((*result) + i,
							(*result) + i + 1,
							len - match[4].rm_eo);
					len--;
					(*result)[len] = '\0';
				}
				break;
			}
			(*result)[i] = tolower((*result)[i]);
		}
	}

	/* unescape non-"reserved" escaped characters */
	for (i = 0; (unsigned)i != len; i++) {
		if ((*result)[i] != '%')
			continue;
		c = tolower((*result)[i + 1]);
		if ('0' <= c && c <= '9')
			m = 16 * (c - '0');
		else if ('a' <= c && c <= 'f')
			m = 16 * (c - 'a' + 10);
		else
			continue;
		c = tolower((*result)[i + 2]);
		if ('0' <= c && c <= '9')
			m += c - '0';
		else if ('a' <= c && c <= 'f')
			m += c - 'a' + 10;
		else
			continue;

		if (m <= 0x20 || strchr(";/?:@&=+$," "<>#%\"{}|\\^[]`", m)) {
			i += 2;
			continue;
		}

		(*result)[i] = m;
		memmove((*result) + i + 1, (*result) + i + 3, len - i - 2);
		len -= 2;
	}

	return URL_FUNC_OK;
}


/**
 * Resolve a relative URL to absolute form.
 *
 * \param  rel   relative URL
 * \param  base  base URL, must be absolute and cleaned as by url_normalize()
 * \return  an absolute URL, allocated on the heap, or 0 on failure
 */

url_func_result url_join(const char *rel, const char *base, char **result)
{
	int m;
	int i, j;
	char *buf = 0;
	const char *scheme = 0, *authority = 0, *path = 0, *query = 0,
			*fragment = 0;
	int scheme_len = 0, authority_len = 0, path_len = 0, query_len = 0,
			fragment_len = 0;
	regmatch_t base_match[10];
	regmatch_t rel_match[10];
	regmatch_t up_match[3];

	(*result) = 0;

	/* see RFC 2396 section 5.2 */
	m = regexec(&url_re, base, 10, base_match, 0);
	if (m) {
		LOG(("base url '%s' failed to match regex", base));
		return URL_FUNC_FAILED;
	}
	/*for (unsigned int i = 0; i != 10; i++) {
		if (base_match[i].rm_so == -1)
			continue;
		fprintf(stderr, "%i: '%.*s'\n", i,
				base_match[i].rm_eo - base_match[i].rm_so,
				base + base_match[i].rm_so);
	}*/
	if (base_match[2].rm_so == -1) {
		LOG(("base url '%s' is not absolute", base));
		return URL_FUNC_FAILED;
	}
	scheme = base + base_match[2].rm_so;
	scheme_len = base_match[2].rm_eo - base_match[2].rm_so;
	if (base_match[4].rm_so != -1) {
		authority = base + base_match[4].rm_so;
		authority_len = base_match[4].rm_eo - base_match[4].rm_so;
	}
	path = base + base_match[5].rm_so;
	path_len = base_match[5].rm_eo - base_match[5].rm_so;

	/* 1) */
	m = regexec(&url_re, rel, 10, rel_match, 0);
	if (m) {
		LOG(("relative url '%s' failed to match regex", rel));
		return URL_FUNC_FAILED;
	}

	/* 2) */
	/* base + "#s" = (current document)#s (see Appendix C.1) */
	/** \todo does (current document) include the query? */
	if (rel_match[9].rm_so != -1) {
		fragment = rel + rel_match[9].rm_so;
		fragment_len = rel_match[9].rm_eo - rel_match[9].rm_so;
	}
	if (rel_match[5].rm_so == rel_match[5].rm_eo &&
			rel_match[2].rm_so == -1 &&
			rel_match[4].rm_so == -1 &&
			rel_match[6].rm_so == -1) {
		goto step7;
	}
	if (rel_match[7].rm_so != -1) {
		query = rel + rel_match[7].rm_so;
		query_len = rel_match[7].rm_eo - rel_match[7].rm_so;
	}

	/* 3) */
	if (rel_match[2].rm_so != -1) {
		scheme = rel + rel_match[2].rm_so;
		scheme_len = rel_match[2].rm_eo - rel_match[2].rm_so;
		authority = 0;
		authority_len = 0;
		if (rel_match[4].rm_so != -1) {
			authority = rel + rel_match[4].rm_so;
			authority_len = rel_match[4].rm_eo - rel_match[4].rm_so;
		}
		path = rel + rel_match[5].rm_so;
		path_len = rel_match[5].rm_eo - rel_match[5].rm_so;
		goto step7;
	}

	/* 4) */
	if (rel_match[4].rm_so != -1) {
		authority = rel + rel_match[4].rm_so;
		authority_len = rel_match[4].rm_eo - rel_match[4].rm_so;
		path = rel + rel_match[5].rm_so;
		path_len = rel_match[5].rm_eo - rel_match[5].rm_so;
		goto step7;
	}

	/* 5) */
	if (rel[rel_match[5].rm_so] == '/') {
		path = rel + rel_match[5].rm_so;
		path_len = rel_match[5].rm_eo - rel_match[5].rm_so;
		goto step7;
	}

	/* 6) */
	buf = malloc(path_len + rel_match[5].rm_eo + 10);
	if (!buf) {
		LOG(("malloc failed"));
		return URL_FUNC_NOMEM;
	}
	/* a) */
	strncpy(buf, path, path_len);
	for (; path_len != 0 && buf[path_len - 1] != '/'; path_len--)
		;
	/* b) */
	strncpy(buf + path_len, rel + rel_match[5].rm_so,
			rel_match[5].rm_eo - rel_match[5].rm_so);
	path_len += rel_match[5].rm_eo - rel_match[5].rm_so;
	/* c) */
	buf[path_len] = 0;
	for (i = j = 0; j != path_len; ) {
		if (j && buf[j - 1] == '/' && buf[j] == '.' &&
				buf[j + 1] == '/')
			j += 2;
		else
			buf[i++] = buf[j++];
	}
	path_len = i;
	/* d) */
	if (2 <= path_len && buf[path_len - 2] == '/' &&
			buf[path_len - 1] == '.')
		path_len--;
	/* e) and f) */
	while (1) {
		buf[path_len] = 0;
		m = regexec(&url_up_re, buf, 3, up_match, 0);
		if (m)
			break;
		if (up_match[1].rm_eo + 4 <= path_len) {
			memmove(buf + up_match[1].rm_so,
					buf + up_match[1].rm_eo + 4,
					path_len - up_match[1].rm_eo - 4);
			path_len -= up_match[1].rm_eo - up_match[1].rm_so + 4;
		} else
			path_len -= up_match[1].rm_eo - up_match[1].rm_so + 3;
	}
	buf[path_len] = 0;
        path = buf;

step7:	/* 7) */
	(*result) = malloc(scheme_len + 1 + 2 + authority_len + path_len + 1 + 1 +
			query_len + 1 + fragment_len + 1);
	if (!(*result)) {
		LOG(("malloc failed"));
		free(buf);
		return URL_FUNC_NOMEM;
	}

	strncpy((*result), scheme, scheme_len);
	(*result)[scheme_len] = ':';
	i = scheme_len + 1;
	if (authority) {
		(*result)[i++] = '/';
		(*result)[i++] = '/';
		strncpy((*result) + i, authority, authority_len);
		i += authority_len;
	}
	if (path_len) {
		strncpy((*result) + i, path, path_len);
		i += path_len;
	} else {
		(*result)[i++] = '/';
	}
	if (query) {
		(*result)[i++] = '?';
		strncpy((*result) + i, query, query_len);
		i += query_len;
	}
	if (fragment) {
		(*result)[i++] = '#';
		strncpy((*result) + i, fragment, fragment_len);
		i += fragment_len;
	}
	(*result)[i] = 0;

	free(buf);

	return URL_FUNC_OK;
}


/**
 * Return the host name from an URL.
 *
 * \param  url  an absolute URL
 * \returns  host name allocated on heap, or 0 on failure
 */

url_func_result url_host(const char *url, char **result)
{
	int m;
	regmatch_t match[10];

	(*result) = 0;

	m = regexec(&url_re, url, 10, match, 0);
	if (m) {
		LOG(("url '%s' failed to match regex", url));
		return URL_FUNC_FAILED;
	}
	if (match[4].rm_so == -1)
		return URL_FUNC_FAILED;

	(*result) = malloc(match[4].rm_eo - match[4].rm_so + 1);
	if (!(*result)) {
		LOG(("malloc failed"));
		return URL_FUNC_NOMEM;
	}
	strncpy((*result), url + match[4].rm_so, match[4].rm_eo - match[4].rm_so);
	(*result)[match[4].rm_eo - match[4].rm_so] = 0;

	return URL_FUNC_OK;
}

/**
 * Return the scheme name from an URL
 *
 * \param url     an absolute URL
 * \param result  pointer to pointer to buffer to hold scheme name
 * \return URL_FUNC_OK on success
 */
url_func_result url_scheme(const char *url, char **result)
{
	int m;
	regmatch_t match[10];

	(*result) = 0;

	m = regexec(&url_re, url, 10, match, 0);
	if (m) {
		LOG(("url '%s' failed to match regex", url));
		return URL_FUNC_FAILED;
	}
	if (match[2].rm_so == -1)
		return URL_FUNC_FAILED;

	(*result) = malloc(match[2].rm_eo - match[2].rm_so + 1);
	if (!(*result)) {
		LOG(("malloc failed"));
		return URL_FUNC_NOMEM;
	}

	strncpy((*result), url + match[2].rm_so, match[2].rm_eo - match[2].rm_so);
	(*result)[match[2].rm_eo - match[2].rm_so] = 0;

	return URL_FUNC_OK;
}

/**
 * Attempt to find a nice filename for a URL.
 *
 * \param  url  an absolute URL
 * \returns  filename allocated on heap, or 0 on memory exhaustion
 */

url_func_result url_nice(const char *url, char **result)
{
	unsigned int i, j, k = 0, so;
	unsigned int len;
	const char *colon;
	char buf[40];
	char *rurl;
	int m;
	regmatch_t match[10];

	/* just in case */
	(*result) = 0;

	(*result) = malloc(40);
	if (!(*result))
		return URL_FUNC_NOMEM;

	len = strlen(url);
	assert(len != 0);
	rurl = malloc(len + 1);
	if (!rurl) {
		free((*result));
		return URL_FUNC_NOMEM;
	}

	/* reverse url into rurl */
	for (i = 0, j = len - 1; i != len; i++, j--)
		rurl[i] = url[j];
	rurl[len] = 0;

	/* prepare a fallback: always succeeds */
	colon = strchr(url, ':');
	if (colon)
		url = colon + 1;
	strncpy((*result), url, 15);
	(*result)[15] = 0;
	for (i = 0; (*result)[i]; i++)
		if (!isalnum((*result)[i]))
			(*result)[i] = '_';

	/* append nice pieces */
	j = 0;
	do {
		m = regexec(&url_nice_re, rurl + j, 10, match, 0);
		if (m)
			break;

		if (match[3].rm_so != match[3].rm_eo) {
			so = match[3].rm_so;
			i = match[3].rm_eo - so;
			if (15 < i) {
				so = match[3].rm_eo - 15;
				i = 15;
			}
			if (15 < k + i)
				break;
			if (k)
				k++;
			strncpy(buf + k, rurl + j + so, i);
			k += i;
			buf[k] = 160;	/* nbsp */
		}

		j += match[0].rm_eo;
	} while (j != len);

	if (k == 0) {
		free(rurl);
		return URL_FUNC_OK;
	}

	/* reverse back */
	for (i = 0, j = k - 1; i != k; i++, j--)
		(*result)[i] = buf[j];
	(*result)[k] = 0;

	for (i = 0; i != k; i++)
		if ((*result)[i] != (char) 0xa0 && !isalnum((*result)[i]))
			(*result)[i] = '_';

	free(rurl);

	return URL_FUNC_OK;
}



#ifdef TEST

int main(int argc, char *argv[])
{
	int i;
	url_func_result res;
	char *s;
	url_init();
	for (i = 1; i != argc; i++) {
/*		printf("==> '%s'\n", argv[i]);
		res = url_normalize(argv[i], &s);
		if (res == URL_FUNC_OK) {
			printf("<== '%s'\n", s);
			free(s);
		}*/
/*		printf("==> '%s'\n", argv[i]);
		res = url_host(argv[i], &s);
		if (res == URL_FUNC_OK) {
			printf("<== '%s'\n", s);
			free(s);
		}*/
		if (1 != i) {
			res = url_join(argv[i], argv[1], &s);
			if (res == URL_FUNC_OK) {
				printf("'%s' + '%s' \t= '%s'\n", argv[1],
						argv[i], s);
				free(s);
			}
		}
/*		res = url_nice(argv[i], &s);
		if (res == URL_FUNC_OK) {
			printf("'%s'\n", s);
			free(s);
		}*/
	}
	return 0;
}

void regcomp_wrapper(regex_t *preg, const char *regex, int cflags)
{
	char errbuf[200];
	int r;
	r = regcomp(preg, regex, cflags);
	if (r) {
		regerror(r, preg, errbuf, sizeof errbuf);
		fprintf(stderr, "Failed to compile regexp '%s'\n", regex);
		fprintf(stderr, "error: %s\n", errbuf);
		exit(1);
	}
}

#endif