Move part of http/
into util/http/
This commit is contained in:
203
util/http/http.c
Normal file
203
util/http/http.c
Normal file
@ -0,0 +1,203 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "http.h"
|
||||
#include "util/opts/log.h"
|
||||
|
||||
static int http_listen(int port, int maxcons)
|
||||
{
|
||||
struct sockaddr_in server = {0};
|
||||
int listenfd = -1;
|
||||
|
||||
// getaddrinfo for host
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = INADDR_ANY;
|
||||
server.sin_port = htons(port);
|
||||
|
||||
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (listenfd < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int optval = 1;
|
||||
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
|
||||
|
||||
if (bind(listenfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
||||
perror("bind");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (listen(listenfd, maxcons) < 0) {
|
||||
perror("listen");
|
||||
goto error;
|
||||
}
|
||||
|
||||
return listenfd;
|
||||
error:
|
||||
if (listenfd >= 0)
|
||||
close(listenfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *http_enum_params(http_worker_t *worker, FILE *stream, http_param_fn fn, void *opaque)
|
||||
{
|
||||
const char *params = strstr(worker->client_method, "?");
|
||||
if (!params) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *ret = NULL;
|
||||
char *start = strdup(params + 1);
|
||||
char *string = start;
|
||||
char *option;
|
||||
|
||||
// Drop after ` `
|
||||
if ((option = strstr(start, " ")) != NULL) {
|
||||
*option = 0;
|
||||
}
|
||||
|
||||
while ((option = strsep(&string, "&")) != NULL) {
|
||||
char *value = option;
|
||||
char *key = strsep(&value, "=");
|
||||
|
||||
ret = fn(worker, stream, key, value, opaque);
|
||||
if (ret) {
|
||||
break;
|
||||
}
|
||||
|
||||
// consume all separators
|
||||
while (strsep(&value, "="));
|
||||
}
|
||||
|
||||
free(start);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void http_process(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
// Read headers
|
||||
if (!fgets(worker->client_method, sizeof(worker->client_method), stream)) {
|
||||
return;
|
||||
}
|
||||
|
||||
worker->range_header[0] = 0;
|
||||
|
||||
// Consume headers
|
||||
for(int i = 0; i < 50; i++) {
|
||||
char line[BUFSIZE];
|
||||
if (!fgets(line, BUFSIZE, stream))
|
||||
return;
|
||||
if (line[0] == '\r' && line[1] == '\n')
|
||||
break;
|
||||
|
||||
if (strcasestr(line, "Range:") == line) {
|
||||
strcpy(worker->range_header, line);
|
||||
}
|
||||
}
|
||||
|
||||
worker->current_method = NULL;
|
||||
|
||||
for (int i = 0; worker->methods[i].name; i++) {
|
||||
const char *name = worker->methods[i].name;
|
||||
int nlen = strlen(worker->methods[i].name);
|
||||
|
||||
if (strncmp(worker->client_method, name, nlen-1))
|
||||
continue;
|
||||
|
||||
// allow last character to match `?` or ` `
|
||||
if (worker->client_method[nlen-1] == name[nlen-1] || (name[nlen-1] == '?' && worker->client_method[nlen-1] == ' ')) {
|
||||
worker->current_method = &worker->methods[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (worker->current_method) {
|
||||
worker->current_method->func(worker, stream);
|
||||
worker->current_method = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
http_404(stream, "Not found.");
|
||||
}
|
||||
|
||||
static void http_client(http_worker_t *worker)
|
||||
{
|
||||
worker->client_host = inet_ntoa(worker->client_addr.sin_addr);
|
||||
LOG_INFO(worker, "Client connected %s.", worker->client_host);
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 3;
|
||||
tv.tv_usec = 0;
|
||||
setsockopt(worker->client_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv));
|
||||
setsockopt(worker->client_fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof(tv));
|
||||
|
||||
int on = 1;
|
||||
setsockopt(worker->client_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on));
|
||||
setsockopt(worker->client_fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
|
||||
|
||||
FILE *stream = fdopen(worker->client_fd, "r+");
|
||||
if (stream) {
|
||||
http_process(worker, stream);
|
||||
fclose(stream);
|
||||
}
|
||||
|
||||
close(worker->client_fd);
|
||||
worker->client_fd = -1;
|
||||
|
||||
LOG_INFO(worker, "Client disconnected %s.", worker->client_host);
|
||||
worker->client_host = NULL;
|
||||
}
|
||||
|
||||
static int http_worker(http_worker_t *worker)
|
||||
{
|
||||
while (1) {
|
||||
unsigned addrlen = sizeof(worker->client_addr);
|
||||
worker->client_fd = accept(worker->listen_fd, (struct sockaddr *)&worker->client_addr, &addrlen);
|
||||
if (worker->client_fd < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
http_client(worker);
|
||||
}
|
||||
|
||||
error:
|
||||
free(worker->name);
|
||||
free(worker);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int http_server(http_server_options_t *options, http_method_t *methods)
|
||||
{
|
||||
int listen_fd = http_listen(options->port, options->maxcons);
|
||||
if (listen_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
sigaction(SIGPIPE, &(struct sigaction){{ SIG_IGN }}, NULL);
|
||||
|
||||
for (int worker = 0; worker < options->maxcons; worker++) {
|
||||
char name[20];
|
||||
sprintf(name, "HTTP%d/%d", options->port, worker);
|
||||
|
||||
http_worker_t *worker = calloc(1, sizeof(http_worker_t));
|
||||
worker->name = strdup(name);
|
||||
worker->listen_fd = listen_fd;
|
||||
worker->methods = methods;
|
||||
worker->client_fd = -1;
|
||||
pthread_create(&worker->thread, NULL, (void *(*)(void*))http_worker, worker);
|
||||
}
|
||||
|
||||
return listen_fd;
|
||||
}
|
||||
|
72
util/http/http.h
Normal file
72
util/http/http.h
Normal file
@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <netinet/ip.h>
|
||||
|
||||
typedef struct buffer_s buffer_t;
|
||||
typedef struct http_worker_s http_worker_t;
|
||||
|
||||
typedef void (*http_method_fn)(struct http_worker_s *worker, FILE *stream);
|
||||
typedef void *(*http_param_fn)(struct http_worker_s *worker, FILE *stream, const char *key, const char *value, void *opaque);
|
||||
|
||||
#define BUFSIZE 256
|
||||
|
||||
typedef struct http_method_s {
|
||||
const char *name;
|
||||
http_method_fn func;
|
||||
const char *content_type;
|
||||
const void *content_body;
|
||||
unsigned content_length;
|
||||
unsigned *content_lengthp;
|
||||
} http_method_t;
|
||||
|
||||
typedef struct http_worker_s {
|
||||
char *name;
|
||||
int listen_fd;
|
||||
http_method_t *methods;
|
||||
pthread_t thread;
|
||||
|
||||
int client_fd;
|
||||
struct sockaddr_in client_addr;
|
||||
char *client_host;
|
||||
char client_method[BUFSIZE];
|
||||
char range_header[BUFSIZE];
|
||||
|
||||
http_method_t *current_method;
|
||||
} http_worker_t;
|
||||
|
||||
typedef struct http_server_options_s {
|
||||
unsigned port;
|
||||
unsigned maxcons;
|
||||
} http_server_options_t;
|
||||
|
||||
int http_server(http_server_options_t *options, http_method_t *methods);
|
||||
void http_content(http_worker_t *worker, FILE *stream);
|
||||
void http_200(FILE *stream, const char *data);
|
||||
void http_404(FILE *stream, const char *data);
|
||||
void http_500(FILE *stream, const char *data);
|
||||
void *http_enum_params(http_worker_t *worker, FILE *stream, http_param_fn fn, void *opaque);
|
||||
|
||||
// M-JPEG
|
||||
void http_snapshot(http_worker_t *worker, FILE *stream);
|
||||
void http_stream(http_worker_t *worker, FILE *stream);
|
||||
void http_jpeg_capture(struct buffer_s *buf);
|
||||
void http_jpeg_lowres_capture(struct buffer_s *buf);
|
||||
bool http_jpeg_needs_buffer();
|
||||
void http_option(http_worker_t *worker, FILE *stream);
|
||||
|
||||
// H264
|
||||
bool http_h264_needs_buffer();
|
||||
void http_h264_capture(buffer_t *buf);
|
||||
void http_h264_lowres_capture(buffer_t *buf);
|
||||
void http_h264_video(http_worker_t *worker, FILE *stream);
|
||||
bool h264_is_key_frame(buffer_t *buf);
|
||||
void http_mkv_video(http_worker_t *worker, FILE *stream);
|
||||
void http_mp4_video(http_worker_t *worker, FILE *stream);
|
||||
void http_mov_video(http_worker_t *worker, FILE *stream);
|
||||
|
||||
#define HTTP_LOW_RES_PARAM "res=low"
|
57
util/http/http_methods.c
Normal file
57
util/http/http_methods.c
Normal file
@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "http.h"
|
||||
|
||||
static void http_write_response(
|
||||
FILE *stream,
|
||||
const char *status,
|
||||
const char *content_type,
|
||||
const char *body,
|
||||
unsigned content_length)
|
||||
{
|
||||
if (content_length == 0 && body)
|
||||
content_length = strlen(body);
|
||||
|
||||
fprintf(stream, "HTTP/1.1 %s\r\n", status ? status : "200 OK");
|
||||
fprintf(stream, "Content-Type: %s\r\n", content_type ? content_type : "text/plain");
|
||||
if (content_length > 0)
|
||||
fprintf(stream, "Content-Length: %d\r\n", content_length);
|
||||
fprintf(stream, "\r\n");
|
||||
if (body) {
|
||||
fwrite(body, 1, content_length, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void http_content(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
if (worker->current_method) {
|
||||
if (worker->current_method->content_lengthp) {
|
||||
worker->current_method->content_length = *worker->current_method->content_lengthp;
|
||||
}
|
||||
|
||||
http_write_response(stream,
|
||||
NULL,
|
||||
worker->current_method->content_type,
|
||||
worker->current_method->content_body,
|
||||
worker->current_method->content_length
|
||||
);
|
||||
} else {
|
||||
http_write_response(stream, NULL, NULL, "No data", 0);
|
||||
}
|
||||
}
|
||||
|
||||
void http_200(FILE *stream, const char *data)
|
||||
{
|
||||
http_write_response(stream, "200 OK", NULL, data ? data : "Nothing here.\n", 0);
|
||||
}
|
||||
|
||||
void http_404(FILE *stream, const char *data)
|
||||
{
|
||||
http_write_response(stream, "404 Not Found", NULL, data ? data : "Nothing here.\n", 0);
|
||||
}
|
||||
|
||||
void http_500(FILE *stream, const char *data)
|
||||
{
|
||||
http_write_response(stream, "500 Server Error", NULL, data ? data : "Server Error\n", 0);
|
||||
}
|
Reference in New Issue
Block a user