summaryrefslogtreecommitdiff
path: root/multipart-replace-http-server.c
blob: c3e62191674bbeca604517ee4460cb058a46c505 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Copyright 2016 Luke Shumaker */

#include <error.h>
#include <errno.h>
#include <netdb.h> /* for {get,free}addrinfo() */
#include <stdlib.h> /* for EXIT_FAILURE */
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h> /* for open */
#include <fcntl.h> /* for open */

#include "util.h"
#include "multipart-replace.h"

struct httpfile {
	char name[256];
	struct multipart_replace_stream *stream;
};

size_t filec = 0;
struct httpfile *filev = NULL;

struct reader_thread_args {
	struct multipart_replace_stream *stream;
	int fd;
	const char *boundary;
};

void *reader_thread(void *args_anon) {
	struct reader_thread_args *args = args_anon;
	multipart_replace_reader(args->stream, args->fd, args->boundary);
	error(EXIT_FAILURE, 0, "multipart_replace stream ended");
	return NULL;
}

void start_multipart_replace_reader(struct multipart_replace_stream *s, int fd, const char *boundary) {
	struct reader_thread_args *args = xrealloc(NULL, sizeof(struct reader_thread_args));
	args->stream = s;
	args->fd = fd;
	args->boundary = boundary;
	pthread_t thread;
	pthread_create(&thread, NULL, reader_thread, args);
}

void file_add(const char *filename) {
	struct multipart_replace_stream *stream = xrealloc(NULL, sizeof(struct multipart_replace_stream));
	init_multipart_replace_stream(stream);
	int fd = open(filename, O_RDONLY);
	if (fd < 0)
		error(EXIT_FAILURE, errno, "opening file: %s", filename);
	filev = xrealloc(filev, (++filec)*sizeof(*filev));
	const char *shortname = strrchr(filename, '/');
	if (shortname == NULL)
		shortname = filename;
	strncpy(filev[filec-1].name, shortname, sizeof(filev[filec-1].name));
	error(0, 0, "added file #%zd: %s", filec-1, filev[filec-1].name);
	filev[filec-1].stream = stream;
	start_multipart_replace_reader(stream, fd, "ffserver" /* FIXME */);
}

struct multipart_replace_stream *file_get(const char *filename) {
	for (size_t i = 0; i < filec; i++) {
		if (strncmp(filename, filev[i].name, sizeof(filev[i].name)) == 0) {
			return filev[i].stream;
		}
	}
	return NULL;
}

int tcp4_parse_listen(const char *port) {
	int r;
	struct addrinfo *addr = NULL;
	struct addrinfo hints = { 0 };
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_PASSIVE;

	if ((r = getaddrinfo(NULL, port, &hints, &addr)) != 0)
		error(EXIT_FAILURE, r, _("Could not resolve TCP4 port %s"), port);

	int sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
	if (sock < 0)
		error(EXIT_FAILURE, errno, _("Could not create a TCP4 socket"));

	int yes = 1;
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

	if (bind(sock, addr->ai_addr, addr->ai_addrlen) < 0)
		error(EXIT_FAILURE, errno, _("Could not bind to TCP4 port %s"), port);

	if (listen(sock, 5) < 0)
		error(EXIT_FAILURE, errno, _("Could not listen on TCP4 port %s"), port);

	freeaddrinfo(addr);
	return sock;
}

void connection_handler(int fd) {
	FILE *netstream = fdopen(fd, "r");
	char *line_buf = NULL;
	size_t line_cap = 0;
	ssize_t line_len = 0;

	line_len = getline(&line_buf, &line_cap, netstream);
	/* expect line to be "GET /path.mjpg HTTP/1.1" */
	if (strncmp(line_buf, "GET ", 4) != 0) {
		dprintf(fd,
		        "HTTP/1.1 405 Method Not Allowed\r\n"
		        "Allow: GET\r\n"
		        "\r\n");
		close(fd);
		return;
	}
	char *version = &line_buf[4];
	char *path = strsep(&version, " ");
	if (strcmp(version, "HTTP/1.1\r\n") != 0 && strcmp(version, "HTTP/1.0\r\n")) {
		close(fd);
		return;
	}
	path = strdupa(path);

	/* run through the rest of the headers */
	while (strcmp(line_buf, "\r\n") != 0 && line_len >= 0)
		line_len = getline(&line_buf, &line_cap, netstream);

	struct multipart_replace_stream *vidstream = file_get(path);
	if (vidstream == NULL) {
		error(0, 0, "404 %s", path);
		dprintf(fd,
		        "HTTP/1.1 404 Not Found\r\n"
		        "\r\n");
		return;
	}

	const char *boundary = "boundary" /* FIXME */;
	error(0, 0, "200 %s", path);
	dprintf(fd,
	        "HTTP/1.1 200 OK\r\n"
	        "Content-Type: multipart/x-mixed-replace;boundary=%s\r\n"
	        "\r\n",
	        boundary);
	multipart_replace_writer(vidstream, fd, boundary);
	close(fd);
}

void *connection_thread(void *arg_anon) {
	int fd = (int)(intptr_t)arg_anon;
	error(0, 0, "Connection %d opened", fd);
	connection_handler(fd);
	error(0, 0, "Connection %d closed", fd);
	return NULL;
}

void usage() {
	printf("Usage: %s [-h] PORT [FILENAME...]\n", program_invocation_name);
}

int main(int argc, char *argv[]) {
	if (argc < 2) {
		dup2(2, 1);
		usage();
		return EXIT_FAILURE;
	}
	if (strcmp(argv[1], "-h") == 0) {
		usage();
		return EXIT_SUCCESS;
	}
	int sock = tcp4_parse_listen(argv[1]);

	for (int i = 2; i < argc; i++)
		file_add(argv[i]);

	signal(SIGPIPE, SIG_IGN);
	while (1) {
		int conn = accept(sock, NULL, NULL);
		if (conn < 0)
			continue;
		pthread_t thread;
		pthread_create(&thread, NULL, connection_thread, (void*)(intptr_t)conn);
	}

	return 0;
}