From caafd0da44c3ef298c66114cc5c0cf2d40031f11 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 24 Oct 2022 00:22:18 +0200 Subject: [PATCH] Add `Access-Control-Allow-Origin: *` everywhere --- cmd/camera-streamer/http.c | 10 ++++++++++ util/http/http.c | 5 ++++- util/http/http.h | 11 ++++++----- util/http/http_methods.c | 2 ++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/camera-streamer/http.c b/cmd/camera-streamer/http.c index 65d8104..ee5915d 100644 --- a/cmd/camera-streamer/http.c +++ b/cmd/camera-streamer/http.c @@ -70,6 +70,15 @@ void camera_http_option(http_worker_t *worker, FILE *stream) } } +void http_cors_options(http_worker_t *worker, FILE *stream) +{ + fprintf(stream, "HTTP/1.1 204 No Data\r\n"); + fprintf(stream, "Access-Control-Allow-Origin: *\r\n"); + fprintf(stream, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n"); + fprintf(stream, "Access-Control-Allow-Headers: Content-Type\r\n"); + fprintf(stream, "\r\n"); +} + http_method_t http_methods[] = { { "GET", "/snapshot", http_snapshot }, { "GET", "/snapshot.jpg", http_snapshot }, @@ -85,5 +94,6 @@ http_method_t http_methods[] = { { "POST", "/webrtc", http_webrtc_offer }, { "GET", "/option", camera_http_option }, { "GET", "/", http_content, "text/html", html_index_html, 0, &html_index_html_len }, + { "OPTIONS", "*/", http_cors_options }, { } }; diff --git a/util/http/http.c b/util/http/http.c index aee1b02..e85f2a1 100644 --- a/util/http/http.c +++ b/util/http/http.c @@ -94,7 +94,6 @@ static void http_process(http_worker_t *worker, FILE *stream) worker->user_agent[0] = 0; worker->content_length = -1; - // request_uri worker->request_method = worker->client_method; @@ -149,6 +148,9 @@ static void http_process(http_worker_t *worker, FILE *stream) continue; if (!strstr(worker->request_params, params+1)) continue; + } else if (method->uri[0] == '*') { + if (strstr(worker->request_uri, method->uri + 1) != worker->request_uri) + continue; } else { if (strcmp(worker->request_uri, method->uri)) continue; @@ -237,6 +239,7 @@ int http_server(http_server_options_t *options, http_method_t *methods) worker->listen_fd = listen_fd; worker->methods = methods; worker->client_fd = -1; + worker->options = *options; pthread_create(&worker->thread, NULL, (void *(*)(void*))http_worker, worker); } diff --git a/util/http/http.h b/util/http/http.h index aa465b1..dc4a9e4 100644 --- a/util/http/http.h +++ b/util/http/http.h @@ -25,11 +25,17 @@ typedef struct http_method_s { unsigned *content_lengthp; } http_method_t; +typedef struct http_server_options_s { + unsigned port; + unsigned maxcons; +} http_server_options_t; + typedef struct http_worker_s { char *name; int listen_fd; http_method_t *methods; pthread_t thread; + http_server_options_t options; int client_fd; int content_length; @@ -46,11 +52,6 @@ typedef struct http_worker_s { 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_write_response(FILE *stream, const char *status, const char *content_type, const char *body, unsigned content_length); diff --git a/util/http/http_methods.c b/util/http/http_methods.c index 498a8f9..cdbdf8f 100644 --- a/util/http/http_methods.c +++ b/util/http/http_methods.c @@ -18,6 +18,8 @@ void http_write_response( 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); + if (!status || strstr(status, "200 OK") == status) + fprintf(stream, "Access-Control-Allow-Origin: *\r\n"); fprintf(stream, "\r\n"); if (body) { fwrite(body, 1, content_length, stream);