From eaf6f3d9ba10b5d828c01d546f5e44593506ee70 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Fri, 8 Apr 2022 14:17:13 +0200 Subject: [PATCH] Add index.html --- cmd/main.c | 13 ++++++-- html/html.h | 4 --- html/index.html | 35 +++++++++++++++++++++ http/http.c | 14 +++++++-- http/http.h | 17 +++++----- http/http_ffmpeg.c | 2 +- http/http_h264.c | 2 +- http/http_jpeg.c | 4 +-- http/http_methods.c | 77 ++++++++++++++++++--------------------------- 9 files changed, 100 insertions(+), 68 deletions(-) delete mode 100644 html/html.h create mode 100644 html/index.html diff --git a/cmd/main.c b/cmd/main.c index 53ec9b3..b2a842e 100644 --- a/cmd/main.c +++ b/cmd/main.c @@ -9,19 +9,26 @@ #include +extern unsigned char html_index_html[]; +extern unsigned int html_index_html_len; +extern unsigned char html_video_html[]; +extern unsigned int html_video_html_len; +extern unsigned char html_jmuxer_min_js[]; +extern unsigned int html_jmuxer_min_js_len; + http_method_t http_methods[] = { { "GET /snapshot?", http_snapshot }, { "GET /stream?", http_stream }, { "GET /?action=snapshot", http_snapshot }, { "GET /?action=stream", http_stream }, - { "GET /video?", http_video_html }, + { "GET /video?", http_content, "text/html", html_video_html, 0, &html_video_html_len }, { "GET /video.h264?", http_h264_video }, #ifdef USE_FFMPEG { "GET /video.mkv?", http_mkv_video }, { "GET /video.mp4?", http_mp4_video }, - { "GET /jmuxer.min.js?", http_jmuxer_js }, + { "GET /jmuxer.min.js?", http_content, "text/javascript", html_jmuxer_min_js, 0, &html_jmuxer_min_js_len }, #endif // USE_FFMPEG - { "GET /?", http_index }, + { "GET /?", http_content, "text/html", html_index_html, 0, &html_index_html_len }, { } }; diff --git a/html/html.h b/html/html.h deleted file mode 100644 index 15b9651..0000000 --- a/html/html.h +++ /dev/null @@ -1,4 +0,0 @@ - ; - ; - ; - ; diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..bd5f99a --- /dev/null +++ b/html/index.html @@ -0,0 +1,35 @@ + + + + + + + + + +
+ +
+ + diff --git a/http/http.c b/http/http.c index 397036f..a6574b7 100644 --- a/http/http.c +++ b/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) diff --git a/http/http.h b/http/http.h index 84925fc..6222d93 100644 --- a/http/http.h +++ b/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); diff --git a/http/http_ffmpeg.c b/http/http_ffmpeg.c index 19cadd3..d4078c3 100644 --- a/http/http_ffmpeg.c +++ b/http/http_ffmpeg.c @@ -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"); diff --git a/http/http_h264.c b/http/http_h264.c index 5053dbb..c34e053 100644 --- a/http/http_h264.c +++ b/http/http_h264.c @@ -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"); diff --git a/http/http_jpeg.c b/http/http_jpeg.c index 2f7e1e3..a1e0021 100644 --- a/http/http_jpeg.c +++ b/http/http_jpeg.c @@ -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); diff --git a/http/http_methods.c b/http/http_methods.c index 7021ca7..315d2cf 100644 --- a/http/http_methods.c +++ b/http/http_methods.c @@ -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); }