summary refs log tree commit diff
path: root/server.c
blob: 49555247a0c811f57906ded00e75eef4a0ffa27c (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Copyright (C) 2020  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <err.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>

#include "server.h"

sqlite3 *db;
struct Statements stmt;

static void prepare(sqlite3_stmt **stmt, const char *query) {
	int error = sqlite3_prepare_v3(
		db, query, -1, SQLITE_PREPARE_PERSISTENT, stmt, NULL
	);
	if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), query);
}

const char *Pages[PagesLen] = {
#define X(page, path) [page] = path,
	ENUM_PAGES
#undef X
};

const struct kvalid Keys[KeysLen] = {
#define X(key, name, valid) [key] = { valid, name },
	ENUM_KEYS
#undef X
};

bool pagePublic;
int pageLimit = 100;
int pageRecent = 500;

static const char CSS[] = {
#include "default.css.h"
};
static const char *Cache = "public, max-age=86400, immutable";

static enum kcgi_err request(struct kreq *req) {
	if (req->method != KMETHOD_HEAD && req->method != KMETHOD_GET) {
		return httpFail(req, KHTTP_405);
	}
	switch (req->page) {
		case Networks: return pageNetworks(req);
		case Contexts: return pageContexts(req);
		case Events:   return pageEvents(req);
		case Search:   return pageSearch(req);
		case Stylesheet: {
			if (req->mime != KMIME_TEXT_CSS) return httpFail(req, KHTTP_404);
			return httpHead(req, KHTTP_200, KMIME_TEXT_CSS)
				|| khttp_head(req, kresps[KRESP_CACHE_CONTROL], "%s", Cache)
				|| khttp_body(req)
				|| khttp_write(req, CSS, sizeof(CSS));
		}
		default: return httpFail(req, KHTTP_404);
	}
}

int main(int argc, char *argv[]) {
	bool fastCGI = false;
	bool test = false;

	for (int opt; 0 < (opt = getopt(argc, argv, "cfl:pr:s:"));) {
		switch (opt) {
			break; case 'c': test = true;
			break; case 'f': fastCGI = true;
			break; case 'l': pageLimit = strtol(optarg, NULL, 10);
			break; case 'p': pagePublic = true;
			break; case 'r': pageRecent = strtol(optarg, NULL, 10);
			break; case 's': htmlStylesheet = optarg;
			break; default:  return EX_USAGE;
		}
	}
	if (optind == argc) errx(EX_USAGE, "database path required");

	int error = sqlite3_open_v2(argv[optind], &db, SQLITE_OPEN_READONLY, NULL);
	if (error) errx(EX_NOINPUT, "%s: %s", argv[optind], sqlite3_errmsg(db));
	atexit(dbClose);

	sqlite3_stmt *check;
	error = sqlite3_prepare_v2(
		db, SQL(PRAGMA user_version;), -1, &check, NULL
	);
	if (error) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));

	error = sqlite3_step(check);
	if (error != SQLITE_ROW) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));

	int version = sqlite3_column_int(check, 0);
	if (version != DatabaseVersion) {
		errx(EX_DATAERR, "unsupported database version %d", version);
	}
	sqlite3_finalize(check);

	prepare(&stmt.networks, NetworksQuery);
	prepare(&stmt.contexts, ContextsQuery);
	prepare(&stmt.eventsAfter, EventsAfterQuery);
	prepare(&stmt.eventsBefore, EventsBeforeQuery);
	
	if (test) return EX_OK;
	
	if (fastCGI) {
		struct kfcgi *fcgi;
		enum kcgi_err error = khttp_fcgi_init(
			&fcgi, Keys, KeysLen, Pages, PagesLen, Networks
		);
		if (error) errx(EX_CONFIG, "khttp_fcgi_init: %s", kcgi_strerror(error));
		for (
			struct kreq req;
			KCGI_OK == (error = khttp_fcgi_parse(fcgi, &req));
			khttp_free(&req)
		) {
			error = request(&req);
			if (error && error != KCGI_HUP) break;
		}
		errx(EX_PROTOCOL, "khttp_fcgi_parse: %s", kcgi_strerror(error));
	} else {
		struct kreq req;
		enum kcgi_err error = khttp_parse(
			&req, Keys, KeysLen, Pages, PagesLen, Networks
		);
		if (error) errx(EX_PROTOCOL, "khttp_parse: %s", kcgi_strerror(error));
		error = request(&req);
		if (error) errx(EX_PROTOCOL, "%s", kcgi_strerror(error));
		khttp_free(&req);
	}
}