summary refs log tree commit diff
path: root/net.c
blob: e90d3c06a1e874b5d085087c3fd32d55597288e7 (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
#include <string.h>
#include <stdio.h>

#include "net.h"
#include "err.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;
	}

	if (adr->prot == PROT_FILE) {
		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 + 1);
	} else {
		return -1;
	}

	return 0;
}

int net_fetch(const struct addr *adr, struct buf *buf) {
	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;
	default:
		return -1;
	}
}