Add /video to automatically give HLS to supported browser

This commit is contained in:
Kamil Trzcinski
2022-09-06 23:04:55 +02:00
parent 5ee0bee59f
commit 5801152b03
12 changed files with 93 additions and 12 deletions

View File

@ -16,6 +16,7 @@
#define HEADER_RANGE "Range:"
#define HEADER_CONTENT_LENGTH "Content-Length:"
#define HEADER_USER_AGENT "User-Agent:"
static int http_listen(int port, int maxcons)
{
@ -95,6 +96,7 @@ static void http_process(http_worker_t *worker, FILE *stream)
}
worker->range_header[0] = 0;
worker->user_agent[0] = 0;
worker->content_length = -1;
// Consume headers
@ -107,9 +109,10 @@ static void http_process(http_worker_t *worker, FILE *stream)
if (strcasestr(line, HEADER_RANGE) == line) {
strcpy(worker->range_header, line);
}
if (strcasestr(line, HEADER_CONTENT_LENGTH) == line) {
} else if (strcasestr(line, HEADER_CONTENT_LENGTH) == line) {
worker->content_length = atoi(line + strlen(HEADER_CONTENT_LENGTH));
} else if (strcasestr(line, HEADER_USER_AGENT) == line) {
strcpy(worker->user_agent, line + strlen(HEADER_USER_AGENT));
}
}
@ -129,6 +132,8 @@ static void http_process(http_worker_t *worker, FILE *stream)
}
}
LOG_INFO(worker, "Request '%s'", worker->client_method);
if (worker->current_method) {
worker->current_method->func(worker, stream);
worker->current_method = NULL;

View File

@ -36,6 +36,7 @@ typedef struct http_worker_s {
char *client_host;
char client_method[BUFSIZE];
char range_header[BUFSIZE];
char user_agent[BUFSIZE];
http_method_t *current_method;
} http_worker_t;
@ -48,6 +49,7 @@ typedef struct http_server_options_s {
int http_server(http_server_options_t *options, http_method_t *methods);
void http_content(http_worker_t *worker, FILE *stream);
void http_write_response(FILE *stream, const char *status, const char *content_type, const char *body, unsigned content_length);
void http_write_responsef(FILE *stream, const char *status, const char *content_type, const char *fmt, ...);
void http_200(FILE *stream, const char *data);
void http_400(FILE *stream, const char *data);
void http_404(FILE *stream, const char *data);

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "http.h"
@ -23,6 +24,17 @@ void http_write_response(
}
}
void http_write_responsef(FILE *stream, const char *status, const char *content_type, const char *fmt, ...)
{
va_list arg;
va_start(arg, fmt);
char *body = NULL;
size_t n = vasprintf(&body, fmt, arg);
http_write_response(stream, status, content_type, body, n);
free(body);
}
void http_content(http_worker_t *worker, FILE *stream)
{
if (worker->current_method) {