summaryrefslogtreecommitdiff
path: root/plugins/static/static.c
blob: 515206880b9f95605d879bd2bd4002b80cbddb36 (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
140
141
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include "memory.h"
#include "plugin.h"
#include "util.h"
#include "buffer.h"
#include "mime.h"


// Will only be set once in the registration function, which is before any threading is done
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;
}

static char* make_relative_path(const char *path) {
	char *rel = malloc(strlen(static_file_dir) + 1 + strlen(path) + 1, char);
	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", "text/plain", 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", "text/plain", strlen(body));
	buffer_append_str(&buffer, body);
	sendall(sock, buffer.buf, buffer.len);
}

static Handler_ret_t connection_handler(int sock, Headers *headers) {
	char *path = NULL;
	struct buffer buffer;
	buffer.buf = NULL;

	if (!path_allowed(headers->url)) {
		send_404(sock);
		goto cleanup_return;
	}

	path = make_relative_path(headers->url);

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

	const char *mimetype = mime_detect(path);
	if (!mimetype) mimetype = "application/octet-stream";

	buffer = build_response_headers("200 OK", mimetype, size);
	if (!buffer_append_file(&buffer, path)) {
		send_500(sock);
		goto cleanup_return;
	}

	sendall(sock, buffer.buf, buffer.len);

cleanup_return:
	if (buffer.buf) buffer_free(buffer);
	if (path) free(path);
	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);
	}

	mime_init();

	callback("static", &connection_handler);
}