aboutsummaryrefslogtreecommitdiff
path: root/server-connection.c
blob: f0ac1e6681a4df2a4d036c4cccdff7902f768d0d (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
#include <server-connection.h>

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include <string.h>

sds constructFilePath(const sds root, const char* file) {
	sds path = sdsdup(root);
	if (root[sdslen(root)-1] != '/' && file[0] != '/')
		path = sdscat(path, "/");
	path = sdscat(path, file);
	if (file[strlen(file)-1] == '/')
		path = sdscat(path, "index.md");
	return path;
}

void on_connection(const char* client, const int fd_client, sds **vhosts, const int vhostsc) {
	printf("[%s@%d] Connected successfully!\n", client, fd_client);

	/* Get address request */
	char address[256];
	memset(address, 0, 256);

	read(fd_client, address, 256);
	printf("[%s@%d] Requested %s\n", client, fd_client, address);

	/* Does vhosts contain an address with the username? */
	int usernameLen = strchr(address, '@') - address;

	const sds *vhost = NULL;
	for (int i = 0; i < vhostsc; i++) {
		if (strncmp(vhosts[i][vh_user], address, usernameLen) == 0) {
			vhost = *vhosts + i;
			break;
		}
	}

	if (vhost == NULL) {
		fprintf(stderr, "[%s@%d] Unknown username in address %s\n", client, fd_client, address);
		return;
	}

	/* Try to open the requested file or the error file */
	sds filePath = constructFilePath(vhost[vh_path], address + usernameLen + 1);

	int fd = open(filePath, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "[%s@%d] Error opening %s\n", client, fd_client, filePath);

		sdsfree(filePath);
		filePath = constructFilePath(vhost[vh_path], vhost[vh_error]);
		fd = open(filePath, O_RDONLY);
		if (fd < 0) {
			fprintf(stderr, "[%s@%d] Error opening %s\n", client, fd_client, filePath);
			sdsfree(filePath);
			return;
		}
	}

	/* Send the file to the client */
	printf("[%s@%d] Serving %s\n", client, fd_client, filePath);
	sdsfree(filePath);

	char buff[256];
	memset(buff, 0, sizeof(buff));
	while (read(fd, buff, 256)) {
		write(fd_client, buff, strlen(buff));
		memset(buff, 0, sizeof(buff));
	}

	/* Finalize */
	close(fd);
	printf("[%s@%d] Served!\n", client, fd_client);
}