summary refs log tree commit diff
path: root/net.c
diff options
context:
space:
mode:
Diffstat (limited to 'net.c')
-rw-r--r--net.c59
1 files changed, 42 insertions, 17 deletions
diff --git a/net.c b/net.c
index b826e96..9a21fb2 100644
--- a/net.c
+++ b/net.c
@@ -3,6 +3,7 @@
 
 #include "net.h"
 #include "err.h"
+#include "doc.h"
 
 int net_addr(const char *url, struct addr *adr, enum protocol prot_default) {
 	char *prot_mark;
@@ -29,38 +30,62 @@ int net_addr(const char *url, struct addr *adr, enum protocol prot_default) {
 	}
 
 	if (adr->prot == PROT_FILE) {
-		adr->type = DOC_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 + 1);
 	} else {
+		adr->host_len = 0;
+		while (*url && *url != '/' && adr->host_len < HOST_MAX) {
+			adr->host[adr->host_len++] = *url++;
+		}
+		adr->host[adr->host_len] = 0;
+		if (*url && *url != '/') {
+			perr("hostname too long");
+			return -1;
+		}
+		if (*url) url++;
+		adr->path_len = 0;
+		while (*url && adr->path_len < HOST_MAX) {
+			adr->path[adr->path_len++] = *url++;
+		}
+		adr->path[adr->path_len] = 0;
+		if (*url) {
+			perr("path too long");
+			return -1;
+		}
+		puts(adr->host);
+		puts(adr->path);
 		return -1;
 	}
 
 	return 0;
 }
 
-int net_fetch(const struct addr *adr, struct buf *buf) {
+static int file_fetch(const struct addr *adr, struct buf *buf, enum doc_type *doct) {
+	FILE *f = fopen(adr->path, "r/o");
+	if (!f) {
+		perr("file not found");
+		return -1;
+	}
+	buf_init(buf, 1024);
+	char b[256];
+	while (fgets(b, sizeof b, f)) {
+		buf_cat(buf, b, strlen(b));
+	}
+	buf_catc(buf, 0);
+	fclose(f);
+	*doct = DOC_PLAIN;
+	return 0;
+}
+
+int net_fetch(const struct addr *adr, struct buf *buf, enum doc_type *doct) {
 	switch (adr->prot) {
 	case PROT_FILE:
-		{
-			FILE *f = fopen(adr->path, "r/o");
-			if (!f) {
-				perr("file not found");
-				return -1;
-			}
-			buf_init(buf, 1024);
-			char b[256];
-			while (fgets(b, sizeof b, f)) {
-				buf_cat(buf, b, strlen(b));
-			}
-			buf_catc(buf, 0);
-			fclose(f);
-		}
-		return 0;
+		return file_fetch(adr, buf, doct);
 	default:
+		perr("unsupported protocol");
 		return -1;
 	}
 }