summary refs log tree commit diff
path: root/net.c
diff options
context:
space:
mode:
authorwrmr2024-11-02 15:10:41 -0500
committerwrmr2024-11-02 15:10:41 -0500
commit58214ec5f982c1b97aadce254c958a5f922c9724 (patch)
treeae64cb697531b0ee619350645e47adf186febfe7 /net.c
parentdf1b0b23569e5244d5b9398802fec6435a995368 (diff)
split main.c in several places, began work on design doc
Diffstat (limited to 'net.c')
-rw-r--r--net.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/net.c b/net.c
new file mode 100644
index 0000000..73f50d4
--- /dev/null
+++ b/net.c
@@ -0,0 +1,34 @@
+#include <string.h>
+#include "net.h"
+
+int net_addr(const char *url, struct addr *adr, enum protocol prot_default) {
+	char *prot_mark;
+	if ((prot_mark = strstr(url, "://"))) {
+		static struct {
+			const char *str;
+			enum protocol prot;
+		} prot_str_tbl[] = {
+			{ "gopher", PROT_GOPHER },
+			{ "gemini", PROT_GEMINI },
+			{ "file", PROT_FILE },
+		};
+		size_t n = prot_mark - url;
+		adr->prot = PROT_UNKNOWN;
+		for (size_t i = 0; i < sizeof prot_str_tbl / sizeof *prot_str_tbl; i++) {
+			if (!strncmp(prot_str_tbl[i].str, url, n)) {
+				adr->prot = prot_str_tbl[i].prot;
+				break;
+			}
+		}
+		url = prot_mark + 3;
+	} else {
+		adr->prot = prot_default;
+	}
+	adr->type = TYPE_PLAIN;
+	adr->host_len = 0;
+	size_t n = strlen(url);
+	if (n > PATH_MAX) return -1;
+	adr->path_len = n;
+	memcpy(adr->path, url, n);
+	return 0;
+}