summaryrefslogtreecommitdiff
path: root/c/webserver.c
blob: 5de519ca4700c4d4b1360173a4fa010ba38f9a00 (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
#include <sys/socket.h>
#include <string.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <unistd.h>
#include <netinet/in.h>

// followed that one guys tutorial. can only send 256 bytes per request
void main() {
	int sock = socket(AF_INET, SOCK_STREAM, 0);
	struct sockaddr_in addr = {
		AF_INET,
		0x901f,
		0
	};
	bind(sock, &addr, sizeof(addr));
	listen(sock, 32);
	int client_fd = accept(sock, 0, 0);
	char buffer[256] = {0};
	recv(client_fd, buffer, 256, 0);

	char* file = buffer + 5;
	*strchr(file, ' ') = 0;
	int opened_fd = open(file, O_RDONLY);
	sendfile(client_fd, opened_fd, 0, 256);
	close(opened_fd);
	close(client_fd);
	close(sock);
}