Add index.html
This commit is contained in:
		
							
								
								
									
										13
									
								
								cmd/main.c
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								cmd/main.c
									
									
									
									
									
								
							| @@ -9,19 +9,26 @@ | ||||
|  | ||||
| #include <signal.h> | ||||
|  | ||||
| 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 }, | ||||
|   { } | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -1,4 +0,0 @@ | ||||
|   ; | ||||
|   ; | ||||
|   ; | ||||
|   ; | ||||
							
								
								
									
										35
									
								
								html/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								html/index.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| <!DOCTYPE html> | ||||
|  | ||||
| <html> | ||||
| <head> | ||||
| 	<meta charset="utf-8" /> | ||||
| 	<style>body { font-family: monospace; }</style> | ||||
| </head> | ||||
|  | ||||
| <body> | ||||
| 	<hr> | ||||
| 	<ul> | ||||
| 		<br> | ||||
| 		<li> | ||||
| 			<a href="snapshot"><b>/snapshot</b></a><br> | ||||
| 			Get a current actual image from the server. | ||||
| 		</li> | ||||
| 		<br> | ||||
| 		<li> | ||||
| 			<a href="stream"><b>/stream</b></a><br> | ||||
| 			Get a live stream. Query params:<br> | ||||
| 		</li> | ||||
| 		<br> | ||||
| 		<li> | ||||
| 			The mjpg-streamer compatibility layer:<br> | ||||
| 			<br> | ||||
| 			<ul> | ||||
| 				<li><a href="?action=snapshot">/?action=snapshot</a> as alias to the <a href="snapshot">/snapshot</a>.</li> | ||||
| 				<br> | ||||
| 				<li><a href="?action=stream">/?action=stream</a> as alias to the <a href="stream">/stream</a>.</li> | ||||
| 			</ul> | ||||
| 		</li> | ||||
| 	</ul> | ||||
| 	<hr> | ||||
| </body> | ||||
| </html> | ||||
							
								
								
									
										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
	 Kamil Trzcinski
					Kamil Trzcinski