summaryrefslogtreecommitdiff
path: root/plugins/static/static.c
blob: 2fb6e0fd625d2e7245805d0b00cb5e73c001cfb6 (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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include "plugin.h"
#include "util.h"
#include "buffer.h"


static char *static_file_dir = NULL;


static bool path_allowed(const char *path) {
	char last = '\0';
	for (size_t i = 0; path[i]; i++) {
		// no unprintable characters
		if (path[i] < 32 || path[i] >= 127) return false;

		// no hidden files or magic directory items like ".."
		if (last == '/' && path[i] == '.') return false;

		last = path[i];
	}

	return true;
}

// Returns pointer to internal buffer, don't free
static const char* make_relative_path(const char *path) {
	static char *rel = NULL;
	static size_t rel_cap = 0;

	size_t necessary = strlen(static_file_dir) + 1 + strlen(path) + 1;
	if (necessary >= rel_cap) {
		rel_cap = necessary;
		rel = realloc(rel, rel_cap);
		assert(rel);
	}

	sprintf(rel, "%s/%s", static_file_dir, path);

	return rel;
}

// If an error occurs, returns -1 and sets errno
static ssize_t query_file_size(const char *path) {
	struct stat st;
	memset(&st, 0, sizeof st);
	if (stat(path, &st) < 0) {
		return -1;
	}
	if (st.st_mode & S_IFREG) {
		return st.st_size;
	} else {
		errno = EISDIR;
		return -1;
	}
}

static struct buffer build_response_headers(const char *status, const char *content_type, size_t body_len) {
	struct buffer buffer = buffer_make(200 + body_len);

	buffer_append_str(&buffer, "HTTP/1.1 ");
	buffer_append_str(&buffer, status);
	buffer_append_str(&buffer, "\r\n");

	if (content_type) {
		buffer_append_str(&buffer, "Content-Type: ");
		buffer_append_str(&buffer, content_type);
		buffer_append_str(&buffer, "\r\n");
	}

	char tempbuf[128];  // size_t is at most 20 chars long
	sprintf(tempbuf, "Content-Length: %zu\r\n", body_len);
	buffer_append_str(&buffer, tempbuf);

	buffer_append_str(&buffer, "Server: cserver\r\n\r\n");

	return buffer;
}

static void send_404(int sock) {
	const char *body = "404 Not found";
	struct buffer buffer = build_response_headers("404 Not found", NULL, strlen(body));
	buffer_append_str(&buffer, body);
	sendall(sock, buffer.buf, buffer.len);
}

static void send_500(int sock) {
	const char *body = "500 Internal server error";
	struct buffer buffer = build_response_headers("500 Internal server error", NULL, strlen(body));
	buffer_append_str(&buffer, body);
	sendall(sock, buffer.buf, buffer.len);
}

static Handler_ret_t connection_handler(int sock, Headers *headers) {
	if (!path_allowed(headers->url)) {
		send_404(sock);
		return HR_HANDLED;
	}

	const char *path = make_relative_path(headers->url);

	ssize_t size = query_file_size(path);
	if (size < 0) {
		if (errno == ENOENT || errno == EISDIR) {
			send_404(sock);
			return HR_HANDLED;
		} else {
			send_500(sock);
			return HR_HANDLED;
		}
	}

	struct buffer buffer = build_response_headers("200 OK", "text/plain; charset=UTF-8", size);
	if (!buffer_append_file(&buffer, path)) {
		buffer_free(buffer);
		send_500(sock);
		return HR_HANDLED;
	}

	sendall(sock, buffer.buf, buffer.len);
	buffer_free(buffer);
	return HR_HANDLED;
}

void plugin_register_yourself(register_callback_t callback) {
	static_file_dir = getenv("STATIC_DIR");
	if (!static_file_dir || access(static_file_dir, R_OK | X_OK) < 0) {
		fprintf(stderr, "cserver: static: Environment variable $STATIC_DIR not set or directory not readable\n");
		exit(1);
	}

	callback("static", &connection_handler);
}