Add index.html
This commit is contained in:
14
http/http.c
14
http/http.c
@ -67,6 +67,8 @@ static void http_process(http_worker_t *worker, FILE *stream)
|
||||
break;
|
||||
}
|
||||
|
||||
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);
|
||||
@ -76,12 +78,18 @@ static void http_process(http_worker_t *worker, FILE *stream)
|
||||
|
||||
// allow last character to match `?` or ` `
|
||||
if (worker->client_method[nlen-1] == name[nlen-1] || name[nlen-1] == '?' && worker->client_method[nlen-1] == ' ') {
|
||||
worker->methods[i].func(worker, stream);
|
||||
return;
|
||||
worker->current_method = &worker->methods[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
http_404(worker, stream);
|
||||
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)
|
||||
|
17
http/http.h
17
http/http.h
@ -14,6 +14,10 @@ typedef void (*http_method_fn)(struct http_worker_s *worker, FILE *stream);
|
||||
typedef struct http_method_s {
|
||||
const char *name;
|
||||
http_method_fn func;
|
||||
const char *content_type;
|
||||
const char *content_body;
|
||||
unsigned content_length;
|
||||
unsigned *content_lengthp;
|
||||
} http_method_t;
|
||||
|
||||
typedef struct http_worker_s {
|
||||
@ -26,6 +30,8 @@ typedef struct http_worker_s {
|
||||
struct sockaddr_in client_addr;
|
||||
char *client_host;
|
||||
char client_method[256];
|
||||
|
||||
http_method_t *current_method;
|
||||
} http_worker_t;
|
||||
|
||||
typedef struct http_server_options_s {
|
||||
@ -34,14 +40,9 @@ typedef struct http_server_options_s {
|
||||
} http_server_options_t;
|
||||
|
||||
int http_server(http_server_options_t *options, http_method_t *methods);
|
||||
|
||||
void http_index(http_worker_t *worker, FILE *stream);
|
||||
void http_video_html(http_worker_t *worker, FILE *stream);
|
||||
void http_jmuxer_js(http_worker_t *worker, FILE *stream);
|
||||
void http_custom_header(FILE *stream, const char *status);
|
||||
void http_404_header(FILE *stream);
|
||||
void http_500_header(FILE *stream);
|
||||
void http_404(http_worker_t *worker, FILE *stream);
|
||||
void http_content(http_worker_t *worker, FILE *stream);
|
||||
void http_404(FILE *stream, const char *data);
|
||||
void http_500(FILE *stream, const char *data);
|
||||
|
||||
// M-JPEG
|
||||
void http_snapshot(http_worker_t *worker, FILE *stream);
|
||||
|
@ -142,7 +142,7 @@ static void http_ffmpeg_video(http_worker_t *worker, FILE *stream, const char *c
|
||||
return;
|
||||
}
|
||||
|
||||
http_500_header(stream);
|
||||
http_500(stream, NULL);
|
||||
|
||||
if (n == 0) {
|
||||
fprintf(stream, "No frames.\n");
|
||||
|
@ -99,7 +99,7 @@ void http_h264_video(http_worker_t *worker, FILE *stream)
|
||||
return;
|
||||
}
|
||||
|
||||
http_500_header(stream);
|
||||
http_500(stream, NULL);
|
||||
|
||||
if (n == 0) {
|
||||
fprintf(stream, "No frames.\n");
|
||||
|
@ -68,7 +68,7 @@ void http_snapshot(http_worker_t *worker, FILE *stream)
|
||||
int n = buffer_lock_write_loop(http_jpeg_buffer_for_res(worker), 1, (buffer_write_fn)http_snapshot_buf_part, stream);
|
||||
|
||||
if (n <= 0) {
|
||||
http_500_header(stream);
|
||||
http_500(stream, NULL);
|
||||
fprintf(stream, "No snapshot captured yet.\r\n");
|
||||
}
|
||||
}
|
||||
@ -96,7 +96,7 @@ void http_stream(http_worker_t *worker, FILE *stream)
|
||||
int n = buffer_lock_write_loop(http_jpeg_buffer_for_res(worker), 0, (buffer_write_fn)http_stream_buf_part, stream);
|
||||
|
||||
if (n == 0) {
|
||||
http_500_header(stream);
|
||||
http_500(stream, NULL);
|
||||
fprintf(stream, "No frames.\n");
|
||||
} else if (n < 0) {
|
||||
fprintf(stream, "Interrupted. Received %d frames", -n);
|
||||
|
@ -3,61 +3,46 @@
|
||||
|
||||
#include "http/http.h"
|
||||
|
||||
void http_index(http_worker_t *worker, FILE *stream)
|
||||
static void http_write_response(
|
||||
FILE *stream,
|
||||
const char *status,
|
||||
const char *content_type,
|
||||
const char *body,
|
||||
unsigned content_length)
|
||||
{
|
||||
fprintf(stream, "HTTP/1.1 200 OK\r\n");
|
||||
fprintf(stream, "Content-Type: text/plain\r\n");
|
||||
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-Type: %d\r\n", content_length);
|
||||
fprintf(stream, "\r\n");
|
||||
fprintf(stream, "Text.\r\n");
|
||||
fflush(stream);
|
||||
if (body) {
|
||||
fwrite(body, 1, content_length, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void http_custom_header(FILE *stream, const char *status)
|
||||
void http_content(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
fprintf(stream, "HTTP/1.1 %s\r\n", status);
|
||||
fprintf(stream, "Content-Type: text/plain\r\n");
|
||||
fprintf(stream, "\r\n");
|
||||
if (worker->current_method) {
|
||||
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_404_header(FILE *stream)
|
||||
void http_404(FILE *stream, const char *data)
|
||||
{
|
||||
http_custom_header(stream, "404 Not Found");
|
||||
fprintf(stream, "HTTP/1.1 404 Not Found\r\n");
|
||||
fprintf(stream, "Content-Type: text/plain\r\n");
|
||||
fprintf(stream, "\r\n");
|
||||
http_write_response(stream, "404 Not Found", NULL, data ? data : "Nothing here.\n", 0);
|
||||
}
|
||||
|
||||
void http_500_header(FILE *stream)
|
||||
void http_500(FILE *stream, const char *data)
|
||||
{
|
||||
http_custom_header(stream, "500 Server Error");
|
||||
}
|
||||
|
||||
void http_404(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
http_404_header(stream);
|
||||
fprintf(stream, "Nothing here?\r\n");
|
||||
}
|
||||
|
||||
void http_video_html(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
extern unsigned char html_video_html[];
|
||||
extern unsigned int html_video_html_len;
|
||||
|
||||
fprintf(stream, "HTTP/1.1 200 OK\r\n");
|
||||
fprintf(stream, "Content-Type: text/html;charset=UTF-8\r\n");
|
||||
fprintf(stream, "\r\n");
|
||||
fwrite(html_video_html, 1, html_video_html_len, stream);
|
||||
fflush(stream);
|
||||
}
|
||||
|
||||
void http_jmuxer_js(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
extern unsigned char html_jmuxer_min_js[];
|
||||
extern unsigned int html_jmuxer_min_js_len;
|
||||
|
||||
fprintf(stream, "HTTP/1.1 200 OK\r\n");
|
||||
fprintf(stream, "Content-Type: text/javascript;charset=UTF-8\r\n");
|
||||
fprintf(stream, "\r\n");
|
||||
fwrite(html_jmuxer_min_js, 1, html_jmuxer_min_js_len, stream);
|
||||
fflush(stream);
|
||||
http_write_response(stream, "500 Server Error", NULL, data ? data : "Server Error\n", 0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user