summary refs log tree commit diff stats
path: root/normalize.c
diff options
context:
space:
mode:
authorAli Fardan <raiz@stellarbound.space>2020-11-22 17:02:51 +0300
committerAli Fardan <raiz@stellarbound.space>2020-11-22 17:02:51 +0300
commit5a66aa1bbace4775025cd99a8ab0a8a18487dfe9 (patch)
treea40b9c727001661fb2200f06b87155a9e5a8db98 /normalize.c
parent7ea4eb3fe07564f605731b4b4eef09a47c0bfb08 (diff)
downloadlibyuri-5a66aa1bbace4775025cd99a8ab0a8a18487dfe9.tar.gz
- implement percent encoding in encode.c
- move syntax macros from decode.c to separate types.h header
- fix normalizer (sorta, needs testing)
- TODO: more testing...
Diffstat (limited to 'normalize.c')
-rw-r--r--normalize.c36
1 files changed, 33 insertions, 3 deletions
diff --git a/normalize.c b/normalize.c
index f8e8dbe..821df35 100644
--- a/normalize.c
+++ b/normalize.c
@@ -32,6 +32,7 @@ int
 uri_normalize(struct uri *u)
 {
 	int i;
+	int therewaspath;
 
 	if (u->scheme) {
 		for (i = 0; i < strlen(u->scheme); i++) {
@@ -47,20 +48,49 @@ uri_normalize(struct uri *u)
 		}
 	}
 
+	if (u->npath != 0)
+		therewaspath = 1;
+	else
+		therewaspath = 0;
+
+	for (i = 0; i < u->npath; i++) {
+		if (i != u->npath-1 || u->npath == 1) {
+			if (strcmp(u->path[i], "") == 0) {
+				if (_eat(u, i) == -1)
+					return -1;
+				i = 0; /* count altered, reset back */
+			}
+		}
+	}
+
 	for (i = 0; i < u->npath; i++) {
 		if (strcmp(u->path[i], ".") == 0) {
 			if (_eat(u, i) == -1)
 				return -1;
+			i = 0; /* count altered, reset back */
 		}
+	}
+
+	for (i = 0; i < u->npath; i++) {
 		if (strcmp(u->path[i], "..") == 0) {
-			if (u->npath >= 2 && i-1 >= 0) {
-				if (_eat(u, i-1) == -1)
-					return -1;
+			if (_eat(u, i) == -1)
+				return -1;
+			if (i-1 >= 0) {
 				if (_eat(u, i-1) == -1)
 					return -1;
 			}
+			i = 0; /* count altered, reset back */
 		}
 	}
 
+	/* if there was a path and all redundant segments were remved
+	 * we'd be left with no path and path list would be set to NULL
+	 * indicating that path has never exists, we don't want that,
+	 * so we just add an empty path back */
+	if (therewaspath && u->npath == 0) {
+		if (uri_append_path(u, "") == -1)
+			return -1;
+	}
+
 	return 0;
 }