summaryrefslogtreecommitdiff
path: root/thread_http.c
diff options
context:
space:
mode:
Diffstat (limited to 'thread_http.c')
-rw-r--r--thread_http.c129
1 files changed, 0 insertions, 129 deletions
diff --git a/thread_http.c b/thread_http.c
deleted file mode 100644
index 353ad0f..0000000
--- a/thread_http.c
+++ /dev/null
@@ -1,129 +0,0 @@
-#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 "main.h"
-#include "wg.h"
-
-static
-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;
-}
-
-static struct wg *wg;
-static struct mpjpeg_stream *video;
-static struct mpjpeg_stream *depth;
-
-static
-void thread_http_connection(int fd) {
- FILE *stream = fdopen(fd, "r");
- char *line_buf = NULL;
- size_t line_cap = 0;
- getline(&line_buf, &line_cap, stream);
- /* expect line to be "GET /kinect-(video|depth).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)
- getline(&line_buf, &line_cap, stream);
-
- const char *boundary = "boundary";
- if (strcmp(path, "/kinect-video.mjpg") == 0) {
- dprintf(fd,
- "HTTP/1.1 200 OK\r\n"
- "Content-Type: multipart/x-mixed-replace;boundary=%s\r\n"
- "\r\n",
- boundary);
- thread_mpjpeg_writer(video, fd, boundary);
- } else if (strcmp(path, "/kinect-depth.mjpg") == 0) {
- dprintf(fd,
- "HTTP/1.1 200 OK\r\n"
- "Content-Type: multipart/x-mixed-replace;boundary=%s\r\n"
- "\r\n",
- boundary);
- thread_mpjpeg_writer(depth, fd, boundary);
- } else {
- printf("not found url: %s\n", path);
- dprintf(fd,
- "HTTP/1.1 404 Not Found\r\n"
- "\r\n");
- }
- close(fd);
-}
-
-static
-void *start_http_connection_inner(void *arg_anon) {
- pthread_setname_np(pthread_self(), "http-connection");
- int fd = (int)(intptr_t)arg_anon;
- thread_http_connection(fd);
- wg_sub(wg, 1);
- return NULL;
-}
-
-int httpsock = 0;
-
-static
-void start_http_connection(int fd) {
- wg_add(wg, 1);
- pthread_t thread;
- pthread_create(&thread, NULL, start_http_connection_inner, (void*)(intptr_t)fd);
-}
-
-void thread_http_listener(struct wg *awg,
- struct mpjpeg_stream *avideo,
- struct mpjpeg_stream *adepth) {
- wg = awg;
- video = avideo;
- depth = adepth;
- httpsock = tcp4_parse_listen("8090");
-
- while (running) {
- int conn = accept(httpsock, NULL, NULL);
- if (conn < 0)
- continue;
- start_http_connection(conn);
- }
- finish(0);
-}