summaryrefslogtreecommitdiff
path: root/multipart-replace-http-server.c
diff options
context:
space:
mode:
Diffstat (limited to 'multipart-replace-http-server.c')
-rw-r--r--multipart-replace-http-server.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/multipart-replace-http-server.c b/multipart-replace-http-server.c
index 785bfe3..d4f0cd3 100644
--- a/multipart-replace-http-server.c
+++ b/multipart-replace-http-server.c
@@ -5,6 +5,7 @@
#include <fcntl.h> /* for open */
#include <netdb.h> /* for {get,free}addrinfo() */
#include <stdio.h>
+#include <stdlib.h> /* atexit */
#include <stdlib.h> /* for EXIT_FAILURE */
#include <string.h>
#include <sys/socket.h>
@@ -60,7 +61,7 @@ void file_add(const char *filename) {
strcpy(&shortname[1], 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);
+ log("added file #%zd: %s", filec-1, filev[filec-1].name);
filev[filec-1].stream = stream;
start_multipart_replace_reader(stream, fd, "ffserver" /* FIXME */);
}
@@ -103,7 +104,7 @@ void connection_handler(int fd) {
line_len = getline(&line_buf, &line_cap, netstream);
if (strcmp(path, "/") == 0) {
- error(0, 0, "200 %s", path);
+ log("200 %s", path);
dprintf(fd,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
@@ -127,7 +128,7 @@ void connection_handler(int fd) {
}
struct multipart_replace_stream *vidstream = file_get(path);
if (vidstream == NULL) {
- error(0, 0, "404 %s", path);
+ log("404 %s", path);
dprintf(fd,
"HTTP/1.1 404 Not Found\r\n"
"\r\n");
@@ -135,7 +136,7 @@ void connection_handler(int fd) {
}
const char *boundary = "boundary" /* FIXME */;
- error(0, 0, "200 %s", path);
+ log("200 %s", path);
dprintf(fd,
"HTTP/1.1 200 OK\r\n"
"Content-Type: multipart/x-mixed-replace;boundary=%s\r\n"
@@ -148,9 +149,9 @@ void connection_handler(int fd) {
void *connection_thread(void *arg_anon) {
pthread_setname_np(pthread_self(), "connection");
int fd = (int)(intptr_t)arg_anon;
- error(0, 0, "Connection %d opened", fd);
+ log("Connection %d opened", fd);
connection_handler(fd);
- error(0, 0, "Connection %d closed", fd);
+ log("Connection %d closed", fd);
return NULL;
}
@@ -275,6 +276,12 @@ void usage() {
"the FILENAMEs should be pipes or sockets.\n");
}
+void cleanup() {
+ log("STOPPING=1");
+ fflush(stderr);
+ sleep(5); /* work around systemd bug dropping log messages */
+}
+
int main(int argc, char *argv[]) {
if (argc >=2 && strcmp(argv[1], "-h") == 0) {
usage();
@@ -285,6 +292,9 @@ int main(int argc, char *argv[]) {
usage();
return EXIT_FAILURE;
}
+
+ atexit(cleanup);
+
int sock = sockstream_listen(argv[1], argv[2]);
if (sock < 0) {
if (sock == -EAI_SYSTEM)