summaryrefslogtreecommitdiff
path: root/utils/nsurl.c
blob: 1814b5485193e7ee403c8d1c4cac19bbc31de459 (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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
/*
 * Copyright 2011 Michael Drake <tlsa@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * NetSurf URL handling (implementation).
 */

#include <assert.h>
#include <ctype.h>
#include <libwapcaplet/libwapcaplet.h>
#include <stdlib.h>
#include <string.h>

#include "utils/errors.h"
#include "utils/log.h"
#include "utils/nsurl.h"
#include "utils/utils.h"


/* Define to enable NSURL debugging */
#undef NSURL_DEBUG


/**
 * NetSurf URL object
 *
 * [scheme]://[username][:password]@[host]:[port][/path][?query][#fragment]
 */
struct nsurl {
	lwc_string *scheme;
	lwc_string *username;
	lwc_string *password;
	lwc_string *host;
	lwc_string *port;
	lwc_string *path;
	lwc_string *query;
	lwc_string *fragment;

	int count;	/* Number of references to NetSurf URL object */

	char *string;	/* Full URL as a string */
	size_t length;	/* Length of string */
};


/** Marker set, indicating positions of sections within a URL string */
struct url_markers {
	size_t start; /** start of URL */
	size_t scheme_end;
	size_t authority;

	size_t colon_first;
	size_t at;
	size_t colon_last;

	size_t path;
	size_t query;
	size_t fragment;

	size_t end; /** end of URL */
};


/** Sections of a URL */
enum url_sections {
	URL_SCHEME,
	URL_CREDENTIALS,
	URL_HOST,
	URL_PATH,
	URL_QUERY,
	URL_FRAGMENT
};


#define nsurl__component_copy(c) (c == NULL) ? NULL : lwc_string_ref(c)

#define nsurl__component_compare(c1, c2, match)		\
	if (c1 && c2)					\
		lwc_string_isequal(c1, c2, match);	\
	else if (c1 || c2)				\
		*match = false;


/**
 * Obtains a set of markers delimiting sections in a URL string
 *
 * \param url_s		URL string
 * \param markers	Updated to mark sections in the URL string
 * \param joining	True iff URL string is a relative URL for joining
 */
static void nsurl__get_string_markers(const char const *url_s,
		struct url_markers *markers, bool joining)
{
	const char *pos = url_s; /** current position in url_s */
	bool is_http = false;

	/* Initialise marker set */
	struct url_markers marker = { 0, 0, 0,   0, 0, 0,   0, 0, 0,   0 };

	/* Skip any leading whitespace in url_s */
	while (isspace(*pos))
		pos++;

	/* Record start point */
	marker.start = pos - url_s;

	marker.authority = marker.colon_first = marker.at =
			marker.colon_last = marker.path = marker.start;

	/* Get scheme */
	if (isalpha(*pos)) {
		pos++;

		while (*pos != ':' && *pos != '\0') {

			if (!isalnum(*pos) && *pos != '+' &&
					*pos != '-' && *pos != '.') {
				/* This character is not valid in the
				 * scheme */
				break;
			}
			pos++;
		}

		if (*pos == ':') {
			/* This delimits the end of the scheme */
			size_t off;

			marker.scheme_end = pos - url_s;

			off = marker.scheme_end - marker.start;

			/* Detect http(s) for scheme specifc normalisation */
			if (off == SLEN("http") &&
					(((*(pos - off + 0) == 'h') ||
					  (*(pos - off + 0) == 'H')) &&
					 ((*(pos - off + 1) == 't') ||
					  (*(pos - off + 1) == 'T')) &&
					 ((*(pos - off + 2) == 't') ||
					  (*(pos - off + 2) == 'T')) &&
					 ((*(pos - off + 3) == 'p') ||
					  (*(pos - off + 3) == 'P')))) {
				is_http = true;
			} else if (off == SLEN("https") &&
					(((*(pos - off + 0) == 'h') ||
					  (*(pos - off + 0) == 'H')) &&
					 ((*(pos - off + 1) == 't') ||
					  (*(pos - off + 1) == 'T')) &&
					 ((*(pos - off + 2) == 't') ||
					  (*(pos - off + 2) == 'T')) &&
					 ((*(pos - off + 3) == 'p') ||
					  (*(pos - off + 3) == 'P')) &&
					 ((*(pos - off + 3) == 's') ||
					  (*(pos - off + 3) == 'S')))) {
				is_http = true;
			}

			/* Skip over colon */
			pos++;

			/* Mark place as start of authority */
			marker.authority = marker.colon_first = marker.at =
					marker.colon_last = marker.path =
					pos - url_s;

		} else {
			/* Not found a scheme  */
			if (joining == false) {
				/* Assuming no scheme == http */
				is_http = true;
			}
		}
	}

	/* Get authority
	 *
	 * If this is a relative url that is to be joined onto a base URL, we
	 * require two slashes to be certain we correctly handle a missing
	 * authority.
	 *
	 * If this URL is not getting joined, we are less strict in the case of
	 * http(s) and will accept any number of slashes, including 0.
	 */
	if (*pos != '\0' && ((joining == false && is_http == true) ||
			(*pos == '/' && *(pos + 1) == '/'))) {
		/* Skip over leading slashes */
		if (is_http == false) {
			if (*pos == '/') pos++;
			if (*pos == '/') pos++;
		} else {
			while (*pos == '/')
				pos++;
		}

		marker.authority = marker.colon_first = marker.at =
				marker.colon_last = marker.path = pos - url_s;

		/* Need to get (or complete) the authority */
		do {
			if (*pos == '/' || *pos == '?' || *pos == '#') {
				/* End of the authority */
				break;

			} else if (*pos == ':' && marker.colon_first ==
					marker.authority) {
				/* could be username:password or host:port
				 * separator */
				marker.colon_first = pos - url_s;

			} else if (*pos == ':' && marker.colon_first !=
					marker.authority) {
				/* could be host:port separator */
				marker.colon_last = pos - url_s;

			} else if (*pos == '@' && marker.at ==
					marker.authority) {
				/* Credentials @ host separator */
				marker.at = pos - url_s;
			}
		} while (*(++pos) != '\0');

		marker.path = pos - url_s;
	}

	/* Get path
	 *
	 * Needs to start with '/' if there's no authority
	 */
	if (*pos == '/' || ((marker.path == marker.authority) &&
			(*pos != '?') && (*pos != '#') && (*pos != '\0'))) {
		while (*(++pos) != '\0') {
			if (*pos == '?' || *pos == '#') {
				/* End of the path */
				break;
			}
		}
	}

	marker.query = pos - url_s;

	/* Get query */
	if (*pos == '?') {
		while (*(++pos) != '\0') {
			if (*pos == '#') {
				/* End of the query */
				break;
			}
		}
	}

	marker.fragment = pos - url_s;

	/* Get fragment */
	if (*pos == '#') {
		while (*(++pos) != '\0')
			;
	}

	/* We got to the end of url_s.
	 * Need to skip back over trailing whitespace to find end of URL */
	pos--;
	while (isspace(*pos))
		pos--;
	marker.end = pos + 1 - url_s;

	/* Got all the URL components pegged out now */
	*markers = marker;
}


/**
 * Remove dot segments from a path, as per rfc 3986, 5.2.4
 *
 * \param path		path to remove dot segments from ('\0' terminated)
 * \param output	path with dot segments removed
 * \return size of output
 */
static size_t nsurl__remove_dot_segments(char *path, char *output)
{
	char *path_pos = path;
	char *output_pos = output;

	while (*path_pos != '\0') {
#ifdef NSURL_DEBUG
		LOG((" in:%s", path_pos));
		LOG(("out:%.*s", output_pos - output, output));
#endif
		if (*path_pos == '.') {
			if (*(path_pos + 1) == '.' &&
					*(path_pos + 2) == '/') {
				/* Found prefix of "../" */
				path_pos += SLEN("../");
				continue;

			} else if (*(path_pos + 1) == '/') {
				/* Found prefix of "./" */
				path_pos += SLEN("./");
				continue;
			}
		} else if (*path_pos == '/' && *(path_pos + 1) == '.') {
			if (*(path_pos + 2) == '/') {
				/* Found prefix of "/./" */
				path_pos += SLEN("/.");
				continue;

			} else if (*(path_pos + 2) == '\0') {
				/* Found "/." at end of path */
				*(output_pos++) = '/';

				/* End of input path */
				break;

			} else if (*(path_pos + 2) == '.') {
				if (*(path_pos + 3) == '/') {
					/* Found prefix of "/../" */
					path_pos += SLEN("/..");

					if (output_pos > output)
						output_pos--;
					while (output_pos > output &&
							*output_pos != '/')
						output_pos--;

					continue;

				} else if (*(path_pos + 3) == '\0') {
					/* Found "/.." at end of path */

					while (output_pos > output &&
							*(output_pos -1 ) !='/')
						output_pos--;

					/* End of input path */
					break;
				}
			}
		} else if (*path_pos == '.') {
			if (*(path_pos + 1) == '\0') {
				/* Found "." at end of path */

				/* End of input path */
				break;

			} else if (*(path_pos + 1) == '.' &&
					*(path_pos + 2) == '\0') {
				/* Found ".." at end of path */

				/* End of input path */
				break;
			}
		}
		/* Copy first character into output path */
		*output_pos++ = *path_pos++;

		/* Copy up to but not including next '/' */
		  while ((*path_pos != '/') && (*path_pos != '\0'))
		  	*output_pos++ = *path_pos++;
	}

	return output_pos - output;
}


/**
 * Get the length of the longest section
 *
 * \param m	markers delimiting url sections in a string
 * \return the length of the longest section
 */
static size_t nsurl__get_longest_section(struct url_markers *m)
{
	size_t length = m->scheme_end - m->start;	/* scheme */

	if (length < m->at - m->authority)		/* credentials */
		length = m->at - m->authority;

	if (length < m->path - m->at)			/* host */
		length = m->path - m->at;

	if (length < m->query - m->path)		/* path */
		length = m->query - m->path;

	if (length < m->fragment - m->query)		/* query */
		length = m->fragment - m->query;

	if (length < m->end - m->fragment)		/* fragment */
		length = m->end - m->fragment;

	return length;
}


/**
 * Converts two hexadecimal digits to a single number
 *
 * \param c1	most significant hex digit
 * \param c2	least significant hex digit
 * \return the total value of the two digit hex number
 *
 * For unescaping url encoded characters.
 */
static inline int nsurl__get_ascii_offset(char c1, char c2)
{
	int offset;

	/* Use 1st char as most significant hex digit */
	if (isdigit(c1))
		offset = 16 * (c1 - '0');
	else if (c1 >= 'a' && c1 <= 'f')
		offset = 16 * (c1 - 'a' + 10);
	else
		/* TODO: return something special to indicate error? */
		return 0;

	/* Use 2nd char as least significant hex digit and sum */
	if (isdigit(c2))
		offset += c2 - '0';
	else if (c2 >= 'a' && c2 <= 'f')
		offset += c2 - 'a' + 10;
	else
		/* TODO: return something special to indicate error? */
		return 0;

	return offset;
}


/**
 * Create the components of a NetSurf URL object for a section of a URL string
 *
 * \param url_s		URL string
 * \param section	Sets which section of URL string is to be normalised
 * \param pegs		Set of markers delimiting the URL string's sections
 * \param pos_norm	A buffer large enough for the normalised string (*3 + 1)
 * \param url		A NetSurf URL object, to which components may be added
 * \return NSERROR_OK on success, appropriate error otherwise
 *
 * The section of url_s is normalised appropriately.
 */
static nserror nsurl__create_from_section(const char const *url_s,
		const enum url_sections section,
		const struct url_markers *pegs,
		char *pos_norm,
		nsurl *url)
{
	int ascii_offset;
	int start;
	int end;
	const char *pos;
	const char *pos_url_s;
	char *norm_start = pos_norm;
	size_t copy_len;
	size_t length;
	enum {
		NSURL_F_IS_HTTP		= (1 << 0),
		NSURL_F_IS_HTTPS	= (1 << 1),
		NSURL_F_NO_PORT		= (1 << 2)
	} flags = 0;

	switch (section) {
	case URL_SCHEME:
		start = pegs->start;
		end = pegs->scheme_end;
		break;

	case URL_CREDENTIALS:
		start = pegs->authority;
		end = pegs->at;
		break;

	case URL_HOST:
		start = (pegs->at == pegs->authority &&
				*(url_s + pegs->at) != '@') ?
				pegs->at :
				pegs->at + 1;
		end = pegs->path;
		break;

	case URL_PATH:
		start = pegs->path;
		end = pegs->query;
		break;

	case URL_QUERY:
		start = pegs->query;
		end = pegs->fragment;
		break;

	case URL_FRAGMENT:
		start = pegs->fragment;
		end = pegs->end;
		break;
	}

	length = end - start;

	/* Stage 1: Normalise the required section */

	pos = pos_url_s = url_s + start;
	copy_len = 0;
	for (; pos < url_s + end; pos++) {
		if (*pos == '%' && (pos + 2 < url_s + end)) {
			/* Might be an escaped character needing unescaped */

			/* Find which character which was escaped */
			ascii_offset = nsurl__get_ascii_offset(*(pos + 1),
					*(pos + 2));

			if (ascii_offset <= 0x20 ||
					strchr(";/?:@&=+$,<>#%\"{}|\\^[]`",
							ascii_offset) ||
					ascii_offset >= 0x7f) {
				/* This character should be escaped after all,
				 * just let it get copied */
				copy_len += 3;
				pos += 2;
				continue;
			}

			if (copy_len > 0) {
				/* Copy up to here */
				memcpy(pos_norm, pos_url_s, copy_len);
				pos_norm += copy_len;
				copy_len = 0;
			}

			/* Put the unescaped character in the normalised URL */
			*(pos_norm++) = (char)ascii_offset;
			pos += 2;
			pos_url_s = pos + 1;

			length -= 2;

		} else if (isspace(*pos)) {
			/* This whitespace needs to be escaped */

			if (copy_len > 0) {
				/* Copy up to here */
				memcpy(pos_norm, pos_url_s, copy_len);
				pos_norm += copy_len;
				copy_len = 0;
			}
			/* escape */

			*(pos_norm++) = '%';
			*(pos_norm++) = digit2lowcase_hex(*pos >> 4);
			*(pos_norm++) = digit2lowcase_hex(*pos & 0xf);
			pos_url_s = pos + 1;

			length += 2;

		} else if ((section == URL_SCHEME || section == URL_HOST) &&
				isupper(*pos)) {
			/* Lower case this letter */

			if (copy_len > 0) {
				/* Copy up to here */
				memcpy(pos_norm, pos_url_s, copy_len);
				pos_norm += copy_len;
				copy_len = 0;
			}
			/* Copy lower cased letter into normalised URL */
			*(pos_norm++) = tolower(*pos);
			pos_url_s = pos + 1;

		} else {
			/* This character is safe in normalised URL */
			copy_len++;
		}
	}

	if (copy_len > 0) {
		/* Copy up to here */
		memcpy(pos_norm, pos_url_s, copy_len);
		pos_norm += copy_len;
	}

	/* Mark end of section */
	(*pos_norm) = '\0';

	/* Stage 2: Create the URL components for the required section */
	switch (section) {
	case URL_SCHEME:
		if (length == 0 || (length == SLEN("http") &&
				strncmp(norm_start, "http",
						SLEN("http")) == 0)) {
			flags |= NSURL_F_IS_HTTP;
		} else if (length == SLEN("https") &&
				strncmp(norm_start, "https",
						SLEN("https")) == 0) {
			flags |= NSURL_F_IS_HTTPS;
		}

		if (length == 0) {
			/* No scheme, assuming http, and add to URL */
			if (lwc_intern_string("http", SLEN("http"),
					&url->scheme) != lwc_error_ok) {
				return NSERROR_NOMEM;
			}
		} else {
			/* Add scheme to URL */
			if (lwc_intern_string(norm_start, length,
					&url->scheme) != lwc_error_ok) {
				return NSERROR_NOMEM;
			}
		}

		break;

	case URL_CREDENTIALS:
		if (length != 0 && *norm_start != ':') {
			char *sec_start = norm_start;
			if (pegs->colon_first != pegs->authority &&
					pegs->at > pegs->colon_first + 1) {
				/* there's a password */
				sec_start += pegs->colon_first -
						pegs->authority + 1;
				if (lwc_intern_string(sec_start,
						pegs->at - pegs->colon_first -1,
						&url->password) !=
						lwc_error_ok) {
					return NSERROR_NOMEM;
				}

				/* update start pos and length for username */
				sec_start = norm_start;
				length -= pegs->at - pegs->colon_first;
			} else if (pegs->colon_first != pegs->authority &&
					pegs->at == pegs->colon_first + 1) {
				/* strip username colon */
				length--;
			}

			/* Username */
			if (lwc_intern_string(sec_start, length,
					&url->username) != lwc_error_ok) {
				return NSERROR_NOMEM;
			}
		}

		break;

	case URL_HOST:
		if (length != 0) {
			size_t colon;
			char *sec_start = norm_start;
			if (pegs->at < pegs->colon_first &&
					pegs->colon_last == pegs->authority) {
				/* There's one colon and it's after @ marker */
				colon = pegs->colon_first;
			} else if (pegs->colon_last != pegs->authority) {
				/* There's more than one colon */
				colon = pegs->colon_last;
			} else {
				/* There's no colon that could be a port
				 * separator */
				flags |= NSURL_F_NO_PORT;
			}

			if (!(flags & NSURL_F_NO_PORT)) {
				/* Determine whether colon is a port separator
				 */
				sec_start += colon - pegs->at;
				while (++sec_start < norm_start + length) {
					if (!isdigit(*sec_start)) {
						/* Character after port isn't a
						 * digit; not a port separator
						 */
						flags |= NSURL_F_NO_PORT;
						break;
					}
				}
			}

			if (!(flags & NSURL_F_NO_PORT)) {
				/* There's a port */
				sec_start = norm_start + colon - pegs->at + 1;
				if (flags & NSURL_F_IS_HTTP &&
						length -
						(colon - pegs->at + 1) == 2 &&
						*sec_start == '8' &&
						*(sec_start + 1) == '0') {
					/* Scheme is http, and port is default
					 * (80) */
					flags |= NSURL_F_NO_PORT;
				}

				if (length - (colon - pegs->at + 1) <= 0) {
					/* No space for a port after the colon
					 */
					flags |= NSURL_F_NO_PORT;
				}

				/* Add non-redundant ports to NetSurf URL */
				sec_start = norm_start + colon - pegs->at + 1;
				if (!(flags & NSURL_F_NO_PORT) &&
						lwc_intern_string(sec_start,
						length - (colon - pegs->at + 1),
						&url->port) != lwc_error_ok) {
					return NSERROR_NOMEM;
				}

				/* update length for host */
				length = colon - pegs->at;
			}

			/* host */
			if (lwc_intern_string(norm_start, length,
					&url->host) != lwc_error_ok) {
				return NSERROR_NOMEM;
			}
		}

		break;

	case URL_PATH:
		if (length != 0) {
			if (lwc_intern_string(norm_start, length,
					&url->path) != lwc_error_ok) {
				return NSERROR_NOMEM;
			}
		} else if (url->host != NULL) {
			/* Set empty path to "/", if there's a host */
			if (lwc_intern_string("/", SLEN("/"),
					&url->path) != lwc_error_ok) {
				return NSERROR_NOMEM;
			}
		}

		break;

	case URL_QUERY:
		if (length != 0) {
			if (lwc_intern_string(norm_start, length,
					&url->query) != lwc_error_ok) {
				return NSERROR_NOMEM;
			}
		}

		break;

	case URL_FRAGMENT:
		if (length != 0) {
			if (lwc_intern_string(norm_start, length,
					&url->fragment) != lwc_error_ok) {
				return NSERROR_NOMEM;
			}
		}

		break;
	}

	return NSERROR_OK;
}


#ifdef NSURL_DEBUG
/**
 * Dump a NetSurf URL's internal components
 *
 * \param url	The NetSurf URL to dump components of
 */
static void nsurl__dump(const nsurl *url)
{
	if (url->scheme)
		LOG(("  Scheme: %s", lwc_string_data(url->scheme)));

	if (url->username)
		LOG(("Username: %s", lwc_string_data(url->username)));

	if (url->password)
		LOG(("Password: %s", lwc_string_data(url->password)));

	if (url->host)
		LOG(("    Host: %s", lwc_string_data(url->host)));

	if (url->port)
		LOG(("    Port: %s", lwc_string_data(url->port)));

	if (url->path)
		LOG(("    Path: %s", lwc_string_data(url->path)));

	if (url->query)
		LOG(("   Query: %s", lwc_string_data(url->query)));

	if (url->fragment)
		LOG(("Fragment: %s", lwc_string_data(url->fragment)));
}
#endif


/******************************************************************************
 * NetSurf URL Public API                                                     *
 ******************************************************************************/

/* exported interface, documented in nsurl.h */
nserror nsurl_create(const char const *url_s, nsurl **url)
{
	struct url_markers m;
	size_t length;
	char *buff;
	nserror e = NSERROR_OK;

	assert(url_s != NULL);

	/* Peg out the URL sections */
	nsurl__get_string_markers(url_s, &m, false);

	/* Get the length of the longest section */
	length = nsurl__get_longest_section(&m);

	/* Create NetSurf URL object */
	*url = calloc(1, sizeof(nsurl));
	if (*url == NULL) {
		return NSERROR_NOMEM;
	}

	/* Allocate enough memory to url escape the longest section */
	buff = malloc(length * 3 + 1);
	if (buff == NULL) {
		free(*url);
		return NSERROR_NOMEM;
	}

	/* Build NetSurf URL object from sections */
	e |= nsurl__create_from_section(url_s, URL_SCHEME, &m, buff, *url);
	e |= nsurl__create_from_section(url_s, URL_CREDENTIALS, &m, buff, *url);
	e |= nsurl__create_from_section(url_s, URL_HOST, &m, buff, *url);
	e |= nsurl__create_from_section(url_s, URL_PATH, &m, buff, *url);
	e |= nsurl__create_from_section(url_s, URL_QUERY, &m, buff, *url);
	e |= nsurl__create_from_section(url_s, URL_FRAGMENT, &m, buff, *url);

	/* Finished with buffer */
	free(buff);

	if (e != NSERROR_OK) {
		free(*url);
		return NSERROR_NOMEM;
	}

	/* Get the complete URL string */
	if (nsurl_get(*url, NSURL_WITH_FRAGMENT, &((*url)->string),
			&((*url)->length)) != NSERROR_OK) {
		free(*url);
		return NSERROR_NOMEM;
	}

	/* Give the URL a reference */
	(*url)->count = 1;

	return NSERROR_OK;
}


/* exported interface, documented in nsurl.h */
nsurl *nsurl_ref(nsurl *url)
{
	assert(url != NULL);

	url->count++;

	return url;
}


/* exported interface, documented in nsurl.h */
void nsurl_unref(nsurl *url)
{
	if (--url->count > 0)
		return;

	assert(url != NULL);

#ifdef NSURL_DEBUG
	nsurl__dump(url);
#endif

	/* Release lwc strings */
	if (url->scheme)
		lwc_string_unref(url->scheme);

	if (url->username)
		lwc_string_unref(url->username);

	if (url->password)
		lwc_string_unref(url->password);

	if (url->host)
		lwc_string_unref(url->host);

	if (url->port)
		lwc_string_unref(url->port);

	if (url->path)
		lwc_string_unref(url->path);

	if (url->query)
		lwc_string_unref(url->query);

	if (url->fragment)
		lwc_string_unref(url->fragment);

	free(url->string);

	/* Free the NetSurf URL */
	free(url);
}


/* exported interface, documented in nsurl.h */
nserror nsurl_compare(const nsurl *url1, const nsurl *url2,
		nsurl_component parts, bool *match)
{
	assert(url1 != NULL);
	assert(url2 != NULL);

	*match = true;

	/* Compare URL components */

	/* Path, host and query first, since they're most likely to differ */

	if (parts & NSURL_PATH) {
		nsurl__component_compare(url1->path, url2->path, match);

		if (*match == false)
			return NSERROR_OK;
	}

	if (parts & NSURL_HOST) {
		nsurl__component_compare(url1->host, url2->host, match);

		if (*match == false)
			return NSERROR_OK;
	}

	if (parts & NSURL_QUERY) {
		nsurl__component_compare(url1->query, url2->query, match);

		if (*match == false)
			return NSERROR_OK;
	}

	if (parts & NSURL_SCHEME) {
		nsurl__component_compare(url1->scheme, url2->scheme, match);

		if (*match == false)
			return NSERROR_OK;
	}

	if (parts & NSURL_USERNAME) {
		nsurl__component_compare(url1->username, url2->username, match);

		if (*match == false)
			return NSERROR_OK;
	}

	if (parts & NSURL_PASSWORD) {
		nsurl__component_compare(url1->password, url2->password, match);

		if (*match == false)
			return NSERROR_OK;
	}

	if (parts & NSURL_PORT) {
		nsurl__component_compare(url1->port, url2->port, match);

		if (*match == false)
			return NSERROR_OK;
	}

	if (parts & NSURL_FRAGMENT) {
		nsurl__component_compare(url1->fragment, url2->fragment, match);

		if (*match == false)
			return NSERROR_OK;
	}

	*match = true;
	return NSERROR_OK;
}


/* exported interface, documented in nsurl.h */
nserror nsurl_get(const nsurl *url, nsurl_component parts,
		char **url_s, size_t *url_l)
{
	size_t scheme;
	size_t username;
	size_t password;
	size_t host;
	size_t port;
	size_t path;
	size_t query;
	size_t fragment;
	char *pos;
	enum {
		NSURL_F_SCHEME			= (1 << 0),
		NSURL_F_SCHEME_PUNCTUATION	= (1 << 1),
		NSURL_F_AUTHORITY_PUNCTUATION	= (1 << 2),
		NSURL_F_USERNAME		= (1 << 3),
		NSURL_F_PASSWORD		= (1 << 4),
		NSURL_F_CREDENTIALS_PUNCTUATION	= (1 << 5),
		NSURL_F_HOST			= (1 << 6),
		NSURL_F_PORT			= (1 << 7),
		NSURL_F_AUTHORITY		= (NSURL_F_USERNAME |
							NSURL_F_PASSWORD |
							NSURL_F_HOST |
							NSURL_F_PORT),
		NSURL_F_PATH			= (1 << 8),
		NSURL_F_QUERY			= (1 << 9),
		NSURL_F_FRAGMENT		= (1 << 10)
	} flags = 0;

	/* Intersection of required parts and available parts gives
	 * the output parts */
	if (url->scheme && parts & NSURL_SCHEME)
		flags |= NSURL_F_SCHEME;
	if (url->username && parts & NSURL_USERNAME)
		flags |= NSURL_F_USERNAME;
	if (url->password && parts & NSURL_PASSWORD)
		flags |= NSURL_F_PASSWORD;
	if (url->host && parts & NSURL_HOST)
		flags |= NSURL_F_HOST;
	if (url->port && parts & NSURL_PORT)
		flags |= NSURL_F_PORT;
	if (url->path && parts & NSURL_PATH)
		flags |= NSURL_F_PATH;
	if (url->query && parts & NSURL_QUERY)
		flags |= NSURL_F_QUERY;
	if (url->fragment && parts & NSURL_FRAGMENT)
		flags |= NSURL_F_FRAGMENT;

	/* Turn on any spanned punctuation */
	if ((flags & NSURL_F_SCHEME) && (parts > NSURL_SCHEME))
		flags |= NSURL_F_SCHEME_PUNCTUATION;
	if ((flags & NSURL_F_SCHEME) && (flags > NSURL_F_SCHEME) &&
			url->path && lwc_string_data(url->path)[0] == '/')
		flags |= NSURL_F_AUTHORITY_PUNCTUATION;
	if ((flags & (NSURL_F_USERNAME | NSURL_F_PASSWORD)) &&
				flags & NSURL_F_HOST)
		flags |= NSURL_F_CREDENTIALS_PUNCTUATION;

	/* Get total output length */
	*url_l = 0;

	if (flags & NSURL_F_SCHEME) {
		scheme = lwc_string_length(url->scheme);
		*url_l += scheme;
	}

	if (flags & NSURL_F_SCHEME_PUNCTUATION)
		*url_l += SLEN(":");

	if (flags & NSURL_F_AUTHORITY_PUNCTUATION)
		*url_l += SLEN("//");

	if (flags & NSURL_F_USERNAME) {
		username = lwc_string_length(url->username);
		*url_l += username;
	}

	if (flags & NSURL_F_PASSWORD) {
		password = lwc_string_length(url->password);
		*url_l += SLEN(":") + password;
	}

	if (flags & NSURL_F_CREDENTIALS_PUNCTUATION)
		*url_l += SLEN("@");

	if (flags & NSURL_F_HOST) {
		host = lwc_string_length(url->host);
		*url_l += host;
	}

	if (flags & NSURL_F_PORT) {
		port = lwc_string_length(url->port);
		*url_l += SLEN(":") + port;
	}

	if (flags & NSURL_F_PATH) {
		path = lwc_string_length(url->path);
		*url_l += path;
	}

	if (flags & NSURL_F_QUERY) {
		query = lwc_string_length(url->query);
		*url_l += query;
	}

	if (flags & NSURL_F_FRAGMENT) {
		fragment = lwc_string_length(url->fragment);
		*url_l += fragment;
	}

	if (*url_l == 0)
		return NSERROR_BAD_URL;

	/* Allocate memory for url string */
	*url_s = malloc(*url_l + 1); /* adding 1 for '\0' */
	if (*url_s == NULL) {
		return NSERROR_NOMEM;
	}

	/* Copy the required parts into the url string */
	pos = *url_s;

	if (flags & NSURL_F_SCHEME) {
		memcpy(pos, lwc_string_data(url->scheme), scheme);
		pos += scheme;
	}

	if (flags & NSURL_F_SCHEME_PUNCTUATION) {
		*(pos++) = ':';
	}

	if (flags & NSURL_F_AUTHORITY_PUNCTUATION) {
		*(pos++) = '/';
		*(pos++) = '/';
	}

	if (flags & NSURL_F_USERNAME) {
		memcpy(pos, lwc_string_data(url->username), username);
		pos += username;
	}

	if (flags & NSURL_F_PASSWORD) {
		*(pos++) = ':';
		memcpy(pos, lwc_string_data(url->password), password);
		pos += password;
	}

	if (flags & NSURL_F_CREDENTIALS_PUNCTUATION) {
		*(pos++) = '@';
	}

	if (flags & NSURL_F_HOST) {
		memcpy(pos, lwc_string_data(url->host), host);
		pos += host;
	}

	if (flags & NSURL_F_PORT) {
		*(pos++) = ':';
		memcpy(pos, lwc_string_data(url->port), port);
		pos += port;
	}

	if (flags & NSURL_F_PATH) {
		memcpy(pos, lwc_string_data(url->path), path);
		pos += path;
	}

	if (flags & NSURL_F_QUERY) {
		memcpy(pos, lwc_string_data(url->query), query);
		pos += query;
	}

	if (flags & NSURL_F_FRAGMENT) {
		memcpy(pos, lwc_string_data(url->fragment), fragment);
		pos += fragment;
	}

	*pos = '\0';

	return NSERROR_OK;
}


/* exported interface, documented in nsurl.h */
char *nsurl_access(const nsurl *url, size_t *url_l)
{
	assert(url != NULL);

	*url_l = url->length;
	return url->string;
}


/* exported interface, documented in nsurl.h */
nserror nsurl_join(const nsurl *base, const char *rel, nsurl **joined)
{
	struct url_markers m;
	size_t length;
	char *buff;
	char *buff_pos;
	char *buff_start;
	nserror error = 0;
	enum {
		NSURL_F_REL		=  0,
		NSURL_F_BASE_SCHEME	= (1 << 0),
		NSURL_F_BASE_AUTHORITY	= (1 << 1),
		NSURL_F_BASE_PATH	= (1 << 2),
		NSURL_F_MERGED_PATH	= (1 << 3),
		NSURL_F_BASE_QUERY	= (1 << 4)
	} joined_parts;

	assert(base != NULL);
	assert(rel != NULL);

	/* Peg out the URL sections */
	nsurl__get_string_markers(rel, &m, true);

	/* Get the length of the longest section */
	length = nsurl__get_longest_section(&m);

	/* Initially assume that the joined URL can be formed entierly from
	 * the relative URL. */
	joined_parts = NSURL_F_REL;

	/* Update joined_compnents to indicate any required parts from the
	 * base URL. */
	if (m.scheme_end - m.start <= 0) {
		/* The relative url has no scheme.
		 * Use base URL's scheme. */
		joined_parts |= NSURL_F_BASE_SCHEME;

		if (m.path - m.authority <= 0) {
			/* The relative URL has no authority.
			 * Use base URL's authority. */
			joined_parts |= NSURL_F_BASE_AUTHORITY;

			if (m.query - m.path <= 0) {
				/* The relative URL has no path.
				 * Use base URL's path. */
				joined_parts |= NSURL_F_BASE_PATH;

				if (m.fragment - m.query <= 0) {
					/* The relative URL has no query.
					 * Use base URL's query. */
					joined_parts |= NSURL_F_BASE_QUERY;
				}

			} else if (*(rel + m.path) != '/') {
				/* Relative URL has relative path */
				joined_parts |= NSURL_F_MERGED_PATH;
			}
		}
	}

	/* Create NetSurf URL object */
	*joined = calloc(1, sizeof(nsurl));
	if (*joined == NULL) {
		return NSERROR_NOMEM;
	}

	/* Allocate enough memory to url escape the longest section, plus
	 * space for path merging (if required). */
	if (joined_parts & NSURL_F_MERGED_PATH) {
		/* Need to merge paths */
		length += lwc_string_length(base->path);
	}
	length *= 4;
	/* Plus space for removing dots from path */
	length += (m.query - m.path) + lwc_string_length(base->path);
	buff = malloc(length + 5);
	if (buff == NULL) {
		free(*joined);
		return NSERROR_NOMEM;
	}

	buff_start = buff_pos = buff;

	/* Form joined URL from base or rel components, as appropriate */

	if (joined_parts & NSURL_F_BASE_SCHEME)
		(*joined)->scheme = nsurl__component_copy(base->scheme);
	else
		error |= nsurl__create_from_section(rel, URL_SCHEME, &m,
				buff, *joined);

	if (joined_parts & NSURL_F_BASE_AUTHORITY) {
		(*joined)->username = nsurl__component_copy(base->username);
		(*joined)->password = nsurl__component_copy(base->password);
		(*joined)->host = nsurl__component_copy(base->host);
		(*joined)->port = nsurl__component_copy(base->port);
	} else {
		error |= nsurl__create_from_section(rel, URL_CREDENTIALS, &m,
				buff, *joined);
		error |= nsurl__create_from_section(rel, URL_HOST, &m,
				buff, *joined);
	}

	if (joined_parts & NSURL_F_BASE_PATH) {
		(*joined)->path = nsurl__component_copy(base->path);

	} else if (joined_parts & NSURL_F_MERGED_PATH) {
		struct url_markers m_path;
		size_t path_len;
		size_t new_length;

		if (base->host != NULL && base->path == NULL) {
			/* Append relative path to "/". */
			*(buff_pos++) = '/';
			memcpy(buff_pos, rel + m.path, m.query - m.path);
			buff_pos += m.query - m.path;

			path_len = 1 + m.query - m.path;

		} else {
			/* Append relative path to all but last segment of
			 * base path. */
			size_t path_end = lwc_string_length(base->path);
			const char *path = lwc_string_data(base->path);

			while (*(path + path_end) != '/' &&
					path_end != 0) {
				path_end--;
			}
			if (*(path + path_end) == '/')
				path_end++;

			/* Copy the base part */
			memcpy(buff_pos, path, path_end);
			buff_pos += path_end;

			/* Copy the relative part */
			memcpy(buff_pos, rel + m.path, m.query - m.path);
			buff_pos += m.query - m.path;

			path_len = path_end + m.query - m.path;
		}

		/* add termination to string */
		*buff_pos++ = '\0';

		new_length = nsurl__remove_dot_segments(buff, buff_pos);

		m_path.path = 0;
		m_path.query = new_length;

		buff_start = buff_pos + new_length;
		error |= nsurl__create_from_section(buff_pos, URL_PATH, &m_path,
				buff_start, *joined);

	} else {
		struct url_markers m_path;
		size_t new_length;

		memcpy(buff_pos, rel + m.path, m.query - m.path);
		buff_pos += m.query - m.path;
		*(buff_pos++) = '\0';

		new_length = nsurl__remove_dot_segments(buff, buff_pos);

		m_path.path = 0;
		m_path.query = new_length;

		buff_start = buff_pos + new_length;
		error |= nsurl__create_from_section(buff_pos, URL_PATH, &m_path,
				buff_start, *joined);
	}

	if (joined_parts & NSURL_F_BASE_QUERY)
		(*joined)->query = nsurl__component_copy(base->query);
	else
		error |= nsurl__create_from_section(rel, URL_QUERY, &m,
				buff, *joined);

	error |= nsurl__create_from_section(rel, URL_FRAGMENT, &m,
			buff, *joined);

	/* Free temporary buffer */
	free(buff);

	if (error != NSERROR_OK) {
		free(*joined);
		return NSERROR_NOMEM;
	}

	/* Get the complete URL string */
	if (nsurl_get(*joined, NSURL_WITH_FRAGMENT, &((*joined)->string),
			&((*joined)->length)) != NSERROR_OK) {
		free(*joined);
		return NSERROR_NOMEM;
	}

	/* Give the URL a reference */
	(*joined)->count = 1;

	return NSERROR_OK;
}