summary refs log tree commit diff stats
path: root/normalize.c
diff options
context:
space:
mode:
authorAli Fardan <raiz@stellarbound.space>2020-11-18 10:55:20 +0300
committerAli Fardan <raiz@stellarbound.space>2020-11-18 10:55:20 +0300
commit6f3d10a3ec09cc278041ced4d4e506cb89a7c69a (patch)
tree5b5c7db3104d805d71029534cffcfd5d9c2aca0b /normalize.c
downloadlibyuri-6f3d10a3ec09cc278041ced4d4e506cb89a7c69a.tar.gz
initial commit; todo: thorough testing
Diffstat (limited to 'normalize.c')
-rw-r--r--normalize.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/normalize.c b/normalize.c
new file mode 100644
index 0000000..835afa9
--- /dev/null
+++ b/normalize.c
@@ -0,0 +1,51 @@
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "yuri.h"
+
+static int
+_eat(struct uri *u, int index)
+{
+	int i;
+	char **path;
+
+	free(u->path[index]);
+	for (i = index+1; i < u->npath; i++)
+		u->path[i-1] = u->path[i];
+	u->npath--;
+
+	path = realloc(u->path, sizeof(*u->path)*u->npath);
+	if (path == NULL)
+		return -1;
+	u->path = path;
+
+	return 0;
+}
+
+int
+uri_normalize(struct uri *u)
+{
+	int i;
+
+	for (i = 0; i < strlen(u->scheme); i++) {
+		if (isalpha(u->scheme[i]))
+			u->scheme[i] = tolower(u->scheme[i]);
+	}
+
+	if (u->authority.host) {
+		for (i = 0; i < strlen(u->authority.host); i++) {
+			if (isalpha(u->authority.host[i]))
+				u->authority.host[i] = tolower(u->authority.host[i]);
+		}
+	}
+
+	for (i = 0; i < u->npath; i++) {
+		if (strcmp(u->path[i], ".") == 0 || strcmp(u->path[i], "..") == 0) {
+			if (_eat(u, i) == -1)
+				return -1;
+		}
+	}
+
+	return 0;
+}